Tidy up power level logic. Add 'Moderator' label to members.

This commit is contained in:
Kegan Dougal 2015-07-21 11:43:18 +01:00
parent 2e2cecdd4f
commit 13f04f77dc
2 changed files with 43 additions and 42 deletions

View File

@ -84,7 +84,10 @@ module.exports = React.createClass({
{muteLabel} {muteLabel}
</div>; </div>;
} }
var opLabel;
if (this.state.isTargetOp) {
opLabel = <div className="mx_MemberInfo_field">Moderator</div>
}
return ( return (
<div className="mx_MemberInfo"> <div className="mx_MemberInfo">
<img className="mx_MemberInfo_chevron" src="img/chevron-right.png" width="9" height="16" /> <img className="mx_MemberInfo_chevron" src="img/chevron-right.png" width="9" height="16" />
@ -95,6 +98,7 @@ module.exports = React.createClass({
width="128" height="128" alt=""/> width="128" height="128" alt=""/>
</div> </div>
<div className="mx_MemberInfo_field">{this.props.member.userId}</div> <div className="mx_MemberInfo_field">{this.props.member.userId}</div>
{opLabel}
<div className="mx_MemberInfo_field">Presence: {this.state.presence}</div> <div className="mx_MemberInfo_field">Presence: {this.state.presence}</div>
<div className="mx_MemberInfo_field">Last active: {activeAgo}</div> <div className="mx_MemberInfo_field">Last active: {activeAgo}</div>
<div className="mx_MemberInfo_button" onClick={this.onChatClick}>Start chat</div> <div className="mx_MemberInfo_button" onClick={this.onChatClick}>Start chat</div>

View File

@ -23,7 +23,8 @@ limitations under the License.
* ban: boolean, * ban: boolean,
* mute: boolean * mute: boolean
* }, * },
* 'muted': boolean * 'muted': boolean,
* 'isTargetOp': boolean
*/ */
'use strict'; 'use strict';
@ -62,10 +63,7 @@ module.exports = {
if ([myUserId, self.props.member.userId].indexOf(member.userId) === -1) { if ([myUserId, self.props.member.userId].indexOf(member.userId) === -1) {
return; return;
} }
self.setState({ self.setState(self._calculateOpsPermissions());
can: self._calculateOpsPermissions(),
muted: self._isMuted(self.props.member)
});
} }
MatrixClientPeg.get().on("RoomMember.powerLevel", updatePowerLevel); MatrixClientPeg.get().on("RoomMember.powerLevel", updatePowerLevel);
this.updatePowerLevelFn = updatePowerLevel; this.updatePowerLevelFn = updatePowerLevel;
@ -73,12 +71,10 @@ module.exports = {
// work out the current state // work out the current state
if (this.props.member) { if (this.props.member) {
var usr = MatrixClientPeg.get().getUser(this.props.member.userId) || {}; var usr = MatrixClientPeg.get().getUser(this.props.member.userId) || {};
this.setState({ var memberState = this._calculateOpsPermissions();
presence: usr.presence || "offline", memberState.presence = usr.presence || "offline";
active: usr.lastActiveAgo || -1, memberState.active = usr.lastActiveAgo || -1;
can: this._calculateOpsPermissions(), this.setState(memberState);
muted: this._isMuted(this.props.member)
});
} }
}, },
@ -220,53 +216,43 @@ module.exports = {
ban: false, ban: false,
mute: false mute: false
}, },
muted: false muted: false,
isTargetOp: false
} }
}, },
_isMuted: function(member) { _calculateOpsPermissions: function() {
var room = MatrixClientPeg.get().getRoom(member.roomId); var defaultPerms = {
can: {},
muted: false
};
var room = MatrixClientPeg.get().getRoom(this.props.member.roomId);
if (!room) { if (!room) {
return false; return defaultPerms;
} }
var powerLevels = room.currentState.getStateEvents( var powerLevels = room.currentState.getStateEvents(
"m.room.power_levels", "" "m.room.power_levels", ""
); );
if (!powerLevels) { if (!powerLevels) {
return false; return defaultPerms;
} }
powerLevels = powerLevels.getContent(); var me = room.getMember(MatrixClientPeg.get().credentials.userId);
var levelToSend = ( var them = this.props.member;
(powerLevels.events ? powerLevels.events["m.room.message"] : null) || return {
powerLevels.events_default can: this._calculateCanPermissions(
); me, them, powerLevels.getContent()
return member.powerLevel < levelToSend; ),
muted: this._isMuted(them, powerLevels.getContent()),
isTargetOp: them.powerLevel >= me.powerLevel && them.powerLevel > 0
};
}, },
_calculateOpsPermissions: function() { _calculateCanPermissions: function(me, them, powerLevels) {
var can = { var can = {
kick: false, kick: false,
ban: false, ban: false,
mute: false mute: false
}; };
var them = this.props.member;
var room = MatrixClientPeg.get().getRoom(this.props.member.roomId);
if (!room) {
console.error("No room found");
return can;
}
var myUserId = MatrixClientPeg.get().credentials.userId;
var me = room.getMember(myUserId);
var powerLevels = room.currentState.getStateEvents(
"m.room.power_levels", ""
);
if (powerLevels) {
powerLevels = powerLevels.getContent();
}
else {
console.log("No power level event found in %s", room.roomId);
return can; // no power level event, don't allow anything.
}
var canAffectUser = them.powerLevel < me.powerLevel; var canAffectUser = them.powerLevel < me.powerLevel;
if (!canAffectUser) { if (!canAffectUser) {
console.log("Cannot affect user: %s >= %s", them.powerLevel, me.powerLevel); console.log("Cannot affect user: %s >= %s", them.powerLevel, me.powerLevel);
@ -280,6 +266,17 @@ module.exports = {
can.ban = me.powerLevel >= powerLevels.ban; can.ban = me.powerLevel >= powerLevels.ban;
can.mute = me.powerLevel >= editPowerLevel; can.mute = me.powerLevel >= editPowerLevel;
return can; return can;
},
_isMuted: function(member, powerLevelContent) {
if (!powerLevelContent || !member) {
return false;
}
var levelToSend = (
(powerLevelContent.events ? powerLevelContent.events["m.room.message"] : null) ||
powerLevelContent.events_default
);
return member.powerLevel < levelToSend;
} }
}; };