2017-07-27 22:37:33 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-06-13 09:39:52 +00:00
|
|
|
Copyright 2018 New Vector Ltd
|
2017-07-27 22:37:33 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
2018-05-27 17:12:55 +00:00
|
|
|
import SdkConfig from "./SdkConfig";
|
|
|
|
import * as url from "url";
|
2017-07-27 22:37:33 +00:00
|
|
|
|
|
|
|
export default class WidgetUtils {
|
|
|
|
/* Returns true if user is able to send state events to modify widgets in this room
|
2018-04-02 09:02:41 +00:00
|
|
|
* (Does not apply to non-room-based / user widgets)
|
2017-07-27 22:37:33 +00: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 15:19:20 +00:00
|
|
|
* @throws Error -- specifies the error reason
|
2017-07-27 22:37:33 +00:00
|
|
|
*/
|
|
|
|
static canUserModifyWidgets(roomId) {
|
|
|
|
if (!roomId) {
|
2017-08-01 10:39:17 +00:00
|
|
|
console.warn('No room ID specified');
|
|
|
|
return false;
|
2017-07-27 22:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
2017-08-01 10:39:17 +00:00
|
|
|
console.warn('User must be be logged in');
|
|
|
|
return false;
|
2017-07-27 22:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
if (!room) {
|
2017-08-01 10:39:17 +00:00
|
|
|
console.warn(`Room ID ${roomId} is not recognised`);
|
|
|
|
return false;
|
2017-07-27 22:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const me = client.credentials.userId;
|
|
|
|
if (!me) {
|
2017-08-01 10:39:17 +00:00
|
|
|
console.warn('Failed to get user ID');
|
|
|
|
return false;
|
2017-07-27 22:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const member = room.getMember(me);
|
|
|
|
if (!member || member.membership !== "join") {
|
2017-08-01 10:39:17 +00:00
|
|
|
console.warn(`User ${me} is not in room ${roomId}`);
|
|
|
|
return false;
|
2017-07-27 22:37:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 10:41:41 +00:00
|
|
|
return room.currentState.maySendStateEvent('im.vector.modular.widgets', me);
|
2017-07-27 22:37:33 +00:00
|
|
|
}
|
2018-05-27 17:12:55 +00: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) {
|
|
|
|
scalarUrls = [SdkConfig.get().integrations_rest_url];
|
|
|
|
}
|
|
|
|
|
|
|
|
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 09:39:52 +00: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.
|
|
|
|
* @returns {Promise} that resolves when the widget is available
|
|
|
|
*/
|
|
|
|
static waitForUserWidget(widgetId, add) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const currentAccountDataEvent = MatrixClientPeg.get().getAccountData('m.widgets');
|
|
|
|
|
|
|
|
// Tests an account data event, returning true if it's in the state
|
|
|
|
// we're waiting for it to be in
|
|
|
|
function eventInIntendedState(ev) {
|
|
|
|
if (!ev || !currentAccountDataEvent.getContent()) return false;
|
|
|
|
if (add) {
|
|
|
|
return ev.getContent()[widgetId] !== undefined;
|
|
|
|
} else {
|
|
|
|
return ev.getContent()[widgetId] === undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eventInIntendedState(currentAccountDataEvent)) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onAccountData(ev) {
|
|
|
|
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"));
|
|
|
|
}, 10000);
|
|
|
|
MatrixClientPeg.get().on('accountData', onAccountData);
|
|
|
|
});
|
|
|
|
}
|
2017-07-27 22:37:33 +00:00
|
|
|
}
|