2021-08-11 17:41:29 -04:00
|
|
|
import { Injectable, OnDestroy, OnInit } from "@angular/core";
|
2018-05-12 18:46:08 -04:00
|
|
|
import { Subscription } from "rxjs/Subscription";
|
|
|
|
import { ScalarWidgetApi } from "../shared/services/scalar/scalar-widget.api";
|
2019-03-24 15:13:30 -04:00
|
|
|
import * as semver from "semver";
|
2019-03-24 16:37:51 -04:00
|
|
|
import { FE_ScalarOpenIdRequestBody } from "../shared/models/scalar-server-responses";
|
2019-03-24 15:13:30 -04:00
|
|
|
|
|
|
|
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];
|
2018-05-12 18:46:08 -04:00
|
|
|
|
2019-03-24 16:37:51 -04:00
|
|
|
export interface OpenIdResponse {
|
|
|
|
openId: FE_ScalarOpenIdRequestBody;
|
|
|
|
blocked: boolean;
|
|
|
|
}
|
|
|
|
|
2021-08-11 17:41:29 -04:00
|
|
|
@Injectable()
|
2018-05-12 18:46:08 -04:00
|
|
|
export abstract class CapableWidget implements OnInit, OnDestroy {
|
|
|
|
|
2019-03-24 15:13:30 -04:00
|
|
|
private requestSubscription: Subscription;
|
|
|
|
private responseSubscription: Subscription;
|
2021-08-11 17:41:29 -04:00
|
|
|
private openIdRequest: { resolve: (a: OpenIdResponse) => void } = null;
|
2018-05-12 18:46:08 -04:00
|
|
|
|
|
|
|
// The capabilities we support
|
|
|
|
protected supportsScreenshots = false;
|
2018-05-14 00:32:13 -04:00
|
|
|
protected supportsStickers = false;
|
2018-07-30 23:55:57 -04:00
|
|
|
protected supportsAlwaysOnScreen = false;
|
2018-05-12 18:46:08 -04:00
|
|
|
|
2019-03-24 15:13:30 -04:00
|
|
|
// Stuff about the client
|
|
|
|
protected clientWidgetApiVersions = [];
|
|
|
|
|
2018-05-12 18:46:08 -04:00
|
|
|
public ngOnInit() {
|
2019-03-24 15:13:30 -04:00
|
|
|
this.requestSubscription = ScalarWidgetApi.requestReceived.subscribe(request => {
|
2018-05-12 18:46:08 -04:00
|
|
|
if (request.action === "capabilities") {
|
|
|
|
const capabilities = [];
|
|
|
|
|
|
|
|
if (this.supportsScreenshots) capabilities.push("m.capability.screenshot");
|
2018-05-14 00:32:13 -04:00
|
|
|
if (this.supportsStickers) capabilities.push("m.sticker");
|
2018-07-30 23:55:57 -04:00
|
|
|
if (this.supportsAlwaysOnScreen) capabilities.push("m.always_on_screen");
|
2018-05-12 18:46:08 -04:00
|
|
|
|
|
|
|
ScalarWidgetApi.replyCapabilities(request, capabilities);
|
2018-07-30 23:55:57 -04:00
|
|
|
this.onCapabilitiesSent();
|
2019-03-24 15:13:30 -04:00
|
|
|
} else if (request.action === "supported_api_versions") {
|
|
|
|
ScalarWidgetApi.replySupportedVersions(request, WIDGET_API_DIMENSION_VERSIONS);
|
2019-03-24 16:37:51 -04:00
|
|
|
} else if (request.action === "openid_credentials" && this.openIdRequest) {
|
|
|
|
if (request.data.success) {
|
|
|
|
this.openIdRequest.resolve({openId: request.data, blocked: false});
|
|
|
|
} else {
|
|
|
|
this.openIdRequest.resolve({openId: null, blocked: true});
|
|
|
|
}
|
|
|
|
this.openIdRequest = null;
|
2019-04-01 21:40:40 -04:00
|
|
|
ScalarWidgetApi.replyAcknowledge(request);
|
2019-03-24 15:13:30 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.responseSubscription = ScalarWidgetApi.replyReceived.subscribe(request => {
|
2019-03-24 16:37:51 -04:00
|
|
|
if (!request.response) return;
|
|
|
|
|
|
|
|
if (request.action === "supported_api_versions") {
|
2019-03-24 15:13:30 -04:00
|
|
|
this.clientWidgetApiVersions = request.response.supported_versions || [];
|
|
|
|
this.onSupportedVersionsFound();
|
2019-03-24 16:37:51 -04:00
|
|
|
} else if (request.action === "get_openid" && this.openIdRequest) {
|
|
|
|
if (request.response.state === "allowed") {
|
|
|
|
this.openIdRequest.resolve({openId: request.response, blocked: false});
|
|
|
|
this.openIdRequest = null;
|
|
|
|
} else if (request.response.state === "blocked") {
|
|
|
|
this.openIdRequest.resolve({openId: null, blocked: true});
|
|
|
|
this.openIdRequest = null;
|
|
|
|
}
|
2018-05-12 18:46:08 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnDestroy() {
|
2019-03-24 15:13:30 -04:00
|
|
|
if (this.requestSubscription) this.requestSubscription.unsubscribe();
|
|
|
|
if (this.responseSubscription) this.responseSubscription.unsubscribe();
|
2018-05-12 18:46:08 -04:00
|
|
|
}
|
2018-07-30 23:55:57 -04:00
|
|
|
|
|
|
|
protected onCapabilitiesSent(): void {
|
2019-03-24 15:13:30 -04:00
|
|
|
ScalarWidgetApi.requestSupportedVersions();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected onSupportedVersionsFound(): void {
|
2019-03-15 22:31:07 -04:00
|
|
|
// Nothing to do here.
|
2018-07-30 23:55:57 -04:00
|
|
|
}
|
2019-03-24 15:13:30 -04:00
|
|
|
|
|
|
|
protected doesSupportAtLeastVersion(apiVersion: string): boolean {
|
|
|
|
for (const version of this.clientWidgetApiVersions) {
|
|
|
|
if (semver.satisfies(semver.clean(version), `>=${apiVersion}`)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-24 16:37:51 -04:00
|
|
|
|
|
|
|
protected getOpenIdInfo(): Promise<OpenIdResponse> {
|
|
|
|
const promise = new Promise<OpenIdResponse>(((resolve, _reject) => {
|
2021-08-11 17:41:29 -04:00
|
|
|
this.openIdRequest = {resolve: resolve};
|
2019-03-24 16:37:51 -04:00
|
|
|
ScalarWidgetApi.requestOpenID();
|
|
|
|
}));
|
|
|
|
return promise;
|
|
|
|
}
|
2018-05-12 18:46:08 -04:00
|
|
|
}
|