2016-08-23 13:00:11 +02:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2017-06-09 12:21:37 +02:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2016-08-23 13:00:11 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Listens for incoming postMessage requests from the integrations UI URL. The following API is exposed:
|
|
|
|
{
|
2017-07-11 16:20:33 +02:00
|
|
|
action: "invite" | "membership_state" | "bot_options" | "set_bot_options" | etc... ,
|
2016-08-23 14:31:55 +02:00
|
|
|
room_id: $ROOM_ID,
|
|
|
|
user_id: $USER_ID
|
2016-08-24 14:23:06 +02:00
|
|
|
// additional request fields
|
2016-08-23 13:00:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
The complete request object is returned to the caller with an additional "response" key like so:
|
|
|
|
{
|
2016-08-24 14:23:06 +02:00
|
|
|
action: "invite" | "membership_state" | "bot_options" | "set_bot_options",
|
2016-08-23 14:31:55 +02:00
|
|
|
room_id: $ROOM_ID,
|
|
|
|
user_id: $USER_ID,
|
2016-08-24 14:23:06 +02:00
|
|
|
// additional request fields
|
2016-08-23 14:31:55 +02:00
|
|
|
response: { ... }
|
2016-08-23 13:00:11 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 14:23:06 +02:00
|
|
|
The "action" determines the format of the request and response. All actions can return an error response.
|
|
|
|
An error response is a "response" object which consists of a sole "error" key to indicate an error.
|
|
|
|
They look like:
|
2016-08-23 13:00:11 +02:00
|
|
|
{
|
2016-08-23 14:31:55 +02:00
|
|
|
error: {
|
|
|
|
message: "Unable to invite user into room.",
|
|
|
|
_error: <Original Error Object>
|
|
|
|
}
|
2016-08-23 13:00:11 +02:00
|
|
|
}
|
|
|
|
The "message" key should be a human-friendly string.
|
|
|
|
|
2016-08-24 14:23:06 +02:00
|
|
|
ACTIONS
|
|
|
|
=======
|
|
|
|
All actions can return an error response instead of the response outlined below.
|
|
|
|
|
|
|
|
invite
|
|
|
|
------
|
|
|
|
Invites a user into a room.
|
|
|
|
|
|
|
|
Request:
|
|
|
|
- room_id is the room to invite the user into.
|
|
|
|
- user_id is the user ID to invite.
|
|
|
|
- No additional fields.
|
|
|
|
Response:
|
|
|
|
{
|
|
|
|
success: true
|
|
|
|
}
|
|
|
|
Example:
|
2016-08-23 13:00:11 +02:00
|
|
|
{
|
2016-08-24 14:23:06 +02:00
|
|
|
action: "invite",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
user_id: "@invitee:bar",
|
|
|
|
response: {
|
|
|
|
success: true
|
|
|
|
}
|
2016-08-23 13:00:11 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 14:23:06 +02:00
|
|
|
set_bot_options
|
|
|
|
---------------
|
|
|
|
Set the m.room.bot.options state event for a bot user.
|
|
|
|
|
|
|
|
Request:
|
|
|
|
- room_id is the room to send the state event into.
|
|
|
|
- user_id is the user ID of the bot who you're setting options for.
|
|
|
|
- "content" is an object consisting of the content you wish to set.
|
|
|
|
Response:
|
2016-08-23 13:00:11 +02:00
|
|
|
{
|
2016-08-24 14:23:06 +02:00
|
|
|
success: true
|
2016-08-23 13:00:11 +02:00
|
|
|
}
|
2016-08-24 14:23:06 +02:00
|
|
|
Example:
|
|
|
|
{
|
|
|
|
action: "set_bot_options",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
user_id: "@bot:bar",
|
|
|
|
content: {
|
|
|
|
default_option: "alpha"
|
|
|
|
},
|
|
|
|
response: {
|
|
|
|
success: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 17:41:08 +02:00
|
|
|
get_membership_count
|
|
|
|
--------------------
|
|
|
|
Get the number of joined users in the room.
|
|
|
|
|
|
|
|
Request:
|
|
|
|
- room_id is the room to get the count in.
|
|
|
|
Response:
|
|
|
|
78
|
|
|
|
Example:
|
|
|
|
{
|
|
|
|
action: "get_membership_count",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
response: 78
|
|
|
|
}
|
|
|
|
|
2017-07-11 16:20:33 +02:00
|
|
|
can_send_event
|
|
|
|
--------------
|
|
|
|
Check if the client can send the given event into the given room. If the client
|
|
|
|
is unable to do this, an error response is returned instead of 'response: false'.
|
|
|
|
|
|
|
|
Request:
|
|
|
|
- room_id is the room to do the check in.
|
|
|
|
- event_type is the event type which will be sent.
|
|
|
|
- is_state is true if the event to be sent is a state event.
|
|
|
|
Response:
|
|
|
|
true
|
|
|
|
Example:
|
|
|
|
{
|
|
|
|
action: "can_send_event",
|
|
|
|
is_state: false,
|
|
|
|
event_type: "m.room.message",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
response: true
|
|
|
|
}
|
|
|
|
|
2017-06-09 12:21:37 +02:00
|
|
|
set_widget
|
|
|
|
----------
|
|
|
|
Set a new widget in the room. Clobbers based on the ID.
|
|
|
|
|
|
|
|
Request:
|
|
|
|
- `room_id` (String) is the room to set the widget in.
|
|
|
|
- `widget_id` (String) is the ID of the widget to add (or replace if it already exists).
|
|
|
|
It can be an arbitrary UTF8 string and is purely for distinguishing between widgets.
|
|
|
|
- `url` (String) is the URL that clients should load in an iframe to run the widget.
|
|
|
|
All widgets must have a valid URL. If the URL is `null` (not `undefined`), the
|
|
|
|
widget will be removed from the room.
|
|
|
|
- `type` (String) is the type of widget, which is provided as a hint for matrix clients so they
|
|
|
|
can configure/lay out the widget in different ways. All widgets must have a type.
|
|
|
|
- `name` (String) is an optional human-readable string about the widget.
|
|
|
|
- `data` (Object) is some optional data about the widget, and can contain arbitrary key/value pairs.
|
|
|
|
Response:
|
|
|
|
{
|
|
|
|
success: true
|
|
|
|
}
|
|
|
|
Example:
|
|
|
|
{
|
|
|
|
action: "set_widget",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
widget_id: "abc123",
|
|
|
|
url: "http://widget.url",
|
|
|
|
type: "example",
|
|
|
|
response: {
|
|
|
|
success: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get_widgets
|
|
|
|
-----------
|
2017-07-11 13:15:27 +02:00
|
|
|
Get a list of all widgets in the room. The response is an array
|
|
|
|
of state events.
|
2017-06-09 12:21:37 +02:00
|
|
|
|
|
|
|
Request:
|
|
|
|
- `room_id` (String) is the room to get the widgets in.
|
|
|
|
Response:
|
2017-07-11 13:15:27 +02:00
|
|
|
[
|
|
|
|
{
|
|
|
|
type: "im.vector.modular.widgets",
|
|
|
|
state_key: "wid1",
|
|
|
|
content: {
|
|
|
|
type: "grafana",
|
|
|
|
url: "https://grafanaurl",
|
|
|
|
name: "dashboard",
|
|
|
|
data: {key: "val"}
|
2017-06-09 12:21:37 +02:00
|
|
|
}
|
2017-07-11 13:15:27 +02:00
|
|
|
room_id: “!foo:bar”,
|
|
|
|
sender: "@alice:localhost"
|
|
|
|
}
|
|
|
|
]
|
2017-06-09 12:21:37 +02:00
|
|
|
Example:
|
|
|
|
{
|
|
|
|
action: "get_widgets",
|
|
|
|
room_id: "!foo:bar",
|
2017-07-11 13:15:27 +02:00
|
|
|
response: [
|
|
|
|
{
|
|
|
|
type: "im.vector.modular.widgets",
|
|
|
|
state_key: "wid1",
|
|
|
|
content: {
|
|
|
|
type: "grafana",
|
|
|
|
url: "https://grafanaurl",
|
|
|
|
name: "dashboard",
|
|
|
|
data: {key: "val"}
|
2017-06-09 12:21:37 +02:00
|
|
|
}
|
2017-07-11 13:15:27 +02:00
|
|
|
room_id: “!foo:bar”,
|
|
|
|
sender: "@alice:localhost"
|
|
|
|
}
|
|
|
|
]
|
2017-06-09 12:21:37 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 17:41:08 +02:00
|
|
|
|
2016-08-24 14:23:06 +02:00
|
|
|
membership_state AND bot_options
|
|
|
|
--------------------------------
|
|
|
|
Get the content of the "m.room.member" or "m.room.bot.options" state event respectively.
|
|
|
|
|
|
|
|
NB: Whilst this API is basically equivalent to getStateEvent, we specifically do not
|
|
|
|
want external entities to be able to query any state event for any room, hence the
|
|
|
|
restrictive API outlined here.
|
2016-08-23 13:00:11 +02:00
|
|
|
|
2016-08-24 14:23:06 +02:00
|
|
|
Request:
|
|
|
|
- room_id is the room which has the state event.
|
|
|
|
- user_id is the state_key parameter which in both cases is a user ID (the member or the bot).
|
|
|
|
- No additional fields.
|
|
|
|
Response:
|
|
|
|
- The event content. If there is no state event, the "response" key should be null.
|
|
|
|
Example:
|
|
|
|
{
|
|
|
|
action: "membership_state",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
user_id: "@somemember:bar",
|
|
|
|
response: {
|
|
|
|
membership: "join",
|
|
|
|
displayname: "Bob",
|
|
|
|
avatar_url: null
|
|
|
|
}
|
|
|
|
}
|
2016-08-23 13:00:11 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
const SdkConfig = require('./SdkConfig');
|
|
|
|
const MatrixClientPeg = require("./MatrixClientPeg");
|
2016-09-07 18:06:57 +02:00
|
|
|
const MatrixEvent = require("matrix-js-sdk").MatrixEvent;
|
|
|
|
const dis = require("./dispatcher");
|
2017-05-25 12:39:08 +02:00
|
|
|
import { _t } from './languageHandler';
|
2016-08-23 13:00:11 +02:00
|
|
|
|
2016-08-23 15:41:47 +02:00
|
|
|
function sendResponse(event, res) {
|
|
|
|
const data = JSON.parse(JSON.stringify(event.data));
|
|
|
|
data.response = res;
|
|
|
|
event.source.postMessage(data, event.origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendError(event, msg, nestedError) {
|
|
|
|
console.error("Action:" + event.data.action + " failed with message: " + msg);
|
2016-08-23 15:50:52 +02:00
|
|
|
const data = JSON.parse(JSON.stringify(event.data));
|
2016-08-23 15:41:47 +02:00
|
|
|
data.response = {
|
|
|
|
error: {
|
|
|
|
message: msg,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if (nestedError) {
|
|
|
|
data.response.error._error = nestedError;
|
|
|
|
}
|
|
|
|
event.source.postMessage(data, event.origin);
|
|
|
|
}
|
|
|
|
|
2016-08-24 14:23:06 +02:00
|
|
|
function inviteUser(event, roomId, userId) {
|
2016-08-23 15:41:47 +02:00
|
|
|
console.log(`Received request to invite ${userId} into room ${roomId}`);
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
2017-05-25 20:21:18 +02:00
|
|
|
sendError(event, _t('You need to be logged in.'));
|
2016-08-23 15:41:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
if (room) {
|
|
|
|
// if they are already invited we can resolve immediately.
|
|
|
|
const member = room.getMember(userId);
|
|
|
|
if (member && member.membership === "invite") {
|
|
|
|
sendResponse(event, {
|
2016-08-24 14:23:06 +02:00
|
|
|
success: true,
|
2016-08-23 15:41:47 +02:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 14:47:35 +02:00
|
|
|
client.invite(roomId, userId).done(function() {
|
2016-08-23 15:41:47 +02:00
|
|
|
sendResponse(event, {
|
2016-08-24 14:23:06 +02:00
|
|
|
success: true,
|
2016-08-23 15:41:47 +02:00
|
|
|
});
|
|
|
|
}, function(err) {
|
2017-05-25 20:21:18 +02:00
|
|
|
sendError(event, _t('You need to be able to invite users to do that.'), err);
|
2016-08-23 15:41:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-09 12:21:37 +02:00
|
|
|
function setWidget(event, roomId) {
|
|
|
|
const widgetId = event.data.widget_id;
|
|
|
|
const widgetType = event.data.type;
|
|
|
|
const widgetUrl = event.data.url;
|
2017-06-09 13:34:19 +02:00
|
|
|
const widgetName = event.data.name; // optional
|
|
|
|
const widgetData = event.data.data; // optional
|
2018-01-08 16:38:01 +01:00
|
|
|
const userWidget = event.data.userWidget;
|
2017-06-09 13:34:19 +02:00
|
|
|
|
2017-06-09 12:21:37 +02:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
|
|
|
sendError(event, _t('You need to be logged in.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-09 13:34:19 +02:00
|
|
|
// both adding/removing widgets need these checks
|
|
|
|
if (!widgetId || widgetUrl === undefined) {
|
|
|
|
sendError(event, _t("Unable to create widget."), new Error("Missing required widget fields."));
|
2017-06-09 12:21:37 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-09 13:34:19 +02:00
|
|
|
|
|
|
|
if (widgetUrl !== null) { // if url is null it is being deleted, don't need to check name/type/etc
|
|
|
|
// check types of fields
|
|
|
|
if (widgetName !== undefined && typeof widgetName !== 'string') {
|
|
|
|
sendError(event, _t("Unable to create widget."), new Error("Optional field 'name' must be a string."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (widgetData !== undefined && !(widgetData instanceof Object)) {
|
|
|
|
sendError(event, _t("Unable to create widget."), new Error("Optional field 'data' must be an Object."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof widgetType !== 'string') {
|
|
|
|
sendError(event, _t("Unable to create widget."), new Error("Field 'type' must be a string."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof widgetUrl !== 'string') {
|
|
|
|
sendError(event, _t("Unable to create widget."), new Error("Field 'url' must be a string or null."));
|
|
|
|
return;
|
|
|
|
}
|
2017-06-09 12:21:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-11 13:15:27 +02:00
|
|
|
let content = {
|
|
|
|
type: widgetType,
|
|
|
|
url: widgetUrl,
|
|
|
|
name: widgetName,
|
|
|
|
data: widgetData,
|
|
|
|
};
|
|
|
|
|
2018-01-08 16:38:01 +01:00
|
|
|
if (userWidget) {
|
2018-01-08 17:47:49 +01:00
|
|
|
console.warn('Adding user widget');
|
2018-01-08 16:38:01 +01:00
|
|
|
const client = MatrixClientPeg.get();
|
2018-01-08 17:47:49 +01:00
|
|
|
let userWidgets = client.getAccountData('m.widgets') || {};
|
2018-01-08 16:38:01 +01:00
|
|
|
|
|
|
|
// Delete existing widget with ID
|
2018-01-08 17:47:49 +01:00
|
|
|
delete userWidgets[widgetId];
|
2018-01-08 16:38:01 +01:00
|
|
|
|
|
|
|
// Add new widget / update
|
|
|
|
if (widgetUrl !== null) {
|
2018-01-08 17:47:49 +01:00
|
|
|
userWidgets[widgetId] = {
|
|
|
|
content: content,
|
|
|
|
sender: client.getUserId(),
|
|
|
|
stateKey: widgetId,
|
|
|
|
type: 'm.widget',
|
|
|
|
};
|
2018-01-08 16:38:01 +01:00
|
|
|
}
|
|
|
|
|
2018-01-08 17:47:49 +01:00
|
|
|
client.setAccountData('m.widgets', {widgets: userWidgets});
|
|
|
|
sendResponse(event, {
|
|
|
|
success: true,
|
|
|
|
});
|
2018-01-08 16:38:01 +01:00
|
|
|
} else { // Room widget
|
|
|
|
if (widgetUrl === null) { // widget is being deleted
|
|
|
|
content = {};
|
|
|
|
}
|
|
|
|
client.sendStateEvent(roomId, "im.vector.modular.widgets", content, widgetId).done(() => {
|
|
|
|
sendResponse(event, {
|
|
|
|
success: true,
|
|
|
|
});
|
|
|
|
}, (err) => {
|
|
|
|
sendError(event, _t('Failed to send request.'), err);
|
2017-06-09 12:21:37 +02:00
|
|
|
});
|
2018-01-08 16:38:01 +01:00
|
|
|
}
|
2017-06-09 12:21:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getWidgets(event, roomId) {
|
2017-07-11 13:15:27 +02:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
|
|
|
sendError(event, _t('You need to be logged in.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
if (!room) {
|
|
|
|
sendError(event, _t('This room is not recognised.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const stateEvents = room.currentState.getStateEvents("im.vector.modular.widgets");
|
|
|
|
// Only return widgets which have required fields
|
2017-10-11 18:56:17 +02:00
|
|
|
const widgetStateEvents = [];
|
2017-07-11 13:15:27 +02:00
|
|
|
stateEvents.forEach((ev) => {
|
|
|
|
if (ev.getContent().type && ev.getContent().url) {
|
|
|
|
widgetStateEvents.push(ev.event); // return the raw event
|
|
|
|
}
|
2017-10-11 18:56:17 +02:00
|
|
|
});
|
2017-07-11 13:15:27 +02:00
|
|
|
|
|
|
|
sendResponse(event, widgetStateEvents);
|
2017-06-09 12:21:37 +02:00
|
|
|
}
|
|
|
|
|
2017-12-05 12:59:02 +01:00
|
|
|
function getRoomEncState(event, roomId) {
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
|
|
|
sendError(event, _t('You need to be logged in.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
if (!room) {
|
|
|
|
sendError(event, _t('This room is not recognised.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const roomIsEncrypted = MatrixClientPeg.get().isRoomEncrypted(roomId);
|
|
|
|
|
|
|
|
sendResponse(event, roomIsEncrypted);
|
|
|
|
}
|
|
|
|
|
2016-09-15 16:24:08 +02:00
|
|
|
function setPlumbingState(event, roomId, status) {
|
|
|
|
if (typeof status !== 'string') {
|
|
|
|
throw new Error('Plumbing state status should be a string');
|
|
|
|
}
|
|
|
|
console.log(`Received request to set plumbing state to status "${status}" in room ${roomId}`);
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
2017-05-25 20:21:18 +02:00
|
|
|
sendError(event, _t('You need to be logged in.'));
|
2016-09-15 16:24:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-10-11 18:56:17 +02:00
|
|
|
client.sendStateEvent(roomId, "m.room.plumbing", { status: status }).done(() => {
|
2016-09-15 16:24:08 +02:00
|
|
|
sendResponse(event, {
|
|
|
|
success: true,
|
|
|
|
});
|
|
|
|
}, (err) => {
|
2017-05-25 20:21:18 +02:00
|
|
|
sendError(event, err.message ? err.message : _t('Failed to send request.'), err);
|
2016-09-15 16:24:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-24 14:23:06 +02:00
|
|
|
function setBotOptions(event, roomId, userId) {
|
|
|
|
console.log(`Received request to set options for bot ${userId} in room ${roomId}`);
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
2017-05-25 20:21:18 +02:00
|
|
|
sendError(event, _t('You need to be logged in.'));
|
2016-08-23 15:41:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-08-24 15:54:44 +02:00
|
|
|
client.sendStateEvent(roomId, "m.room.bot.options", event.data.content, "_" + userId).done(() => {
|
2016-08-24 14:23:06 +02:00
|
|
|
sendResponse(event, {
|
|
|
|
success: true,
|
|
|
|
});
|
|
|
|
}, (err) => {
|
2017-05-25 20:21:18 +02:00
|
|
|
sendError(event, err.message ? err.message : _t('Failed to send request.'), err);
|
2016-08-24 14:23:06 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-07 10:57:07 +02:00
|
|
|
function setBotPower(event, roomId, userId, level) {
|
|
|
|
if (!(Number.isInteger(level) && level >= 0)) {
|
2017-05-25 20:21:18 +02:00
|
|
|
sendError(event, _t('Power level must be positive integer.'));
|
2016-09-07 10:57:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-08 18:38:51 +02:00
|
|
|
console.log(`Received request to set power level to ${level} for bot ${userId} in room ${roomId}.`);
|
2016-09-07 10:57:07 +02:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
2017-05-25 20:21:18 +02:00
|
|
|
sendError(event, _t('You need to be logged in.'));
|
2016-09-07 10:57:07 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-09-07 18:06:57 +02:00
|
|
|
|
2016-09-07 18:08:02 +02:00
|
|
|
client.getStateEvent(roomId, "m.room.power_levels", "").then((powerLevels) => {
|
2017-10-11 18:56:17 +02:00
|
|
|
const powerEvent = new MatrixEvent(
|
2016-09-07 18:06:57 +02:00
|
|
|
{
|
|
|
|
type: "m.room.power_levels",
|
2016-09-07 18:08:02 +02:00
|
|
|
content: powerLevels,
|
2017-10-11 18:56:17 +02:00
|
|
|
},
|
2016-09-07 18:06:57 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
client.setPowerLevel(roomId, userId, level, powerEvent).done(() => {
|
|
|
|
sendResponse(event, {
|
|
|
|
success: true,
|
|
|
|
});
|
|
|
|
}, (err) => {
|
2017-05-25 20:21:18 +02:00
|
|
|
sendError(event, err.message ? err.message : _t('Failed to send request.'), err);
|
2016-09-07 10:57:07 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-24 14:23:06 +02:00
|
|
|
function getMembershipState(event, roomId, userId) {
|
2016-08-23 15:41:47 +02:00
|
|
|
console.log(`membership_state of ${userId} in room ${roomId} requested.`);
|
2016-08-24 14:23:06 +02:00
|
|
|
returnStateEvent(event, roomId, "m.room.member", userId);
|
|
|
|
}
|
|
|
|
|
2016-09-05 15:58:16 +02:00
|
|
|
function getJoinRules(event, roomId) {
|
|
|
|
console.log(`join_rules of ${roomId} requested.`);
|
2016-09-06 11:29:38 +02:00
|
|
|
returnStateEvent(event, roomId, "m.room.join_rules", "");
|
2016-09-05 15:58:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 14:23:06 +02:00
|
|
|
function botOptions(event, roomId, userId) {
|
|
|
|
console.log(`bot_options of ${userId} in room ${roomId} requested.`);
|
2016-08-24 15:54:44 +02:00
|
|
|
returnStateEvent(event, roomId, "m.room.bot.options", "_" + userId);
|
2016-08-24 14:23:06 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 17:41:08 +02:00
|
|
|
function getMembershipCount(event, roomId) {
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
|
|
|
sendError(event, _t('You need to be logged in.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
if (!room) {
|
|
|
|
sendError(event, _t('This room is not recognised.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const count = room.getJoinedMembers().length;
|
|
|
|
sendResponse(event, count);
|
|
|
|
}
|
|
|
|
|
2017-07-11 16:20:33 +02:00
|
|
|
function canSendEvent(event, roomId) {
|
|
|
|
const evType = "" + event.data.event_type; // force stringify
|
|
|
|
const isState = Boolean(event.data.is_state);
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
|
|
|
sendError(event, _t('You need to be logged in.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
if (!room) {
|
|
|
|
sendError(event, _t('This room is not recognised.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const me = client.credentials.userId;
|
|
|
|
const member = room.getMember(me);
|
|
|
|
if (!member || member.membership !== "join") {
|
|
|
|
sendError(event, _t('You are not in this room.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let canSend = false;
|
|
|
|
if (isState) {
|
|
|
|
canSend = room.currentState.maySendStateEvent(evType, me);
|
2017-10-11 18:56:17 +02:00
|
|
|
} else {
|
2017-07-11 16:20:33 +02:00
|
|
|
canSend = room.currentState.maySendEvent(evType, me);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!canSend) {
|
2017-07-14 12:17:59 +02:00
|
|
|
sendError(event, _t('You do not have permission to do that in this room.'));
|
2017-07-11 16:20:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendResponse(event, true);
|
|
|
|
}
|
|
|
|
|
2016-08-24 14:23:06 +02:00
|
|
|
function returnStateEvent(event, roomId, eventType, stateKey) {
|
2016-08-23 15:41:47 +02:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
2017-05-25 20:21:18 +02:00
|
|
|
sendError(event, _t('You need to be logged in.'));
|
2016-08-23 15:41:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
if (!room) {
|
2017-05-25 20:21:18 +02:00
|
|
|
sendError(event, _t('This room is not recognised.'));
|
2016-08-23 15:41:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-08-24 14:23:06 +02:00
|
|
|
const stateEvent = room.currentState.getStateEvents(eventType, stateKey);
|
|
|
|
if (!stateEvent) {
|
|
|
|
sendResponse(event, null);
|
2016-08-24 15:10:21 +02:00
|
|
|
return;
|
2016-08-23 15:41:47 +02:00
|
|
|
}
|
2016-08-24 14:23:06 +02:00
|
|
|
sendResponse(event, stateEvent.getContent());
|
2016-08-23 15:41:47 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 18:56:17 +02:00
|
|
|
let currentRoomId = null;
|
|
|
|
let currentRoomAlias = null;
|
2016-09-05 16:13:48 +02:00
|
|
|
|
|
|
|
// Listen for when a room is viewed
|
|
|
|
dis.register(onAction);
|
|
|
|
function onAction(payload) {
|
2016-09-06 11:36:44 +02:00
|
|
|
if (payload.action !== "view_room") {
|
2016-09-06 11:29:38 +02:00
|
|
|
return;
|
2016-09-05 16:13:48 +02:00
|
|
|
}
|
2016-09-06 11:29:38 +02:00
|
|
|
currentRoomId = payload.room_id;
|
2016-09-09 17:06:19 +02:00
|
|
|
currentRoomAlias = payload.room_alias;
|
2016-09-05 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
2016-08-23 13:00:11 +02:00
|
|
|
const onMessage = function(event) {
|
2016-08-23 14:31:55 +02:00
|
|
|
if (!event.origin) { // stupid chrome
|
|
|
|
event.origin = event.originalEvent.origin;
|
|
|
|
}
|
|
|
|
|
2016-12-06 15:30:21 +01:00
|
|
|
// Check that the integrations UI URL starts with the origin of the event
|
|
|
|
// This means the URL could contain a path (like /develop) and still be used
|
|
|
|
// to validate event origins, which do not specify paths.
|
|
|
|
// (See https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage)
|
|
|
|
//
|
|
|
|
// All strings start with the empty string, so for sanity return if the length
|
|
|
|
// of the event origin is 0.
|
2017-10-11 18:56:17 +02:00
|
|
|
const url = SdkConfig.get().integrations_ui_url;
|
2017-07-07 18:44:25 +02:00
|
|
|
if (event.origin.length === 0 || !url.startsWith(event.origin) || !event.data.action) {
|
2016-09-19 11:38:42 +02:00
|
|
|
return; // don't log this - debugging APIs like to spam postMessage which floods the log otherwise
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.data.action === "close_scalar") {
|
|
|
|
dis.dispatch({ action: "close_scalar" });
|
|
|
|
sendResponse(event, null);
|
2016-08-23 14:31:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-24 14:23:06 +02:00
|
|
|
const roomId = event.data.room_id;
|
|
|
|
const userId = event.data.user_id;
|
|
|
|
if (!roomId) {
|
2017-05-23 16:16:31 +02:00
|
|
|
sendError(event, _t('Missing room_id in request'));
|
2016-08-24 14:23:06 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-09-09 17:06:19 +02:00
|
|
|
let promise = Promise.resolve(currentRoomId);
|
2016-09-05 16:13:48 +02:00
|
|
|
if (!currentRoomId) {
|
2016-09-09 17:06:19 +02:00
|
|
|
if (!currentRoomAlias) {
|
2017-05-23 16:16:31 +02:00
|
|
|
sendError(event, _t('Must be viewing a room'));
|
2016-09-09 17:14:41 +02:00
|
|
|
return;
|
2016-09-09 17:06:19 +02:00
|
|
|
}
|
|
|
|
// no room ID but there is an alias, look it up.
|
|
|
|
console.log("Looking up alias " + currentRoomAlias);
|
|
|
|
promise = MatrixClientPeg.get().getRoomIdForAlias(currentRoomAlias).then((res) => {
|
|
|
|
return res.room_id;
|
|
|
|
});
|
2016-09-05 16:13:48 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 17:06:19 +02:00
|
|
|
promise.then((viewingRoomId) => {
|
|
|
|
if (roomId !== viewingRoomId) {
|
2017-05-23 16:16:31 +02:00
|
|
|
sendError(event, _t('Room %(roomId)s not visible', {roomId: roomId}));
|
2016-09-09 17:06:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-09-07 10:58:48 +02:00
|
|
|
|
2017-06-09 12:21:37 +02:00
|
|
|
// These APIs don't require userId
|
2016-09-09 17:06:19 +02:00
|
|
|
if (event.data.action === "join_rules_state") {
|
|
|
|
getJoinRules(event, roomId);
|
|
|
|
return;
|
2016-09-15 16:24:08 +02:00
|
|
|
} else if (event.data.action === "set_plumbing_state") {
|
|
|
|
setPlumbingState(event, roomId, event.data.status);
|
|
|
|
return;
|
2017-06-06 17:41:08 +02:00
|
|
|
} else if (event.data.action === "get_membership_count") {
|
|
|
|
getMembershipCount(event, roomId);
|
|
|
|
return;
|
2017-06-09 12:21:37 +02:00
|
|
|
} else if (event.data.action === "set_widget") {
|
|
|
|
setWidget(event, roomId);
|
|
|
|
return;
|
|
|
|
} else if (event.data.action === "get_widgets") {
|
|
|
|
getWidgets(event, roomId);
|
|
|
|
return;
|
2017-12-05 12:59:02 +01:00
|
|
|
} else if (event.data.action === "get_room_enc_state") {
|
|
|
|
getRoomEncState(event, roomId);
|
|
|
|
return;
|
2017-07-11 16:20:33 +02:00
|
|
|
} else if (event.data.action === "can_send_event") {
|
|
|
|
canSendEvent(event, roomId);
|
|
|
|
return;
|
2016-09-09 17:06:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!userId) {
|
2017-05-23 16:16:31 +02:00
|
|
|
sendError(event, _t('Missing user_id in request'));
|
2016-09-09 17:06:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (event.data.action) {
|
|
|
|
case "membership_state":
|
|
|
|
getMembershipState(event, roomId, userId);
|
|
|
|
break;
|
|
|
|
case "invite":
|
|
|
|
inviteUser(event, roomId, userId);
|
|
|
|
break;
|
|
|
|
case "bot_options":
|
|
|
|
botOptions(event, roomId, userId);
|
|
|
|
break;
|
|
|
|
case "set_bot_options":
|
|
|
|
setBotOptions(event, roomId, userId);
|
|
|
|
break;
|
|
|
|
case "set_bot_power":
|
|
|
|
setBotPower(event, roomId, userId, event.data.level);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn("Unhandled postMessage event with action '" + event.data.action +"'");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}, (err) => {
|
|
|
|
console.error(err);
|
2017-05-23 16:16:31 +02:00
|
|
|
sendError(event, _t('Failed to lookup current room') + '.');
|
2017-01-20 15:22:27 +01:00
|
|
|
});
|
2016-08-23 13:00:11 +02:00
|
|
|
};
|
|
|
|
|
2017-06-30 16:42:51 +02:00
|
|
|
let listenerCount = 0;
|
2016-08-23 13:00:11 +02:00
|
|
|
module.exports = {
|
2016-08-23 14:31:55 +02:00
|
|
|
startListening: function() {
|
2017-06-30 16:42:51 +02:00
|
|
|
if (listenerCount === 0) {
|
|
|
|
window.addEventListener("message", onMessage, false);
|
|
|
|
}
|
|
|
|
listenerCount += 1;
|
2016-08-23 14:31:55 +02:00
|
|
|
},
|
2016-08-23 13:00:11 +02:00
|
|
|
|
2016-08-23 14:31:55 +02:00
|
|
|
stopListening: function() {
|
2017-06-30 16:42:51 +02:00
|
|
|
listenerCount -= 1;
|
|
|
|
if (listenerCount === 0) {
|
|
|
|
window.removeEventListener("message", onMessage);
|
|
|
|
}
|
|
|
|
if (listenerCount < 0) {
|
|
|
|
// Make an error so we get a stack trace
|
|
|
|
const e = new Error(
|
|
|
|
"ScalarMessaging: mismatched startListening / stopListening detected." +
|
2017-10-11 18:56:17 +02:00
|
|
|
" Negative count",
|
2017-06-30 16:42:51 +02:00
|
|
|
);
|
|
|
|
console.error(e);
|
|
|
|
}
|
2016-08-23 15:41:47 +02:00
|
|
|
},
|
2016-08-23 13:00:11 +02:00
|
|
|
};
|