2017-07-28 01:37:33 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-06-13 12:39:52 +03:00
|
|
|
Copyright 2018 New Vector Ltd
|
2019-03-24 08:25:31 +03:00
|
|
|
Copyright 2019 Travis Ralston
|
2017-07-28 01:37:33 +03: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.
|
|
|
|
*/
|
|
|
|
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from '../MatrixClientPeg';
|
2018-06-26 13:59:16 +03:00
|
|
|
import SdkConfig from "../SdkConfig";
|
|
|
|
import dis from '../dispatcher';
|
2018-05-27 20:12:55 +03:00
|
|
|
import * as url from "url";
|
2018-07-03 13:16:44 +03:00
|
|
|
import WidgetEchoStore from '../stores/WidgetEchoStore';
|
2017-07-28 01:37:33 +03:00
|
|
|
|
2018-07-03 13:55:41 +03:00
|
|
|
// How long we wait for the state event echo to come back from the server
|
2018-07-16 15:18:10 +03:00
|
|
|
// before waitFor[Room/User]Widget rejects its promise
|
2018-07-03 13:55:41 +03:00
|
|
|
const WIDGET_WAIT_TIME = 20000;
|
2018-07-12 20:43:49 +03:00
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2019-03-24 08:25:31 +03:00
|
|
|
import ActiveWidgetStore from "../stores/ActiveWidgetStore";
|
2019-08-10 02:08:26 +03:00
|
|
|
import {IntegrationManagers} from "../integrations/IntegrationManagers";
|
2018-07-12 20:43:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encodes a URI according to a set of template variables. Variables will be
|
|
|
|
* passed through encodeURIComponent.
|
|
|
|
* @param {string} pathTemplate The path with template variables e.g. '/foo/$bar'.
|
|
|
|
* @param {Object} variables The key/value pairs to replace the template
|
|
|
|
* variables with. E.g. { '$bar': 'baz' }.
|
|
|
|
* @return {string} The result of replacing all template variables e.g. '/foo/baz'.
|
|
|
|
*/
|
|
|
|
function encodeUri(pathTemplate, variables) {
|
|
|
|
for (const key in variables) {
|
|
|
|
if (!variables.hasOwnProperty(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pathTemplate = pathTemplate.replace(
|
|
|
|
key, encodeURIComponent(variables[key]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return pathTemplate;
|
|
|
|
}
|
2018-07-03 13:55:41 +03:00
|
|
|
|
2017-07-28 01:37:33 +03:00
|
|
|
export default class WidgetUtils {
|
|
|
|
/* Returns true if user is able to send state events to modify widgets in this room
|
2018-04-02 12:02:41 +03:00
|
|
|
* (Does not apply to non-room-based / user widgets)
|
2017-07-28 01:37:33 +03: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 18:19:20 +03:00
|
|
|
* @throws Error -- specifies the error reason
|
2017-07-28 01:37:33 +03:00
|
|
|
*/
|
|
|
|
static canUserModifyWidgets(roomId) {
|
|
|
|
if (!roomId) {
|
2017-08-01 13:39:17 +03:00
|
|
|
console.warn('No room ID specified');
|
|
|
|
return false;
|
2017-07-28 01:37:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
2017-08-01 13:39:17 +03:00
|
|
|
console.warn('User must be be logged in');
|
|
|
|
return false;
|
2017-07-28 01:37:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
if (!room) {
|
2017-08-01 13:39:17 +03:00
|
|
|
console.warn(`Room ID ${roomId} is not recognised`);
|
|
|
|
return false;
|
2017-07-28 01:37:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const me = client.credentials.userId;
|
|
|
|
if (!me) {
|
2017-08-01 13:39:17 +03:00
|
|
|
console.warn('Failed to get user ID');
|
|
|
|
return false;
|
2017-07-28 01:37:33 +03:00
|
|
|
}
|
|
|
|
|
2018-08-02 12:42:05 +03:00
|
|
|
if (room.getMyMembership() !== "join") {
|
2017-08-01 13:39:17 +03:00
|
|
|
console.warn(`User ${me} is not in room ${roomId}`);
|
|
|
|
return false;
|
2017-07-28 01:37:33 +03:00
|
|
|
}
|
|
|
|
|
2017-08-01 13:41:41 +03:00
|
|
|
return room.currentState.maySendStateEvent('im.vector.modular.widgets', me);
|
2017-07-28 01:37:33 +03:00
|
|
|
}
|
2018-05-27 20:12:55 +03: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
|
|
|
|
*/
|
|
|
|
static isScalarUrl(testUrlString) {
|
|
|
|
if (!testUrlString) {
|
|
|
|
console.error('Scalar URL check failed. No URL specified');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const testUrl = url.parse(testUrlString);
|
|
|
|
let scalarUrls = SdkConfig.get().integrations_widgets_urls;
|
|
|
|
if (!scalarUrls || scalarUrls.length === 0) {
|
2019-08-10 02:08:26 +03:00
|
|
|
const defaultManager = IntegrationManagers.sharedInstance().getPrimaryManager();
|
2019-09-16 11:12:15 +03:00
|
|
|
if (defaultManager) {
|
|
|
|
scalarUrls = [defaultManager.apiUrl];
|
|
|
|
} else {
|
|
|
|
scalarUrls = [];
|
|
|
|
}
|
2018-05-27 20:12:55 +03: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 &&
|
|
|
|
testUrl.pathname.startsWith(scalarUrl.pathname)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-13 12:39:52 +03: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 15:49:23 +03:00
|
|
|
* @returns {Promise} that resolves when the widget is in the
|
2018-06-14 15:03:42 +03:00
|
|
|
* requested state according to the `add` param
|
2018-06-13 12:39:52 +03:00
|
|
|
*/
|
|
|
|
static waitForUserWidget(widgetId, add) {
|
|
|
|
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
|
|
|
|
function eventInIntendedState(ev) {
|
2018-06-13 17:50:19 +03:00
|
|
|
if (!ev || !ev.getContent()) return false;
|
2018-06-13 12:39:52 +03:00
|
|
|
if (add) {
|
|
|
|
return ev.getContent()[widgetId] !== undefined;
|
|
|
|
} else {
|
|
|
|
return ev.getContent()[widgetId] === undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 17:50:19 +03:00
|
|
|
const startingAccountDataEvent = MatrixClientPeg.get().getAccountData('m.widgets');
|
|
|
|
if (eventInIntendedState(startingAccountDataEvent)) {
|
2018-06-13 12:39:52 +03:00
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onAccountData(ev) {
|
2018-06-13 17:50:19 +03:00
|
|
|
const currentAccountDataEvent = MatrixClientPeg.get().getAccountData('m.widgets');
|
2018-06-13 12:39:52 +03:00
|
|
|
if (eventInIntendedState(currentAccountDataEvent)) {
|
|
|
|
MatrixClientPeg.get().removeListener('accountData', onAccountData);
|
|
|
|
clearTimeout(timerId);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const timerId = setTimeout(() => {
|
|
|
|
MatrixClientPeg.get().removeListener('accountData', onAccountData);
|
|
|
|
reject(new Error("Timed out waiting for widget ID " + widgetId + " to appear"));
|
2018-07-03 13:55:41 +03:00
|
|
|
}, WIDGET_WAIT_TIME);
|
2018-06-13 12:39:52 +03:00
|
|
|
MatrixClientPeg.get().on('accountData', onAccountData);
|
|
|
|
});
|
|
|
|
}
|
2018-06-13 17:50:19 +03: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 15:49:23 +03:00
|
|
|
* @returns {Promise} that resolves when the widget is in the
|
2018-06-14 15:03:42 +03:00
|
|
|
* requested state according to the `add` param
|
2018-06-13 17:50:19 +03:00
|
|
|
*/
|
|
|
|
static waitForRoomWidget(widgetId, roomId, add) {
|
|
|
|
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
|
|
|
|
function eventsInIntendedState(evList) {
|
|
|
|
const widgetPresent = evList.some((ev) => {
|
|
|
|
return ev.getContent() && ev.getContent()['id'] === widgetId;
|
|
|
|
});
|
|
|
|
if (add) {
|
|
|
|
return widgetPresent;
|
|
|
|
} else {
|
|
|
|
return !widgetPresent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const room = MatrixClientPeg.get().getRoom(roomId);
|
|
|
|
const startingWidgetEvents = room.currentState.getStateEvents('im.vector.modular.widgets');
|
|
|
|
if (eventsInIntendedState(startingWidgetEvents)) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onRoomStateEvents(ev) {
|
|
|
|
if (ev.getRoomId() !== roomId) return;
|
|
|
|
|
|
|
|
const currentWidgetEvents = room.currentState.getStateEvents('im.vector.modular.widgets');
|
|
|
|
|
|
|
|
if (eventsInIntendedState(currentWidgetEvents)) {
|
|
|
|
MatrixClientPeg.get().removeListener('RoomState.events', onRoomStateEvents);
|
|
|
|
clearTimeout(timerId);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const timerId = setTimeout(() => {
|
|
|
|
MatrixClientPeg.get().removeListener('RoomState.events', onRoomStateEvents);
|
|
|
|
reject(new Error("Timed out waiting for widget ID " + widgetId + " to appear"));
|
2018-07-03 13:55:41 +03:00
|
|
|
}, WIDGET_WAIT_TIME);
|
2018-06-13 17:50:19 +03:00
|
|
|
MatrixClientPeg.get().on('RoomState.events', onRoomStateEvents);
|
|
|
|
});
|
|
|
|
}
|
2018-06-25 17:30:04 +03:00
|
|
|
|
|
|
|
static setUserWidget(widgetId, widgetType, widgetUrl, widgetName, widgetData) {
|
|
|
|
const content = {
|
|
|
|
type: widgetType,
|
|
|
|
url: widgetUrl,
|
|
|
|
name: widgetName,
|
|
|
|
data: widgetData,
|
|
|
|
};
|
|
|
|
|
|
|
|
const client = MatrixClientPeg.get();
|
2019-10-02 17:26:54 +03:00
|
|
|
// Get the current widgets and clone them before we modify them, otherwise
|
|
|
|
// we'll modify the content of the old event.
|
|
|
|
const userWidgets = JSON.parse(JSON.stringify(WidgetUtils.getUserWidgets()));
|
2018-06-25 17:30:04 +03:00
|
|
|
|
|
|
|
// Delete existing widget with ID
|
|
|
|
try {
|
|
|
|
delete userWidgets[widgetId];
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`$widgetId is non-configurable`);
|
|
|
|
}
|
|
|
|
|
2018-06-26 18:33:28 +03:00
|
|
|
const addingWidget = Boolean(widgetUrl);
|
2018-06-26 17:21:22 +03:00
|
|
|
|
2018-06-25 17:30:04 +03:00
|
|
|
// Add new widget / update
|
2018-06-26 17:21:22 +03:00
|
|
|
if (addingWidget) {
|
2018-06-25 17:30:04 +03:00
|
|
|
userWidgets[widgetId] = {
|
|
|
|
content: content,
|
|
|
|
sender: client.getUserId(),
|
|
|
|
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 17:21:22 +03:00
|
|
|
return WidgetUtils.waitForUserWidget(widgetId, addingWidget);
|
2018-06-25 17:30:04 +03:00
|
|
|
}).then(() => {
|
|
|
|
dis.dispatch({ action: "user_widget_updated" });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-26 18:33:28 +03:00
|
|
|
static setRoomWidget(roomId, widgetId, widgetType, widgetUrl, widgetName, widgetData) {
|
2018-06-25 17:30:04 +03:00
|
|
|
let content;
|
|
|
|
|
2018-06-26 18:33:28 +03:00
|
|
|
const addingWidget = Boolean(widgetUrl);
|
2018-06-26 17:21:22 +03:00
|
|
|
|
|
|
|
if (addingWidget) {
|
2018-06-25 17:30:04 +03:00
|
|
|
content = {
|
|
|
|
type: widgetType,
|
|
|
|
url: widgetUrl,
|
|
|
|
name: widgetName,
|
|
|
|
data: widgetData,
|
|
|
|
};
|
2018-06-26 17:21:22 +03:00
|
|
|
} else {
|
|
|
|
content = {};
|
2018-06-25 17:30:04 +03:00
|
|
|
}
|
|
|
|
|
2018-07-05 20:43:20 +03:00
|
|
|
WidgetEchoStore.setRoomWidgetEcho(roomId, widgetId, content);
|
2018-07-03 13:16:44 +03:00
|
|
|
|
2018-06-25 17:30:04 +03:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
// TODO - Room widgets need to be moved to 'm.widget' state events
|
|
|
|
// https://docs.google.com/document/d/1uPF7XWY_dXTKVKV7jZQ2KmsI19wn9-kFRgQ1tFQP7wQ/edit?usp=sharing
|
|
|
|
return client.sendStateEvent(roomId, "im.vector.modular.widgets", content, widgetId).then(() => {
|
2018-06-26 17:21:22 +03:00
|
|
|
return WidgetUtils.waitForRoomWidget(widgetId, roomId, addingWidget);
|
2018-07-03 13:16:44 +03:00
|
|
|
}).finally(() => {
|
2018-07-05 20:43:20 +03:00
|
|
|
WidgetEchoStore.removeRoomWidgetEcho(roomId, widgetId);
|
2018-06-25 17:30:04 +03:00
|
|
|
});
|
|
|
|
}
|
2018-06-26 13:52:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get room specific widgets
|
|
|
|
* @param {object} room The room to get widgets force
|
|
|
|
* @return {[object]} Array containing current / active room widgets
|
|
|
|
*/
|
|
|
|
static getRoomWidgets(room) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
static getUserWidgets() {
|
|
|
|
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 17:41:43 +03:00
|
|
|
return userWidgets.getContent();
|
2018-06-26 13:52:21 +03:00
|
|
|
}
|
2018-06-26 17:41:43 +03:00
|
|
|
return {};
|
2018-06-26 13:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get user specific widgets (not linked to a specific room) as an array
|
|
|
|
* @return {[object]} Array containing current / active user widgets
|
|
|
|
*/
|
|
|
|
static getUserWidgetsArray() {
|
2018-06-26 17:42:29 +03:00
|
|
|
return Object.values(WidgetUtils.getUserWidgets());
|
2018-06-26 13:52:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get active stickerpicker widgets (stickerpickers are user widgets by nature)
|
|
|
|
* @return {[object]} Array containing current / active stickerpicker widgets
|
|
|
|
*/
|
|
|
|
static getStickerpickerWidgets() {
|
|
|
|
const widgets = WidgetUtils.getUserWidgetsArray();
|
|
|
|
return widgets.filter((widget) => widget.content && widget.content.type === "m.stickerpicker");
|
|
|
|
}
|
|
|
|
|
2019-08-10 02:35:59 +03:00
|
|
|
/**
|
|
|
|
* Get all integration manager widgets for this user.
|
|
|
|
* @returns {Object[]} An array of integration manager user widgets.
|
|
|
|
*/
|
|
|
|
static getIntegrationManagerWidgets() {
|
|
|
|
const widgets = WidgetUtils.getUserWidgetsArray();
|
2019-08-14 18:33:02 +03:00
|
|
|
return widgets.filter(w => w.content && w.content.type === "m.integration_manager");
|
2019-08-10 02:35:59 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 00:35:39 +03:00
|
|
|
static removeIntegrationManagerWidgets() {
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
|
|
|
throw new Error('User not logged in');
|
|
|
|
}
|
2019-08-22 03:43:29 +03:00
|
|
|
const widgets = client.getAccountData('m.widgets');
|
|
|
|
if (!widgets) return;
|
|
|
|
const userWidgets = widgets.getContent() || {};
|
2019-08-13 00:35:39 +03:00
|
|
|
Object.entries(userWidgets).forEach(([key, widget]) => {
|
2019-08-14 18:33:02 +03:00
|
|
|
if (widget.content && widget.content.type === "m.integration_manager") {
|
2019-08-13 00:35:39 +03:00
|
|
|
delete userWidgets[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return client.setAccountData('m.widgets', userWidgets);
|
|
|
|
}
|
|
|
|
|
2019-08-14 17:57:38 +03:00
|
|
|
static addIntegrationManagerWidget(name: string, uiUrl: string, apiUrl: string) {
|
|
|
|
return WidgetUtils.setUserWidget(
|
|
|
|
"integration_manager_" + (new Date().getTime()),
|
2019-08-14 18:33:02 +03:00
|
|
|
"m.integration_manager",
|
2019-08-14 17:57:38 +03:00
|
|
|
uiUrl,
|
|
|
|
"Integration Manager: " + name,
|
|
|
|
{"api_url": apiUrl},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-26 13:52:21 +03:00
|
|
|
/**
|
|
|
|
* Remove all stickerpicker widgets (stickerpickers are user widgets by nature)
|
|
|
|
* @return {Promise} Resolves on account data updated
|
|
|
|
*/
|
|
|
|
static removeStickerpickerWidgets() {
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
|
|
|
throw new Error('User not logged in');
|
|
|
|
}
|
2019-08-22 03:43:29 +03:00
|
|
|
const widgets = client.getAccountData('m.widgets');
|
|
|
|
if (!widgets) return;
|
|
|
|
const userWidgets = widgets.getContent() || {};
|
2018-06-26 13:52:21 +03:00
|
|
|
Object.entries(userWidgets).forEach(([key, widget]) => {
|
|
|
|
if (widget.content && widget.content.type === 'm.stickerpicker') {
|
|
|
|
delete userWidgets[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return client.setAccountData('m.widgets', userWidgets);
|
|
|
|
}
|
2018-07-12 20:43:49 +03:00
|
|
|
|
2019-11-19 03:56:33 +03:00
|
|
|
static makeAppConfig(appId, app, senderUserId, roomId, eventId) {
|
2018-07-12 20:43:49 +03:00
|
|
|
const myUserId = MatrixClientPeg.get().credentials.userId;
|
|
|
|
const user = MatrixClientPeg.get().getUser(myUserId);
|
|
|
|
const params = {
|
|
|
|
'$matrix_user_id': myUserId,
|
|
|
|
'$matrix_room_id': roomId,
|
|
|
|
'$matrix_display_name': user ? user.displayName : myUserId,
|
|
|
|
'$matrix_avatar_url': user ? MatrixClientPeg.get().mxcUrlToHttp(user.avatarUrl) : '',
|
|
|
|
|
|
|
|
// TODO: Namespace themes through some standard
|
|
|
|
'$theme': SettingsStore.getValue("theme"),
|
|
|
|
};
|
|
|
|
|
2019-11-18 23:16:36 +03:00
|
|
|
if (!senderUserId) {
|
|
|
|
throw new Error("Widgets must be created by someone - provide a senderUserId");
|
|
|
|
}
|
|
|
|
app.creatorUserId = senderUserId;
|
|
|
|
|
2018-07-12 20:43:49 +03:00
|
|
|
app.id = appId;
|
2019-11-19 03:56:33 +03:00
|
|
|
app.eventId = eventId;
|
2018-07-12 20:43:49 +03:00
|
|
|
app.name = app.name || app.type;
|
|
|
|
|
|
|
|
if (app.data) {
|
|
|
|
Object.keys(app.data).forEach((key) => {
|
|
|
|
params['$' + key] = app.data[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
app.waitForIframeLoad = (app.data.waitForIframeLoad === 'false' ? false : true);
|
|
|
|
}
|
|
|
|
|
2020-03-19 00:50:05 +03:00
|
|
|
if (app.type === 'jitsi') {
|
|
|
|
console.log("Replacing Jitsi widget URL with local wrapper");
|
2020-03-19 20:51:01 +03:00
|
|
|
app.url = WidgetUtils.getLocalJitsiWrapperUrl({forLocalRender: true});
|
2020-03-19 00:50:05 +03:00
|
|
|
}
|
|
|
|
|
2018-07-12 20:43:49 +03:00
|
|
|
app.url = encodeUri(app.url, params);
|
|
|
|
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
|
|
|
static getCapWhitelistForAppTypeInRoomId(appType, roomId) {
|
|
|
|
const enableScreenshots = SettingsStore.getValue("enableWidgetScreenshots", roomId);
|
|
|
|
|
|
|
|
const capWhitelist = enableScreenshots ? ["m.capability.screenshot"] : [];
|
|
|
|
|
|
|
|
// Obviously anyone that can add a widget can claim it's a jitsi widget,
|
|
|
|
// so this doesn't really offer much over the set of domains we load
|
|
|
|
// widgets from at all, but it probably makes sense for sanity.
|
|
|
|
if (appType == 'jitsi') capWhitelist.push("m.always_on_screen");
|
|
|
|
|
|
|
|
return capWhitelist;
|
|
|
|
}
|
2019-03-24 08:25:31 +03:00
|
|
|
|
|
|
|
static getWidgetSecurityKey(widgetId, widgetUrl, isUserWidget) {
|
|
|
|
let widgetLocation = ActiveWidgetStore.getRoomId(widgetId);
|
|
|
|
|
|
|
|
if (isUserWidget) {
|
|
|
|
const userWidget = WidgetUtils.getUserWidgetsArray()
|
|
|
|
.find((w) => w.id === widgetId && w.content && w.content.url === widgetUrl);
|
|
|
|
|
|
|
|
if (!userWidget) {
|
|
|
|
throw new Error("No matching user widget to form security key");
|
|
|
|
}
|
|
|
|
|
|
|
|
widgetLocation = userWidget.sender;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!widgetLocation) {
|
|
|
|
throw new Error("Failed to locate where the widget resides");
|
|
|
|
}
|
|
|
|
|
|
|
|
return encodeURIComponent(`${widgetLocation}::${widgetUrl}`);
|
|
|
|
}
|
2020-03-19 00:50:05 +03:00
|
|
|
|
2020-03-19 20:51:01 +03:00
|
|
|
static getLocalJitsiWrapperUrl(opts: {forLocalRender?: boolean}) {
|
2020-03-19 00:50:05 +03:00
|
|
|
// NB. we can't just encodeURIComponent all of these because the $ signs need to be there
|
|
|
|
const queryString = [
|
|
|
|
'conferenceDomain=$domain',
|
|
|
|
'conferenceId=$conferenceId',
|
|
|
|
'isAudioOnly=$isAudioOnly',
|
|
|
|
'displayName=$matrix_display_name',
|
|
|
|
'avatarUrl=$matrix_avatar_url',
|
|
|
|
'userId=$matrix_user_id',
|
|
|
|
].join('&');
|
|
|
|
|
2020-03-21 13:42:07 +03:00
|
|
|
let baseUrl = window.location;
|
|
|
|
if (window.location.protocol !== "https:" && !opts.forLocalRender) {
|
2020-03-19 00:50:05 +03: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-03-21 13:42:07 +03:00
|
|
|
baseUrl = "https://riot.im/app";
|
2020-03-19 00:50:05 +03:00
|
|
|
}
|
2020-03-21 13:42:07 +03:00
|
|
|
const url = new URL("jitsi.html#" + queryString, baseUrl); // this strips hash fragment from baseUrl
|
|
|
|
return url.href;
|
2020-03-19 00:50:05 +03:00
|
|
|
}
|
2017-07-28 01:37:33 +03:00
|
|
|
}
|