2017-12-28 20:22:50 -05:00
import { Component , OnDestroy , OnInit } from "@angular/core" ;
2018-03-24 19:16:52 -04:00
import { FE_NebConfiguration } from "../../../shared/models/admin-responses" ;
2017-12-28 20:22:50 -05:00
import { AdminNebApiService } from "../../../shared/services/admin/admin-neb-api.service" ;
import { ActivatedRoute } from "@angular/router" ;
import { ToasterService } from "angular2-toaster" ;
import { FE_Integration } from "../../../shared/models/integration" ;
2018-03-25 23:01:05 -04:00
import { NEB_HAS_CONFIG , NEB_IS_COMPLEX } from "../../../shared/models/neb" ;
2018-03-24 23:17:44 -04:00
import { AdminNebGiphyConfigComponent } from "../config/giphy/giphy.component" ;
import { NebBotConfigurationDialogContext } from "../config/config-context" ;
2018-03-24 23:44:05 -04:00
import { AdminNebGuggyConfigComponent } from "../config/guggy/guggy.component" ;
2018-03-24 23:50:30 -04:00
import { AdminNebGoogleConfigComponent } from "../config/google/google.component" ;
2018-03-24 23:55:35 -04:00
import { AdminNebImgurConfigComponent } from "../config/imgur/imgur.component" ;
2020-10-23 07:30:20 -04:00
import { TranslateService } from "@ngx-translate/core" ;
2021-08-16 18:00:59 -04:00
import { NgbModal } from "@ng-bootstrap/ng-bootstrap" ;
2017-12-28 20:22:50 -05:00
@Component ( {
templateUrl : "./edit.component.html" ,
styleUrls : [ "./edit.component.scss" ] ,
} )
export class AdminEditNebComponent implements OnInit , OnDestroy {
public isLoading = true ;
public isUpdating = false ;
public isUpstream = false ;
public nebConfig : FE_NebConfiguration ;
private subscription : any ;
2018-01-31 18:30:08 -05:00
private overlappingTypes : string [ ] = [ ] ;
2017-12-28 20:22:50 -05:00
2018-03-24 23:17:44 -04:00
constructor ( private nebApi : AdminNebApiService ,
private route : ActivatedRoute ,
2021-08-16 18:00:59 -04:00
private modal : NgbModal ,
2020-10-23 07:30:20 -04:00
private toaster : ToasterService ,
public translate : TranslateService ) {
this . translate = translate ;
2017-12-28 20:22:50 -05:00
}
public ngOnInit() {
this . subscription = this . route . params . subscribe ( params = > {
2018-03-24 14:19:29 -04:00
this . loadNeb ( Number ( params [ "nebId" ] ) ) ;
2017-12-28 20:22:50 -05:00
} ) ;
}
public ngOnDestroy() {
this . subscription . unsubscribe ( ) ;
}
public isOverlapping ( bot : FE_Integration ) {
return this . overlappingTypes . indexOf ( bot . type ) !== - 1 ;
}
public hasConfig ( bot : FE_Integration ) : boolean {
2018-03-24 23:17:44 -04:00
return NEB_HAS_CONFIG . indexOf ( bot . type ) !== - 1 && ! this . isUpstream ;
2017-12-28 20:22:50 -05:00
}
2018-03-24 16:54:12 -04:00
public async toggleBot ( bot : FE_Integration ) {
2017-12-28 20:22:50 -05:00
bot . isEnabled = ! bot . isEnabled ;
this . isUpdating = true ;
2018-03-24 16:54:12 -04:00
try {
await this . nebApi . toggleIntegration ( this . nebConfig . id , bot . type , bot . isEnabled ) ;
2017-12-28 20:22:50 -05:00
this . isUpdating = false ;
2020-10-23 07:30:20 -04:00
this . translate . get ( 'Integration updated' ) . subscribe ( ( res : string ) = > { this . toaster . pop ( "success" , res ) ; } ) ;
2018-03-24 16:54:12 -04:00
} catch ( err ) {
2017-12-28 20:22:50 -05:00
console . error ( err ) ;
bot . isEnabled = ! bot . isEnabled ; // revert change
this . isUpdating = false ;
2020-10-23 07:30:20 -04:00
this . translate . get ( 'Error updating integration' ) . subscribe ( ( res : string ) = > { this . toaster . pop ( "error" , res ) ; } ) ;
2018-03-24 16:54:12 -04:00
return ;
}
// Only update the service configuration if
2018-03-24 23:17:44 -04:00
if ( bot . isEnabled && ! this . isUpstream ) {
2018-03-24 16:54:12 -04:00
if ( this . hasConfig ( bot ) ) {
this . editBot ( bot ) ;
2018-03-25 23:01:05 -04:00
} else if ( NEB_IS_COMPLEX . indexOf ( bot . type ) === - 1 ) {
2018-03-24 16:54:12 -04:00
try {
await this . nebApi . setIntegrationConfiguration ( this . nebConfig . id , bot . type , { } ) ;
} catch ( err ) {
console . error ( err ) ;
2020-10-23 07:30:20 -04:00
this . translate . get ( [ 'Failed to configure the integration' , 'Manual troubleshooting may be requred' ] ) . subscribe ( ( res : string ) = > { this . toaster . pop ( "warning" , res [ 0 ] , res [ 1 ] ) ; } ) ;
2018-03-24 16:54:12 -04:00
return ;
}
}
}
2017-12-28 20:22:50 -05:00
}
public editBot ( bot : FE_Integration ) {
2021-08-16 18:00:59 -04:00
let component ;
2018-03-24 23:44:05 -04:00
if ( bot . type === "giphy" ) component = AdminNebGiphyConfigComponent ;
if ( bot . type === "guggy" ) component = AdminNebGuggyConfigComponent ;
2018-03-24 23:50:30 -04:00
if ( bot . type === "google" ) component = AdminNebGoogleConfigComponent ;
2018-03-24 23:55:35 -04:00
if ( bot . type === "imgur" ) component = AdminNebImgurConfigComponent ;
2018-03-24 23:44:05 -04:00
2018-03-24 23:50:30 -04:00
if ( ! component ) throw new Error ( "No config component for " + bot . type ) ;
2021-08-16 18:00:59 -04:00
const nebBotRef = this . modal . open ( component , {
backdrop : 'static' ,
2018-03-24 23:17:44 -04:00
size : 'lg' ,
2021-08-16 18:00:59 -04:00
} ) ;
const nebBotInstance = nebBotRef . componentInstance as NebBotConfigurationDialogContext ;
nebBotInstance . neb = this . nebConfig ;
nebBotInstance . integration = bot ;
2017-12-28 20:22:50 -05:00
}
private loadNeb ( nebId : number ) {
this . isLoading = true ;
this . nebApi . getConfigurations ( ) . then ( configs = > {
const handledTypes : string [ ] = [ ] ;
for ( const config of configs ) {
2018-03-23 23:29:48 -04:00
if ( config . id === nebId ) {
2017-12-28 20:22:50 -05:00
this . nebConfig = config ;
} else {
for ( const type of config . integrations ) {
2018-03-23 22:15:59 -04:00
if ( type . isEnabled ) handledTypes . push ( type . type ) ;
2017-12-28 20:22:50 -05:00
}
}
}
this . overlappingTypes = handledTypes ;
this . isUpstream = ! ! this . nebConfig . upstreamId ;
this . isLoading = false ;
} ) . catch ( err = > {
console . error ( err ) ;
2020-10-23 07:30:20 -04:00
this . translate . get ( 'Could not get go-neb configuration' ) . subscribe ( ( res : string ) = > { this . toaster . pop ( 'error' , res ) ; } ) ;
2017-12-28 20:22:50 -05:00
} ) ;
}
}