2018-05-11 23:58:10 -04:00
|
|
|
import { ScalarToWidgetRequest } from "../../models/scalar-widget-actions";
|
|
|
|
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";
|
2018-05-11 23:58:10 -04:00
|
|
|
|
|
|
|
export class ScalarWidgetApi {
|
|
|
|
|
|
|
|
public static requestReceived: Subject<ScalarToWidgetRequest> = new ReplaySubject();
|
2018-05-14 00:32:13 -04:00
|
|
|
public static widgetId: 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,
|
|
|
|
w: sticker.thumbnail.width / 2,
|
|
|
|
h: sticker.thumbnail.height / 2,
|
|
|
|
thumbnail_url: sticker.thumbnail.mxc,
|
|
|
|
thumbnail_info: {
|
|
|
|
mimetype: sticker.image.mimetype,
|
|
|
|
w: sticker.thumbnail.width / 2,
|
|
|
|
h: sticker.thumbnail.height / 2,
|
|
|
|
},
|
|
|
|
|
|
|
|
// This has to be included in the info object so it makes it to the event
|
|
|
|
dimension: {
|
|
|
|
license: pack.license,
|
|
|
|
author: pack.author,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
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-05-11 23:58:10 -04:00
|
|
|
ScalarWidgetApi.requestReceived.next(event.data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|