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

Commit

Permalink
Use MatrixClientPeg::safeGet in src/components/views/* (#10987)
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored Jun 15, 2023
1 parent 4243847 commit 280f6a9
Show file tree
Hide file tree
Showing 81 changed files with 265 additions and 250 deletions.
2 changes: 1 addition & 1 deletion src/components/views/avatars/DecoratedRoomAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export default class DecoratedRoomAvatar extends React.PureComponent<IProps, ISt
if (otherUserId && this.props.room.getJoinedMemberCount() === 2) {
// Track presence, if available
if (isPresenceEnabled(this.props.room.client)) {
this.dmUser = MatrixClientPeg.get().getUser(otherUserId);
this.dmUser = MatrixClientPeg.safeGet().getUser(otherUserId);
icon = this.getPresenceIcon();
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/avatars/RoomAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class RoomAvatar extends React.Component<IProps, IState> {
}

public componentDidMount(): void {
MatrixClientPeg.get().on(RoomStateEvent.Events, this.onRoomStateEvents);
MatrixClientPeg.safeGet().on(RoomStateEvent.Events, this.onRoomStateEvents);
}

public componentWillUnmount(): void {
Expand Down
20 changes: 10 additions & 10 deletions src/components/views/context_menus/MessageContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
}

public componentDidMount(): void {
MatrixClientPeg.get().on(RoomMemberEvent.PowerLevel, this.checkPermissions);
MatrixClientPeg.safeGet().on(RoomMemberEvent.PowerLevel, this.checkPermissions);

// re-check the permissions on send progress (`maySendRedactionForEvent` only returns true for events that have
// been fully sent and echoed back, and we want to ensure the "Remove" option is added once that happens.)
Expand All @@ -162,7 +162,7 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
}

private checkPermissions = (): void => {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const room = cli.getRoom(this.props.mxEvent.getRoomId());

// We explicitly decline to show the redact option on ACL events as it has a potential
Expand All @@ -184,7 +184,7 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
};

private isPinned(): boolean {
const room = MatrixClientPeg.get().getRoom(this.props.mxEvent.getRoomId());
const room = MatrixClientPeg.safeGet().getRoom(this.props.mxEvent.getRoomId());
const pinnedEvent = room?.currentState.getStateEvents(EventType.RoomPinnedEvents, "");
if (!pinnedEvent) return false;
const content = pinnedEvent.getContent();
Expand All @@ -195,13 +195,13 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
return (
M_POLL_START.matches(mxEvent.getType()) &&
this.state.canRedact &&
!isPollEnded(mxEvent, MatrixClientPeg.get())
!isPollEnded(mxEvent, MatrixClientPeg.safeGet())
);
}

private onResendReactionsClick = (): void => {
for (const reaction of this.getUnsentReactions()) {
Resend.resend(MatrixClientPeg.get(), reaction);
Resend.resend(MatrixClientPeg.safeGet(), reaction);
}
this.closeMenu();
};
Expand Down Expand Up @@ -253,7 +253,7 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
};

private onPinClick = (): void => {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const room = cli.getRoom(this.props.mxEvent.getRoomId());
if (!room) return;
const eventId = this.props.mxEvent.getId();
Expand Down Expand Up @@ -318,7 +318,7 @@ export default class MessageContextMenu extends React.Component<IProps, IState>

private onEditClick = (): void => {
editEvent(
MatrixClientPeg.get(),
MatrixClientPeg.safeGet(),
this.props.mxEvent,
this.context.timelineRenderingType,
this.props.getRelationsForEvent,
Expand All @@ -345,7 +345,7 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
};

private onEndPollClick = (): void => {
const matrixClient = MatrixClientPeg.get();
const matrixClient = MatrixClientPeg.safeGet();
Modal.createDialog(
EndPollDialog,
{
Expand All @@ -359,7 +359,7 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
};

private getReactions(filter: (e: MatrixEvent) => boolean): MatrixEvent[] {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const room = cli.getRoom(this.props.mxEvent.getRoomId());
const eventId = this.props.mxEvent.getId();
return (
Expand All @@ -386,7 +386,7 @@ export default class MessageContextMenu extends React.Component<IProps, IState>
};

public render(): React.ReactNode {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const me = cli.getUserId();
const { mxEvent, rightClick, link, eventTileOps, reactions, collapseReplyChain, ...other } = this.props;
delete other.getRelationsForEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const ThreadListContextMenu: React.FC<ThreadListContextMenuProps> = ({
onMenuToggle?.(menuDisplayed);
}, [menuDisplayed, onMenuToggle]);

const room = MatrixClientPeg.get().getRoom(mxEvent.getRoomId());
const room = MatrixClientPeg.safeGet().getRoom(mxEvent.getRoomId());
const isMainSplitTimelineShown = !!room && !WidgetLayoutStore.instance.hasMaximisedWidget(room);
return (
<React.Fragment>
Expand Down
6 changes: 3 additions & 3 deletions src/components/views/dialogs/CreateRoomDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class CreateRoomDialog extends React.Component<IProps, IState> {
joinRule = JoinRule.Restricted;
}

const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
this.state = {
isPublic: this.props.defaultPublic || false,
isEncrypted: this.props.defaultEncrypted ?? privateShouldBeEncrypted(cli),
Expand Down Expand Up @@ -222,7 +222,7 @@ export default class CreateRoomDialog extends React.Component<IProps, IState> {

let aliasField: JSX.Element | undefined;
if (this.state.joinRule === JoinRule.Public) {
const domain = MatrixClientPeg.get().getDomain()!;
const domain = MatrixClientPeg.safeGet().getDomain()!;
aliasField = (
<div className="mx_CreateRoomDialog_aliasContainer">
<RoomAliasField
Expand Down Expand Up @@ -285,7 +285,7 @@ export default class CreateRoomDialog extends React.Component<IProps, IState> {
let e2eeSection: JSX.Element | undefined;
if (this.state.joinRule !== JoinRule.Public) {
let microcopy: string;
if (privateShouldBeEncrypted(MatrixClientPeg.get())) {
if (privateShouldBeEncrypted(MatrixClientPeg.safeGet())) {
if (this.state.canChangeEncryption) {
microcopy = isVideoRoom
? _t("You can't disable this later. The room will be encrypted but the embedded call will not.")
Expand Down
6 changes: 3 additions & 3 deletions src/components/views/dialogs/DeactivateAccountDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default class DeactivateAccountDialog extends React.Component<IProps, ISt
// XXX: this should be returning a promise to maintain the state inside the state machine correct
// but given that a deactivation is followed by a local logout and all object instances being thrown away
// this isn't done.
MatrixClientPeg.get()
MatrixClientPeg.safeGet()
.deactivateAccount(auth, this.state.shouldErase)
.then((r) => {
// Deactivation worked - logout & close this dialog
Expand Down Expand Up @@ -158,7 +158,7 @@ export default class DeactivateAccountDialog extends React.Component<IProps, ISt
}

private initAuth(shouldErase: boolean): void {
MatrixClientPeg.get()
MatrixClientPeg.safeGet()
.deactivateAccount(null, shouldErase)
.then((r) => {
// If we got here, oops. The server didn't require any auth.
Expand Down Expand Up @@ -190,7 +190,7 @@ export default class DeactivateAccountDialog extends React.Component<IProps, ISt
<div>
{this.state.bodyText}
<InteractiveAuth
matrixClient={MatrixClientPeg.get()}
matrixClient={MatrixClientPeg.safeGet()}
authData={this.state.authData}
// XXX: onUIAuthComplete breaches the expected method contract, it gets away with it because it
// knows the entire app is about to die as a result of the account deactivation.
Expand Down
6 changes: 3 additions & 3 deletions src/components/views/dialogs/IncomingSasDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default class IncomingSasDialog extends React.Component<IProps, IState> {

private async fetchOpponentProfile(): Promise<void> {
try {
const prof = await MatrixClientPeg.get().getProfileInfo(this.props.verifier.userId);
const prof = await MatrixClientPeg.safeGet().getProfileInfo(this.props.verifier.userId);
this.setState({
opponentProfile: prof,
});
Expand Down Expand Up @@ -143,7 +143,7 @@ export default class IncomingSasDialog extends React.Component<IProps, IState> {
};

private renderPhaseStart(): ReactNode {
const isSelf = this.props.verifier.userId === MatrixClientPeg.get().getUserId();
const isSelf = this.props.verifier.userId === MatrixClientPeg.safeGet().getUserId();

let profile;
const oppProfile = this.state.opponentProfile;
Expand Down Expand Up @@ -233,7 +233,7 @@ export default class IncomingSasDialog extends React.Component<IProps, IState> {
sas={this.showSasEvent.sas}
onCancel={this.onCancelClick}
onDone={this.onSasMatchesClick}
isSelf={this.props.verifier.userId === MatrixClientPeg.get().getUserId()}
isSelf={this.props.verifier.userId === MatrixClientPeg.safeGet().getUserId()}
inDialog={true}
/>
);
Expand Down
28 changes: 14 additions & 14 deletions src/components/views/dialogs/InviteDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,12 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial

this.profilesStore = SdkContextClass.instance.userProfilesStore;

const alreadyInvited = new Set([MatrixClientPeg.get().getUserId()!]);
const alreadyInvited = new Set([MatrixClientPeg.safeGet().getUserId()!]);
const welcomeUserId = SdkConfig.get("welcome_user_id");
if (welcomeUserId) alreadyInvited.add(welcomeUserId);

if (isRoomInvite(props)) {
const room = MatrixClientPeg.get().getRoom(props.roomId);
const room = MatrixClientPeg.safeGet().getRoom(props.roomId);
if (!room) throw new Error("Room ID given to InviteDialog does not look like a room");
room.getMembersWithMembership("invite").forEach((m) => alreadyInvited.add(m.userId));
room.getMembersWithMembership("join").forEach((m) => alreadyInvited.add(m.userId));
Expand All @@ -395,7 +395,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
numSuggestionsShown: INITIAL_ROOMS_SHOWN,
serverResultsMixin: [],
threepidResultsMixin: [],
canUseIdentityServer: !!MatrixClientPeg.get().getIdentityServerUrl(),
canUseIdentityServer: !!MatrixClientPeg.safeGet().getIdentityServerUrl(),
tryingIdentityServer: false,
consultFirst: false,
dialPadValue: "",
Expand All @@ -407,7 +407,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
}

public componentDidMount(): void {
this.encryptionByDefault = privateShouldBeEncrypted(MatrixClientPeg.get());
this.encryptionByDefault = privateShouldBeEncrypted(MatrixClientPeg.safeGet());

if (this.props.initialText) {
this.updateSuggestions(this.props.initialText);
Expand All @@ -428,7 +428,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
// Also pull in all the rooms tagged as DefaultTagID.DM so we don't miss anything. Sometimes the
// room list doesn't tag the room for the DMRoomMap, but does for the room list.
const dmTaggedRooms = RoomListStore.instance.orderedLists[DefaultTagID.DM] || [];
const myUserId = MatrixClientPeg.get().getUserId();
const myUserId = MatrixClientPeg.safeGet().getUserId();
for (const dmRoom of dmTaggedRooms) {
const otherMembers = dmRoom.getJoinedMembers().filter((u) => u.userId !== myUserId);
for (const member of otherMembers) {
Expand Down Expand Up @@ -491,7 +491,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
}

private buildSuggestions(excludedTargetIds: Set<string>): { userId: string; user: Member }[] {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const activityScores = buildActivityScores(cli);
const memberScores = buildMemberScores(cli);

Expand Down Expand Up @@ -560,7 +560,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
this.setBusy(true);

try {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const targets = this.convertFilter();
await startDmOnFirstMessage(cli, targets);
this.props.onFinished(true);
Expand Down Expand Up @@ -601,7 +601,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
const targets = this.convertFilter();
const targetIds = targets.map((t) => t.userId);

const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const room = cli.getRoom(this.props.roomId);
if (!room) {
logger.error("Failed to find the room to invite users to");
Expand Down Expand Up @@ -694,7 +694,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
};

private updateSuggestions = async (term: string): Promise<void> => {
MatrixClientPeg.get()
MatrixClientPeg.safeGet()
.searchUserDirectory({ term })
.then(async (r): Promise<void> => {
if (term !== this.state.filterText) {
Expand Down Expand Up @@ -774,7 +774,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial

if (term !== this.state.filterText) return; // abandon hope

const lookup = await MatrixClientPeg.get().lookupThreePid("email", term, token);
const lookup = await MatrixClientPeg.safeGet().lookupThreePid("email", term, token);
if (term !== this.state.filterText) return; // abandon hope

if (!lookup || !lookup.mxid) {
Expand Down Expand Up @@ -986,7 +986,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial

// Update the IS in account data. Actually using it may trigger terms.
// eslint-disable-next-line react-hooks/rules-of-hooks
setToDefaultIdentityServer(MatrixClientPeg.get());
setToDefaultIdentityServer(MatrixClientPeg.safeGet());
this.setState({ canUseIdentityServer: true, tryingIdentityServer: false });
};

Expand Down Expand Up @@ -1289,7 +1289,7 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
const hasSelection =
this.state.targets.length > 0 || (this.state.filterText && this.state.filterText.includes("@"));

const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const userId = cli.getUserId()!;
if (this.props.kind === InviteKind.Dm) {
title = _t("Direct Messages");
Expand Down Expand Up @@ -1332,11 +1332,11 @@ export default class InviteDialog extends React.PureComponent<Props, IInviteDial
<p>{_t("If you can't see who you're looking for, send them your invite link below.")}</p>
</div>
);
const link = makeUserPermalink(MatrixClientPeg.get().getUserId()!);
const link = makeUserPermalink(MatrixClientPeg.safeGet().getSafeUserId());
footer = (
<div className="mx_InviteDialog_footer">
<h3>{_t("Or send invite link")}</h3>
<CopyableText getTextToCopy={() => makeUserPermalink(MatrixClientPeg.get().getUserId()!)}>
<CopyableText getTextToCopy={() => makeUserPermalink(MatrixClientPeg.safeGet().getSafeUserId())}>
<a className="mx_InviteDialog_footer_link" href={link} onClick={this.onLinkClick}>
{link}
</a>
Expand Down
6 changes: 3 additions & 3 deletions src/components/views/dialogs/LogoutDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class LogoutDialog extends React.Component<IProps, IState> {
public constructor(props: IProps) {
super(props);

const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const shouldLoadBackupStatus = cli.isCryptoEnabled() && !cli.getKeyBackupEnabled();

this.state = {
Expand All @@ -66,7 +66,7 @@ export default class LogoutDialog extends React.Component<IProps, IState> {

private async loadBackupStatus(): Promise<void> {
try {
const backupInfo = await MatrixClientPeg.get().getKeyBackupVersion();
const backupInfo = await MatrixClientPeg.safeGet().getKeyBackupVersion();
this.setState({
loading: false,
backupInfo,
Expand All @@ -86,7 +86,7 @@ export default class LogoutDialog extends React.Component<IProps, IState> {
typeof ExportE2eKeysDialog
>,
{
matrixClient: MatrixClientPeg.get(),
matrixClient: MatrixClientPeg.safeGet(),
},
);
};
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/dialogs/MessageEditHistoryDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class MessageEditHistoryDialog extends React.PureComponent<IProps
const opts = { from: this.state.nextBatch ?? undefined };
const roomId = this.props.mxEvent.getRoomId()!;
const eventId = this.props.mxEvent.getId()!;
const client = MatrixClientPeg.get();
const client = MatrixClientPeg.safeGet();

const { resolve, reject, promise } = defer<boolean>();
let result: Awaited<ReturnType<MatrixClient["relations"]>>;
Expand Down Expand Up @@ -102,7 +102,7 @@ export default class MessageEditHistoryDialog extends React.PureComponent<IProps

private locallyRedactEventsIfNeeded(newEvents: MatrixEvent[]): void {
const roomId = this.props.mxEvent.getRoomId();
const client = MatrixClientPeg.get();
const client = MatrixClientPeg.safeGet();
const room = client.getRoom(roomId);
if (!room) return;
const pendingEvents = room.getPendingEvents();
Expand Down
4 changes: 2 additions & 2 deletions src/components/views/dialogs/ModalWidgetDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default class ModalWidgetDialog extends React.PureComponent<IProps, IStat

this.widget = new ElementWidget({
...this.props.widgetDefinition,
creatorUserId: MatrixClientPeg.get().getSafeUserId(),
creatorUserId: MatrixClientPeg.safeGet().getSafeUserId(),
id: `modal_${this.props.sourceWidgetId}`,
});
this.possibleButtons = (this.props.widgetDefinition.buttons || []).map((b) => b.id);
Expand Down Expand Up @@ -130,7 +130,7 @@ export default class ModalWidgetDialog extends React.PureComponent<IProps, IStat
public render(): React.ReactNode {
const templated = this.widget.getCompleteUrl({
widgetRoomId: this.props.widgetRoomId,
currentUserId: MatrixClientPeg.get().getSafeUserId(),
currentUserId: MatrixClientPeg.safeGet().getSafeUserId(),
userDisplayName: OwnProfileStore.instance.displayName ?? undefined,
userHttpAvatarUrl: OwnProfileStore.instance.getHttpAvatarUrl() ?? undefined,
clientId: ELEMENT_CLIENT_ID,
Expand Down
Loading

0 comments on commit 280f6a9

Please sign in to comment.