From 2454a71b38857861e43b88aa0eaab40871a41397 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 21 Jul 2015 13:24:59 +0100 Subject: [PATCH] Implement create/remove mod button. --- skins/base/views/molecules/MemberInfo.js | 15 +++++-- src/controllers/molecules/MemberInfo.js | 53 ++++++++++++++++++++---- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/skins/base/views/molecules/MemberInfo.js b/skins/base/views/molecules/MemberInfo.js index 45db0c30..8e1e383f 100644 --- a/skins/base/views/molecules/MemberInfo.js +++ b/skins/base/views/molecules/MemberInfo.js @@ -67,7 +67,7 @@ module.exports = React.createClass({ if (this.state.active >= 0) { activeAgo = this.getDuration(this.state.active); } - var kickButton, banButton, muteButton; + var kickButton, banButton, muteButton, giveModButton; if (this.state.can.kick) { kickButton =
Kick @@ -84,9 +84,17 @@ module.exports = React.createClass({ {muteLabel}
; } + if (this.state.can.modifyLevel) { + var giveOpLabel = this.state.isTargetMod ? "Revoke Mod" : "Make Mod"; + giveModButton =
+ {giveOpLabel} +
+ } + var opLabel; - if (this.state.isTargetOp) { - opLabel =
Moderator
+ if (this.state.isTargetMod) { + var level = this.props.member.powerLevelNorm + "%"; + opLabel =
Moderator ({level})
} return (
@@ -105,6 +113,7 @@ module.exports = React.createClass({ {muteButton} {kickButton} {banButton} + {giveModButton}
); } diff --git a/src/controllers/molecules/MemberInfo.js b/src/controllers/molecules/MemberInfo.js index d335eec5..4f222c5d 100644 --- a/src/controllers/molecules/MemberInfo.js +++ b/src/controllers/molecules/MemberInfo.js @@ -21,10 +21,11 @@ limitations under the License. * 'can': { * kick: boolean, * ban: boolean, - * mute: boolean + * mute: boolean, + * modifyLevel: boolean * }, * 'muted': boolean, - * 'isTargetOp': boolean + * 'isTargetMod': boolean */ 'use strict'; @@ -158,6 +159,40 @@ module.exports = { }); }, + onModToggle: function() { + var roomId = this.props.member.roomId; + var target = this.props.member.userId; + var room = MatrixClientPeg.get().getRoom(roomId); + if (!room) { + return; + } + var powerLevelEvent = room.currentState.getStateEvents( + "m.room.power_levels", "" + ); + if (!powerLevelEvent) { + return; + } + var me = room.getMember(MatrixClientPeg.get().credentials.userId); + if (!me) { + return; + } + var defaultLevel = powerLevelEvent.getContent().users_default; + var modLevel = me.powerLevel - 1; + // toggle the level + var newLevel = this.state.isTargetMod ? defaultLevel : modLevel; + MatrixClientPeg.get().setPowerLevel(roomId, target, newLevel, powerLevelEvent).done( + function() { + // NO-OP; rely on the m.room.member event coming down else we could + // get out of sync if we force setState here! + console.log("Mod toggle success"); + }, function(err) { + Modal.createDialog(ErrorDialog, { + title: "Mod error", + description: err.message + }); + }); + }, + onChatClick: function() { // check if there are any existing rooms with just us and them (1:1) // If so, just view that room. If not, create a private room with them. @@ -214,17 +249,19 @@ module.exports = { can: { kick: false, ban: false, - mute: false + mute: false, + modifyLevel: false }, muted: false, - isTargetOp: false + isTargetMod: false } }, _calculateOpsPermissions: function() { var defaultPerms = { can: {}, - muted: false + muted: false, + modifyLevel: false }; var room = MatrixClientPeg.get().getRoom(this.props.member.roomId); if (!room) { @@ -243,7 +280,7 @@ module.exports = { me, them, powerLevels.getContent() ), muted: this._isMuted(them, powerLevels.getContent()), - isTargetOp: them.powerLevel >= me.powerLevel && them.powerLevel > 0 + isTargetMod: them.powerLevel > powerLevels.getContent().users_default }; }, @@ -251,7 +288,8 @@ module.exports = { var can = { kick: false, ban: false, - mute: false + mute: false, + modifyLevel: false }; var canAffectUser = them.powerLevel < me.powerLevel; if (!canAffectUser) { @@ -265,6 +303,7 @@ module.exports = { can.kick = me.powerLevel >= powerLevels.kick; can.ban = me.powerLevel >= powerLevels.ban; can.mute = me.powerLevel >= editPowerLevel; + can.modifyLevel = me.powerLevel > them.powerLevel; return can; },