From ccf3c1de7a19d2ea6a74481a044c5af15b012355 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Thu, 27 Jul 2017 23:37:33 +0100 Subject: [PATCH] Add widget utility class. Add static method to determine if user can modify widgets in the specified room. --- src/WidgetUtils.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/WidgetUtils.js diff --git a/src/WidgetUtils.js b/src/WidgetUtils.js new file mode 100644 index 0000000000..60bda8c5b1 --- /dev/null +++ b/src/WidgetUtils.js @@ -0,0 +1,53 @@ +/* +Copyright 2015, 2016 OpenMarket Ltd +Copyright 2017 Vector Creations Ltd + +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'; + +export default class WidgetUtils { + + /* Returns true if user is able to send state events to modify widgets in this room + * @param roomId -- The ID of the room to check + * @return Boolean -- true if the user can modify widgets in this room + */ + static canUserModifyWidgets(roomId) { + if (!roomId) { + throw new Error('No room ID specified'); + } + + const client = MatrixClientPeg.get(); + if (!client) { + throw new Error('User must be be logged in'); + } + + const room = client.getRoom(roomId); + if (!room) { + throw new Error(`Room ID ${roomId} is not recognised`); + } + + const me = client.credentials.userId; + if (!me) { + throw new Error('Failed to get user ID'); + } + + const member = room.getMember(me); + if (!member || member.membership !== "join") { + throw new Error(`User ${me} is not in room ${roomId}`); + } + + return room.currentState.maySendStateEvent('set_widget', me); + } +}