2016-08-23 11:00:11 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2017-06-09 10:21:37 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-06-13 09:39:52 +00:00
|
|
|
Copyright 2018 New Vector Ltd
|
2016-08-23 11:00:11 +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.
|
|
|
|
*/
|
|
|
|
|
2020-04-22 06:34:08 +00:00
|
|
|
// TODO: Generify the name of this and all components within - it's not just for scalar.
|
|
|
|
|
2016-08-23 11:00:11 +00:00
|
|
|
/*
|
|
|
|
Listens for incoming postMessage requests from the integrations UI URL. The following API is exposed:
|
|
|
|
{
|
2017-07-11 14:20:33 +00:00
|
|
|
action: "invite" | "membership_state" | "bot_options" | "set_bot_options" | etc... ,
|
2016-08-23 12:31:55 +00:00
|
|
|
room_id: $ROOM_ID,
|
|
|
|
user_id: $USER_ID
|
2016-08-24 12:23:06 +00:00
|
|
|
// additional request fields
|
2016-08-23 11:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
The complete request object is returned to the caller with an additional "response" key like so:
|
|
|
|
{
|
2016-08-24 12:23:06 +00:00
|
|
|
action: "invite" | "membership_state" | "bot_options" | "set_bot_options",
|
2016-08-23 12:31:55 +00:00
|
|
|
room_id: $ROOM_ID,
|
|
|
|
user_id: $USER_ID,
|
2016-08-24 12:23:06 +00:00
|
|
|
// additional request fields
|
2016-08-23 12:31:55 +00:00
|
|
|
response: { ... }
|
2016-08-23 11:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 12:23:06 +00: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 11:00:11 +00:00
|
|
|
{
|
2016-08-23 12:31:55 +00:00
|
|
|
error: {
|
|
|
|
message: "Unable to invite user into room.",
|
|
|
|
_error: <Original Error Object>
|
|
|
|
}
|
2016-08-23 11:00:11 +00:00
|
|
|
}
|
|
|
|
The "message" key should be a human-friendly string.
|
|
|
|
|
2016-08-24 12:23:06 +00:00
|
|
|
ACTIONS
|
|
|
|
=======
|
|
|
|
All actions can return an error response instead of the response outlined below.
|
|
|
|
|
|
|
|
invite
|
|
|
|
------
|
2021-12-10 14:26:38 +00:00
|
|
|
Invites a user into a room. The request will no-op if the user is already joined OR invited to the room.
|
2016-08-24 12:23:06 +00:00
|
|
|
|
|
|
|
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 11:00:11 +00:00
|
|
|
{
|
2016-08-24 12:23:06 +00:00
|
|
|
action: "invite",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
user_id: "@invitee:bar",
|
|
|
|
response: {
|
|
|
|
success: true
|
|
|
|
}
|
2016-08-23 11:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 12:23:06 +00: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 11:00:11 +00:00
|
|
|
{
|
2016-08-24 12:23:06 +00:00
|
|
|
success: true
|
2016-08-23 11:00:11 +00:00
|
|
|
}
|
2016-08-24 12:23:06 +00:00
|
|
|
Example:
|
|
|
|
{
|
|
|
|
action: "set_bot_options",
|
|
|
|
room_id: "!foo:bar",
|
|
|
|
user_id: "@bot:bar",
|
|
|
|
content: {
|
|
|
|
default_option: "alpha"
|
|
|
|
},
|
|
|
|
response: {
|
|
|
|
success: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 15:41:08 +00: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 14:20:33 +00: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 10:21:37 +00: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.
|
2022-05-19 22:22:38 +00:00
|
|
|
- `avatar_url` (String) is some optional mxc: URI pointing to the avatar of the widget.
|
2017-06-09 10:21:37 +00:00
|
|
|
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 11:15:27 +00:00
|
|
|
Get a list of all widgets in the room. The response is an array
|
|
|
|
of state events.
|
2017-06-09 10:21:37 +00:00
|
|
|
|
|
|
|
Request:
|
|
|
|
- `room_id` (String) is the room to get the widgets in.
|
|
|
|
Response:
|
2017-07-11 11:15:27 +00:00
|
|
|
[
|
|
|
|
{
|
2020-08-03 15:02:26 +00:00
|
|
|
// TODO: Enable support for m.widget event type (https://github.com/vector-im/element-web/issues/13111)
|
2017-07-11 11:15:27 +00:00
|
|
|
type: "im.vector.modular.widgets",
|
|
|
|
state_key: "wid1",
|
|
|
|
content: {
|
|
|
|
type: "grafana",
|
|
|
|
url: "https://grafanaurl",
|
|
|
|
name: "dashboard",
|
|
|
|
data: {key: "val"}
|
2017-06-09 10:21:37 +00:00
|
|
|
}
|
2021-10-30 01:59:21 +00:00
|
|
|
room_id: "!foo:bar",
|
2017-07-11 11:15:27 +00:00
|
|
|
sender: "@alice:localhost"
|
|
|
|
}
|
|
|
|
]
|
2017-06-09 10:21:37 +00:00
|
|
|
Example:
|
|
|
|
{
|
|
|
|
action: "get_widgets",
|
|
|
|
room_id: "!foo:bar",
|
2017-07-11 11:15:27 +00:00
|
|
|
response: [
|
|
|
|
{
|
2020-08-03 15:02:26 +00:00
|
|
|
// TODO: Enable support for m.widget event type (https://github.com/vector-im/element-web/issues/13111)
|
2017-07-11 11:15:27 +00:00
|
|
|
type: "im.vector.modular.widgets",
|
|
|
|
state_key: "wid1",
|
|
|
|
content: {
|
|
|
|
type: "grafana",
|
|
|
|
url: "https://grafanaurl",
|
|
|
|
name: "dashboard",
|
|
|
|
data: {key: "val"}
|
2017-06-09 10:21:37 +00:00
|
|
|
}
|
2021-10-30 01:59:21 +00:00
|
|
|
room_id: "!foo:bar",
|
2017-07-11 11:15:27 +00:00
|
|
|
sender: "@alice:localhost"
|
|
|
|
}
|
|
|
|
]
|
2017-06-09 10:21:37 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 12:23:06 +00: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 11:00:11 +00:00
|
|
|
|
2016-08-24 12:23:06 +00: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
|
|
|
|
}
|
|
|
|
}
|
2022-06-16 23:19:04 +00:00
|
|
|
|
|
|
|
get_open_id_token
|
|
|
|
-----------------
|
|
|
|
Get an openID token for the current user session.
|
|
|
|
Request: No parameters
|
|
|
|
Response:
|
|
|
|
- The openId token object as described in https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3useruseridopenidrequest_token
|
2016-08-23 11:00:11 +00:00
|
|
|
*/
|
|
|
|
|
2021-03-19 02:50:34 +00:00
|
|
|
import { MatrixEvent } from 'matrix-js-sdk/src/models/event';
|
2021-10-22 22:23:32 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
|
|
|
import { MatrixClientPeg } from './MatrixClientPeg';
|
2020-05-14 02:41:41 +00:00
|
|
|
import dis from './dispatcher/dispatcher';
|
2018-06-26 10:59:16 +00:00
|
|
|
import WidgetUtils from './utils/WidgetUtils';
|
2022-03-23 05:26:30 +00:00
|
|
|
import { RoomViewStore } from './stores/RoomViewStore';
|
2017-05-25 10:39:08 +00:00
|
|
|
import { _t } from './languageHandler';
|
2021-06-29 12:11:58 +00:00
|
|
|
import { IntegrationManagers } from "./integrations/IntegrationManagers";
|
|
|
|
import { WidgetType } from "./widgets/WidgetType";
|
|
|
|
import { objectClone } from "./utils/objects";
|
2016-08-23 11:00:11 +00:00
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
enum Action {
|
|
|
|
CloseScalar = "close_scalar",
|
|
|
|
GetWidgets = "get_widgets",
|
|
|
|
SetWidget = "set_widget",
|
|
|
|
JoinRulesState = "join_rules_state",
|
|
|
|
SetPlumbingState = "set_plumbing_state",
|
|
|
|
GetMembershipCount = "get_membership_count",
|
|
|
|
GetRoomEncryptionState = "get_room_enc_state",
|
|
|
|
CanSendEvent = "can_send_event",
|
|
|
|
MembershipState = "membership_state",
|
|
|
|
invite = "invite",
|
|
|
|
BotOptions = "bot_options",
|
|
|
|
SetBotOptions = "set_bot_options",
|
|
|
|
SetBotPower = "set_bot_power",
|
2022-06-16 23:19:04 +00:00
|
|
|
GetOpenIdToken = "get_open_id_token"
|
2021-09-27 13:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sendResponse(event: MessageEvent<any>, res: any): void {
|
2020-06-22 20:14:43 +00:00
|
|
|
const data = objectClone(event.data);
|
2016-08-23 13:41:47 +00:00
|
|
|
data.response = res;
|
2021-09-27 13:18:31 +00:00
|
|
|
// @ts-ignore
|
2016-08-23 13:41:47 +00:00
|
|
|
event.source.postMessage(data, event.origin);
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
function sendError(event: MessageEvent<any>, msg: string, nestedError?: Error): void {
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error("Action:" + event.data.action + " failed with message: " + msg);
|
2020-06-22 20:14:43 +00:00
|
|
|
const data = objectClone(event.data);
|
2016-08-23 13:41:47 +00:00
|
|
|
data.response = {
|
|
|
|
error: {
|
|
|
|
message: msg,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if (nestedError) {
|
|
|
|
data.response.error._error = nestedError;
|
|
|
|
}
|
2021-09-27 13:18:31 +00:00
|
|
|
// @ts-ignore
|
2016-08-23 13:41:47 +00:00
|
|
|
event.source.postMessage(data, event.origin);
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
function inviteUser(event: MessageEvent<any>, roomId: string, userId: string): void {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log(`Received request to invite ${userId} into room ${roomId}`);
|
2016-08-23 13:41:47 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
2017-05-25 18:21:18 +00:00
|
|
|
sendError(event, _t('You need to be logged in.'));
|
2016-08-23 13:41:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
if (room) {
|
2021-12-10 14:26:38 +00:00
|
|
|
// if they are already invited or joined we can resolve immediately.
|
2016-08-23 13:41:47 +00:00
|
|
|
const member = room.getMember(userId);
|
2021-12-10 14:26:38 +00:00
|
|
|
if (member && ["join", "invite"].includes(member.membership)) {
|
2016-08-23 13:41:47 +00:00
|
|
|
sendResponse(event, {
|
2016-08-24 12:23:06 +00:00
|
|
|
success: true,
|
2016-08-23 13:41:47 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 10:03:05 +00:00
|
|
|
client.invite(roomId, userId).then(function() {
|
2016-08-23 13:41:47 +00:00
|
|
|
sendResponse(event, {
|
2016-08-24 12:23:06 +00:00
|
|
|
success: true,
|
2016-08-23 13:41:47 +00:00
|
|
|
});
|
|
|
|
}, function(err) {
|
2017-05-25 18:21:18 +00:00
|
|
|
sendError(event, _t('You need to be able to invite users to do that.'), err);
|
2016-08-23 13:41:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
function setWidget(event: MessageEvent<any>, roomId: string): void {
|
2017-06-09 10:21:37 +00:00
|
|
|
const widgetId = event.data.widget_id;
|
2020-04-09 21:25:11 +00:00
|
|
|
let widgetType = event.data.type;
|
2017-06-09 10:21:37 +00:00
|
|
|
const widgetUrl = event.data.url;
|
2017-06-09 11:34:19 +00:00
|
|
|
const widgetName = event.data.name; // optional
|
|
|
|
const widgetData = event.data.data; // optional
|
2022-05-19 22:22:38 +00:00
|
|
|
const widgetAvatarUrl = event.data.avatar_url; // optional
|
2018-01-08 15:38:01 +00:00
|
|
|
const userWidget = event.data.userWidget;
|
2017-06-09 11:34:19 +00: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 10:21:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-06-09 11:34:19 +00: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;
|
|
|
|
}
|
2022-05-19 22:22:38 +00:00
|
|
|
if (widgetAvatarUrl !== undefined && typeof widgetAvatarUrl !== 'string') {
|
|
|
|
sendError(
|
|
|
|
event,
|
|
|
|
_t("Unable to create widget."),
|
|
|
|
new Error("Optional field 'avatar_url' must be a string."),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2017-06-09 11:34:19 +00:00
|
|
|
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 10:21:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 21:25:11 +00:00
|
|
|
// convert the widget type to a known widget type
|
|
|
|
widgetType = WidgetType.fromString(widgetType);
|
|
|
|
|
2018-01-08 15:38:01 +00:00
|
|
|
if (userWidget) {
|
2018-06-25 14:30:04 +00:00
|
|
|
WidgetUtils.setUserWidget(widgetId, widgetType, widgetUrl, widgetName, widgetData).then(() => {
|
2018-03-29 19:07:26 +00:00
|
|
|
sendResponse(event, {
|
|
|
|
success: true,
|
|
|
|
});
|
2018-02-07 09:23:00 +00:00
|
|
|
|
2018-03-29 19:07:26 +00:00
|
|
|
dis.dispatch({ action: "user_widget_updated" });
|
2018-05-15 13:53:49 +00:00
|
|
|
}).catch((e) => {
|
2018-05-15 14:19:28 +00:00
|
|
|
sendError(event, _t('Unable to create widget.'), e);
|
2018-03-29 19:07:26 +00:00
|
|
|
});
|
2018-01-08 15:38:01 +00:00
|
|
|
} else { // Room widget
|
2018-01-09 14:37:45 +00:00
|
|
|
if (!roomId) {
|
|
|
|
sendError(event, _t('Missing roomId.'), null);
|
|
|
|
}
|
2022-05-19 22:22:38 +00:00
|
|
|
WidgetUtils.setRoomWidget(roomId, widgetId, widgetType, widgetUrl, widgetName, widgetData, widgetAvatarUrl)
|
|
|
|
.then(() => {
|
|
|
|
sendResponse(event, {
|
|
|
|
success: true,
|
|
|
|
});
|
|
|
|
}, (err) => {
|
|
|
|
sendError(event, _t('Failed to send request.'), err);
|
2018-01-08 15:38:01 +00:00
|
|
|
});
|
|
|
|
}
|
2017-06-09 10:21:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
function getWidgets(event: MessageEvent<any>, roomId: string): void {
|
2017-07-11 11:15:27 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
|
|
|
sendError(event, _t('You need to be logged in.'));
|
|
|
|
return;
|
|
|
|
}
|
2018-01-09 14:37:45 +00:00
|
|
|
let widgetStateEvents = [];
|
|
|
|
|
|
|
|
if (roomId) {
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
if (!room) {
|
|
|
|
sendError(event, _t('This room is not recognised.'));
|
|
|
|
return;
|
2017-07-11 11:15:27 +00:00
|
|
|
}
|
2018-06-26 11:28:59 +00:00
|
|
|
// XXX: This gets the raw event object (I think because we can't
|
|
|
|
// send the MatrixEvent over postMessage?)
|
|
|
|
widgetStateEvents = WidgetUtils.getRoomWidgets(room).map((ev) => ev.event);
|
2018-01-09 14:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add user widgets (not linked to a specific room)
|
2018-06-26 10:52:21 +00:00
|
|
|
const userWidgets = WidgetUtils.getUserWidgetsArray();
|
2018-01-11 10:32:37 +00:00
|
|
|
widgetStateEvents = widgetStateEvents.concat(userWidgets);
|
2017-07-11 11:15:27 +00:00
|
|
|
|
|
|
|
sendResponse(event, widgetStateEvents);
|
2017-06-09 10:21:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
function getRoomEncState(event: MessageEvent<any>, roomId: string): void {
|
2017-12-05 11:59:02 +00: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 roomIsEncrypted = MatrixClientPeg.get().isRoomEncrypted(roomId);
|
|
|
|
|
|
|
|
sendResponse(event, roomIsEncrypted);
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
function setPlumbingState(event: MessageEvent<any>, roomId: string, status: string): void {
|
2016-09-15 14:24:08 +00:00
|
|
|
if (typeof status !== 'string') {
|
|
|
|
throw new Error('Plumbing state status should be a string');
|
|
|
|
}
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log(`Received request to set plumbing state to status "${status}" in room ${roomId}`);
|
2016-09-15 14:24:08 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
2017-05-25 18:21:18 +00:00
|
|
|
sendError(event, _t('You need to be logged in.'));
|
2016-09-15 14:24:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-18 10:03:05 +00:00
|
|
|
client.sendStateEvent(roomId, "m.room.plumbing", { status: status }).then(() => {
|
2016-09-15 14:24:08 +00:00
|
|
|
sendResponse(event, {
|
|
|
|
success: true,
|
|
|
|
});
|
|
|
|
}, (err) => {
|
2017-05-25 18:21:18 +00:00
|
|
|
sendError(event, err.message ? err.message : _t('Failed to send request.'), err);
|
2016-09-15 14:24:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
function setBotOptions(event: MessageEvent<any>, roomId: string, userId: string): void {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log(`Received request to set options for bot ${userId} in room ${roomId}`);
|
2016-08-24 12:23:06 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
2017-05-25 18:21:18 +00:00
|
|
|
sendError(event, _t('You need to be logged in.'));
|
2016-08-23 13:41:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-18 10:03:05 +00:00
|
|
|
client.sendStateEvent(roomId, "m.room.bot.options", event.data.content, "_" + userId).then(() => {
|
2016-08-24 12:23:06 +00:00
|
|
|
sendResponse(event, {
|
|
|
|
success: true,
|
|
|
|
});
|
|
|
|
}, (err) => {
|
2017-05-25 18:21:18 +00:00
|
|
|
sendError(event, err.message ? err.message : _t('Failed to send request.'), err);
|
2016-08-24 12:23:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-06 15:51:07 +00:00
|
|
|
async function setBotPower(
|
|
|
|
event: MessageEvent<any>, roomId: string, userId: string, level: number, ignoreIfGreater?: boolean,
|
|
|
|
): Promise<void> {
|
2016-09-07 08:57:07 +00:00
|
|
|
if (!(Number.isInteger(level) && level >= 0)) {
|
2017-05-25 18:21:18 +00:00
|
|
|
sendError(event, _t('Power level must be positive integer.'));
|
2016-09-07 08:57:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log(`Received request to set power level to ${level} for bot ${userId} in room ${roomId}.`);
|
2016-09-07 08:57:07 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
2017-05-25 18:21:18 +00:00
|
|
|
sendError(event, _t('You need to be logged in.'));
|
2016-09-07 08:57:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-09-07 16:06:57 +00:00
|
|
|
|
2021-10-06 15:51:07 +00:00
|
|
|
try {
|
|
|
|
const powerLevels = await client.getStateEvent(roomId, "m.room.power_levels", "");
|
2016-09-07 16:06:57 +00:00
|
|
|
|
2021-10-06 15:48:17 +00:00
|
|
|
// If the PL is equal to or greater than the requested PL, ignore.
|
2021-10-06 15:51:07 +00:00
|
|
|
if (ignoreIfGreater === true) {
|
2021-10-06 15:48:17 +00:00
|
|
|
// As per https://matrix.org/docs/spec/client_server/r0.6.0#m-room-power-levels
|
2021-12-01 17:23:26 +00:00
|
|
|
const currentPl = powerLevels.users?.[userId] ?? powerLevels.users_default ?? 0;
|
2021-10-06 15:48:17 +00:00
|
|
|
if (currentPl >= level) {
|
2021-10-06 15:51:07 +00:00
|
|
|
return sendResponse(event, {
|
2021-10-06 15:48:17 +00:00
|
|
|
success: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-10-06 15:51:07 +00:00
|
|
|
await client.setPowerLevel(roomId, userId, level, new MatrixEvent(
|
|
|
|
{
|
|
|
|
type: "m.room.power_levels",
|
|
|
|
content: powerLevels,
|
|
|
|
},
|
|
|
|
));
|
|
|
|
return sendResponse(event, {
|
|
|
|
success: true,
|
2016-09-07 08:57:07 +00:00
|
|
|
});
|
2021-10-06 15:51:07 +00:00
|
|
|
} catch (err) {
|
|
|
|
sendError(event, err.message ? err.message : _t('Failed to send request.'), err);
|
|
|
|
}
|
2016-09-07 08:57:07 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
function getMembershipState(event: MessageEvent<any>, roomId: string, userId: string): void {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log(`membership_state of ${userId} in room ${roomId} requested.`);
|
2016-08-24 12:23:06 +00:00
|
|
|
returnStateEvent(event, roomId, "m.room.member", userId);
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
function getJoinRules(event: MessageEvent<any>, roomId: string): void {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log(`join_rules of ${roomId} requested.`);
|
2016-09-06 09:29:38 +00:00
|
|
|
returnStateEvent(event, roomId, "m.room.join_rules", "");
|
2016-09-05 13:58:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
function botOptions(event: MessageEvent<any>, roomId: string, userId: string): void {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log(`bot_options of ${userId} in room ${roomId} requested.`);
|
2016-08-24 13:54:44 +00:00
|
|
|
returnStateEvent(event, roomId, "m.room.bot.options", "_" + userId);
|
2016-08-24 12:23:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
function getMembershipCount(event: MessageEvent<any>, roomId: string): void {
|
2017-06-06 15:41:08 +00: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;
|
|
|
|
}
|
2018-08-22 15:35:58 +00:00
|
|
|
const count = room.getJoinedMemberCount();
|
2017-06-06 15:41:08 +00:00
|
|
|
sendResponse(event, count);
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
function canSendEvent(event: MessageEvent<any>, roomId: string): void {
|
2017-07-11 14:20:33 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-08-22 15:35:58 +00:00
|
|
|
if (room.getMyMembership() !== "join") {
|
2017-07-11 14:20:33 +00:00
|
|
|
sendError(event, _t('You are not in this room.'));
|
|
|
|
return;
|
|
|
|
}
|
2018-08-22 15:35:58 +00:00
|
|
|
const me = client.credentials.userId;
|
2017-07-11 14:20:33 +00:00
|
|
|
|
|
|
|
let canSend = false;
|
|
|
|
if (isState) {
|
|
|
|
canSend = room.currentState.maySendStateEvent(evType, me);
|
2017-10-11 16:56:17 +00:00
|
|
|
} else {
|
2017-07-11 14:20:33 +00:00
|
|
|
canSend = room.currentState.maySendEvent(evType, me);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!canSend) {
|
2017-07-14 10:17:59 +00:00
|
|
|
sendError(event, _t('You do not have permission to do that in this room.'));
|
2017-07-11 14:20:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendResponse(event, true);
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
function returnStateEvent(event: MessageEvent<any>, roomId: string, eventType: string, stateKey: string): void {
|
2016-08-23 13:41:47 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
2017-05-25 18:21:18 +00:00
|
|
|
sendError(event, _t('You need to be logged in.'));
|
2016-08-23 13:41:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
if (!room) {
|
2017-05-25 18:21:18 +00:00
|
|
|
sendError(event, _t('This room is not recognised.'));
|
2016-08-23 13:41:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-08-24 12:23:06 +00:00
|
|
|
const stateEvent = room.currentState.getStateEvents(eventType, stateKey);
|
|
|
|
if (!stateEvent) {
|
|
|
|
sendResponse(event, null);
|
2016-08-24 13:10:21 +00:00
|
|
|
return;
|
2016-08-23 13:41:47 +00:00
|
|
|
}
|
2016-08-24 12:23:06 +00:00
|
|
|
sendResponse(event, stateEvent.getContent());
|
2016-08-23 13:41:47 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 23:19:04 +00:00
|
|
|
async function getOpenIdToken(event: MessageEvent<any>) {
|
|
|
|
try {
|
|
|
|
const tokenObject = MatrixClientPeg.get().getOpenIdToken();
|
|
|
|
sendResponse(event, tokenObject);
|
|
|
|
} catch (ex) {
|
|
|
|
logger.warn("Unable to fetch openId token.", ex);
|
|
|
|
sendError(event, 'Unable to fetch openId token.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
const onMessage = function(event: MessageEvent<any>): void {
|
2016-08-23 12:31:55 +00:00
|
|
|
if (!event.origin) { // stupid chrome
|
2021-09-27 13:18:31 +00:00
|
|
|
// @ts-ignore
|
2016-08-23 12:31:55 +00:00
|
|
|
event.origin = event.originalEvent.origin;
|
|
|
|
}
|
|
|
|
|
2016-12-06 14:30:21 +00: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)
|
2019-07-12 18:18:30 +00:00
|
|
|
let configUrl;
|
|
|
|
try {
|
2019-08-23 15:12:40 +00:00
|
|
|
if (!openManagerUrl) openManagerUrl = IntegrationManagers.sharedInstance().getPrimaryManager().uiUrl;
|
|
|
|
configUrl = new URL(openManagerUrl);
|
2019-07-12 18:18:30 +00:00
|
|
|
} catch (e) {
|
|
|
|
// No integrations UI URL, ignore silently.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let eventOriginUrl;
|
|
|
|
try {
|
|
|
|
eventOriginUrl = new URL(event.origin);
|
|
|
|
} catch (e) {
|
2019-07-12 18:04:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-19 15:39:13 +00:00
|
|
|
// TODO -- Scalar postMessage API should be namespaced with event.data.api field
|
|
|
|
// Fix following "if" statement to respond only to specific API messages.
|
|
|
|
if (
|
2019-07-17 08:42:04 +00:00
|
|
|
configUrl.origin !== eventOriginUrl.origin ||
|
2017-12-19 15:39:13 +00:00
|
|
|
!event.data.action ||
|
|
|
|
event.data.api // Ignore messages with specific API set
|
|
|
|
) {
|
2019-07-17 08:42:04 +00:00
|
|
|
// don't log this - debugging APIs and browser add-ons like to spam
|
|
|
|
// postMessage which floods the log otherwise
|
|
|
|
return;
|
2016-09-19 09:38:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
if (event.data.action === Action.CloseScalar) {
|
|
|
|
dis.dispatch({ action: Action.CloseScalar });
|
2016-09-19 09:38:42 +00:00
|
|
|
sendResponse(event, null);
|
2016-08-23 12:31:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-24 12:23:06 +00:00
|
|
|
const roomId = event.data.room_id;
|
|
|
|
const userId = event.data.user_id;
|
2018-01-09 14:37:45 +00:00
|
|
|
|
2018-03-13 11:58:18 +00:00
|
|
|
if (!roomId) {
|
|
|
|
// These APIs don't require roomId
|
|
|
|
// Get and set user widgets (not associated with a specific room)
|
|
|
|
// If roomId is specified, it must be validated, so room-based widgets agreed
|
|
|
|
// handled further down.
|
2021-09-27 13:18:31 +00:00
|
|
|
if (event.data.action === Action.GetWidgets) {
|
2018-03-13 10:07:27 +00:00
|
|
|
getWidgets(event, null);
|
|
|
|
return;
|
2022-01-05 09:54:38 +00:00
|
|
|
} else if (event.data.action === Action.SetWidget) {
|
2018-03-13 10:07:27 +00:00
|
|
|
setWidget(event, null);
|
|
|
|
return;
|
2018-03-13 11:58:18 +00:00
|
|
|
} else {
|
|
|
|
sendError(event, _t('Missing room_id in request'));
|
|
|
|
return;
|
2018-03-13 10:07:27 +00:00
|
|
|
}
|
2018-01-09 14:37:45 +00:00
|
|
|
}
|
2016-09-05 14:13:48 +00:00
|
|
|
|
2022-03-23 05:26:30 +00:00
|
|
|
if (roomId !== RoomViewStore.instance.getRoomId()) {
|
2021-06-29 12:11:58 +00:00
|
|
|
sendError(event, _t('Room %(roomId)s not visible', { roomId: roomId }));
|
2018-06-13 09:39:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-09-07 08:58:48 +00:00
|
|
|
|
2018-06-13 09:39:52 +00:00
|
|
|
// Get and set room-based widgets
|
2021-09-27 13:18:31 +00:00
|
|
|
if (event.data.action === Action.GetWidgets) {
|
2018-06-13 09:39:52 +00:00
|
|
|
getWidgets(event, roomId);
|
|
|
|
return;
|
2021-09-27 13:18:31 +00:00
|
|
|
} else if (event.data.action === Action.SetWidget) {
|
2018-06-13 09:39:52 +00:00
|
|
|
setWidget(event, roomId);
|
|
|
|
return;
|
|
|
|
}
|
2018-02-23 14:53:52 +00:00
|
|
|
|
2018-06-13 09:39:52 +00:00
|
|
|
// These APIs don't require userId
|
2021-09-27 13:18:31 +00:00
|
|
|
if (event.data.action === Action.JoinRulesState) {
|
2018-06-13 09:39:52 +00:00
|
|
|
getJoinRules(event, roomId);
|
|
|
|
return;
|
2021-09-27 13:18:31 +00:00
|
|
|
} else if (event.data.action === Action.SetPlumbingState) {
|
2018-06-13 09:39:52 +00:00
|
|
|
setPlumbingState(event, roomId, event.data.status);
|
|
|
|
return;
|
2021-09-27 13:18:31 +00:00
|
|
|
} else if (event.data.action === Action.GetMembershipCount) {
|
2018-06-13 09:39:52 +00:00
|
|
|
getMembershipCount(event, roomId);
|
|
|
|
return;
|
2021-09-27 13:18:31 +00:00
|
|
|
} else if (event.data.action === Action.GetRoomEncryptionState) {
|
2018-06-13 09:39:52 +00:00
|
|
|
getRoomEncState(event, roomId);
|
|
|
|
return;
|
2021-09-27 13:18:31 +00:00
|
|
|
} else if (event.data.action === Action.CanSendEvent) {
|
2018-06-13 09:39:52 +00:00
|
|
|
canSendEvent(event, roomId);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-09 15:06:19 +00:00
|
|
|
|
2018-06-13 09:39:52 +00:00
|
|
|
if (!userId) {
|
|
|
|
sendError(event, _t('Missing user_id in request'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (event.data.action) {
|
2021-09-27 13:18:31 +00:00
|
|
|
case Action.MembershipState:
|
2018-06-13 09:39:52 +00:00
|
|
|
getMembershipState(event, roomId, userId);
|
|
|
|
break;
|
2021-09-27 13:18:31 +00:00
|
|
|
case Action.invite:
|
2018-06-13 09:39:52 +00:00
|
|
|
inviteUser(event, roomId, userId);
|
|
|
|
break;
|
2021-09-27 13:18:31 +00:00
|
|
|
case Action.BotOptions:
|
2018-06-13 09:39:52 +00:00
|
|
|
botOptions(event, roomId, userId);
|
|
|
|
break;
|
2021-09-27 13:18:31 +00:00
|
|
|
case Action.SetBotOptions:
|
2018-06-13 09:39:52 +00:00
|
|
|
setBotOptions(event, roomId, userId);
|
|
|
|
break;
|
2021-09-27 13:18:31 +00:00
|
|
|
case Action.SetBotPower:
|
2021-10-06 15:48:17 +00:00
|
|
|
setBotPower(event, roomId, userId, event.data.level, event.data.ignoreIfGreater);
|
2018-06-13 09:39:52 +00:00
|
|
|
break;
|
2022-06-16 23:19:04 +00:00
|
|
|
case Action.GetOpenIdToken:
|
|
|
|
getOpenIdToken(event);
|
|
|
|
break;
|
2018-06-13 09:39:52 +00:00
|
|
|
default:
|
2021-10-15 14:31:29 +00:00
|
|
|
logger.warn("Unhandled postMessage event with action '" + event.data.action +"'");
|
2018-06-13 09:39:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-08-23 11:00:11 +00:00
|
|
|
};
|
|
|
|
|
2017-06-30 14:42:51 +00:00
|
|
|
let listenerCount = 0;
|
2021-09-27 13:18:31 +00:00
|
|
|
let openManagerUrl: string = null;
|
2016-08-23 11:00:11 +00:00
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
export function startListening(): void {
|
2019-12-20 00:45:24 +00:00
|
|
|
if (listenerCount === 0) {
|
|
|
|
window.addEventListener("message", onMessage, false);
|
|
|
|
}
|
|
|
|
listenerCount += 1;
|
|
|
|
}
|
2019-08-23 15:12:40 +00:00
|
|
|
|
2021-09-27 13:18:31 +00:00
|
|
|
export function stopListening(): void {
|
2019-12-20 00:45:24 +00: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." +
|
|
|
|
" Negative count",
|
|
|
|
);
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error(e);
|
2019-12-20 00:45:24 +00:00
|
|
|
}
|
|
|
|
}
|