2017-05-27 19:45:07 -04:00
|
|
|
import { Injectable } from "@angular/core";
|
|
|
|
import * as randomString from "random-string";
|
2017-08-29 00:08:32 -04:00
|
|
|
import {
|
2017-12-10 04:35:24 -05:00
|
|
|
CanSendEventResponse,
|
2017-12-09 18:34:59 -05:00
|
|
|
JoinRuleStateResponse,
|
2018-03-31 16:37:36 -04:00
|
|
|
MembershipStateResponse,
|
|
|
|
RoomEncryptionStatusResponse,
|
2019-07-11 00:17:16 -04:00
|
|
|
ScalarSuccessResponse,
|
|
|
|
ScalarWidget,
|
2018-03-31 16:37:36 -04:00
|
|
|
SetPowerLevelResponse,
|
2017-08-29 00:08:32 -04:00
|
|
|
WidgetsResponse
|
2018-03-24 19:16:52 -04:00
|
|
|
} from "../../models/server-client-responses";
|
2017-12-24 04:02:57 -05:00
|
|
|
import { EditableWidget } from "../../models/widget";
|
2017-05-27 19:45:07 -04:00
|
|
|
|
|
|
|
@Injectable()
|
2017-12-20 23:28:43 -05:00
|
|
|
export class ScalarClientApiService {
|
2017-05-27 19:45:07 -04:00
|
|
|
|
2017-12-09 18:34:59 -05:00
|
|
|
private static actionMap: { [key: string]: { resolve: (obj: any) => void, reject: (obj: any) => void } } = {};
|
2017-05-27 19:45:07 -04:00
|
|
|
|
2017-12-09 18:34:59 -05:00
|
|
|
public static getAndRemoveActionHandler(requestKey: string): { resolve: (obj: any) => void, reject: (obj: any) => void } {
|
2021-09-01 19:01:01 -04:00
|
|
|
const handler = ScalarClientApiService.actionMap[requestKey];
|
2017-12-20 23:28:43 -05:00
|
|
|
ScalarClientApiService.actionMap[requestKey] = null;
|
2017-05-27 19:45:07 -04:00
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public inviteUser(roomId: string, userId): Promise<ScalarSuccessResponse> {
|
|
|
|
return this.callAction("invite", {
|
|
|
|
room_id: roomId,
|
|
|
|
user_id: userId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public getMembershipState(roomId: string, userId: string): Promise<MembershipStateResponse> {
|
|
|
|
return this.callAction("membership_state", {
|
|
|
|
room_id: roomId,
|
|
|
|
user_id: userId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-04 23:31:31 -04:00
|
|
|
public getJoinRule(roomId: string): Promise<JoinRuleStateResponse> {
|
|
|
|
return this.callAction("join_rules_state", {
|
|
|
|
room_id: roomId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-14 00:32:13 -04:00
|
|
|
public getWidgets(roomId?: string): Promise<WidgetsResponse> {
|
2017-08-29 00:08:32 -04:00
|
|
|
return this.callAction("get_widgets", {
|
|
|
|
room_id: roomId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:44:20 -05:00
|
|
|
public setWidget(roomId: string, widget: EditableWidget): Promise<ScalarSuccessResponse> {
|
2017-08-29 00:08:32 -04:00
|
|
|
return this.callAction("set_widget", {
|
|
|
|
room_id: roomId,
|
|
|
|
widget_id: widget.id,
|
|
|
|
type: widget.type,
|
|
|
|
url: widget.url,
|
|
|
|
name: widget.name,
|
|
|
|
data: widget.data
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-14 00:32:13 -04:00
|
|
|
public setUserWidget(widget: EditableWidget): Promise<ScalarSuccessResponse> {
|
2017-08-29 00:08:32 -04:00
|
|
|
return this.callAction("set_widget", {
|
2018-05-14 00:32:13 -04:00
|
|
|
userWidget: true,
|
2017-08-29 00:08:32 -04:00
|
|
|
widget_id: widget.id,
|
2018-05-14 00:32:13 -04:00
|
|
|
type: widget.type,
|
|
|
|
url: widget.url,
|
|
|
|
name: widget.name,
|
|
|
|
data: widget.data
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-11 00:17:16 -04:00
|
|
|
public deleteWidget(roomId: string, widget: EditableWidget | ScalarWidget): Promise<ScalarSuccessResponse> {
|
2018-05-14 00:32:13 -04:00
|
|
|
const anyWidget: any = widget;
|
|
|
|
return this.callAction("set_widget", {
|
|
|
|
room_id: roomId,
|
|
|
|
widget_id: anyWidget.id || anyWidget.state_key,
|
|
|
|
type: widget.type, // required for some reason
|
|
|
|
url: ""
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-11 00:17:16 -04:00
|
|
|
public deleteUserWidget(widget: EditableWidget | ScalarWidget): Promise<ScalarSuccessResponse> {
|
2018-05-14 00:32:13 -04:00
|
|
|
const anyWidget: any = widget;
|
|
|
|
return this.callAction("set_widget", {
|
|
|
|
userWidget: true,
|
|
|
|
widget_id: anyWidget.id || anyWidget.state_key,
|
2017-08-29 00:08:32 -04:00
|
|
|
type: widget.type, // required for some reason
|
|
|
|
url: ""
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-28 02:53:12 -04:00
|
|
|
public close(): void {
|
|
|
|
this.callAction("close_scalar", {});
|
|
|
|
}
|
|
|
|
|
2017-12-10 04:35:24 -05:00
|
|
|
public canSendEvent(roomId: string, eventType: string, isState: boolean): Promise<CanSendEventResponse> {
|
|
|
|
return this.callAction("can_send_event", {
|
|
|
|
room_id: roomId,
|
|
|
|
event_type: eventType,
|
|
|
|
is_state: isState,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-10 05:17:33 -05:00
|
|
|
public isRoomEncrypted(roomId: string): Promise<RoomEncryptionStatusResponse> {
|
|
|
|
return this.callAction("get_room_enc_state", {
|
|
|
|
room_id: roomId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-31 16:37:36 -04:00
|
|
|
public setUserPowerLevel(roomId: string, userId: string, powerLevel: number): Promise<SetPowerLevelResponse> {
|
|
|
|
return this.callAction("set_bot_power", {
|
|
|
|
room_id: roomId,
|
|
|
|
user_id: userId,
|
|
|
|
level: powerLevel,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-26 00:07:41 -04:00
|
|
|
private callAction(action, payload): Promise<any> {
|
2021-09-01 19:01:01 -04:00
|
|
|
const requestKey = randomString({length: 20});
|
2017-05-27 19:45:07 -04:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!window.opener) {
|
|
|
|
// Mimic an error response from scalar
|
|
|
|
reject({response: {error: {message: "No window.opener", _error: new Error("No window.opener")}}});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-20 23:28:43 -05:00
|
|
|
ScalarClientApiService.actionMap[requestKey] = {
|
2017-05-27 19:45:07 -04:00
|
|
|
resolve: resolve,
|
|
|
|
reject: reject
|
|
|
|
};
|
|
|
|
|
2021-09-01 19:01:01 -04:00
|
|
|
const request = JSON.parse(JSON.stringify(payload));
|
2017-05-27 19:45:07 -04:00
|
|
|
request["request_id"] = requestKey;
|
|
|
|
request["action"] = action;
|
|
|
|
|
|
|
|
window.opener.postMessage(request, "*");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the event listener here to ensure it gets created
|
|
|
|
window.addEventListener("message", event => {
|
|
|
|
if (!event.data) return;
|
|
|
|
|
2021-09-01 19:01:01 -04:00
|
|
|
const requestKey = event.data["request_id"];
|
2017-05-27 19:45:07 -04:00
|
|
|
if (!requestKey) return;
|
|
|
|
|
2021-09-01 19:01:01 -04:00
|
|
|
const action = ScalarClientApiService.getAndRemoveActionHandler(requestKey);
|
2017-05-27 19:45:07 -04:00
|
|
|
if (!action) return;
|
|
|
|
|
|
|
|
if (event.data.response && event.data.response.error) action.reject(event.data);
|
|
|
|
else action.resolve(event.data);
|
|
|
|
});
|