2017-07-28 06:37:33 +08:00
|
|
|
/*
|
|
|
|
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
|
2018-04-02 17:02:41 +08:00
|
|
|
* (Does not apply to non-room-based / user widgets)
|
2017-07-28 06:37:33 +08: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 23:19:20 +08:00
|
|
|
* @throws Error -- specifies the error reason
|
2017-07-28 06:37:33 +08:00
|
|
|
*/
|
|
|
|
static canUserModifyWidgets(roomId) {
|
|
|
|
if (!roomId) {
|
2017-08-01 18:39:17 +08:00
|
|
|
console.warn('No room ID specified');
|
|
|
|
return false;
|
2017-07-28 06:37:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (!client) {
|
2017-08-01 18:39:17 +08:00
|
|
|
console.warn('User must be be logged in');
|
|
|
|
return false;
|
2017-07-28 06:37:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
if (!room) {
|
2017-08-01 18:39:17 +08:00
|
|
|
console.warn(`Room ID ${roomId} is not recognised`);
|
|
|
|
return false;
|
2017-07-28 06:37:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const me = client.credentials.userId;
|
|
|
|
if (!me) {
|
2017-08-01 18:39:17 +08:00
|
|
|
console.warn('Failed to get user ID');
|
|
|
|
return false;
|
2017-07-28 06:37:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const member = room.getMember(me);
|
|
|
|
if (!member || member.membership !== "join") {
|
2017-08-01 18:39:17 +08:00
|
|
|
console.warn(`User ${me} is not in room ${roomId}`);
|
|
|
|
return false;
|
2017-07-28 06:37:33 +08:00
|
|
|
}
|
|
|
|
|
2017-08-01 18:41:41 +08:00
|
|
|
return room.currentState.maySendStateEvent('im.vector.modular.widgets', me);
|
2017-07-28 06:37:33 +08:00
|
|
|
}
|
|
|
|
}
|