2019-03-24 16:37:51 -04:00
import { ChangeDetectorRef , Component , OnDestroy , OnInit } from "@angular/core" ;
import { ScalarWidgetApi } from "../../shared/services/scalar/scalar-widget.api" ;
import { CapableWidget , WIDGET_API_VERSION_OPENID } from "../capable-widget" ;
import { ActivatedRoute } from "@angular/router" ;
import { ScalarServerApiService } from "../../shared/services/scalar/scalar-server-api.service" ;
2020-10-23 07:30:20 -04:00
import { TranslateService } from "@ngx-translate/core" ;
2019-03-24 16:37:51 -04:00
@Component ( {
2021-09-01 19:01:01 -04:00
selector : "app-reauth-example-widget-wrapper" ,
2019-03-24 16:37:51 -04:00
templateUrl : "manager-test.component.html" ,
styleUrls : [ "manager-test.component.scss" ] ,
} )
2021-09-01 19:01:01 -04:00
export class ManagerTestWidgetWrapperComponent
extends CapableWidget
implements OnInit , OnDestroy {
public readonly STATE_NEUTRAL = "neutral" ;
public readonly STATE_OK = "ok" ;
public readonly STATE_ERROR = "error" ;
2019-03-24 16:37:51 -04:00
public isBusy = true ;
public isSupported = true ;
public selfState = this . STATE_NEUTRAL ;
public managerState = this . STATE_NEUTRAL ;
public homeserverState = this . STATE_NEUTRAL ;
2020-10-23 07:30:20 -04:00
public message : string ;
2019-03-24 16:37:51 -04:00
2021-09-01 19:01:01 -04:00
constructor (
activatedRoute : ActivatedRoute ,
private scalarApi : ScalarServerApiService ,
private changeDetector : ChangeDetectorRef ,
public translate : TranslateService
) {
2019-03-24 16:37:51 -04:00
super ( ) ;
2020-10-23 07:30:20 -04:00
this . translate = translate ;
2021-09-01 19:01:01 -04:00
this . translate
. get (
"Click the button to test your connection. This may cause your client to ask if it is okay to share your identity with the widget - this is required to test your connection to your homeserver."
)
. subscribe ( ( res : string ) = > {
this . message = res ;
} ) ;
2019-03-24 16:37:51 -04:00
const params : any = activatedRoute . snapshot . queryParams ;
ScalarWidgetApi . widgetId = params . widgetId ;
}
protected onSupportedVersionsFound ( ) : void {
super . onSupportedVersionsFound ( ) ;
2021-09-01 19:01:01 -04:00
this . isSupported = this . doesSupportAtLeastVersion (
WIDGET_API_VERSION_OPENID
) ;
2019-03-24 16:37:51 -04:00
this . isBusy = false ;
if ( ! this . isSupported ) {
this . selfState = this . STATE_ERROR ;
}
this . changeDetector . detectChanges ( ) ;
}
public async start ( ) : Promise < any > {
this . selfState = this . STATE_NEUTRAL ;
this . managerState = this . STATE_NEUTRAL ;
this . homeserverState = this . STATE_NEUTRAL ;
2021-09-01 19:01:01 -04:00
this . translate
. get ( "Please accept the prompt to verify your identity." )
. subscribe ( ( res : string ) = > {
this . message = res ;
} ) ;
2019-03-24 16:37:51 -04:00
this . isBusy = true ;
const response = await this . getOpenIdInfo ( ) ;
if ( response . blocked ) {
this . isBusy = false ;
this . selfState = this . STATE_ERROR ;
2021-09-01 19:01:01 -04:00
this . translate
. get ( "You have blocked this widget from verifying your identity." )
. subscribe ( ( res : string ) = > {
this . message = res ;
} ) ;
2019-03-24 16:37:51 -04:00
return ;
}
this . selfState = this . STATE_OK ;
2021-09-01 19:01:01 -04:00
this . translate
. get ( "Checking connectivity to integration manager..." )
. subscribe ( ( res : string ) = > {
this . message = res ;
} ) ;
2019-03-24 16:37:51 -04:00
try {
await this . scalarApi . ping ( ) ;
this . managerState = this . STATE_OK ;
2021-09-01 19:01:01 -04:00
this . translate
. get ( "Checking connectivity to homeserver..." )
. subscribe ( ( res : string ) = > {
this . message = res ;
} ) ;
2019-03-24 16:37:51 -04:00
} catch ( e ) {
console . error ( e ) ;
this . isBusy = false ;
this . managerState = this . STATE_ERROR ;
2021-09-01 19:01:01 -04:00
this . translate
. get (
"Error checking if the integration manager is alive. This usually means that the manager which served this widget has gone offline."
)
. subscribe ( ( res : string ) = > {
this . message = res ;
} ) ;
2019-03-24 16:37:51 -04:00
return ;
}
try {
await this . scalarApi . register ( response . openId ) ;
this . homeserverState = this . STATE_OK ;
2021-09-01 19:01:01 -04:00
this . translate
. get ( "You're all set! Click the button below to re-run the test." )
. subscribe ( ( res : string ) = > {
this . message = res ;
} ) ;
2019-03-24 16:37:51 -04:00
this . isBusy = false ;
} catch ( e ) {
this . isBusy = false ;
this . homeserverState = this . STATE_ERROR ;
2021-09-01 19:01:01 -04:00
this . translate
. get (
"Error contacting homeserver. This usually means your federation setup is incorrect, or your homeserver is offline. Consult your homeserver's documentation for how to set up federation."
)
. subscribe ( ( res : string ) = > {
this . message = res ;
} ) ;
2019-03-24 16:37:51 -04:00
}
}
}