RoomSettings: refactor permissions calculations

The logic for calculating who had permission for what was impenetrable (and
wrong, in parts), so rewrite it to be a bit clearer.
This commit is contained in:
Richard van der Hoff 2016-06-23 10:36:16 +01:00
parent 5195140ff8
commit a04f03669c

View File

@ -22,6 +22,13 @@ var Modal = require('../../../Modal');
var ObjectUtils = require("../../../ObjectUtils"); var ObjectUtils = require("../../../ObjectUtils");
var dis = require("../../../dispatcher"); var dis = require("../../../dispatcher");
// parse a string as an integer; if the input is undefined, or cannot be parsed
// as an integer, return a default.
function parseIntWithDefault(val, def) {
var res = parseInt(val);
return isNaN(res) ? def : res;
}
module.exports = React.createClass({ module.exports = React.createClass({
displayName: 'RoomSettings', displayName: 'RoomSettings',
@ -251,7 +258,7 @@ module.exports = React.createClass({
power_levels_changed: true power_levels_changed: true
}); });
}, },
_yankValueFromEvent: function(stateEventType, keyName, defaultValue) { _yankValueFromEvent: function(stateEventType, keyName, defaultValue) {
// E.g.("m.room.name","name") would yank the "name" content key from "m.room.name" // E.g.("m.room.name","name") would yank the "name" content key from "m.room.name"
var event = this.props.room.currentState.getStateEvents(stateEventType, ''); var event = this.props.room.currentState.getStateEvents(stateEventType, '');
@ -286,7 +293,7 @@ module.exports = React.createClass({
}, },
}); });
}, },
_onRoomAccessRadioToggle: function(ev) { _onRoomAccessRadioToggle: function(ev) {
// join_rule // join_rule
@ -368,58 +375,29 @@ module.exports = React.createClass({
var EditableText = sdk.getComponent('elements.EditableText'); var EditableText = sdk.getComponent('elements.EditableText');
var PowerSelector = sdk.getComponent('elements.PowerSelector'); var PowerSelector = sdk.getComponent('elements.PowerSelector');
var power_levels = this.props.room.currentState.getStateEvents('m.room.power_levels', '');
var events_levels = (power_levels ? power_levels.getContent().events : {}) || {};
var cli = MatrixClientPeg.get(); var cli = MatrixClientPeg.get();
var roomState = this.props.room.currentState; var roomState = this.props.room.currentState;
var user_id = cli.credentials.userId; var user_id = cli.credentials.userId;
if (power_levels) { var power_level_event = roomState.getStateEvents('m.room.power_levels', '');
power_levels = power_levels.getContent(); var power_levels = power_level_event ? power_level_event.getContent() : {};
var events_levels = power_levels.events || {};
var user_levels = power_levels.users || {};
var ban_level = parseInt(power_levels.ban); var ban_level = parseIntWithDefault(power_levels.ban, 50);
var kick_level = parseInt(power_levels.kick); var kick_level = parseIntWithDefault(power_levels.kick, 50);
var redact_level = parseInt(power_levels.redact); var redact_level = parseIntWithDefault(power_levels.redact, 50);
var invite_level = parseInt(power_levels.invite || 0); var invite_level = parseIntWithDefault(power_levels.invite, 50);
var send_level = parseInt(power_levels.events_default || 0); var send_level = parseIntWithDefault(power_levels.events_default, 0);
var state_level = parseInt(power_levels.state_default || 50); var state_level = power_level_event ? parseIntWithDefault(power_levels.state_default, 50) : 0;
var default_user_level = parseInt(power_levels.users_default || 0); var default_user_level = parseIntWithDefault(power_levels.users_default, 0);
if (power_levels.ban == undefined) ban_level = 50; var current_user_level = user_levels[user_id];
if (power_levels.kick == undefined) kick_level = 50; if (current_user_level === undefined) {
if (power_levels.redact == undefined) redact_level = 50; current_user_level = default_user_level;
var user_levels = power_levels.users || {};
var current_user_level = user_levels[user_id];
if (current_user_level == undefined) current_user_level = default_user_level;
var power_level_level = events_levels["m.room.power_levels"];
if (power_level_level == undefined) {
power_level_level = state_level;
}
var can_change_levels = current_user_level >= power_level_level;
} else {
var ban_level = 50;
var kick_level = 50;
var redact_level = 50;
var invite_level = 0;
var send_level = 0;
var state_level = 0;
var default_user_level = 0;
var user_levels = [];
var events_levels = [];
var current_user_level = 0;
var power_level_level = 0;
var can_change_levels = false;
} }
var state_default = (parseInt(power_levels ? power_levels.state_default : 0) || 0); var can_change_levels = roomState.mayClientSendStateEvent("m.room.power_levels", cli);
var canSetTag = !cli.isGuest(); var canSetTag = !cli.isGuest();
@ -488,7 +466,7 @@ module.exports = React.createClass({
var tagsSection = null; var tagsSection = null;
if (canSetTag || self.state.tags) { if (canSetTag || self.state.tags) {
var tagsSection = var tagsSection =
<div className="mx_RoomSettings_tags"> <div className="mx_RoomSettings_tags">
Tagged as: { canSetTag ? Tagged as: { canSetTag ?
(tags.map(function(tag, i) { (tags.map(function(tag, i) {