Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2648 from matrix-org/travis/fix-room-settings
Browse files Browse the repository at this point in the history
Add a bit of safety around reading events for room settings
  • Loading branch information
turt2live authored Feb 19, 2019
2 parents 07f49e8 + f46df2d commit 424de00
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/components/views/settings/tabs/RolesRoomSettingsTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ export default class RolesRoomSettingsTab extends React.Component {
_onPowerLevelsChanged = (value, powerLevelKey) => {
const client = MatrixClientPeg.get();
const room = client.getRoom(this.props.roomId);
let plContent = room.currentState.getStateEvents('m.room.power_levels', '').getContent() || {};
const plEvent = room.currentState.getStateEvents('m.room.power_levels', '');
let plContent = plEvent ? (plEvent.getContent() || {}) : {};

// Clone the power levels just in case
plContent = Object.assign({}, plContent);
Expand Down Expand Up @@ -151,7 +152,8 @@ export default class RolesRoomSettingsTab extends React.Component {

const client = MatrixClientPeg.get();
const room = client.getRoom(this.props.roomId);
const plContent = room.currentState.getStateEvents('m.room.power_levels', '').getContent() || {};
const plEvent = room.currentState.getStateEvents('m.room.power_levels', '');
const plContent = plEvent ? (plEvent.getContent() || {}) : {};
const canChangeLevels = room.currentState.mayClientSendStateEvent('m.room.power_levels', client);

const powerLevelDescriptors = {
Expand Down
24 changes: 21 additions & 3 deletions src/components/views/settings/tabs/SecurityRoomSettingsTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,31 @@ export default class SecurityRoomSettingsTab extends React.Component {

const room = MatrixClientPeg.get().getRoom(this.props.roomId);
const state = room.currentState;
const joinRule = state.getStateEvents("m.room.join_rules", "").getContent()['join_rule'];
const guestAccess = state.getStateEvents("m.room.guest_access", "").getContent()['guest_access'];
const history = state.getStateEvents("m.room.history_visibility", "").getContent()['history_visibility'];

const joinRule = this._pullContentPropertyFromEvent(
state.getStateEvents("m.room.join_rules", ""),
'join_rule',
'invite',
);
const guestAccess = this._pullContentPropertyFromEvent(
state.getStateEvents("m.room.guest_access", ""),
'guest_access',
'forbidden',
);
const history = this._pullContentPropertyFromEvent(
state.getStateEvents("m.room.history_visibility", ""),
'history_visibility',
'shared',
);
const encrypted = MatrixClientPeg.get().isRoomEncrypted(this.props.roomId);
this.setState({joinRule, guestAccess, history, encrypted});
}

_pullContentPropertyFromEvent(event, key, defaultValue) {
if (!event || !event.getContent()) return defaultValue;
return event.getContent()[key] || defaultValue;
}

componentWillUnmount(): void {
MatrixClientPeg.get().removeListener("RoomState.events", this._onStateEvent);
}
Expand Down

0 comments on commit 424de00

Please sign in to comment.