Detect API version of Riot before rendering widget

This commit is contained in:
Travis Ralston 2019-03-15 20:46:04 -06:00
parent 11ed6342f6
commit d29e8531d9
2 changed files with 33 additions and 6 deletions

View file

@ -88,6 +88,10 @@ export class ScalarWidgetApi {
ScalarWidgetApi.callAction("get_openid", {});
}
public static requestSupportedVersions(): void {
ScalarWidgetApi.callAction("supported_api_versions", {});
}
private static callAction(action, payload) {
if (!window.opener) {
return;

View file

@ -14,12 +14,12 @@ import { FE_ScalarOpenIdRequestBody } from "../../shared/models/scalar-server-re
})
export class ReauthExampleWidgetWrapperComponent extends CapableWidget implements OnInit, OnDestroy {
public busy = false;
public busy = true; // busy until we load supported versions
public hasOpenId = false;
public userId: string;
public blocked = false;
public error = false;
public stateMessage = "";
public stateMessage = "Checking client version...";
private widgetReplySubscription: Subscription;
private widgetRequestSubscription: Subscription;
@ -38,14 +38,30 @@ export class ReauthExampleWidgetWrapperComponent extends CapableWidget implement
}
public ngOnInit(): void {
// TODO: Check that the client supports this (API version 0.0.2 at least)
super.ngOnInit();
this.widgetReplySubscription = ScalarWidgetApi.replyReceived.subscribe(async response => {
if (response.action !== "get_openid") return;
const data = response.response;
if (response.action === "supported_api_versions") {
if (!data || !data.supported_versions) {
this.stateMessage = "Invalid API version response";
this.changeDetector.detectChanges();
return;
}
if (data.supported_versions.indexOf("0.0.2") === -1) {
this.stateMessage = "Your client is not supported by this widget.";
this.changeDetector.detectChanges();
return;
}
this.busy = false;
this.changeDetector.detectChanges();
return;
}
if (response.action !== "get_openid") return;
try {
if (data.state === "request") {
this.stateMessage = "Waiting for you to accept the prompt...";
@ -96,6 +112,13 @@ export class ReauthExampleWidgetWrapperComponent extends CapableWidget implement
if (this.widgetRequestSubscription) this.widgetRequestSubscription.unsubscribe();
}
protected onCapabilitiesSent(): void {
super.onCapabilitiesSent();
// Start a request for supported API versions
ScalarWidgetApi.requestSupportedVersions();
}
public onReauthStart(): void {
this.busy = true;
this.error = false;