Use MatrixClientPeg::safeGet in src/{stores,hooks,components/structures}/* (#10988)

This commit is contained in:
Michael Telatynski
2023-06-15 15:11:49 +01:00
committed by GitHub
parent 707fd9ccf0
commit dd46db4817
34 changed files with 139 additions and 130 deletions
+1 -1
View File
@@ -51,7 +51,7 @@ export default class ActiveWidgetStore extends EventEmitter {
}
public start(): void {
MatrixClientPeg.get().on(RoomStateEvent.Events, this.onRoomStateEvents);
MatrixClientPeg.safeGet().on(RoomStateEvent.Events, this.onRoomStateEvents);
}
public stop(): void {
+1 -1
View File
@@ -159,7 +159,7 @@ export class OwnProfileStore extends AsyncStoreWithClient<IState> {
);
private onStateEvents = async (ev: MatrixEvent): Promise<void> => {
const myUserId = MatrixClientPeg.get().getUserId();
const myUserId = MatrixClientPeg.safeGet().getUserId();
if (ev.getType() === EventType.RoomMember && ev.getSender() === myUserId && ev.getStateKey() === myUserId) {
await this.onProfileUpdate();
}
+6 -6
View File
@@ -306,7 +306,7 @@ export class RoomViewStore extends EventEmitter {
this.setState({ shouldPeek: false });
}
awaitRoomDownSync(MatrixClientPeg.get(), payload.roomId).then((room) => {
awaitRoomDownSync(MatrixClientPeg.safeGet(), payload.roomId).then((room) => {
const numMembers = room.getJoinedMemberCount();
const roomSize =
numMembers > 1000
@@ -361,7 +361,7 @@ export class RoomViewStore extends EventEmitter {
private async viewRoom(payload: ViewRoomPayload): Promise<void> {
if (payload.room_id) {
const room = MatrixClientPeg.get().getRoom(payload.room_id);
const room = MatrixClientPeg.safeGet().getRoom(payload.room_id);
if (payload.metricsTrigger !== null && payload.room_id !== this.state.roomId) {
let activeSpace: ViewRoomEvent["activeSpace"];
@@ -488,7 +488,7 @@ export class RoomViewStore extends EventEmitter {
viewingCall: payload.view_call ?? false,
});
try {
const result = await MatrixClientPeg.get().getRoomIdForAlias(payload.room_alias);
const result = await MatrixClientPeg.safeGet().getRoomIdForAlias(payload.room_alias);
storeRoomAliasInCache(payload.room_alias, result.room_id);
roomId = result.room_id;
} catch (err) {
@@ -531,12 +531,12 @@ export class RoomViewStore extends EventEmitter {
joining: true,
});
const cli = MatrixClientPeg.get();
// take a copy of roomAlias & roomId as they may change by the time the join is complete
const { roomAlias, roomId = payload.roomId } = this.state;
const address = roomAlias || roomId!;
const viaServers = this.state.viaServers || [];
try {
const cli = MatrixClientPeg.safeGet();
await retry<Room, MatrixError>(
() =>
cli.joinRoom(address, {
@@ -568,7 +568,7 @@ export class RoomViewStore extends EventEmitter {
}
private getInvitingUserId(roomId: string): string | undefined {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const room = cli.getRoom(roomId);
if (room?.getMyMembership() === "invite") {
const myMember = room.getMember(cli.getSafeUserId());
@@ -596,7 +596,7 @@ export class RoomViewStore extends EventEmitter {
// provide a better error message for invites
if (invitingUserId) {
// if the inviting user is on the same HS, there can only be one cause: they left.
if (invitingUserId.endsWith(`:${MatrixClientPeg.get().getDomain()}`)) {
if (invitingUserId.endsWith(`:${MatrixClientPeg.safeGet().getDomain()}`)) {
description = _t("The person who invited you has already left.");
} else {
description = _t("The person who invited you has already left, or their server is offline.");
+16 -14
View File
@@ -62,7 +62,7 @@ export class SetupEncryptionStore extends EventEmitter {
this.started = true;
this.phase = Phase.Loading;
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
cli.on(CryptoEvent.VerificationRequest, this.onVerificationRequest);
cli.on(CryptoEvent.UserTrustStatusChanged, this.onUserTrustStatusChanged);
@@ -83,15 +83,17 @@ export class SetupEncryptionStore extends EventEmitter {
}
this.started = false;
this.verificationRequest?.off(VerificationRequestEvent.Change, this.onVerificationRequestChange);
if (MatrixClientPeg.get()) {
MatrixClientPeg.get().removeListener(CryptoEvent.VerificationRequest, this.onVerificationRequest);
MatrixClientPeg.get().removeListener(CryptoEvent.UserTrustStatusChanged, this.onUserTrustStatusChanged);
const cli = MatrixClientPeg.get();
if (!!cli) {
cli.removeListener(CryptoEvent.VerificationRequest, this.onVerificationRequest);
cli.removeListener(CryptoEvent.UserTrustStatusChanged, this.onUserTrustStatusChanged);
}
}
public async fetchKeyInfo(): Promise<void> {
if (!this.started) return; // bail if we were stopped
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
const keys = await cli.isSecretStored("m.cross_signing.master");
if (keys === null || Object.keys(keys).length === 0) {
this.keyId = null;
@@ -120,8 +122,8 @@ export class SetupEncryptionStore extends EventEmitter {
public async usePassPhrase(): Promise<void> {
this.phase = Phase.Busy;
this.emit("update");
const cli = MatrixClientPeg.get();
try {
const cli = MatrixClientPeg.safeGet();
const backupInfo = await cli.getKeyBackupVersion();
this.backupInfo = backupInfo;
this.emit("update");
@@ -161,8 +163,8 @@ export class SetupEncryptionStore extends EventEmitter {
}
private onUserTrustStatusChanged = async (userId: string): Promise<void> => {
if (userId !== MatrixClientPeg.get().getUserId()) return;
const publicKeysTrusted = await MatrixClientPeg.get().getCrypto()?.getCrossSigningKeyId();
if (userId !== MatrixClientPeg.safeGet().getSafeUserId()) return;
const publicKeysTrusted = await MatrixClientPeg.safeGet().getCrypto()?.getCrossSigningKeyId();
if (publicKeysTrusted) {
this.phase = Phase.Done;
this.emit("update");
@@ -184,7 +186,7 @@ export class SetupEncryptionStore extends EventEmitter {
// At this point, the verification has finished, we just need to wait for
// cross signing to be ready to use, so wait for the user trust status to
// change (or change to DONE if it's already ready).
const publicKeysTrusted = await MatrixClientPeg.get().getCrypto()?.getCrossSigningKeyId();
const publicKeysTrusted = await MatrixClientPeg.safeGet().getCrypto()?.getCrossSigningKeyId();
this.phase = publicKeysTrusted ? Phase.Done : Phase.Busy;
this.emit("update");
}
@@ -217,7 +219,7 @@ export class SetupEncryptionStore extends EventEmitter {
// secret storage and setting up a new recovery key, then
// create new cross-signing keys once that succeeds.
await accessSecretStorage(async (): Promise<void> => {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
await cli.bootstrapCrossSigning({
authUploadDeviceSigningKeys: async (makeRequest): Promise<void> => {
const cachedPassword = SdkContextClass.instance.accountPasswordStore.getPassword();
@@ -227,9 +229,9 @@ export class SetupEncryptionStore extends EventEmitter {
type: "m.login.password",
identifier: {
type: "m.id.user",
user: cli.getUserId(),
user: cli.getSafeUserId(),
},
user: cli.getUserId(),
user: cli.getSafeUserId(),
password: cachedPassword,
});
return;
@@ -265,12 +267,12 @@ export class SetupEncryptionStore extends EventEmitter {
this.phase = Phase.Finished;
this.emit("update");
// async - ask other clients for keys, if necessary
MatrixClientPeg.get().crypto?.cancelAndResendAllOutgoingKeyRequests();
MatrixClientPeg.safeGet().crypto?.cancelAndResendAllOutgoingKeyRequests();
}
private async setActiveVerificationRequest(request: VerificationRequest): Promise<void> {
if (!this.started) return; // bail if we were stopped
if (request.otherUserId !== MatrixClientPeg.get().getUserId()) return;
if (request.otherUserId !== MatrixClientPeg.safeGet().getUserId()) return;
if (this.verificationRequest) {
this.verificationRequest.off(VerificationRequestEvent.Change, this.onVerificationRequestChange);
@@ -59,7 +59,7 @@ export class RoomNotificationState extends NotificationState implements IDestroy
};
private handleReadReceipt = (event: MatrixEvent, room: Room): void => {
if (!readReceiptChangeIsFor(event, MatrixClientPeg.get())) return; // not our own - ignore
if (!readReceiptChangeIsFor(event, MatrixClientPeg.safeGet())) return; // not our own - ignore
if (room.roomId !== this.room.roomId) return; // not for us - ignore
this.updateNotificationState();
};
+2 -2
View File
@@ -310,7 +310,7 @@ export default class RightPanelStore extends ReadyWatchingStore {
// RightPanelPhases.RoomMemberInfo -> needs to be changed to RightPanelPhases.EncryptionPanel if there is a pending verification request
const { member } = card.state;
const pendingRequest = member
? pendingVerificationRequestForUser(MatrixClientPeg.get(), member)
? pendingVerificationRequestForUser(MatrixClientPeg.safeGet(), member)
: undefined;
if (pendingRequest) {
return {
@@ -344,7 +344,7 @@ export default class RightPanelStore extends ReadyWatchingStore {
if (!this.currentCard?.state) return;
const { member } = this.currentCard.state;
if (!member) return;
const pendingRequest = pendingVerificationRequestForUser(MatrixClientPeg.get(), member);
const pendingRequest = pendingVerificationRequestForUser(MatrixClientPeg.safeGet(), member);
if (pendingRequest) {
this.currentCard.state.verificationRequest = pendingRequest;
this.emitAndUpdateSettings();
@@ -56,7 +56,7 @@ export const sortRooms = (rooms: Room[]): Room[] => {
// See https://github.com/vector-im/element-web/issues/14458
let myUserId = "";
if (MatrixClientPeg.get()) {
myUserId = MatrixClientPeg.get().getUserId()!;
myUserId = MatrixClientPeg.get()!.getSafeUserId();
}
const tsCache: { [roomId: string]: number } = {};
+2 -2
View File
@@ -20,7 +20,7 @@ import { MatrixClientPeg } from "../../../MatrixClientPeg";
import { DefaultTagID, TagID } from "../models";
export function isSelf(event: MatrixEvent): boolean {
const selfUserId = MatrixClientPeg.get().getSafeUserId();
const selfUserId = MatrixClientPeg.safeGet().getSafeUserId();
if (event.getType() === "m.room.member") {
return event.getStateKey() === selfUserId;
}
@@ -31,7 +31,7 @@ export function shouldPrefixMessagesIn(roomId: string, tagId?: TagID): boolean {
if (tagId !== DefaultTagID.DM) return true;
// We don't prefix anything in 1:1s
const room = MatrixClientPeg.get().getRoom(roomId);
const room = MatrixClientPeg.safeGet().getRoom(roomId);
if (!room) return true;
return room.currentState.getJoinedMemberCount() !== 2;
}
+1 -1
View File
@@ -167,7 +167,7 @@ export class StopGapWidget extends EventEmitter {
public constructor(private appTileProps: IAppTileProps) {
super();
this.client = MatrixClientPeg.get();
this.client = MatrixClientPeg.safeGet();
let app = appTileProps.app;
// Backwards compatibility: not all old widgets have a creatorUserId
+7 -7
View File
@@ -138,7 +138,7 @@ export class StopGapWidgetDriver extends WidgetDriver {
WidgetEventCapability.forStateEvent(
EventDirection.Send,
"org.matrix.msc3401.call.member",
MatrixClientPeg.get().getUserId()!,
MatrixClientPeg.safeGet().getSafeUserId(),
).raw,
);
this.allowedCapabilities.add(
@@ -266,7 +266,7 @@ export class StopGapWidgetDriver extends WidgetDriver {
encrypted: boolean,
contentMap: { [userId: string]: { [deviceId: string]: object } },
): Promise<void> {
const client = MatrixClientPeg.get();
const client = MatrixClientPeg.safeGet();
if (encrypted) {
const deviceInfoMap = await client.crypto!.deviceList.downloadKeys(Object.keys(contentMap), false);
@@ -382,7 +382,7 @@ export class StopGapWidgetDriver extends WidgetDriver {
if (opts.approved) {
return observer.update({
state: OpenIDRequestState.Allowed,
token: await MatrixClientPeg.get().getOpenIdToken(),
token: await MatrixClientPeg.safeGet().getOpenIdToken(),
});
}
@@ -393,7 +393,7 @@ export class StopGapWidgetDriver extends WidgetDriver {
);
const getToken = (): Promise<IOpenIDCredentials> => {
return MatrixClientPeg.get().getOpenIdToken();
return MatrixClientPeg.safeGet().getOpenIdToken();
};
if (oidcState === OIDCState.Denied) {
@@ -425,7 +425,7 @@ export class StopGapWidgetDriver extends WidgetDriver {
}
public async *getTurnServers(): AsyncGenerator<ITurnServer> {
const client = MatrixClientPeg.get();
const client = MatrixClientPeg.safeGet();
if (!client.pollingTurnServers || !client.getTurnServers().length) return;
let setTurnServer: (server: ITurnServer) => void;
@@ -468,7 +468,7 @@ export class StopGapWidgetDriver extends WidgetDriver {
limit?: number,
direction?: "f" | "b",
): Promise<IReadEventRelationsResult> {
const client = MatrixClientPeg.get();
const client = MatrixClientPeg.safeGet();
const dir = direction as Direction;
roomId = roomId ?? SdkContextClass.instance.roomViewStore.getRoomId() ?? undefined;
@@ -492,7 +492,7 @@ export class StopGapWidgetDriver extends WidgetDriver {
}
public async searchUserDirectory(searchTerm: string, limit?: number): Promise<ISearchUserDirectoryResult> {
const client = MatrixClientPeg.get();
const client = MatrixClientPeg.safeGet();
const { limited, results } = await client.searchUserDirectory({ term: searchTerm, limit });