Merge pull request #319 from matrix-org/rav/room_power_levels

RoomSettings: refactor permissions calculations
This commit is contained in:
David Baker 2016-06-23 10:43:58 +01:00 committed by GitHub
commit e741226c02

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',
@ -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();