Move API version check to CapableWidget

This commit is contained in:
Travis Ralston 2019-03-24 13:13:30 -06:00
parent f894af79b6
commit 83b2062d87
4 changed files with 56 additions and 32 deletions

6
package-lock.json generated
View File

@ -11474,9 +11474,9 @@
}
},
"semver": {
"version": "5.5.0",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz",
"integrity": "sha512-4SJ3dm0WAwWy/NVeioZh5AntkdJoWKxHxcmyP622fOkgHa4z3R0TdBJICINyaSDE6uNwVc8gZr+ZinwZAH4xIA=="
"version": "5.6.0",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.6.0.tgz",
"integrity": "sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg=="
},
"semver-dsl": {
"version": "1.0.1",

View File

@ -40,6 +40,7 @@
"request": "^2.88.0",
"request-promise": "^4.2.4",
"require-dir-all": "^0.4.15",
"semver": "^5.6.0",
"sequelize": "^4.39.1",
"sequelize-typescript": "^0.6.6",
"sharp": "^0.21.1",

View File

@ -1,18 +1,28 @@
import { OnDestroy, OnInit } from "@angular/core";
import { Subscription } from "rxjs/Subscription";
import { ScalarWidgetApi } from "../shared/services/scalar/scalar-widget.api";
import * as semver from "semver";
export const WIDGET_API_VERSION_BASIC = "0.0.1";
export const WIDGET_API_VERSION_OPENID = "0.0.2";
export const WIDGET_API_DIMENSION_VERSIONS = [WIDGET_API_VERSION_BASIC, WIDGET_API_VERSION_OPENID];
export abstract class CapableWidget implements OnInit, OnDestroy {
private widgetApiSubscription: Subscription;
private requestSubscription: Subscription;
private responseSubscription: Subscription;
// The capabilities we support
protected supportsScreenshots = false;
protected supportsStickers = false;
protected supportsAlwaysOnScreen = false;
// Stuff about the client
protected clientWidgetApiVersions = [];
public ngOnInit() {
this.widgetApiSubscription = ScalarWidgetApi.requestReceived.subscribe(request => {
this.requestSubscription = ScalarWidgetApi.requestReceived.subscribe(request => {
if (request.action === "capabilities") {
const capabilities = [];
@ -22,15 +32,37 @@ export abstract class CapableWidget implements OnInit, OnDestroy {
ScalarWidgetApi.replyCapabilities(request, capabilities);
this.onCapabilitiesSent();
} else if (request.action === "supported_api_versions") {
ScalarWidgetApi.replySupportedVersions(request, WIDGET_API_DIMENSION_VERSIONS);
}
});
this.responseSubscription = ScalarWidgetApi.replyReceived.subscribe(request => {
if (request.action === "supported_api_versions" && request.response) {
this.clientWidgetApiVersions = request.response.supported_versions || [];
this.onSupportedVersionsFound();
}
});
}
public ngOnDestroy() {
if (this.widgetApiSubscription) this.widgetApiSubscription.unsubscribe();
if (this.requestSubscription) this.requestSubscription.unsubscribe();
if (this.responseSubscription) this.responseSubscription.unsubscribe();
}
protected onCapabilitiesSent(): void {
ScalarWidgetApi.requestSupportedVersions();
}
protected onSupportedVersionsFound(): void {
// Nothing to do here.
}
protected doesSupportAtLeastVersion(apiVersion: string): boolean {
for (const version of this.clientWidgetApiVersions) {
if (semver.satisfies(semver.clean(version), `>=${apiVersion}`)) {
return true;
}
}
return false;
}
}

View File

@ -1,7 +1,7 @@
import { ChangeDetectorRef, Component, OnDestroy, OnInit } from "@angular/core";
import { ScalarWidgetApi } from "../../shared/services/scalar/scalar-widget.api";
import { Subscription } from "rxjs";
import { CapableWidget } from "../capable-widget";
import { CapableWidget, WIDGET_API_VERSION_OPENID } from "../capable-widget";
import { ActivatedRoute } from "@angular/router";
import { ScalarServerApiService } from "../../shared/services/scalar/scalar-server-api.service";
import { SessionStorage } from "../../shared/SessionStorage";
@ -41,26 +41,6 @@ export class ReauthExampleWidgetWrapperComponent extends CapableWidget implement
super.ngOnInit();
this.widgetReplySubscription = ScalarWidgetApi.replyReceived.subscribe(async response => {
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.stateMessage = "";
this.changeDetector.detectChanges();
return;
}
if (response.action !== "get_openid") return;
try {
@ -113,11 +93,22 @@ 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();
protected onSupportedVersionsFound(): void {
super.onSupportedVersionsFound();
if (!this.doesSupportAtLeastVersion(WIDGET_API_VERSION_OPENID)) {
this.busy = true;
this.error = true;
this.hasOpenId = false;
this.blocked = false;
this.stateMessage = "Your client is too old to use this widget, sorry";
} else {
this.busy = false;
this.error = false;
this.hasOpenId = false;
this.blocked = false;
this.stateMessage = null;
}
this.changeDetector.detectChanges();
}
public onReauthStart(): void {