From 636925314259c48c96fe0c07c07b2e1cca237177 Mon Sep 17 00:00:00 2001 From: Kegsay Date: Tue, 11 Jul 2017 15:20:33 +0100 Subject: [PATCH] Scalar messaging: Add can_send_event operation (#1204) This is mainly for use to pre-emptively show/hide buttons. --- src/ScalarMessaging.js | 61 ++++++++++++++++++++++++++++++++++++- src/i18n/strings/en_EN.json | 2 ++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/ScalarMessaging.js b/src/ScalarMessaging.js index df89f7dba2..88a78595d6 100644 --- a/src/ScalarMessaging.js +++ b/src/ScalarMessaging.js @@ -18,7 +18,7 @@ limitations under the License. /* Listens for incoming postMessage requests from the integrations UI URL. The following API is exposed: { - action: "invite" | "membership_state" | "bot_options" | "set_bot_options", + action: "invite" | "membership_state" | "bot_options" | "set_bot_options" | etc... , room_id: $ROOM_ID, user_id: $USER_ID // additional request fields @@ -110,6 +110,26 @@ Example: response: 78 } +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 +} + set_widget ---------- Set a new widget in the room. Clobbers based on the ID. @@ -442,6 +462,42 @@ function getMembershipCount(event, roomId) { sendResponse(event, count); } +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); + } + else { + canSend = room.currentState.maySendEvent(evType, me); + } + + if (!canSend) { + sendError(event, _t('You do not have permission in this room.')); + return; + } + + sendResponse(event, true); +} + function returnStateEvent(event, roomId, eventType, stateKey) { const client = MatrixClientPeg.get(); if (!client) { @@ -538,6 +594,9 @@ const onMessage = function(event) { } else if (event.data.action === "get_widgets") { getWidgets(event, roomId); return; + } else if (event.data.action === "can_send_event") { + canSendEvent(event, roomId); + return; } if (!userId) { diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index fc4257cbc1..d68d517302 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -661,6 +661,8 @@ "Would you like to accept or decline this invitation?": "Would you like to accept or decline this invitation?", "You already have existing direct chats with this user:": "You already have existing direct chats with this user:", "You are already in a call.": "You are already in a call.", + "You are not in this room.": "You are not in this room.", + "You do not have permission to do that in this room.": "You do not have permission to do that in this room.", "You're not in any rooms yet! Press to make a room or to browse the directory": "You're not in any rooms yet! Press to make a room or to browse the directory", "You are trying to access %(roomName)s.": "You are trying to access %(roomName)s.", "You cannot place a call with yourself.": "You cannot place a call with yourself.",