From f46df2ddd9c800ac806be9a31291a9f97d706e93 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 14 Feb 2019 11:09:37 -0700 Subject: [PATCH] Add a bit of safety around reading events for room settings Fixes https://github.com/vector-im/riot-web/issues/8530 and maybe https://github.com/vector-im/riot-web/issues/8641 --- .../settings/tabs/RolesRoomSettingsTab.js | 6 +++-- .../settings/tabs/SecurityRoomSettingsTab.js | 24 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/components/views/settings/tabs/RolesRoomSettingsTab.js b/src/components/views/settings/tabs/RolesRoomSettingsTab.js index 2195017782..d223e8f2e9 100644 --- a/src/components/views/settings/tabs/RolesRoomSettingsTab.js +++ b/src/components/views/settings/tabs/RolesRoomSettingsTab.js @@ -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); @@ -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 = { diff --git a/src/components/views/settings/tabs/SecurityRoomSettingsTab.js b/src/components/views/settings/tabs/SecurityRoomSettingsTab.js index 593e8151d2..698f67dd18 100644 --- a/src/components/views/settings/tabs/SecurityRoomSettingsTab.js +++ b/src/components/views/settings/tabs/SecurityRoomSettingsTab.js @@ -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); }