Refactor SecurityRoomSettingsTab and remove unused state (#8306)

This commit is contained in:
Michael Telatynski 2022-04-14 10:38:51 +01:00 committed by GitHub
parent c35fc169f5
commit bcb45f1447
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,5 @@
/* /*
Copyright 2019-2021 The Matrix.org Foundation C.I.C. Copyright 2019-2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -15,14 +15,13 @@ limitations under the License.
*/ */
import React from 'react'; import React from 'react';
import { GuestAccess, HistoryVisibility, JoinRule, RestrictedAllowType } from "matrix-js-sdk/src/@types/partials"; import { GuestAccess, HistoryVisibility, JoinRule } from "matrix-js-sdk/src/@types/partials";
import { MatrixEvent } from "matrix-js-sdk/src/models/event"; import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state"; import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
import { EventType } from 'matrix-js-sdk/src/@types/event'; import { EventType } from 'matrix-js-sdk/src/@types/event';
import { logger } from "matrix-js-sdk/src/logger"; import { logger } from "matrix-js-sdk/src/logger";
import { _t } from "../../../../../languageHandler"; import { _t } from "../../../../../languageHandler";
import { MatrixClientPeg } from "../../../../../MatrixClientPeg";
import LabelledToggleSwitch from "../../../elements/LabelledToggleSwitch"; import LabelledToggleSwitch from "../../../elements/LabelledToggleSwitch";
import Modal from "../../../../../Modal"; import Modal from "../../../../../Modal";
import QuestionDialog from "../../../dialogs/QuestionDialog"; import QuestionDialog from "../../../dialogs/QuestionDialog";
@ -39,6 +38,7 @@ import ErrorDialog from "../../../dialogs/ErrorDialog";
import SettingsFieldset from '../../SettingsFieldset'; import SettingsFieldset from '../../SettingsFieldset';
import ExternalLink from '../../../elements/ExternalLink'; import ExternalLink from '../../../elements/ExternalLink';
import PosthogTrackers from "../../../../../PosthogTrackers"; import PosthogTrackers from "../../../../../PosthogTrackers";
import MatrixClientContext from "../../../../../contexts/MatrixClientContext";
interface IProps { interface IProps {
roomId: string; roomId: string;
@ -46,7 +46,6 @@ interface IProps {
} }
interface IState { interface IState {
restrictedAllowRoomIds?: string[];
guestAccess: GuestAccess; guestAccess: GuestAccess;
history: HistoryVisibility; history: HistoryVisibility;
hasAliases: boolean; hasAliases: boolean;
@ -55,52 +54,33 @@ interface IState {
} }
export default class SecurityRoomSettingsTab extends React.Component<IProps, IState> { export default class SecurityRoomSettingsTab extends React.Component<IProps, IState> {
constructor(props) { static contextType = MatrixClientContext;
super(props); public context!: React.ContextType<typeof MatrixClientContext>;
constructor(props, context) {
super(props, context);
const state = context.getRoom(this.props.roomId).currentState;
this.state = { this.state = {
guestAccess: GuestAccess.Forbidden, guestAccess: this.pullContentPropertyFromEvent<GuestAccess>(
history: HistoryVisibility.Shared, state.getStateEvents(EventType.RoomGuestAccess, ""),
hasAliases: false, 'guest_access',
encrypted: false, GuestAccess.Forbidden,
),
history: this.pullContentPropertyFromEvent<HistoryVisibility>(
state.getStateEvents(EventType.RoomHistoryVisibility, ""),
'history_visibility',
HistoryVisibility.Shared,
),
hasAliases: false, // async loaded in componentDidMount
encrypted: context.isRoomEncrypted(this.props.roomId),
showAdvancedSection: false, showAdvancedSection: false,
}; };
} }
// TODO: [REACT-WARNING] Move this to constructor componentDidMount() {
UNSAFE_componentWillMount() { // eslint-disable-line this.context.on(RoomStateEvent.Events, this.onStateEvent);
const cli = MatrixClientPeg.get();
cli.on(RoomStateEvent.Events, this.onStateEvent);
const room = cli.getRoom(this.props.roomId);
const state = room.currentState;
const joinRuleEvent = state.getStateEvents(EventType.RoomJoinRules, "");
const joinRule: JoinRule = this.pullContentPropertyFromEvent<JoinRule>(
joinRuleEvent,
'join_rule',
JoinRule.Invite,
);
const restrictedAllowRoomIds = joinRule === JoinRule.Restricted
? joinRuleEvent?.getContent().allow
?.filter(a => a.type === RestrictedAllowType.RoomMembership)
?.map(a => a.room_id)
: undefined;
const guestAccess: GuestAccess = this.pullContentPropertyFromEvent<GuestAccess>(
state.getStateEvents(EventType.RoomGuestAccess, ""),
'guest_access',
GuestAccess.Forbidden,
);
const history: HistoryVisibility = this.pullContentPropertyFromEvent<HistoryVisibility>(
state.getStateEvents(EventType.RoomHistoryVisibility, ""),
'history_visibility',
HistoryVisibility.Shared,
);
const encrypted = MatrixClientPeg.get().isRoomEncrypted(this.props.roomId);
this.setState({ restrictedAllowRoomIds, guestAccess, history, encrypted });
this.hasAliases().then(hasAliases => this.setState({ hasAliases })); this.hasAliases().then(hasAliases => this.setState({ hasAliases }));
} }
@ -109,7 +89,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
} }
componentWillUnmount() { componentWillUnmount() {
MatrixClientPeg.get().removeListener(RoomStateEvent.Events, this.onStateEvent); this.context.removeListener(RoomStateEvent.Events, this.onStateEvent);
} }
private onStateEvent = (e: MatrixEvent) => { private onStateEvent = (e: MatrixEvent) => {
@ -123,7 +103,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
}; };
private onEncryptionChange = async () => { private onEncryptionChange = async () => {
if (MatrixClientPeg.get().getRoom(this.props.roomId)?.getJoinRule() === JoinRule.Public) { if (this.context.getRoom(this.props.roomId)?.getJoinRule() === JoinRule.Public) {
const dialog = Modal.createTrackedDialog('Confirm Public Encrypted Room', '', QuestionDialog, { const dialog = Modal.createTrackedDialog('Confirm Public Encrypted Room', '', QuestionDialog, {
title: _t('Are you sure you want to add encryption to this public room?'), title: _t('Are you sure you want to add encryption to this public room?'),
description: <div> description: <div>
@ -178,7 +158,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
const beforeEncrypted = this.state.encrypted; const beforeEncrypted = this.state.encrypted;
this.setState({ encrypted: true }); this.setState({ encrypted: true });
MatrixClientPeg.get().sendStateEvent( this.context.sendStateEvent(
this.props.roomId, EventType.RoomEncryption, this.props.roomId, EventType.RoomEncryption,
{ algorithm: "m.megolm.v1.aes-sha2" }, { algorithm: "m.megolm.v1.aes-sha2" },
).catch((e) => { ).catch((e) => {
@ -196,8 +176,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
this.setState({ guestAccess }); this.setState({ guestAccess });
const client = MatrixClientPeg.get(); this.context.sendStateEvent(this.props.roomId, EventType.RoomGuestAccess, {
client.sendStateEvent(this.props.roomId, EventType.RoomGuestAccess, {
guest_access: guestAccess, guest_access: guestAccess,
}, "").catch((e) => { }, "").catch((e) => {
logger.error(e); logger.error(e);
@ -227,7 +206,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
if (beforeHistory === history) return; if (beforeHistory === history) return;
this.setState({ history: history }); this.setState({ history: history });
MatrixClientPeg.get().sendStateEvent(this.props.roomId, EventType.RoomHistoryVisibility, { this.context.sendStateEvent(this.props.roomId, EventType.RoomHistoryVisibility, {
history_visibility: history, history_visibility: history,
}, "").catch((e) => { }, "").catch((e) => {
logger.error(e); logger.error(e);
@ -236,11 +215,11 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
}; };
private updateBlacklistDevicesFlag = (checked: boolean) => { private updateBlacklistDevicesFlag = (checked: boolean) => {
MatrixClientPeg.get().getRoom(this.props.roomId).setBlacklistUnverifiedDevices(checked); this.context.getRoom(this.props.roomId).setBlacklistUnverifiedDevices(checked);
}; };
private async hasAliases(): Promise<boolean> { private async hasAliases(): Promise<boolean> {
const cli = MatrixClientPeg.get(); const cli = this.context;
if (await cli.doesServerSupportUnstableFeature("org.matrix.msc2432")) { if (await cli.doesServerSupportUnstableFeature("org.matrix.msc2432")) {
const response = await cli.unstableGetLocalAliases(this.props.roomId); const response = await cli.unstableGetLocalAliases(this.props.roomId);
const localAliases = response.aliases; const localAliases = response.aliases;
@ -254,7 +233,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
} }
private renderJoinRule() { private renderJoinRule() {
const client = MatrixClientPeg.get(); const client = this.context;
const room = client.getRoom(this.props.roomId); const room = client.getRoom(this.props.roomId);
let aliasWarning = null; let aliasWarning = null;
@ -333,7 +312,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
return null; return null;
} }
const client = MatrixClientPeg.get(); const client = this.context;
const history = this.state.history; const history = this.state.history;
const state = client.getRoom(this.props.roomId).currentState; const state = client.getRoom(this.props.roomId).currentState;
const canChangeHistory = state.mayClientSendStateEvent(EventType.RoomHistoryVisibility, client); const canChangeHistory = state.mayClientSendStateEvent(EventType.RoomHistoryVisibility, client);
@ -380,7 +359,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
}; };
private renderAdvanced() { private renderAdvanced() {
const client = MatrixClientPeg.get(); const client = this.context;
const guestAccess = this.state.guestAccess; const guestAccess = this.state.guestAccess;
const state = client.getRoom(this.props.roomId).currentState; const state = client.getRoom(this.props.roomId).currentState;
const canSetGuestAccess = state.mayClientSendStateEvent(EventType.RoomGuestAccess, client); const canSetGuestAccess = state.mayClientSendStateEvent(EventType.RoomGuestAccess, client);
@ -400,7 +379,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
} }
render() { render() {
const client = MatrixClientPeg.get(); const client = this.context;
const room = client.getRoom(this.props.roomId); const room = client.getRoom(this.props.roomId);
const isEncrypted = this.state.encrypted; const isEncrypted = this.state.encrypted;
const hasEncryptionPermission = room.currentState.mayClientSendStateEvent(EventType.RoomEncryption, client); const hasEncryptionPermission = room.currentState.mayClientSendStateEvent(EventType.RoomEncryption, client);