Simplify canUserModifyWidgets API

This commit is contained in:
Richard Lewis 2017-08-01 11:39:17 +01:00
parent a310ff3bab
commit 15f39040a0
2 changed files with 12 additions and 12 deletions

View file

@ -25,27 +25,32 @@ export default class WidgetUtils {
*/ */
static canUserModifyWidgets(roomId) { static canUserModifyWidgets(roomId) {
if (!roomId) { if (!roomId) {
throw new Error('No room ID specified'); console.warn('No room ID specified');
return false;
} }
const client = MatrixClientPeg.get(); const client = MatrixClientPeg.get();
if (!client) { if (!client) {
throw new Error('User must be be logged in'); console.warn('User must be be logged in');
return false;
} }
const room = client.getRoom(roomId); const room = client.getRoom(roomId);
if (!room) { if (!room) {
throw new Error(`Room ID ${roomId} is not recognised`); console.warn(`Room ID ${roomId} is not recognised`);
return false;
} }
const me = client.credentials.userId; const me = client.credentials.userId;
if (!me) { if (!me) {
throw new Error('Failed to get user ID'); console.warn('Failed to get user ID');
return false;
} }
const member = room.getMember(me); const member = room.getMember(me);
if (!member || member.membership !== "join") { if (!member || member.membership !== "join") {
throw new Error(`User ${me} is not in room ${roomId}`); console.warn(`User ${me} is not in room ${roomId}`);
return false;
} }
return room.currentState.maySendStateEvent('set_widget', me); return room.currentState.maySendStateEvent('set_widget', me);

View file

@ -102,13 +102,8 @@ export default React.createClass({
}, },
_canUserModify: function() { _canUserModify: function() {
try {
return WidgetUtils.canUserModifyWidgets(this.props.room.roomId); return WidgetUtils.canUserModifyWidgets(this.props.room.roomId);
} catch(err) {
console.error(err);
return false;
} }
},
_onEditClick: function(e) { _onEditClick: function(e) {
console.log("Edit widget ID ", this.props.id); console.log("Edit widget ID ", this.props.id);