2019-03-13 02:28:12 -04:00
|
|
|
import { ScalarFromWidgetResponse, ScalarToWidgetRequest } from "../../models/scalar-widget-actions";
|
2018-05-11 23:58:10 -04:00
|
|
|
import { ReplaySubject } from "rxjs/ReplaySubject";
|
|
|
|
import { Subject } from "rxjs/Subject";
|
2018-05-14 00:32:13 -04:00
|
|
|
import { FE_Sticker, FE_StickerPack } from "../../models/integration";
|
2019-03-15 22:16:13 -04:00
|
|
|
import * as randomString from "random-string";
|
2018-05-11 23:58:10 -04:00
|
|
|
|
|
|
|
export class ScalarWidgetApi {
|
|
|
|
|
|
|
|
public static requestReceived: Subject<ScalarToWidgetRequest> = new ReplaySubject();
|
2019-03-13 02:28:12 -04:00
|
|
|
public static replyReceived: Subject<ScalarFromWidgetResponse> = new ReplaySubject();
|
2018-05-14 00:32:13 -04:00
|
|
|
public static widgetId: string;
|
2019-03-13 02:28:12 -04:00
|
|
|
public static inFlightRequestIds: string[] = [];
|
2018-05-11 23:58:10 -04:00
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static replyScreenshot(request: ScalarToWidgetRequest, data: Blob): void {
|
|
|
|
ScalarWidgetApi.replyEvent(request, {screenshot: data});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static replySupportedVersions(request: ScalarToWidgetRequest, versions: string[]): void {
|
|
|
|
ScalarWidgetApi.replyEvent(request, {supported_versions: versions});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static replyCapabilities(request: ScalarToWidgetRequest,
|
|
|
|
capabilities: (
|
|
|
|
"m.text" | "m.image" | "m.sticker" | "m.capability.screenshot" | "m.capability.request_data" |
|
|
|
|
"m.capability.request_messages" | "m.capability.room_membership" | string)[]
|
|
|
|
): void {
|
|
|
|
ScalarWidgetApi.replyEvent(request, {capabilities: capabilities});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static replyError(request: ScalarToWidgetRequest, error: Error, message: string = null): void {
|
2018-05-12 18:46:08 -04:00
|
|
|
ScalarWidgetApi.replyEvent(request, {error: {message: message || error.message, _error: error}});
|
2018-05-11 23:58:10 -04:00
|
|
|
}
|
|
|
|
|
2018-05-14 00:32:13 -04:00
|
|
|
public static replyAcknowledge(request: ScalarToWidgetRequest): void {
|
|
|
|
ScalarWidgetApi.replyEvent(request, {success: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static sendSticker(sticker: FE_Sticker, pack: FE_StickerPack): void {
|
|
|
|
ScalarWidgetApi.callAction("m.sticker", {
|
|
|
|
data: {
|
|
|
|
description: sticker.description,
|
|
|
|
content: {
|
|
|
|
url: sticker.thumbnail.mxc,
|
|
|
|
info: {
|
|
|
|
mimetype: sticker.image.mimetype,
|
2021-01-23 08:42:14 -05:00
|
|
|
w: Math.round(sticker.thumbnail.width / 2),
|
|
|
|
h: Math.round(sticker.thumbnail.height / 2),
|
2018-05-14 00:32:13 -04:00
|
|
|
thumbnail_url: sticker.thumbnail.mxc,
|
|
|
|
thumbnail_info: {
|
|
|
|
mimetype: sticker.image.mimetype,
|
2021-01-23 08:42:14 -05:00
|
|
|
w: Math.round(sticker.thumbnail.width / 2),
|
|
|
|
h: Math.round(sticker.thumbnail.height / 2),
|
2018-05-14 00:32:13 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// This has to be included in the info object so it makes it to the event
|
|
|
|
dimension: {
|
|
|
|
license: pack.license,
|
|
|
|
author: pack.author,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-30 23:55:57 -04:00
|
|
|
public static sendSetAlwaysOnScreen(alwaysVisible: boolean): void {
|
|
|
|
ScalarWidgetApi.callAction("set_always_on_screen", {
|
2020-07-15 20:24:23 -04:00
|
|
|
// Send the value here and in data due to a Element bug.
|
2018-07-30 23:55:57 -04:00
|
|
|
data: {
|
|
|
|
value: alwaysVisible,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-24 01:55:29 -04:00
|
|
|
public static openIntegrationManager(integrationType: string, integrationId: string): void {
|
|
|
|
ScalarWidgetApi.callAction("integration_manager_open", {
|
|
|
|
data: {
|
|
|
|
integType: integrationType,
|
|
|
|
integId: integrationId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-13 02:28:12 -04:00
|
|
|
public static requestOpenID(): void {
|
|
|
|
ScalarWidgetApi.callAction("get_openid", {});
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:46:04 -04:00
|
|
|
public static requestSupportedVersions(): void {
|
|
|
|
ScalarWidgetApi.callAction("supported_api_versions", {});
|
|
|
|
}
|
|
|
|
|
2018-05-11 23:58:10 -04:00
|
|
|
private static callAction(action, payload) {
|
|
|
|
if (!window.opener) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let request = JSON.parse(JSON.stringify(payload));
|
|
|
|
request["api"] = "fromWidget";
|
|
|
|
request["widgetId"] = ScalarWidgetApi.widgetId;
|
|
|
|
request["action"] = action;
|
2019-03-15 22:16:13 -04:00
|
|
|
request["requestId"] = randomString({length: 160});
|
2018-05-11 23:58:10 -04:00
|
|
|
|
2019-03-13 02:28:12 -04:00
|
|
|
ScalarWidgetApi.inFlightRequestIds.push(request["requestId"]);
|
|
|
|
|
2019-03-15 22:31:07 -04:00
|
|
|
console.log("[Dimension] Sending fromWidget: ", request);
|
2018-05-11 23:58:10 -04:00
|
|
|
window.opener.postMessage(request, "*");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static replyEvent(request: ScalarToWidgetRequest, payload) {
|
|
|
|
if (!window.opener) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let requestClone = JSON.parse(JSON.stringify(request));
|
2018-05-12 17:58:23 -04:00
|
|
|
requestClone["response"] = payload;
|
2019-03-15 22:31:07 -04:00
|
|
|
|
|
|
|
console.log("[Dimension] Sending postMessage response: ", request);
|
2018-05-11 23:58:10 -04:00
|
|
|
window.opener.postMessage(requestClone, "*");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the event listener here to ensure it gets created
|
|
|
|
window.addEventListener("message", event => {
|
|
|
|
if (!event.data) return;
|
|
|
|
|
|
|
|
if (event.data.api === "toWidget" && event.data.action) {
|
2018-05-14 00:32:13 -04:00
|
|
|
if (event.data.widgetId && !ScalarWidgetApi.widgetId) ScalarWidgetApi.widgetId = event.data.widgetId;
|
2018-10-17 21:53:26 -04:00
|
|
|
console.log(`[Dimension] Received toWidget request at widget ID ${ScalarWidgetApi.widgetId}: ${JSON.stringify(event.data)}`);
|
2018-05-11 23:58:10 -04:00
|
|
|
ScalarWidgetApi.requestReceived.next(event.data);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-13 02:28:12 -04:00
|
|
|
|
|
|
|
if (event.data.api === "fromWidget" && ScalarWidgetApi.inFlightRequestIds.indexOf(event.data.requestId) !== -1) {
|
|
|
|
if (event.data.widgetId && !ScalarWidgetApi.widgetId) ScalarWidgetApi.widgetId = event.data.widgetId;
|
|
|
|
console.log(`[Dimension] Received fromWidget response at widget ID ${ScalarWidgetApi.widgetId}: ${JSON.stringify(event.data)}`);
|
|
|
|
|
|
|
|
const idx = ScalarWidgetApi.inFlightRequestIds.indexOf(event.data.requestId);
|
|
|
|
ScalarWidgetApi.inFlightRequestIds.splice(idx, 1);
|
|
|
|
ScalarWidgetApi.replyReceived.next(event.data);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-05-11 23:58:10 -04:00
|
|
|
});
|