2017-07-27 23:37:33 +01:00
|
|
|
/*
|
2019-03-23 23:25:31 -06:00
|
|
|
Copyright 2019 Travis Ralston
|
2020-10-21 12:44:16 +01:00
|
|
|
Copyright 2017 - 2020 The Matrix.org Foundation C.I.C.
|
2017-07-27 23:37:33 +01:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-09-23 11:01:19 +01:00
|
|
|
import * as url from "url";
|
2022-03-22 18:14:11 -04:00
|
|
|
import { base32 } from "rfc4648";
|
2022-05-06 13:09:53 -06:00
|
|
|
import { IWidget, IWidgetData } from "matrix-widget-api";
|
2021-06-18 16:21:46 +01:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-02-22 12:18:08 +00:00
|
|
|
import { ClientEvent, RoomStateEvent } from "matrix-js-sdk/src/matrix";
|
2022-03-22 18:14:11 -04:00
|
|
|
import { CallType } from "matrix-js-sdk/src/webrtc/call";
|
2022-06-02 09:10:22 -04:00
|
|
|
import { randomString, randomLowercaseString, randomUppercaseString } from "matrix-js-sdk/src/randomstring";
|
2020-09-23 11:01:19 +01:00
|
|
|
|
2021-06-18 16:21:46 +01:00
|
|
|
import { MatrixClientPeg } from "../MatrixClientPeg";
|
2022-07-12 08:23:35 -04:00
|
|
|
import PlatformPeg from "../PlatformPeg";
|
2018-06-26 11:59:16 +01:00
|
|
|
import SdkConfig from "../SdkConfig";
|
2020-05-13 20:41:41 -06:00
|
|
|
import dis from "../dispatcher/dispatcher";
|
2018-07-03 11:16:44 +01:00
|
|
|
import WidgetEchoStore from "../stores/WidgetEchoStore";
|
2021-06-18 16:21:46 +01:00
|
|
|
import { IntegrationManagers } from "../integrations/IntegrationManagers";
|
|
|
|
|
import { WidgetType } from "../widgets/WidgetType";
|
2022-03-22 18:14:11 -04:00
|
|
|
import { Jitsi } from "../widgets/Jitsi";
|
2021-06-18 16:21:46 +01:00
|
|
|
import { objectClone } from "./objects";
|
|
|
|
|
import { _t } from "../languageHandler";
|
|
|
|
|
import { IApp } from "../stores/WidgetStore";
|
2020-09-23 11:01:19 +01:00
|
|
|
|
|
|
|
|
// How long we wait for the state event echo to come back from the server
|
|
|
|
|
// before waitFor[Room/User]Widget rejects its promise
|
|
|
|
|
const WIDGET_WAIT_TIME = 20000;
|
|
|
|
|
|
2020-10-19 20:56:41 +01:00
|
|
|
export interface IWidgetEvent {
|
2020-09-23 11:01:19 +01:00
|
|
|
id: string;
|
|
|
|
|
type: string;
|
|
|
|
|
sender: string;
|
2020-09-24 09:49:30 +01:00
|
|
|
// eslint-disable-next-line camelcase
|
2020-09-23 11:01:19 +01:00
|
|
|
state_key: string;
|
2023-05-10 08:41:55 +01:00
|
|
|
content: IApp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UserWidget extends Omit<IWidgetEvent, "content"> {
|
|
|
|
|
content: IWidget & Partial<IApp>;
|
2020-09-23 11:01:19 +01:00
|
|
|
}
|
2018-07-12 18:43:49 +01:00
|
|
|
|
2017-07-27 23:37:33 +01:00
|
|
|
export default class WidgetUtils {
|
|
|
|
|
/* Returns true if user is able to send state events to modify widgets in this room
|
2018-04-02 10:02:41 +01:00
|
|
|
* (Does not apply to non-room-based / user widgets)
|
2017-07-27 23:37:33 +01:00
|
|
|
* @param roomId -- The ID of the room to check
|
|
|
|
|
* @return Boolean -- true if the user can modify widgets in this room
|
2017-07-28 16:19:20 +01:00
|
|
|
* @throws Error -- specifies the error reason
|
2017-07-27 23:37:33 +01:00
|
|
|
*/
|
2023-03-08 11:48:58 +00:00
|
|
|
public static canUserModifyWidgets(roomId?: string): boolean {
|
2017-07-27 23:37:33 +01:00
|
|
|
if (!roomId) {
|
2021-10-15 16:31:29 +02:00
|
|
|
logger.warn("No room ID specified");
|
2017-08-01 11:39:17 +01:00
|
|
|
return false;
|
2017-07-27 23:37:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
if (!client) {
|
2021-10-15 16:31:29 +02:00
|
|
|
logger.warn("User must be be logged in");
|
2017-08-01 11:39:17 +01:00
|
|
|
return false;
|
2017-07-27 23:37:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
|
if (!room) {
|
2021-10-15 16:31:29 +02:00
|
|
|
logger.warn(`Room ID ${roomId} is not recognised`);
|
2017-08-01 11:39:17 +01:00
|
|
|
return false;
|
2017-07-27 23:37:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const me = client.credentials.userId;
|
|
|
|
|
if (!me) {
|
2021-10-15 16:31:29 +02:00
|
|
|
logger.warn("Failed to get user ID");
|
2017-08-01 11:39:17 +01:00
|
|
|
return false;
|
2017-07-27 23:37:33 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-02 11:42:05 +02:00
|
|
|
if (room.getMyMembership() !== "join") {
|
2021-10-15 16:31:29 +02:00
|
|
|
logger.warn(`User ${me} is not in room ${roomId}`);
|
2017-08-01 11:39:17 +01:00
|
|
|
return false;
|
2017-07-27 23:37:33 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 16:02:26 +01:00
|
|
|
// TODO: Enable support for m.widget event type (https://github.com/vector-im/element-web/issues/13111)
|
2017-08-01 11:41:41 +01:00
|
|
|
return room.currentState.maySendStateEvent("im.vector.modular.widgets", me);
|
2017-07-27 23:37:33 +01:00
|
|
|
}
|
2018-05-27 11:12:55 -06:00
|
|
|
|
2020-04-22 00:34:08 -06:00
|
|
|
// TODO: Generify the name of this function. It's not just scalar.
|
2018-05-27 11:12:55 -06:00
|
|
|
/**
|
|
|
|
|
* Returns true if specified url is a scalar URL, typically https://scalar.vector.im/api
|
|
|
|
|
* @param {[type]} testUrlString URL to check
|
|
|
|
|
* @return {Boolean} True if specified URL is a scalar URL
|
|
|
|
|
*/
|
2023-02-16 17:21:44 +00:00
|
|
|
public static isScalarUrl(testUrlString?: string): boolean {
|
2018-05-27 11:12:55 -06:00
|
|
|
if (!testUrlString) {
|
2021-10-15 16:30:53 +02:00
|
|
|
logger.error("Scalar URL check failed. No URL specified");
|
2018-05-27 11:12:55 -06:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const testUrl = url.parse(testUrlString);
|
|
|
|
|
let scalarUrls = SdkConfig.get().integrations_widgets_urls;
|
|
|
|
|
if (!scalarUrls || scalarUrls.length === 0) {
|
2019-08-09 17:08:26 -06:00
|
|
|
const defaultManager = IntegrationManagers.sharedInstance().getPrimaryManager();
|
2019-09-16 10:12:15 +02:00
|
|
|
if (defaultManager) {
|
|
|
|
|
scalarUrls = [defaultManager.apiUrl];
|
|
|
|
|
} else {
|
|
|
|
|
scalarUrls = [];
|
|
|
|
|
}
|
2018-05-27 11:12:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < scalarUrls.length; i++) {
|
|
|
|
|
const scalarUrl = url.parse(scalarUrls[i]);
|
|
|
|
|
if (testUrl && scalarUrl) {
|
|
|
|
|
if (
|
|
|
|
|
testUrl.protocol === scalarUrl.protocol &&
|
|
|
|
|
testUrl.host === scalarUrl.host &&
|
2023-04-03 20:26:55 +12:00
|
|
|
scalarUrl.pathname &&
|
2023-02-13 17:01:43 +00:00
|
|
|
testUrl.pathname?.startsWith(scalarUrl.pathname)
|
2018-05-27 11:12:55 -06:00
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-06-13 10:39:52 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a promise that resolves when a widget with the given
|
|
|
|
|
* ID has been added as a user widget (ie. the accountData event
|
|
|
|
|
* arrives) or rejects after a timeout
|
|
|
|
|
*
|
|
|
|
|
* @param {string} widgetId The ID of the widget to wait for
|
|
|
|
|
* @param {boolean} add True to wait for the widget to be added,
|
|
|
|
|
* false to wait for it to be deleted.
|
2018-06-14 13:49:23 +01:00
|
|
|
* @returns {Promise} that resolves when the widget is in the
|
2018-06-14 13:03:42 +01:00
|
|
|
* requested state according to the `add` param
|
2018-06-13 10:39:52 +01:00
|
|
|
*/
|
2022-12-16 12:29:59 +00:00
|
|
|
public static waitForUserWidget(widgetId: string, add: boolean): Promise<void> {
|
2018-06-13 10:39:52 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
// Tests an account data event, returning true if it's in the state
|
|
|
|
|
// we're waiting for it to be in
|
2023-02-13 17:01:43 +00:00
|
|
|
function eventInIntendedState(ev?: MatrixEvent): boolean {
|
|
|
|
|
if (!ev) return false;
|
2018-06-13 10:39:52 +01:00
|
|
|
if (add) {
|
|
|
|
|
return ev.getContent()[widgetId] !== undefined;
|
|
|
|
|
} else {
|
|
|
|
|
return ev.getContent()[widgetId] === undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 15:50:19 +01:00
|
|
|
const startingAccountDataEvent = MatrixClientPeg.get().getAccountData("m.widgets");
|
|
|
|
|
if (eventInIntendedState(startingAccountDataEvent)) {
|
2018-06-13 10:39:52 +01:00
|
|
|
resolve();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
function onAccountData(ev: MatrixEvent): void {
|
2018-06-13 15:50:19 +01:00
|
|
|
const currentAccountDataEvent = MatrixClientPeg.get().getAccountData("m.widgets");
|
2018-06-13 10:39:52 +01:00
|
|
|
if (eventInIntendedState(currentAccountDataEvent)) {
|
2022-02-22 12:18:08 +00:00
|
|
|
MatrixClientPeg.get().removeListener(ClientEvent.AccountData, onAccountData);
|
2018-06-13 10:39:52 +01:00
|
|
|
clearTimeout(timerId);
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-30 11:32:56 +00:00
|
|
|
const timerId = window.setTimeout(() => {
|
2022-02-22 12:18:08 +00:00
|
|
|
MatrixClientPeg.get().removeListener(ClientEvent.AccountData, onAccountData);
|
2018-06-13 10:39:52 +01:00
|
|
|
reject(new Error("Timed out waiting for widget ID " + widgetId + " to appear"));
|
2018-07-03 11:55:41 +01:00
|
|
|
}, WIDGET_WAIT_TIME);
|
2022-02-22 12:18:08 +00:00
|
|
|
MatrixClientPeg.get().on(ClientEvent.AccountData, onAccountData);
|
2018-06-13 10:39:52 +01:00
|
|
|
});
|
|
|
|
|
}
|
2018-06-13 15:50:19 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a promise that resolves when a widget with the given
|
|
|
|
|
* ID has been added as a room widget in the given room (ie. the
|
|
|
|
|
* room state event arrives) or rejects after a timeout
|
|
|
|
|
*
|
|
|
|
|
* @param {string} widgetId The ID of the widget to wait for
|
|
|
|
|
* @param {string} roomId The ID of the room to wait for the widget in
|
|
|
|
|
* @param {boolean} add True to wait for the widget to be added,
|
|
|
|
|
* false to wait for it to be deleted.
|
2018-06-14 13:49:23 +01:00
|
|
|
* @returns {Promise} that resolves when the widget is in the
|
2018-06-14 13:03:42 +01:00
|
|
|
* requested state according to the `add` param
|
2018-06-13 15:50:19 +01:00
|
|
|
*/
|
2022-12-16 12:29:59 +00:00
|
|
|
public static waitForRoomWidget(widgetId: string, roomId: string, add: boolean): Promise<void> {
|
2018-06-13 15:50:19 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
// Tests a list of state events, returning true if it's in the state
|
|
|
|
|
// we're waiting for it to be in
|
2023-02-13 17:01:43 +00:00
|
|
|
function eventsInIntendedState(evList?: MatrixEvent[]): boolean {
|
|
|
|
|
const widgetPresent = evList?.some((ev) => {
|
2018-06-13 15:50:19 +01:00
|
|
|
return ev.getContent() && ev.getContent()["id"] === widgetId;
|
|
|
|
|
});
|
|
|
|
|
if (add) {
|
2023-02-13 17:01:43 +00:00
|
|
|
return !!widgetPresent;
|
2018-06-13 15:50:19 +01:00
|
|
|
} else {
|
|
|
|
|
return !widgetPresent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const room = MatrixClientPeg.get().getRoom(roomId);
|
2020-08-03 16:02:26 +01:00
|
|
|
// TODO: Enable support for m.widget event type (https://github.com/vector-im/element-web/issues/13111)
|
2023-02-13 17:01:43 +00:00
|
|
|
const startingWidgetEvents = room?.currentState.getStateEvents("im.vector.modular.widgets");
|
2018-06-13 15:50:19 +01:00
|
|
|
if (eventsInIntendedState(startingWidgetEvents)) {
|
|
|
|
|
resolve();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
function onRoomStateEvents(ev: MatrixEvent): void {
|
2022-02-24 14:39:25 +00:00
|
|
|
if (ev.getRoomId() !== roomId || ev.getType() !== "im.vector.modular.widgets") return;
|
2018-06-13 15:50:19 +01:00
|
|
|
|
2020-08-03 16:02:26 +01:00
|
|
|
// TODO: Enable support for m.widget event type (https://github.com/vector-im/element-web/issues/13111)
|
2023-02-13 17:01:43 +00:00
|
|
|
const currentWidgetEvents = room?.currentState.getStateEvents("im.vector.modular.widgets");
|
2018-06-13 15:50:19 +01:00
|
|
|
|
|
|
|
|
if (eventsInIntendedState(currentWidgetEvents)) {
|
2022-02-22 12:18:08 +00:00
|
|
|
MatrixClientPeg.get().removeListener(RoomStateEvent.Events, onRoomStateEvents);
|
2018-06-13 15:50:19 +01:00
|
|
|
clearTimeout(timerId);
|
|
|
|
|
resolve();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-30 11:32:56 +00:00
|
|
|
const timerId = window.setTimeout(() => {
|
2022-02-22 12:18:08 +00:00
|
|
|
MatrixClientPeg.get().removeListener(RoomStateEvent.Events, onRoomStateEvents);
|
2018-06-13 15:50:19 +01:00
|
|
|
reject(new Error("Timed out waiting for widget ID " + widgetId + " to appear"));
|
2018-07-03 11:55:41 +01:00
|
|
|
}, WIDGET_WAIT_TIME);
|
2022-02-22 12:18:08 +00:00
|
|
|
MatrixClientPeg.get().on(RoomStateEvent.Events, onRoomStateEvents);
|
2018-06-13 15:50:19 +01:00
|
|
|
});
|
|
|
|
|
}
|
2018-06-25 15:30:04 +01:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public static setUserWidget(
|
2020-09-24 09:49:30 +01:00
|
|
|
widgetId: string,
|
|
|
|
|
widgetType: WidgetType,
|
|
|
|
|
widgetUrl: string,
|
|
|
|
|
widgetName: string,
|
2020-10-19 20:56:41 +01:00
|
|
|
widgetData: IWidgetData,
|
2023-01-12 13:25:14 +00:00
|
|
|
): Promise<void> {
|
2018-06-25 15:30:04 +01:00
|
|
|
const client = MatrixClientPeg.get();
|
2019-10-02 15:26:54 +01:00
|
|
|
// Get the current widgets and clone them before we modify them, otherwise
|
|
|
|
|
// we'll modify the content of the old event.
|
2020-06-22 14:14:43 -06:00
|
|
|
const userWidgets = objectClone(WidgetUtils.getUserWidgets());
|
2018-06-25 15:30:04 +01:00
|
|
|
|
|
|
|
|
// Delete existing widget with ID
|
|
|
|
|
try {
|
|
|
|
|
delete userWidgets[widgetId];
|
|
|
|
|
} catch (e) {
|
2021-10-15 16:30:53 +02:00
|
|
|
logger.error(`$widgetId is non-configurable`);
|
2018-06-25 15:30:04 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-26 16:33:28 +01:00
|
|
|
const addingWidget = Boolean(widgetUrl);
|
2018-06-26 15:21:22 +01:00
|
|
|
|
2023-03-23 13:25:16 -04:00
|
|
|
const userId = client.getSafeUserId();
|
|
|
|
|
|
|
|
|
|
const content = {
|
2023-05-10 08:41:55 +01:00
|
|
|
id: widgetId,
|
2023-03-23 13:25:16 -04:00
|
|
|
type: widgetType.preferred,
|
|
|
|
|
url: widgetUrl,
|
|
|
|
|
name: widgetName,
|
|
|
|
|
data: widgetData,
|
|
|
|
|
creatorUserId: userId,
|
|
|
|
|
};
|
|
|
|
|
|
2018-06-25 15:30:04 +01:00
|
|
|
// Add new widget / update
|
2018-06-26 15:21:22 +01:00
|
|
|
if (addingWidget) {
|
2018-06-25 15:30:04 +01:00
|
|
|
userWidgets[widgetId] = {
|
|
|
|
|
content: content,
|
2023-03-23 13:25:16 -04:00
|
|
|
sender: userId,
|
2018-06-25 15:30:04 +01:00
|
|
|
state_key: widgetId,
|
|
|
|
|
type: "m.widget",
|
|
|
|
|
id: widgetId,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This starts listening for when the echo comes back from the server
|
|
|
|
|
// since the widget won't appear added until this happens. If we don't
|
|
|
|
|
// wait for this, the action will complete but if the user is fast enough,
|
|
|
|
|
// the widget still won't actually be there.
|
|
|
|
|
return client
|
|
|
|
|
.setAccountData("m.widgets", userWidgets)
|
|
|
|
|
.then(() => {
|
2018-06-26 15:21:22 +01:00
|
|
|
return WidgetUtils.waitForUserWidget(widgetId, addingWidget);
|
2018-06-25 15:30:04 +01:00
|
|
|
})
|
|
|
|
|
.then(() => {
|
|
|
|
|
dis.dispatch({ action: "user_widget_updated" });
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public static setRoomWidget(
|
2020-09-24 09:49:30 +01:00
|
|
|
roomId: string,
|
|
|
|
|
widgetId: string,
|
2020-10-19 21:02:48 +01:00
|
|
|
widgetType?: WidgetType,
|
|
|
|
|
widgetUrl?: string,
|
|
|
|
|
widgetName?: string,
|
2023-01-12 13:25:14 +00:00
|
|
|
widgetData?: IWidgetData,
|
2022-05-20 00:22:38 +02:00
|
|
|
widgetAvatarUrl?: string,
|
2023-01-12 13:25:14 +00:00
|
|
|
): Promise<void> {
|
|
|
|
|
let content: Partial<IWidget> & { avatar_url?: string };
|
2018-06-25 15:30:04 +01:00
|
|
|
|
2018-06-26 16:33:28 +01:00
|
|
|
const addingWidget = Boolean(widgetUrl);
|
2018-06-26 15:21:22 +01:00
|
|
|
|
|
|
|
|
if (addingWidget) {
|
2018-06-25 15:30:04 +01:00
|
|
|
content = {
|
2020-08-03 16:02:26 +01:00
|
|
|
// TODO: Enable support for m.widget event type (https://github.com/vector-im/element-web/issues/13111)
|
|
|
|
|
// For now we'll send the legacy event type for compatibility with older apps/elements
|
2023-02-13 17:01:43 +00:00
|
|
|
type: widgetType?.legacy,
|
2018-06-25 15:30:04 +01:00
|
|
|
url: widgetUrl,
|
|
|
|
|
name: widgetName,
|
|
|
|
|
data: widgetData,
|
2022-05-20 00:22:38 +02:00
|
|
|
avatar_url: widgetAvatarUrl,
|
2018-06-25 15:30:04 +01:00
|
|
|
};
|
2018-06-26 15:21:22 +01:00
|
|
|
} else {
|
|
|
|
|
content = {};
|
2018-06-25 15:30:04 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
return WidgetUtils.setRoomWidgetContent(roomId, widgetId, content as IWidget);
|
2021-01-29 14:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public static setRoomWidgetContent(roomId: string, widgetId: string, content: IWidget): Promise<void> {
|
2021-01-29 14:26:33 +00:00
|
|
|
const addingWidget = !!content.url;
|
|
|
|
|
|
2018-07-05 18:43:20 +01:00
|
|
|
WidgetEchoStore.setRoomWidgetEcho(roomId, widgetId, content);
|
2018-07-03 11:16:44 +01:00
|
|
|
|
2018-06-25 15:30:04 +01:00
|
|
|
const client = MatrixClientPeg.get();
|
2020-08-03 16:02:26 +01:00
|
|
|
// TODO: Enable support for m.widget event type (https://github.com/vector-im/element-web/issues/13111)
|
2018-06-25 15:30:04 +01:00
|
|
|
return client
|
|
|
|
|
.sendStateEvent(roomId, "im.vector.modular.widgets", content, widgetId)
|
|
|
|
|
.then(() => {
|
2018-06-26 15:21:22 +01:00
|
|
|
return WidgetUtils.waitForRoomWidget(widgetId, roomId, addingWidget);
|
2018-07-03 11:16:44 +01:00
|
|
|
})
|
|
|
|
|
.finally(() => {
|
2018-07-05 18:43:20 +01:00
|
|
|
WidgetEchoStore.removeRoomWidgetEcho(roomId, widgetId);
|
2018-06-25 15:30:04 +01:00
|
|
|
});
|
|
|
|
|
}
|
2018-06-26 11:52:21 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get room specific widgets
|
2020-04-09 15:11:57 -06:00
|
|
|
* @param {Room} room The room to get widgets force
|
2018-06-26 11:52:21 +01:00
|
|
|
* @return {[object]} Array containing current / active room widgets
|
|
|
|
|
*/
|
2023-01-12 13:25:14 +00:00
|
|
|
public static getRoomWidgets(room: Room): MatrixEvent[] {
|
2020-08-03 16:02:26 +01:00
|
|
|
// TODO: Enable support for m.widget event type (https://github.com/vector-im/element-web/issues/13111)
|
2018-06-26 11:52:21 +01:00
|
|
|
const appsStateEvents = room.currentState.getStateEvents("im.vector.modular.widgets");
|
|
|
|
|
if (!appsStateEvents) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return appsStateEvents.filter((ev) => {
|
|
|
|
|
return ev.getContent().type && ev.getContent().url;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get user specific widgets (not linked to a specific room)
|
|
|
|
|
* @return {object} Event content object containing current / active user widgets
|
|
|
|
|
*/
|
2023-05-10 08:41:55 +01:00
|
|
|
public static getUserWidgets(): Record<string, UserWidget> {
|
2018-06-26 11:52:21 +01:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
if (!client) {
|
|
|
|
|
throw new Error("User not logged in");
|
|
|
|
|
}
|
|
|
|
|
const userWidgets = client.getAccountData("m.widgets");
|
|
|
|
|
if (userWidgets && userWidgets.getContent()) {
|
2018-06-26 15:41:43 +01:00
|
|
|
return userWidgets.getContent();
|
2018-06-26 11:52:21 +01:00
|
|
|
}
|
2018-06-26 15:41:43 +01:00
|
|
|
return {};
|
2018-06-26 11:52:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get user specific widgets (not linked to a specific room) as an array
|
|
|
|
|
* @return {[object]} Array containing current / active user widgets
|
|
|
|
|
*/
|
2023-05-10 08:41:55 +01:00
|
|
|
public static getUserWidgetsArray(): UserWidget[] {
|
2018-06-26 15:42:29 +01:00
|
|
|
return Object.values(WidgetUtils.getUserWidgets());
|
2018-06-26 11:52:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get active stickerpicker widgets (stickerpickers are user widgets by nature)
|
|
|
|
|
* @return {[object]} Array containing current / active stickerpicker widgets
|
|
|
|
|
*/
|
2023-05-10 08:41:55 +01:00
|
|
|
public static getStickerpickerWidgets(): UserWidget[] {
|
2018-06-26 11:52:21 +01:00
|
|
|
const widgets = WidgetUtils.getUserWidgetsArray();
|
2023-05-10 08:41:55 +01:00
|
|
|
return widgets.filter((widget) => widget.content?.type === "m.stickerpicker");
|
2018-06-26 11:52:21 +01:00
|
|
|
}
|
|
|
|
|
|
2019-08-09 17:35:59 -06:00
|
|
|
/**
|
|
|
|
|
* Get all integration manager widgets for this user.
|
|
|
|
|
* @returns {Object[]} An array of integration manager user widgets.
|
|
|
|
|
*/
|
2023-05-10 08:41:55 +01:00
|
|
|
public static getIntegrationManagerWidgets(): UserWidget[] {
|
2019-08-09 17:35:59 -06:00
|
|
|
const widgets = WidgetUtils.getUserWidgetsArray();
|
2023-05-10 08:41:55 +01:00
|
|
|
return widgets.filter((w) => w.content?.type === "m.integration_manager");
|
2019-08-09 17:35:59 -06:00
|
|
|
}
|
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public static getRoomWidgetsOfType(room: Room, type: WidgetType): MatrixEvent[] {
|
2021-06-18 16:21:46 +01:00
|
|
|
const widgets = WidgetUtils.getRoomWidgets(room) || [];
|
|
|
|
|
return widgets.filter((w) => {
|
2020-04-09 15:11:57 -06:00
|
|
|
const content = w.getContent();
|
|
|
|
|
return content.url && type.matches(content.type);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public static async removeIntegrationManagerWidgets(): Promise<void> {
|
2019-08-12 15:35:39 -06:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
if (!client) {
|
|
|
|
|
throw new Error("User not logged in");
|
|
|
|
|
}
|
2019-08-21 18:43:29 -06:00
|
|
|
const widgets = client.getAccountData("m.widgets");
|
|
|
|
|
if (!widgets) return;
|
2021-06-17 14:49:27 +01:00
|
|
|
const userWidgets: Record<string, IWidgetEvent> = widgets.getContent() || {};
|
2019-08-12 15:35:39 -06:00
|
|
|
Object.entries(userWidgets).forEach(([key, widget]) => {
|
2019-08-14 09:33:02 -06:00
|
|
|
if (widget.content && widget.content.type === "m.integration_manager") {
|
2019-08-12 15:35:39 -06:00
|
|
|
delete userWidgets[key];
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-07-10 15:43:46 +01:00
|
|
|
await client.setAccountData("m.widgets", userWidgets);
|
2019-08-12 15:35:39 -06:00
|
|
|
}
|
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public static addIntegrationManagerWidget(name: string, uiUrl: string, apiUrl: string): Promise<void> {
|
2019-08-14 08:57:38 -06:00
|
|
|
return WidgetUtils.setUserWidget(
|
|
|
|
|
"integration_manager_" + new Date().getTime(),
|
2020-04-21 16:01:10 -06:00
|
|
|
WidgetType.INTEGRATION_MANAGER,
|
2019-08-14 08:57:38 -06:00
|
|
|
uiUrl,
|
2021-07-13 16:04:50 +01:00
|
|
|
"Integration manager: " + name,
|
2021-06-29 13:11:58 +01:00
|
|
|
{ api_url: apiUrl },
|
2019-08-14 08:57:38 -06:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-26 11:52:21 +01:00
|
|
|
/**
|
|
|
|
|
* Remove all stickerpicker widgets (stickerpickers are user widgets by nature)
|
|
|
|
|
* @return {Promise} Resolves on account data updated
|
|
|
|
|
*/
|
2022-12-16 12:29:59 +00:00
|
|
|
public static async removeStickerpickerWidgets(): Promise<void> {
|
2018-06-26 11:52:21 +01:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
if (!client) {
|
|
|
|
|
throw new Error("User not logged in");
|
|
|
|
|
}
|
2019-08-21 18:43:29 -06:00
|
|
|
const widgets = client.getAccountData("m.widgets");
|
|
|
|
|
if (!widgets) return;
|
2020-10-19 20:56:41 +01:00
|
|
|
const userWidgets: Record<string, IWidgetEvent> = widgets.getContent() || {};
|
2018-06-26 11:52:21 +01:00
|
|
|
Object.entries(userWidgets).forEach(([key, widget]) => {
|
|
|
|
|
if (widget.content && widget.content.type === "m.stickerpicker") {
|
|
|
|
|
delete userWidgets[key];
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-07-10 15:43:46 +01:00
|
|
|
await client.setAccountData("m.widgets", userWidgets);
|
2018-06-26 11:52:21 +01:00
|
|
|
}
|
2018-07-12 18:43:49 +01:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public static async addJitsiWidget(
|
2022-03-22 18:14:11 -04:00
|
|
|
roomId: string,
|
|
|
|
|
type: CallType,
|
|
|
|
|
name: string,
|
2022-06-02 09:10:22 -04:00
|
|
|
isVideoChannel: boolean,
|
2022-03-22 18:14:11 -04:00
|
|
|
oobRoomName?: string,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const domain = Jitsi.getInstance().preferredDomain;
|
2023-03-16 10:35:17 +00:00
|
|
|
const auth = (await Jitsi.getInstance().getJitsiAuth()) ?? undefined;
|
2022-06-02 09:10:22 -04:00
|
|
|
const widgetId = randomString(24); // Must be globally unique
|
2022-03-22 18:14:11 -04:00
|
|
|
|
|
|
|
|
let confId;
|
|
|
|
|
if (auth === "openidtoken-jwt") {
|
|
|
|
|
// Create conference ID from room ID
|
|
|
|
|
// For compatibility with Jitsi, use base32 without padding.
|
|
|
|
|
// More details here:
|
|
|
|
|
// https://github.com/matrix-org/prosody-mod-auth-matrix-user-verification
|
|
|
|
|
confId = base32.stringify(Buffer.from(roomId), { pad: false });
|
|
|
|
|
} else {
|
|
|
|
|
// Create a random conference ID
|
|
|
|
|
confId = `Jitsi${randomUppercaseString(1)}${randomLowercaseString(23)}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Remove URL hacks when the mobile clients eventually support v2 widgets
|
|
|
|
|
const widgetUrl = new URL(WidgetUtils.getLocalJitsiWrapperUrl({ auth }));
|
|
|
|
|
widgetUrl.search = ""; // Causes the URL class use searchParams instead
|
|
|
|
|
widgetUrl.searchParams.set("confId", confId);
|
|
|
|
|
|
|
|
|
|
await WidgetUtils.setRoomWidget(roomId, widgetId, WidgetType.JITSI, widgetUrl.toString(), name, {
|
|
|
|
|
conferenceId: confId,
|
|
|
|
|
roomName: oobRoomName ?? MatrixClientPeg.get().getRoom(roomId)?.name,
|
|
|
|
|
isAudioOnly: type === CallType.Voice,
|
2022-06-02 09:10:22 -04:00
|
|
|
isVideoChannel,
|
2022-03-22 18:14:11 -04:00
|
|
|
domain,
|
|
|
|
|
auth,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public static makeAppConfig(
|
2020-09-24 09:49:30 +01:00
|
|
|
appId: string,
|
|
|
|
|
app: Partial<IApp>,
|
|
|
|
|
senderUserId: string,
|
2022-09-16 11:12:27 -04:00
|
|
|
roomId: string | undefined,
|
|
|
|
|
eventId: string | undefined,
|
2020-09-24 09:49:30 +01:00
|
|
|
): IApp {
|
2019-11-18 13:16:36 -07:00
|
|
|
if (!senderUserId) {
|
|
|
|
|
throw new Error("Widgets must be created by someone - provide a senderUserId");
|
|
|
|
|
}
|
|
|
|
|
app.creatorUserId = senderUserId;
|
|
|
|
|
|
2018-07-12 18:43:49 +01:00
|
|
|
app.id = appId;
|
2020-09-07 16:17:25 +01:00
|
|
|
app.roomId = roomId;
|
2019-11-18 17:56:33 -07:00
|
|
|
app.eventId = eventId;
|
2018-07-12 18:43:49 +01:00
|
|
|
app.name = app.name || app.type;
|
|
|
|
|
|
2020-09-24 09:28:49 +01:00
|
|
|
return app as IApp;
|
2018-07-12 18:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public static getLocalJitsiWrapperUrl(opts: { forLocalRender?: boolean; auth?: string } = {}): string {
|
2020-03-18 15:50:05 -06:00
|
|
|
// NB. we can't just encodeURIComponent all of these because the $ signs need to be there
|
2020-09-08 11:31:40 +03:00
|
|
|
const queryStringParts = [
|
2020-03-18 15:50:05 -06:00
|
|
|
"conferenceDomain=$domain",
|
|
|
|
|
"conferenceId=$conferenceId",
|
|
|
|
|
"isAudioOnly=$isAudioOnly",
|
2023-03-15 13:51:00 +01:00
|
|
|
"startWithAudioMuted=$startWithAudioMuted",
|
|
|
|
|
"startWithVideoMuted=$startWithVideoMuted",
|
2022-04-01 10:36:10 -04:00
|
|
|
"isVideoChannel=$isVideoChannel",
|
2020-03-18 15:50:05 -06:00
|
|
|
"displayName=$matrix_display_name",
|
|
|
|
|
"avatarUrl=$matrix_avatar_url",
|
|
|
|
|
"userId=$matrix_user_id",
|
2020-09-04 12:42:46 +03:00
|
|
|
"roomId=$matrix_room_id",
|
2020-10-19 18:47:27 +01:00
|
|
|
"theme=$theme",
|
2021-02-15 08:53:37 -07:00
|
|
|
"roomName=$roomName",
|
2023-02-13 17:01:43 +00:00
|
|
|
`supportsScreensharing=${PlatformPeg.get()?.supportsJitsiScreensharing()}`,
|
2022-10-06 21:19:01 +02:00
|
|
|
"language=$org.matrix.msc2873.client_language",
|
2020-09-04 12:42:46 +03:00
|
|
|
];
|
|
|
|
|
if (opts.auth) {
|
2020-09-08 11:31:40 +03:00
|
|
|
queryStringParts.push(`auth=${opts.auth}`);
|
2020-09-04 12:42:46 +03:00
|
|
|
}
|
2020-09-08 11:31:40 +03:00
|
|
|
const queryString = queryStringParts.join("&");
|
2020-03-18 15:50:05 -06:00
|
|
|
|
2020-09-23 11:01:19 +01:00
|
|
|
let baseUrl = window.location.href;
|
2020-03-21 10:42:07 +00:00
|
|
|
if (window.location.protocol !== "https:" && !opts.forLocalRender) {
|
2020-03-18 15:50:05 -06:00
|
|
|
// Use an external wrapper if we're not locally rendering the widget. This is usually
|
|
|
|
|
// the URL that will end up in the widget event, so we want to make sure it's relatively
|
|
|
|
|
// safe to send.
|
|
|
|
|
// We'll end up using a local render URL when we see a Jitsi widget anyways, so this is
|
|
|
|
|
// really just for backwards compatibility and to appease the spec.
|
2020-08-03 16:02:26 +01:00
|
|
|
baseUrl = "https://app.element.io/";
|
2020-03-18 15:50:05 -06:00
|
|
|
}
|
2020-03-21 10:42:07 +00:00
|
|
|
const url = new URL("jitsi.html#" + queryString, baseUrl); // this strips hash fragment from baseUrl
|
|
|
|
|
return url.href;
|
2020-03-18 15:50:05 -06:00
|
|
|
}
|
2020-09-07 16:17:25 +01:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public static getWidgetName(app?: IApp): string {
|
2020-09-08 17:40:57 +01:00
|
|
|
return app?.name?.trim() || _t("Unknown App");
|
2020-08-28 10:50:27 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public static getWidgetDataTitle(app?: IApp): string {
|
2020-09-08 17:40:57 +01:00
|
|
|
return app?.data?.title?.trim() || "";
|
2020-08-28 10:50:27 +01:00
|
|
|
}
|
2020-09-08 10:19:51 +01:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public static getWidgetUid(app?: IApp): string {
|
2022-03-15 08:15:26 -04:00
|
|
|
return app ? WidgetUtils.calcWidgetUid(app.id, app.roomId) : "";
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public static calcWidgetUid(widgetId: string, roomId?: string): string {
|
2022-03-15 08:15:26 -04:00
|
|
|
return roomId ? `room_${roomId}_${widgetId}` : `user_${widgetId}`;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public static editWidget(room: Room, app: IApp): void {
|
2022-05-06 12:46:26 -06:00
|
|
|
// noinspection JSIgnoredPromiseFromCall
|
|
|
|
|
IntegrationManagers.sharedInstance()
|
|
|
|
|
.getPrimaryManager()
|
2023-02-16 17:21:44 +00:00
|
|
|
?.open(room, "type_" + app.type, app.id);
|
2020-09-08 10:19:51 +01:00
|
|
|
}
|
2020-10-09 08:42:21 +01:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public static isManagedByManager(app: IApp): boolean {
|
2020-10-09 08:42:21 +01:00
|
|
|
if (WidgetUtils.isScalarUrl(app.url)) {
|
|
|
|
|
const managers = IntegrationManagers.sharedInstance();
|
|
|
|
|
if (managers.hasManager()) {
|
|
|
|
|
// TODO: Pick the right manager for the widget
|
|
|
|
|
const defaultManager = managers.getPrimaryManager();
|
2023-02-16 17:21:44 +00:00
|
|
|
return WidgetUtils.isScalarUrl(defaultManager?.apiUrl);
|
2020-10-09 08:42:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-07-27 23:37:33 +01:00
|
|
|
}
|