Use MatrixClientPeg::safeGet in src/components/views/* (#10987)

This commit is contained in:
Michael Telatynski 2023-06-15 08:46:19 +01:00 committed by GitHub
parent 4243847f4f
commit 280f6a9d93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
81 changed files with 265 additions and 250 deletions

View file

@ -22,7 +22,7 @@ import EditableTextContainer from "../elements/EditableTextContainer";
export default class ChangeDisplayName extends React.Component {
private getDisplayName = async (): Promise<string> => {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
try {
const res = await cli.getProfileInfo(cli.getUserId()!);
return res.displayname ?? "";
@ -32,7 +32,7 @@ export default class ChangeDisplayName extends React.Component {
};
private changeDisplayName = (newDisplayname: string): Promise<{}> => {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
return cli.setDisplayName(newDisplayname).catch(function () {
throw new Error("Failed to set display name");
});

View file

@ -93,7 +93,7 @@ export default class ChangePassword extends React.Component<IProps, IState> {
}
private async onChangePassword(oldPassword: string, newPassword: string): Promise<void> {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
// if the server supports it then don't sign user out of all devices
const serverSupportsControlOfDevicesLogout = await cli.doesServerSupportLogoutDevices();
@ -235,7 +235,7 @@ export default class ChangePassword extends React.Component<IProps, IState> {
typeof ExportE2eKeysDialog
>,
{
matrixClient: MatrixClientPeg.get(),
matrixClient: MatrixClientPeg.safeGet(),
},
);
};

View file

@ -38,7 +38,7 @@ export default class CryptographyPanel extends React.Component<IProps, IState> {
}
public render(): React.ReactNode {
const client = MatrixClientPeg.get();
const client = MatrixClientPeg.safeGet();
const deviceId = client.deviceId;
let identityKey = client.getDeviceEd25519Key();
if (!identityKey) {
@ -103,7 +103,7 @@ export default class CryptographyPanel extends React.Component<IProps, IState> {
import("../../../async-components/views/dialogs/security/ExportE2eKeysDialog") as unknown as Promise<
typeof ExportE2eKeysDialog
>,
{ matrixClient: MatrixClientPeg.get() },
{ matrixClient: MatrixClientPeg.safeGet() },
);
};
@ -112,11 +112,11 @@ export default class CryptographyPanel extends React.Component<IProps, IState> {
import("../../../async-components/views/dialogs/security/ImportE2eKeysDialog") as unknown as Promise<
typeof ImportE2eKeysDialog
>,
{ matrixClient: MatrixClientPeg.get() },
{ matrixClient: MatrixClientPeg.safeGet() },
);
};
private updateBlacklistDevicesFlag = (checked: boolean): void => {
MatrixClientPeg.get().setGlobalBlacklistUnverifiedDevices(checked);
MatrixClientPeg.safeGet().setGlobalBlacklistUnverifiedDevices(checked);
};
}

View file

@ -62,8 +62,8 @@ export default class FontScalingPanel extends React.Component<IProps, IState> {
public async componentDidMount(): Promise<void> {
// Fetch the current user profile for the message preview
const client = MatrixClientPeg.get();
const userId = client.getUserId()!;
const client = MatrixClientPeg.safeGet();
const userId = client.getSafeUserId();
const profileInfo = await client.getProfileInfo(userId);
if (this.unmounted) return;

View file

@ -270,7 +270,7 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
}
private async refreshFromAccountData(): Promise<void> {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const settingsEvent = cli.getAccountData(getLocalNotificationAccountDataEventType(cli.deviceId));
if (settingsEvent) {
const notificationsEnabled = !(settingsEvent.getContent() as LocalNotificationSettings).is_silenced;
@ -279,14 +279,14 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
}
private persistLocalNotificationSettings(enabled: boolean): Promise<{}> {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
return cli.setAccountData(getLocalNotificationAccountDataEventType(cli.deviceId), {
is_silenced: !enabled,
});
}
private async refreshRules(): Promise<Partial<IState>> {
const ruleSets = await MatrixClientPeg.get().getPushRules()!;
const ruleSets = await MatrixClientPeg.safeGet().getPushRules()!;
const categories: Record<string, RuleClass> = {
[RuleId.Master]: RuleClass.Master,
@ -384,11 +384,11 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
}
private refreshPushers(): Promise<Partial<IState>> {
return MatrixClientPeg.get().getPushers();
return MatrixClientPeg.safeGet().getPushers();
}
private refreshThreepids(): Promise<Partial<IState>> {
return MatrixClientPeg.get().getThreePids();
return MatrixClientPeg.safeGet().getThreePids();
}
private showSaveError(): void {
@ -403,7 +403,7 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
const masterRule = this.state.masterPushRule!;
try {
await MatrixClientPeg.get().setPushRuleEnabled("global", masterRule.kind, masterRule.rule_id, !checked);
await MatrixClientPeg.safeGet().setPushRuleEnabled("global", masterRule.kind, masterRule.rule_id, !checked);
await this.refreshFromServer();
} catch (e) {
this.setState({ phase: Phase.Error });
@ -428,7 +428,7 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
try {
if (checked) {
await MatrixClientPeg.get().setPusher({
await MatrixClientPeg.safeGet().setPusher({
kind: "email",
app_id: "m.email",
pushkey: email,
@ -446,7 +446,7 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
} else {
const pusher = this.state.pushers?.find((p) => p.kind === "email" && p.pushkey === email);
if (pusher) {
await MatrixClientPeg.get().removePusher(pusher.pushkey, pusher.app_id);
await MatrixClientPeg.safeGet().removePusher(pusher.pushkey, pusher.app_id);
}
}
@ -477,7 +477,7 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
}));
try {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
if (rule.ruleId === KEYWORD_RULE_ID) {
// should not encounter this
if (!this.state.vectorKeywordRuleInfo) {
@ -536,7 +536,7 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
private onClearNotificationsClicked = async (): Promise<void> => {
try {
this.setState({ clearingNotifications: true });
const client = MatrixClientPeg.get();
const client = MatrixClientPeg.safeGet();
await clearAllNotifications(client);
} finally {
this.setState({ clearingNotifications: false });
@ -560,7 +560,7 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
for (const word of diff.removed) {
for (const rule of originalRules.filter((r) => r.pattern === word)) {
await MatrixClientPeg.get().deletePushRule("global", rule.kind, rule.rule_id);
await MatrixClientPeg.safeGet().deletePushRule("global", rule.kind, rule.rule_id);
}
}
@ -577,12 +577,12 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
}
const kind = PushRuleKind.ContentSpecific;
for (const word of diff.added) {
await MatrixClientPeg.get().addPushRule("global", kind, word, {
await MatrixClientPeg.safeGet().addPushRule("global", kind, word, {
actions: PushRuleVectorState.actionsFor(ruleVectorState),
pattern: word,
});
if (ruleVectorState === VectorState.Off) {
await MatrixClientPeg.get().setPushRuleEnabled("global", kind, word, false);
await MatrixClientPeg.safeGet().setPushRuleEnabled("global", kind, word, false);
}
}
@ -730,7 +730,7 @@ export default class Notifications extends React.PureComponent<IProps, IState> {
let clearNotifsButton: JSX.Element | undefined;
if (
category === RuleClass.VectorOther &&
MatrixClientPeg.get()
MatrixClientPeg.safeGet()
.getRooms()
.some((r) => r.getUnreadNotificationCount() > 0)
) {

View file

@ -47,7 +47,7 @@ export default class ProfileSettings extends React.Component<{}, IState> {
public constructor(props: {}) {
super(props);
this.userId = MatrixClientPeg.get().getSafeUserId();
this.userId = MatrixClientPeg.safeGet().getSafeUserId();
let avatarUrl = OwnProfileStore.instance.avatarMxc;
if (avatarUrl) avatarUrl = mediaFromMxc(avatarUrl).getSquareThumbnailHttp(96);
this.state = {
@ -96,11 +96,11 @@ export default class ProfileSettings extends React.Component<{}, IState> {
if (!this.state.enableProfileSave) return;
this.setState({ enableProfileSave: false });
const client = MatrixClientPeg.get();
const newState: Partial<IState> = {};
const displayName = this.state.displayName.trim();
try {
const client = MatrixClientPeg.safeGet();
if (this.state.originalDisplayName !== this.state.displayName) {
await client.setDisplayName(displayName);
newState.originalDisplayName = displayName;

View file

@ -69,16 +69,16 @@ export default class SecureBackupPanel extends React.PureComponent<{}, IState> {
public componentDidMount(): void {
this.checkKeyBackupStatus();
MatrixClientPeg.get().on(CryptoEvent.KeyBackupStatus, this.onKeyBackupStatus);
MatrixClientPeg.get().on(CryptoEvent.KeyBackupSessionsRemaining, this.onKeyBackupSessionsRemaining);
MatrixClientPeg.safeGet().on(CryptoEvent.KeyBackupStatus, this.onKeyBackupStatus);
MatrixClientPeg.safeGet().on(CryptoEvent.KeyBackupSessionsRemaining, this.onKeyBackupSessionsRemaining);
}
public componentWillUnmount(): void {
this.unmounted = true;
if (MatrixClientPeg.get()) {
MatrixClientPeg.get().removeListener(CryptoEvent.KeyBackupStatus, this.onKeyBackupStatus);
MatrixClientPeg.get().removeListener(
MatrixClientPeg.get()!.removeListener(CryptoEvent.KeyBackupStatus, this.onKeyBackupStatus);
MatrixClientPeg.get()!.removeListener(
CryptoEvent.KeyBackupSessionsRemaining,
this.onKeyBackupSessionsRemaining,
);
@ -100,7 +100,7 @@ export default class SecureBackupPanel extends React.PureComponent<{}, IState> {
private async checkKeyBackupStatus(): Promise<void> {
this.getUpdatedDiagnostics();
try {
const keyBackupResult = await MatrixClientPeg.get().checkKeyBackup();
const keyBackupResult = await MatrixClientPeg.safeGet().checkKeyBackup();
this.setState({
loading: false,
error: null,
@ -123,8 +123,8 @@ export default class SecureBackupPanel extends React.PureComponent<{}, IState> {
this.setState({ loading: true });
this.getUpdatedDiagnostics();
try {
const backupInfo = await MatrixClientPeg.get().getKeyBackupVersion();
const backupSigStatus = backupInfo ? await MatrixClientPeg.get().isKeyBackupTrusted(backupInfo) : null;
const backupInfo = await MatrixClientPeg.safeGet().getKeyBackupVersion();
const backupSigStatus = backupInfo ? await MatrixClientPeg.safeGet().isKeyBackupTrusted(backupInfo) : null;
if (this.unmounted) return;
this.setState({
loading: false,
@ -145,7 +145,7 @@ export default class SecureBackupPanel extends React.PureComponent<{}, IState> {
}
private async getUpdatedDiagnostics(): Promise<void> {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const crypto = cli.crypto;
if (!crypto) return;
@ -195,7 +195,7 @@ export default class SecureBackupPanel extends React.PureComponent<{}, IState> {
onFinished: (proceed) => {
if (!proceed) return;
this.setState({ loading: true });
MatrixClientPeg.get()
MatrixClientPeg.safeGet()
.deleteKeyBackupVersion(this.state.backupInfo!.version!)
.then(() => {
this.loadBackupStatus();
@ -246,7 +246,7 @@ export default class SecureBackupPanel extends React.PureComponent<{}, IState> {
} else if (backupInfo) {
let restoreButtonCaption = _t("Restore from Backup");
if (MatrixClientPeg.get().getKeyBackupEnabled()) {
if (MatrixClientPeg.safeGet().getKeyBackupEnabled()) {
statusDescription = <p> {_t("This session is backing up your keys.")}</p>;
} else {
statusDescription = (
@ -272,7 +272,7 @@ export default class SecureBackupPanel extends React.PureComponent<{}, IState> {
}
let uploadStatus: ReactNode;
if (!MatrixClientPeg.get().getKeyBackupEnabled()) {
if (!MatrixClientPeg.safeGet().getKeyBackupEnabled()) {
// No upload status to show when backup disabled.
uploadStatus = "";
} else if (sessionsRemaining > 0) {
@ -311,8 +311,9 @@ export default class SecureBackupPanel extends React.PureComponent<{}, IState> {
<span className="mx_SecureBackupPanel_deviceName">{deviceName}</span>
);
const fromThisDevice =
sig.device && sig.device.getFingerprint() === MatrixClientPeg.get().getDeviceEd25519Key();
const fromThisUser = sig.crossSigningId && sig.deviceId === MatrixClientPeg.get().getCrossSigningId();
sig.device && sig.device.getFingerprint() === MatrixClientPeg.safeGet().getDeviceEd25519Key();
const fromThisUser =
sig.crossSigningId && sig.deviceId === MatrixClientPeg.safeGet().getCrossSigningId();
let sigStatus;
if (sig.valid && fromThisUser) {
sigStatus = _t(
@ -419,7 +420,7 @@ export default class SecureBackupPanel extends React.PureComponent<{}, IState> {
</AccessibleButton>,
);
if (!isSecureBackupRequired(MatrixClientPeg.get())) {
if (!isSecureBackupRequired(MatrixClientPeg.safeGet())) {
actions.push(
<AccessibleButton key="delete" kind="danger" onClick={this.deleteBackup}>
{_t("Delete Backup")}

View file

@ -88,7 +88,7 @@ export default class SetIdServer extends React.Component<IProps, IState> {
super(props);
let defaultIdServer = "";
if (!MatrixClientPeg.get().getIdentityServerUrl() && getDefaultIdentityServerUrl()) {
if (!MatrixClientPeg.safeGet().getIdentityServerUrl() && getDefaultIdentityServerUrl()) {
// If no identity server is configured but there's one in the config, prepopulate
// the field to help the user.
defaultIdServer = abbreviateUrl(getDefaultIdentityServerUrl());
@ -96,7 +96,7 @@ export default class SetIdServer extends React.Component<IProps, IState> {
this.state = {
defaultIdServer,
currentClientIdServer: MatrixClientPeg.get().getIdentityServerUrl(),
currentClientIdServer: MatrixClientPeg.safeGet().getIdentityServerUrl(),
idServer: "",
busy: false,
disconnectBusy: false,
@ -118,7 +118,7 @@ export default class SetIdServer extends React.Component<IProps, IState> {
if (payload.action !== "id_server_changed") return;
this.setState({
currentClientIdServer: MatrixClientPeg.get().getIdentityServerUrl(),
currentClientIdServer: MatrixClientPeg.safeGet().getIdentityServerUrl(),
});
};
@ -149,7 +149,7 @@ export default class SetIdServer extends React.Component<IProps, IState> {
private saveIdServer = (fullUrl: string): void => {
// Account data change will update localstorage, client, etc through dispatcher
MatrixClientPeg.get().setAccountData("m.identity_server", {
MatrixClientPeg.safeGet().setAccountData("m.identity_server", {
base_url: fullUrl,
});
this.setState({
@ -181,7 +181,7 @@ export default class SetIdServer extends React.Component<IProps, IState> {
let save = true;
// Double check that the identity server even has terms of service.
const hasTerms = await doesIdentityServerHaveTerms(MatrixClientPeg.get(), fullUrl);
const hasTerms = await doesIdentityServerHaveTerms(MatrixClientPeg.safeGet(), fullUrl);
if (!hasTerms) {
const [confirmed] = await this.showNoTermsWarning(fullUrl);
save = !!confirmed;
@ -217,7 +217,7 @@ export default class SetIdServer extends React.Component<IProps, IState> {
busy: false,
checking: false,
error: errStr ?? undefined,
currentClientIdServer: MatrixClientPeg.get().getIdentityServerUrl(),
currentClientIdServer: MatrixClientPeg.safeGet().getIdentityServerUrl(),
});
};
@ -272,7 +272,7 @@ export default class SetIdServer extends React.Component<IProps, IState> {
let currentServerReachable = true;
try {
threepids = await timeout(
getThreepidsWithBindStatus(MatrixClientPeg.get()),
getThreepidsWithBindStatus(MatrixClientPeg.safeGet()),
Promise.reject(new Error("Timeout attempting to reach identity server")),
REACHABILITY_TIMEOUT,
);
@ -362,7 +362,7 @@ export default class SetIdServer extends React.Component<IProps, IState> {
private disconnectIdServer = (): void => {
// Account data change will update localstorage, client, etc through dispatcher
MatrixClientPeg.get().setAccountData("m.identity_server", {
MatrixClientPeg.safeGet().setAccountData("m.identity_server", {
base_url: null, // clear
});
@ -376,7 +376,7 @@ export default class SetIdServer extends React.Component<IProps, IState> {
this.setState({
busy: false,
error: undefined,
currentClientIdServer: MatrixClientPeg.get().getIdentityServerUrl(),
currentClientIdServer: MatrixClientPeg.safeGet().getIdentityServerUrl(),
idServer: newFieldVal,
});
};

View file

@ -77,7 +77,7 @@ export class ExistingEmailAddress extends React.Component<IExistingEmailAddressP
e.stopPropagation();
e.preventDefault();
MatrixClientPeg.get()
MatrixClientPeg.safeGet()
.deleteThreePid(this.props.email.medium, this.props.email.address)
.then(() => {
return this.props.onRemoved(this.props.email);
@ -181,7 +181,7 @@ export default class EmailAddresses extends React.Component<IProps, IState> {
return;
}
const task = new AddThreepid(MatrixClientPeg.get());
const task = new AddThreepid(MatrixClientPeg.safeGet());
this.setState({ verifying: true, continueDisabled: true, addTask: task });
task.addEmailAddress(email)

View file

@ -72,7 +72,7 @@ export class ExistingPhoneNumber extends React.Component<IExistingPhoneNumberPro
e.stopPropagation();
e.preventDefault();
MatrixClientPeg.get()
MatrixClientPeg.safeGet()
.deleteThreePid(this.props.msisdn.medium, this.props.msisdn.address)
.then(() => {
return this.props.onRemoved(this.props.msisdn);
@ -182,7 +182,7 @@ export default class PhoneNumbers extends React.Component<IProps, IState> {
const phoneNumber = this.state.newPhoneNumber;
const phoneCountry = this.state.phoneCountry;
const task = new AddThreepid(MatrixClientPeg.get());
const task = new AddThreepid(MatrixClientPeg.safeGet());
this.setState({ verifying: true, continueDisabled: true, addTask: task });
task.addMsisdn(phoneCountry, phoneNumber)

View file

@ -78,7 +78,7 @@ export class EmailAddress extends React.Component<IEmailAddressProps, IEmailAddr
}
private async changeBinding({ bind, label, errorTitle }: Binding): Promise<void> {
if (!(await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind())) {
if (!(await MatrixClientPeg.safeGet().doesServerSupportSeparateAddAndBind())) {
return this.changeBindingTangledAddBind({ bind, label, errorTitle });
}
@ -86,7 +86,7 @@ export class EmailAddress extends React.Component<IEmailAddressProps, IEmailAddr
try {
if (bind) {
const task = new AddThreepid(MatrixClientPeg.get());
const task = new AddThreepid(MatrixClientPeg.safeGet());
this.setState({
verifying: true,
continueDisabled: true,
@ -97,7 +97,7 @@ export class EmailAddress extends React.Component<IEmailAddressProps, IEmailAddr
continueDisabled: false,
});
} else {
await MatrixClientPeg.get().unbindThreePid(medium, address);
await MatrixClientPeg.safeGet().unbindThreePid(medium, address);
}
this.setState({ bound: bind });
} catch (err) {
@ -117,7 +117,7 @@ export class EmailAddress extends React.Component<IEmailAddressProps, IEmailAddr
private async changeBindingTangledAddBind({ bind, label, errorTitle }: Binding): Promise<void> {
const { medium, address } = this.props.email;
const task = new AddThreepid(MatrixClientPeg.get());
const task = new AddThreepid(MatrixClientPeg.safeGet());
this.setState({
verifying: true,
continueDisabled: true,
@ -125,7 +125,7 @@ export class EmailAddress extends React.Component<IEmailAddressProps, IEmailAddr
});
try {
await MatrixClientPeg.get().deleteThreePid(medium, address);
await MatrixClientPeg.safeGet().deleteThreePid(medium, address);
if (bind) {
await task.bindEmailAddress(address);
} else {

View file

@ -74,7 +74,7 @@ export class PhoneNumber extends React.Component<IPhoneNumberProps, IPhoneNumber
}
private async changeBinding({ bind, label, errorTitle }: Binding): Promise<void> {
if (!(await MatrixClientPeg.get().doesServerSupportSeparateAddAndBind())) {
if (!(await MatrixClientPeg.safeGet().doesServerSupportSeparateAddAndBind())) {
return this.changeBindingTangledAddBind({ bind, label, errorTitle });
}
@ -82,7 +82,7 @@ export class PhoneNumber extends React.Component<IPhoneNumberProps, IPhoneNumber
try {
if (bind) {
const task = new AddThreepid(MatrixClientPeg.get());
const task = new AddThreepid(MatrixClientPeg.safeGet());
this.setState({
verifying: true,
continueDisabled: true,
@ -98,7 +98,7 @@ export class PhoneNumber extends React.Component<IPhoneNumberProps, IPhoneNumber
continueDisabled: false,
});
} else {
await MatrixClientPeg.get().unbindThreePid(medium, address);
await MatrixClientPeg.safeGet().unbindThreePid(medium, address);
}
this.setState({ bound: bind });
} catch (err) {
@ -118,7 +118,7 @@ export class PhoneNumber extends React.Component<IPhoneNumberProps, IPhoneNumber
private async changeBindingTangledAddBind({ bind, label, errorTitle }: Binding): Promise<void> {
const { medium, address } = this.props.msisdn;
const task = new AddThreepid(MatrixClientPeg.get());
const task = new AddThreepid(MatrixClientPeg.safeGet());
this.setState({
verifying: true,
continueDisabled: true,
@ -126,7 +126,7 @@ export class PhoneNumber extends React.Component<IPhoneNumberProps, IPhoneNumber
});
try {
await MatrixClientPeg.get().deleteThreePid(medium, address);
await MatrixClientPeg.safeGet().deleteThreePid(medium, address);
// XXX: Sydent will accept a number without country code if you add
// a leading plus sign to a number in E.164 format (which the 3PID
// address is), but this goes against the spec.