2017-12-28 20:22:50 -05:00
import { Component } from "@angular/core" ;
import { ToasterService } from "angular2-toaster" ;
import { AdminNebApiService } from "../../shared/services/admin/admin-neb-api.service" ;
import { AdminUpstreamApiService } from "../../shared/services/admin/admin-upstream-api.service" ;
import { AdminAppserviceApiService } from "../../shared/services/admin/admin-appservice-api.service" ;
2018-03-24 19:16:52 -04:00
import { FE_Appservice , FE_NebConfiguration , FE_Upstream } from "../../shared/models/admin-responses" ;
2018-01-31 18:30:08 -05:00
import { ActivatedRoute , Router } from "@angular/router" ;
2018-03-24 14:18:38 -04:00
import {
AdminNebAppserviceConfigComponent ,
AppserviceConfigDialogContext
} from "./appservice-config/appservice-config.component" ;
import { Modal , overlayConfigFactory } from "ngx-modialog" ;
2020-10-23 07:30:20 -04:00
import { TranslateService } from "@ngx-translate/core" ;
2017-12-28 20:22:50 -05:00
@Component ( {
templateUrl : "./neb.component.html" ,
styleUrls : [ "./neb.component.scss" ] ,
} )
export class AdminNebComponent {
public isLoading = true ;
public isAddingModularNeb = false ;
public hasModularNeb = false ;
public upstreams : FE_Upstream [ ] ;
public appservices : FE_Appservice [ ] ;
public configurations : FE_NebConfiguration [ ] ;
constructor ( private nebApi : AdminNebApiService ,
private upstreamApi : AdminUpstreamApiService ,
private appserviceApi : AdminAppserviceApiService ,
2018-01-31 18:30:08 -05:00
private toaster : ToasterService ,
private router : Router ,
2018-03-24 14:18:38 -04:00
private activatedRoute : ActivatedRoute ,
2020-10-23 07:30:20 -04:00
private modal : Modal ,
public translate : TranslateService ) {
this . translate = translate ;
2017-12-28 20:22:50 -05:00
this . reload ( ) . then ( ( ) = > this . isLoading = false ) . catch ( error = > {
console . error ( error ) ;
2020-10-23 07:30:20 -04:00
this . translate . get ( 'Error loading go-neb configuration' ) . subscribe ( ( res : string ) = > { this . toaster . pop ( "error" , res ) ; } ) ;
2017-12-28 20:22:50 -05:00
} ) ;
}
private reload ( ) : Promise < any > {
return Promise . all ( [
this . loadAppservices ( ) ,
this . loadConfigurations ( ) ,
this . loadUpstreams ( ) ,
] ) ;
}
private loadUpstreams ( ) : Promise < any > {
return this . upstreamApi . getUpstreams ( ) . then ( upstreams = > {
this . upstreams = upstreams ;
} ) ;
}
private loadAppservices ( ) : Promise < any > {
return this . appserviceApi . getAppservices ( ) . then ( appservices = > {
this . appservices = appservices ;
} ) ;
}
private loadConfigurations ( ) : Promise < any > {
return this . nebApi . getConfigurations ( ) . then ( nebConfigs = > {
this . configurations = nebConfigs ;
this . isLoading = false ;
this . hasModularNeb = false ;
for ( const neb of this . configurations ) {
if ( neb . upstreamId ) {
this . hasModularNeb = true ;
break ;
}
}
} ) ;
}
public showAppserviceConfig ( neb : FE_NebConfiguration ) {
2018-03-24 14:18:38 -04:00
this . modal . open ( AdminNebAppserviceConfigComponent , overlayConfigFactory ( {
neb : neb ,
isBlocking : true ,
size : 'lg' ,
} , AppserviceConfigDialogContext ) ) ;
2017-12-28 20:22:50 -05:00
}
2018-03-23 22:15:51 -04:00
public getEnabledBotsString ( neb : FE_NebConfiguration ) : string {
const result = neb . integrations . filter ( i = > i . isEnabled ) . map ( i = > i . displayName ) . join ( ", " ) ;
if ( ! result ) return "None" ;
return result ;
}
2017-12-28 20:22:50 -05:00
public addSelfHostedNeb() {
2018-01-31 18:30:08 -05:00
this . router . navigate ( [ "new" , "selfhosted" ] , { relativeTo : this.activatedRoute } ) ;
2017-12-28 20:22:50 -05:00
}
public addModularHostedNeb() {
this . isAddingModularNeb = true ;
const createNeb = ( upstream : FE_Upstream ) = > {
2018-03-31 01:12:31 -04:00
return this . nebApi . newUpstreamConfiguration ( upstream ) . then ( neb = > {
2017-12-28 20:22:50 -05:00
this . configurations . push ( neb ) ;
2020-10-23 07:30:20 -04:00
this . translate . get ( [ 'matrix.org\'s go-neb added' , 'Click the pencil icon to enable the bots.' ] ) . subscribe ( ( res : string ) = > { this . toaster . pop ( "success" , res [ 0 ] , res [ 1 ] ) ; } ) ;
2017-12-28 20:22:50 -05:00
this . isAddingModularNeb = false ;
this . hasModularNeb = true ;
} ) . catch ( error = > {
console . error ( error ) ;
this . isAddingModularNeb = false ;
2020-10-23 07:30:20 -04:00
this . translate . get ( 'Error adding matrix.org\'s go-neb' ) . subscribe ( ( res : string ) = > { this . toaster . pop ( "error" , res ) ; } ) ;
2017-12-28 20:22:50 -05:00
} ) ;
} ;
const vectorUpstreams = this . upstreams . filter ( u = > u . type === "vector" ) ;
if ( vectorUpstreams . length === 0 ) {
console . log ( "Creating default scalar upstream" ) ;
const scalarUrl = "https://scalar.vector.im/api" ;
this . upstreamApi . newUpstream ( "modular" , "vector" , scalarUrl , scalarUrl ) . then ( upstream = > {
this . upstreams . push ( upstream ) ;
createNeb ( upstream ) ;
} ) . catch ( err = > {
console . error ( err ) ;
2020-10-23 07:30:20 -04:00
this . translate . get ( 'Error creating matrix.org go-neb' ) . subscribe ( ( res : string ) = > { this . toaster . pop ( "error" , res ) ; } ) ;
2017-12-28 20:22:50 -05:00
} ) ;
} else createNeb ( vectorUpstreams [ 0 ] ) ;
}
}