1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-08-07 21:23:00 +03:00

Replace some usages of the MatrixClientPeg in favour of passing it around

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski
2024-06-20 12:01:55 +01:00
parent f9e2425ac7
commit c0495178e3
5 changed files with 78 additions and 76 deletions

View File

@@ -18,7 +18,7 @@ limitations under the License.
*/ */
import React from "react"; import React from "react";
import { MatrixError, RuleId, TweakName, SyncState } from "matrix-js-sdk/src/matrix"; import { MatrixError, RuleId, TweakName, SyncState, MatrixClient } from "matrix-js-sdk/src/matrix";
import { import {
CallError, CallError,
CallErrorCode, CallErrorCode,
@@ -169,6 +169,7 @@ export default class LegacyCallHandler extends EventEmitter {
private assertedIdentityNativeUsers = new Map<string, string>(); private assertedIdentityNativeUsers = new Map<string, string>();
private silencedCalls = new Set<string>(); // callIds private silencedCalls = new Set<string>(); // callIds
private matrixClient?: MatrixClient;
public static get instance(): LegacyCallHandler { public static get instance(): LegacyCallHandler {
if (!window.mxLegacyCallHandler) { if (!window.mxLegacyCallHandler) {
@@ -190,7 +191,7 @@ export default class LegacyCallHandler extends EventEmitter {
if (this.shouldObeyAssertedfIdentity()) { if (this.shouldObeyAssertedfIdentity()) {
const nativeUser = this.assertedIdentityNativeUsers.get(call.callId); const nativeUser = this.assertedIdentityNativeUsers.get(call.callId);
if (nativeUser) { if (nativeUser) {
const room = findDMForUser(MatrixClientPeg.safeGet(), nativeUser); const room = findDMForUser(this.matrixClient!, nativeUser);
if (room) return room.roomId; if (room) return room.roomId;
} }
} }
@@ -198,7 +199,7 @@ export default class LegacyCallHandler extends EventEmitter {
return VoipUserMapper.sharedInstance().nativeRoomForVirtualRoom(call.roomId) ?? call.roomId ?? null; return VoipUserMapper.sharedInstance().nativeRoomForVirtualRoom(call.roomId) ?? call.roomId ?? null;
} }
public start(): void { public start(client: MatrixClient): void {
// add empty handlers for media actions, otherwise the media keys // add empty handlers for media actions, otherwise the media keys
// end up causing the audio elements with our ring/ringback etc // end up causing the audio elements with our ring/ringback etc
// audio clips in to play. // audio clips in to play.
@@ -211,8 +212,9 @@ export default class LegacyCallHandler extends EventEmitter {
navigator.mediaSession.setActionHandler("nexttrack", function () {}); navigator.mediaSession.setActionHandler("nexttrack", function () {});
} }
this.matrixClient = client;
if (SettingsStore.getValue(UIFeature.Voip)) { if (SettingsStore.getValue(UIFeature.Voip)) {
MatrixClientPeg.safeGet().on(CallEventHandlerEvent.Incoming, this.onCallIncoming); this.matrixClient.on(CallEventHandlerEvent.Incoming, this.onCallIncoming);
} }
this.checkProtocols(CHECK_PROTOCOLS_ATTEMPTS); this.checkProtocols(CHECK_PROTOCOLS_ATTEMPTS);
@@ -269,8 +271,8 @@ export default class LegacyCallHandler extends EventEmitter {
} }
public isForcedSilent(): boolean { public isForcedSilent(): boolean {
const cli = MatrixClientPeg.safeGet(); const cli = this.matrixClient;
return localNotificationsAreSilenced(cli); return !cli || localNotificationsAreSilenced(cli);
} }
public silenceCall(callId?: string): void { public silenceCall(callId?: string): void {
@@ -308,8 +310,9 @@ export default class LegacyCallHandler extends EventEmitter {
} }
private async checkProtocols(maxTries: number): Promise<void> { private async checkProtocols(maxTries: number): Promise<void> {
if (!this.matrixClient) return;
try { try {
const protocols = await MatrixClientPeg.safeGet().getThirdpartyProtocols(); const protocols = await this.matrixClient.getThirdpartyProtocols();
if (protocols[PROTOCOL_PSTN] !== undefined) { if (protocols[PROTOCOL_PSTN] !== undefined) {
this.supportsPstnProtocol = Boolean(protocols[PROTOCOL_PSTN]); this.supportsPstnProtocol = Boolean(protocols[PROTOCOL_PSTN]);
@@ -356,7 +359,7 @@ export default class LegacyCallHandler extends EventEmitter {
public async pstnLookup(phoneNumber: string): Promise<ThirdpartyLookupResponse[]> { public async pstnLookup(phoneNumber: string): Promise<ThirdpartyLookupResponse[]> {
try { try {
return await MatrixClientPeg.safeGet().getThirdpartyUser( return await this.matrixClient!.getThirdpartyUser(
this.pstnSupportPrefixed ? PROTOCOL_PSTN_PREFIXED : PROTOCOL_PSTN, this.pstnSupportPrefixed ? PROTOCOL_PSTN_PREFIXED : PROTOCOL_PSTN,
{ {
"m.id.phone": phoneNumber, "m.id.phone": phoneNumber,
@@ -370,7 +373,7 @@ export default class LegacyCallHandler extends EventEmitter {
public async sipVirtualLookup(nativeMxid: string): Promise<ThirdpartyLookupResponse[]> { public async sipVirtualLookup(nativeMxid: string): Promise<ThirdpartyLookupResponse[]> {
try { try {
return await MatrixClientPeg.safeGet().getThirdpartyUser(PROTOCOL_SIP_VIRTUAL, { return await this.matrixClient!.getThirdpartyUser(PROTOCOL_SIP_VIRTUAL, {
native_mxid: nativeMxid, native_mxid: nativeMxid,
}); });
} catch (e) { } catch (e) {
@@ -381,7 +384,7 @@ export default class LegacyCallHandler extends EventEmitter {
public async sipNativeLookup(virtualMxid: string): Promise<ThirdpartyLookupResponse[]> { public async sipNativeLookup(virtualMxid: string): Promise<ThirdpartyLookupResponse[]> {
try { try {
return await MatrixClientPeg.safeGet().getThirdpartyUser(PROTOCOL_SIP_NATIVE, { return await this.matrixClient!.getThirdpartyUser(PROTOCOL_SIP_NATIVE, {
virtual_mxid: virtualMxid, virtual_mxid: virtualMxid,
}); });
} catch (e) { } catch (e) {
@@ -413,9 +416,9 @@ export default class LegacyCallHandler extends EventEmitter {
// get ready to send encrypted events in the room, so if the user does answer // get ready to send encrypted events in the room, so if the user does answer
// the call, we'll be ready to send. NB. This is the protocol-level room ID not // the call, we'll be ready to send. NB. This is the protocol-level room ID not
// the mapped one: that's where we'll send the events. // the mapped one: that's where we'll send the events.
const cli = MatrixClientPeg.safeGet(); const cli = this.matrixClient;
const room = cli.getRoom(call.roomId); const room = cli?.getRoom(call.roomId);
if (room) cli.prepareToEncrypt(room); if (room) cli?.prepareToEncrypt(room);
}; };
public getCallById(callId: string): MatrixCall | null { public getCallById(callId: string): MatrixCall | null {
@@ -452,7 +455,7 @@ export default class LegacyCallHandler extends EventEmitter {
} }
public getAllActiveCallsForPip(roomId: string): MatrixCall[] { public getAllActiveCallsForPip(roomId: string): MatrixCall[] {
const room = MatrixClientPeg.safeGet().getRoom(roomId); const room = this.matrixClient?.getRoom(roomId);
if (room && WidgetLayoutStore.instance.hasMaximisedWidget(room)) { if (room && WidgetLayoutStore.instance.hasMaximisedWidget(room)) {
// This checks if there is space for the call view in the aux panel // This checks if there is space for the call view in the aux panel
// If there is no space any call should be displayed in PiP // If there is no space any call should be displayed in PiP
@@ -559,7 +562,7 @@ export default class LegacyCallHandler extends EventEmitter {
} }
if ( if (
MatrixClientPeg.safeGet().getTurnServers().length === 0 && this.matrixClient?.getTurnServers().length === 0 &&
SettingsStore.getValue("fallbackICEServerAllowed") === null SettingsStore.getValue("fallbackICEServerAllowed") === null
) { ) {
this.showICEFallbackPrompt(); this.showICEFallbackPrompt();
@@ -627,7 +630,7 @@ export default class LegacyCallHandler extends EventEmitter {
// this if we want the actual, native room to exist (which we do). This is why it's // this if we want the actual, native room to exist (which we do). This is why it's
// important to only obey asserted identity in trusted environments, since anyone you're // important to only obey asserted identity in trusted environments, since anyone you're
// on a call with can cause you to send a room invite to someone. // on a call with can cause you to send a room invite to someone.
await ensureDMExists(MatrixClientPeg.safeGet(), newNativeAssertedIdentity); await ensureDMExists(this.matrixClient!, newNativeAssertedIdentity);
const newMappedRoomId = this.roomIdForCall(call); const newMappedRoomId = this.roomIdForCall(call);
logger.log(`Old room ID: ${mappedRoomId}, new room ID: ${newMappedRoomId}`); logger.log(`Old room ID: ${mappedRoomId}, new room ID: ${newMappedRoomId}`);
@@ -667,9 +670,7 @@ export default class LegacyCallHandler extends EventEmitter {
switch (newState) { switch (newState) {
case CallState.Ringing: { case CallState.Ringing: {
const incomingCallPushRule = new PushProcessor(MatrixClientPeg.safeGet()).getPushRuleById( const incomingCallPushRule = new PushProcessor(this.matrixClient!).getPushRuleById(RuleId.IncomingCall);
RuleId.IncomingCall,
);
const pushRuleEnabled = incomingCallPushRule?.enabled; const pushRuleEnabled = incomingCallPushRule?.enabled;
// actions can be either Tweaks | PushRuleActionName, ie an object or a string type enum // actions can be either Tweaks | PushRuleActionName, ie an object or a string type enum
// and we want to only run this check on the Tweaks // and we want to only run this check on the Tweaks
@@ -814,7 +815,7 @@ export default class LegacyCallHandler extends EventEmitter {
} }
private showICEFallbackPrompt(): void { private showICEFallbackPrompt(): void {
const cli = MatrixClientPeg.safeGet(); const cli = this.matrixClient;
Modal.createDialog( Modal.createDialog(
QuestionDialog, QuestionDialog,
{ {
@@ -824,7 +825,7 @@ export default class LegacyCallHandler extends EventEmitter {
<p> <p>
{_t( {_t(
"voip|misconfigured_server_description", "voip|misconfigured_server_description",
{ homeserverDomain: cli.getDomain() }, { homeserverDomain: cli!.getDomain() },
{ code: (sub: string) => <code>{sub}</code> }, { code: (sub: string) => <code>{sub}</code> },
)} )}
</p> </p>
@@ -841,7 +842,7 @@ export default class LegacyCallHandler extends EventEmitter {
cancelButton: _t("action|ok"), cancelButton: _t("action|ok"),
onFinished: (allow) => { onFinished: (allow) => {
SettingsStore.setValue("fallbackICEServerAllowed", null, SettingLevel.DEVICE, allow); SettingsStore.setValue("fallbackICEServerAllowed", null, SettingLevel.DEVICE, allow);
cli.setFallbackICEServerAllowed(!!allow); cli!.setFallbackICEServerAllowed(!!allow);
}, },
}, },
undefined, undefined,
@@ -882,7 +883,7 @@ export default class LegacyCallHandler extends EventEmitter {
} }
private async placeMatrixCall(roomId: string, type: CallType, transferee?: MatrixCall): Promise<void> { private async placeMatrixCall(roomId: string, type: CallType, transferee?: MatrixCall): Promise<void> {
const cli = MatrixClientPeg.safeGet(); const cli = this.matrixClient;
const mappedRoomId = (await VoipUserMapper.sharedInstance().getOrCreateVirtualRoomForRoom(roomId)) || roomId; const mappedRoomId = (await VoipUserMapper.sharedInstance().getOrCreateVirtualRoomForRoom(roomId)) || roomId;
logger.debug("Mapped real room " + roomId + " to room ID " + mappedRoomId); logger.debug("Mapped real room " + roomId + " to room ID " + mappedRoomId);
@@ -892,15 +893,15 @@ export default class LegacyCallHandler extends EventEmitter {
// in this queue, and since we're about to place a new call, they can only be events from // in this queue, and since we're about to place a new call, they can only be events from
// previous calls that are probably stale by now, so just cancel them. // previous calls that are probably stale by now, so just cancel them.
if (mappedRoomId !== roomId) { if (mappedRoomId !== roomId) {
const mappedRoom = cli.getRoom(mappedRoomId); const mappedRoom = cli!.getRoom(mappedRoomId);
if (mappedRoom?.getPendingEvents().length) { if (mappedRoom?.getPendingEvents().length) {
Resend.cancelUnsentEvents(mappedRoom); Resend.cancelUnsentEvents(mappedRoom);
} }
} }
const timeUntilTurnCresExpire = cli.getTurnServersExpiry() - Date.now(); const timeUntilTurnCresExpire = cli!.getTurnServersExpiry() - Date.now();
logger.log("Current turn creds expire in " + timeUntilTurnCresExpire + " ms"); logger.log("Current turn creds expire in " + timeUntilTurnCresExpire + " ms");
const call = cli.createCall(mappedRoomId)!; const call = cli!.createCall(mappedRoomId)!;
try { try {
this.addCallForRoom(roomId, call); this.addCallForRoom(roomId, call);
@@ -929,7 +930,7 @@ export default class LegacyCallHandler extends EventEmitter {
} }
public async placeCall(roomId: string, type: CallType, transferee?: MatrixCall): Promise<void> { public async placeCall(roomId: string, type: CallType, transferee?: MatrixCall): Promise<void> {
const cli = MatrixClientPeg.safeGet(); const cli = this.matrixClient;
// Pause current broadcast, if any // Pause current broadcast, if any
SdkContextClass.instance.voiceBroadcastPlaybacksStore.getCurrent()?.pause(); SdkContextClass.instance.voiceBroadcastPlaybacksStore.getCurrent()?.pause();
@@ -946,7 +947,7 @@ export default class LegacyCallHandler extends EventEmitter {
} }
// if the runtime env doesn't do VoIP, whine. // if the runtime env doesn't do VoIP, whine.
if (!cli.supportsVoip()) { if (!cli!.supportsVoip()) {
Modal.createDialog(ErrorDialog, { Modal.createDialog(ErrorDialog, {
title: _t("voip|unsupported"), title: _t("voip|unsupported"),
description: _t("voip|unsupported_browser"), description: _t("voip|unsupported_browser"),
@@ -954,7 +955,7 @@ export default class LegacyCallHandler extends EventEmitter {
return; return;
} }
if (cli.getSyncState() === SyncState.Error) { if (cli!.getSyncState() === SyncState.Error) {
Modal.createDialog(ErrorDialog, { Modal.createDialog(ErrorDialog, {
title: _t("voip|connection_lost"), title: _t("voip|connection_lost"),
description: _t("voip|connection_lost_description"), description: _t("voip|connection_lost_description"),
@@ -971,7 +972,7 @@ export default class LegacyCallHandler extends EventEmitter {
return; return;
} }
const room = cli.getRoom(roomId); const room = cli!.getRoom(roomId);
if (!room) { if (!room) {
logger.error(`Room ${roomId} does not exist.`); logger.error(`Room ${roomId} does not exist.`);
return; return;
@@ -1072,7 +1073,7 @@ export default class LegacyCallHandler extends EventEmitter {
nativeUserId = userId; nativeUserId = userId;
} }
const roomId = await ensureDMExists(MatrixClientPeg.safeGet(), nativeUserId); const roomId = await ensureDMExists(this.matrixClient!, nativeUserId);
if (!roomId) { if (!roomId) {
throw new Error("Failed to ensure DM exists for dialing number"); throw new Error("Failed to ensure DM exists for dialing number");
} }
@@ -1112,7 +1113,7 @@ export default class LegacyCallHandler extends EventEmitter {
public async startTransferToMatrixID(call: MatrixCall, destination: string, consultFirst: boolean): Promise<void> { public async startTransferToMatrixID(call: MatrixCall, destination: string, consultFirst: boolean): Promise<void> {
if (consultFirst) { if (consultFirst) {
const dmRoomId = await ensureDMExists(MatrixClientPeg.safeGet(), destination); const dmRoomId = await ensureDMExists(this.matrixClient!, destination);
if (!dmRoomId) { if (!dmRoomId) {
logger.log("Failed to transfer call, could not ensure dm exists"); logger.log("Failed to transfer call, could not ensure dm exists");
Modal.createDialog(ErrorDialog, { Modal.createDialog(ErrorDialog, {
@@ -1171,7 +1172,7 @@ export default class LegacyCallHandler extends EventEmitter {
} }
private async placeJitsiCall(roomId: string, type: CallType): Promise<void> { private async placeJitsiCall(roomId: string, type: CallType): Promise<void> {
const client = MatrixClientPeg.safeGet(); const client = this.matrixClient;
logger.info(`Place conference call in ${roomId}`); logger.info(`Place conference call in ${roomId}`);
dis.dispatch({ action: "appsDrawer", show: true }); dis.dispatch({ action: "appsDrawer", show: true });
@@ -1180,7 +1181,7 @@ export default class LegacyCallHandler extends EventEmitter {
const widget = WidgetStore.instance.getApps(roomId).find((app) => WidgetType.JITSI.matches(app.type)); const widget = WidgetStore.instance.getApps(roomId).find((app) => WidgetType.JITSI.matches(app.type));
if (widget) { if (widget) {
// If there already is a Jitsi widget, pin it // If there already is a Jitsi widget, pin it
const room = client.getRoom(roomId); const room = client!.getRoom(roomId);
if (isNotNull(room)) { if (isNotNull(room)) {
WidgetLayoutStore.instance.moveToContainer(room, widget, Container.Top); WidgetLayoutStore.instance.moveToContainer(room, widget, Container.Top);
} }
@@ -1188,7 +1189,7 @@ export default class LegacyCallHandler extends EventEmitter {
} }
try { try {
await WidgetUtils.addJitsiWidget(client, roomId, type, "Jitsi", false); await WidgetUtils.addJitsiWidget(client!, roomId, type, "Jitsi", false);
logger.log("Jitsi widget added"); logger.log("Jitsi widget added");
} catch (e) { } catch (e) {
if (e instanceof MatrixError && e.errcode === "M_FORBIDDEN") { if (e instanceof MatrixError && e.errcode === "M_FORBIDDEN") {

View File

@@ -995,12 +995,12 @@ async function startMatrixClient(
ToastStore.sharedInstance().reset(); ToastStore.sharedInstance().reset();
DialogOpener.instance.prepare(client); DialogOpener.instance.prepare(client);
Notifier.start(); Notifier.start(client);
UserActivity.sharedInstance().start(); UserActivity.sharedInstance().start();
DMRoomMap.makeShared(client).start(); DMRoomMap.makeShared(client).start();
IntegrationManagers.sharedInstance().startWatching(); IntegrationManagers.sharedInstance().startWatching(client);
ActiveWidgetStore.instance.start(); ActiveWidgetStore.instance.start(client);
LegacyCallHandler.instance.start(); LegacyCallHandler.instance.start(client);
// Start Mjolnir even though we haven't checked the feature flag yet. Starting // Start Mjolnir even though we haven't checked the feature flag yet. Starting
// the thing just wastes CPU cycles, but should result in no actual functionality // the thing just wastes CPU cycles, but should result in no actual functionality

View File

@@ -29,11 +29,11 @@ import {
IRoomTimelineData, IRoomTimelineData,
M_LOCATION, M_LOCATION,
EventType, EventType,
MatrixClient,
} from "matrix-js-sdk/src/matrix"; } from "matrix-js-sdk/src/matrix";
import { logger } from "matrix-js-sdk/src/logger"; import { logger } from "matrix-js-sdk/src/logger";
import { PermissionChanged as PermissionChangedEvent } from "@matrix-org/analytics-events/types/typescript/PermissionChanged"; import { PermissionChanged as PermissionChangedEvent } from "@matrix-org/analytics-events/types/typescript/PermissionChanged";
import { MatrixClientPeg } from "./MatrixClientPeg";
import { PosthogAnalytics } from "./PosthogAnalytics"; import { PosthogAnalytics } from "./PosthogAnalytics";
import SdkConfig from "./SdkConfig"; import SdkConfig from "./SdkConfig";
import PlatformPeg from "./PlatformPeg"; import PlatformPeg from "./PlatformPeg";
@@ -74,7 +74,7 @@ Override both the content body and the TextForEvent handler for specific msgtype
This is useful when the content body contains fallback text that would explain that the client can't handle a particular This is useful when the content body contains fallback text that would explain that the client can't handle a particular
type of tile. type of tile.
*/ */
const msgTypeHandlers: Record<string, (event: MatrixEvent) => string | null> = { const msgTypeHandlers: Record<string, (event: MatrixEvent, client: MatrixClient) => string | null> = {
[MsgType.KeyVerificationRequest]: (event: MatrixEvent) => { [MsgType.KeyVerificationRequest]: (event: MatrixEvent) => {
const name = (event.sender || {}).name; const name = (event.sender || {}).name;
return _t("notifier|m.key.verification.request", { name }); return _t("notifier|m.key.verification.request", { name });
@@ -85,7 +85,7 @@ const msgTypeHandlers: Record<string, (event: MatrixEvent) => string | null> = {
[M_LOCATION.altName]: (event: MatrixEvent) => { [M_LOCATION.altName]: (event: MatrixEvent) => {
return TextForEvent.textForLocationEvent(event)(); return TextForEvent.textForLocationEvent(event)();
}, },
[MsgType.Audio]: (event: MatrixEvent): string | null => { [MsgType.Audio]: (event: MatrixEvent, client: MatrixClient): string | null => {
if (event.getContent()?.[VoiceBroadcastChunkEventType]) { if (event.getContent()?.[VoiceBroadcastChunkEventType]) {
if (event.getContent()?.[VoiceBroadcastChunkEventType]?.sequence === 1) { if (event.getContent()?.[VoiceBroadcastChunkEventType]?.sequence === 1) {
// Show a notification for the first broadcast chunk. // Show a notification for the first broadcast chunk.
@@ -97,7 +97,7 @@ const msgTypeHandlers: Record<string, (event: MatrixEvent) => string | null> = {
return null; return null;
} }
return TextForEvent.textForEvent(event, MatrixClientPeg.safeGet()); return TextForEvent.textForEvent(event, client);
}, },
}; };
@@ -111,20 +111,21 @@ class NotifierClass {
private toolbarHidden?: boolean; private toolbarHidden?: boolean;
private isSyncing?: boolean; private isSyncing?: boolean;
private matrixClient?: MatrixClient;
public notificationMessageForEvent(ev: MatrixEvent): string | null { public notificationMessageForEvent(ev: MatrixEvent): string | null {
const msgType = ev.getContent().msgtype; const msgType = ev.getContent().msgtype;
if (msgType && msgTypeHandlers.hasOwnProperty(msgType)) { if (msgType && msgTypeHandlers.hasOwnProperty(msgType)) {
return msgTypeHandlers[msgType](ev); return msgTypeHandlers[msgType](ev, this.matrixClient!);
} }
return TextForEvent.textForEvent(ev, MatrixClientPeg.safeGet()); return TextForEvent.textForEvent(ev, this.matrixClient!);
} }
// XXX: exported for tests // XXX: exported for tests
public displayPopupNotification(ev: MatrixEvent, room: Room): void { public displayPopupNotification(ev: MatrixEvent, room: Room): void {
const plaf = PlatformPeg.get(); const plaf = PlatformPeg.get();
const cli = MatrixClientPeg.safeGet(); const cli = this.matrixClient;
if (!plaf) { if (!plaf || !cli) {
return; return;
} }
if (!plaf.supportsNotifications() || !plaf.maySendNotifications()) { if (!plaf.supportsNotifications() || !plaf.maySendNotifications()) {
@@ -221,8 +222,8 @@ class NotifierClass {
// XXX: Exported for tests // XXX: Exported for tests
public async playAudioNotification(ev: MatrixEvent, room: Room): Promise<void> { public async playAudioNotification(ev: MatrixEvent, room: Room): Promise<void> {
const cli = MatrixClientPeg.safeGet(); const cli = this.matrixClient;
if (localNotificationsAreSilenced(cli)) { if (!cli || localNotificationsAreSilenced(cli)) {
return; return;
} }
@@ -251,23 +252,24 @@ class NotifierClass {
} }
} }
public start(): void { public start(client: MatrixClient): void {
const cli = MatrixClientPeg.safeGet(); this.matrixClient = client;
cli.on(RoomEvent.Timeline, this.onEvent); this.matrixClient.on(RoomEvent.Timeline, this.onEvent);
cli.on(RoomEvent.Receipt, this.onRoomReceipt); this.matrixClient.on(RoomEvent.Receipt, this.onRoomReceipt);
cli.on(MatrixEventEvent.Decrypted, this.onEventDecrypted); this.matrixClient.on(MatrixEventEvent.Decrypted, this.onEventDecrypted);
cli.on(ClientEvent.Sync, this.onSyncStateChange); this.matrixClient.on(ClientEvent.Sync, this.onSyncStateChange);
this.toolbarHidden = false; this.toolbarHidden = false;
this.isSyncing = false; this.isSyncing = false;
} }
public stop(): void { public stop(): void {
if (MatrixClientPeg.get()) { if (this.matrixClient) {
MatrixClientPeg.get()!.removeListener(RoomEvent.Timeline, this.onEvent); this.matrixClient.removeListener(RoomEvent.Timeline, this.onEvent);
MatrixClientPeg.get()!.removeListener(RoomEvent.Receipt, this.onRoomReceipt); this.matrixClient.removeListener(RoomEvent.Receipt, this.onRoomReceipt);
MatrixClientPeg.get()!.removeListener(MatrixEventEvent.Decrypted, this.onEventDecrypted); this.matrixClient.removeListener(MatrixEventEvent.Decrypted, this.onEventDecrypted);
MatrixClientPeg.get()!.removeListener(ClientEvent.Sync, this.onSyncStateChange); this.matrixClient.removeListener(ClientEvent.Sync, this.onSyncStateChange);
} }
this.matrixClient = undefined;
this.isSyncing = false; this.isSyncing = false;
} }
@@ -370,11 +372,10 @@ class NotifierClass {
} }
public shouldShowPrompt(): boolean { public shouldShowPrompt(): boolean {
const client = MatrixClientPeg.get(); if (!this.matrixClient) {
if (!client) {
return false; return false;
} }
const isGuest = client.isGuest(); const isGuest = this.matrixClient.isGuest();
return ( return (
!isGuest && !isGuest &&
this.supportsDesktopNotifications() && this.supportsDesktopNotifications() &&
@@ -403,7 +404,7 @@ class NotifierClass {
// wait for first non-cached sync to complete // wait for first non-cached sync to complete
if (![SyncState.Stopped, SyncState.Error].includes(state) && !data?.fromCache) { if (![SyncState.Stopped, SyncState.Error].includes(state) && !data?.fromCache) {
createLocalNotificationSettingsIfNeeded(MatrixClientPeg.safeGet()); createLocalNotificationSettingsIfNeeded(this.matrixClient!);
} }
}; };
@@ -417,10 +418,10 @@ class NotifierClass {
if (removed) return; // only notify for new events, not removed ones if (removed) return; // only notify for new events, not removed ones
if (!data.liveEvent || !!toStartOfTimeline) return; // only notify for new things, not old. if (!data.liveEvent || !!toStartOfTimeline) return; // only notify for new things, not old.
if (!this.isSyncing) return; // don't alert for any messages initially if (!this.isSyncing) return; // don't alert for any messages initially
if (ev.getSender() === MatrixClientPeg.safeGet().getUserId()) return; if (ev.getSender() === this.matrixClient?.getUserId()) return;
if (data.timeline.getTimelineSet().threadListType !== null) return; // Ignore events on the thread list generated timelines if (data.timeline.getTimelineSet().threadListType !== null) return; // Ignore events on the thread list generated timelines
MatrixClientPeg.safeGet().decryptEventIfNeeded(ev); this.matrixClient?.decryptEventIfNeeded(ev);
// If it's an encrypted event and the type is still 'm.room.encrypted', // If it's an encrypted event and the type is still 'm.room.encrypted',
// it hasn't yet been decrypted, so wait until it is. // it hasn't yet been decrypted, so wait until it is.
@@ -478,14 +479,14 @@ class NotifierClass {
roomId = nativeRoomId; roomId = nativeRoomId;
} }
} }
const room = MatrixClientPeg.safeGet().getRoom(roomId); const room = this.matrixClient?.getRoom(roomId);
if (!room) { if (!room) {
// e.g we are in the process of joining a room. // e.g we are in the process of joining a room.
// Seen in the Playwright lazy-loading test. // Seen in the Playwright lazy-loading test.
return; return;
} }
const actions = MatrixClientPeg.safeGet().getPushActionsForEvent(ev); const actions = this.matrixClient?.getPushActionsForEvent(ev);
if (actions?.notify) { if (actions?.notify) {
this.performCustomEventHandling(ev); this.performCustomEventHandling(ev);

View File

@@ -25,7 +25,6 @@ import { IntegrationManagerInstance, Kind } from "./IntegrationManagerInstance";
import IntegrationsImpossibleDialog from "../components/views/dialogs/IntegrationsImpossibleDialog"; import IntegrationsImpossibleDialog from "../components/views/dialogs/IntegrationsImpossibleDialog";
import IntegrationsDisabledDialog from "../components/views/dialogs/IntegrationsDisabledDialog"; import IntegrationsDisabledDialog from "../components/views/dialogs/IntegrationsDisabledDialog";
import WidgetUtils from "../utils/WidgetUtils"; import WidgetUtils from "../utils/WidgetUtils";
import { MatrixClientPeg } from "../MatrixClientPeg";
const KIND_PREFERENCE = [ const KIND_PREFERENCE = [
// Ordered: first is most preferred, last is least preferred. // Ordered: first is most preferred, last is least preferred.
@@ -52,9 +51,9 @@ export class IntegrationManagers {
this.compileManagers(); this.compileManagers();
} }
public startWatching(): void { public startWatching(client: MatrixClient): void {
this.stopWatching(); this.stopWatching();
this.client = MatrixClientPeg.safeGet(); this.client = client;
this.client.on(ClientEvent.AccountData, this.onAccountData); this.client.on(ClientEvent.AccountData, this.onAccountData);
this.client.on(ClientEvent.ClientWellKnown, this.setupHomeserverManagers); this.client.on(ClientEvent.ClientWellKnown, this.setupHomeserverManagers);
this.compileManagers(); this.compileManagers();

View File

@@ -15,9 +15,8 @@ limitations under the License.
*/ */
import EventEmitter from "events"; import EventEmitter from "events";
import { MatrixEvent, RoomStateEvent, RoomState } from "matrix-js-sdk/src/matrix"; import { MatrixEvent, RoomStateEvent, RoomState, MatrixClient } from "matrix-js-sdk/src/matrix";
import { MatrixClientPeg } from "../MatrixClientPeg";
import WidgetUtils from "../utils/WidgetUtils"; import WidgetUtils from "../utils/WidgetUtils";
import { WidgetMessagingStore } from "./widgets/WidgetMessagingStore"; import { WidgetMessagingStore } from "./widgets/WidgetMessagingStore";
@@ -41,6 +40,7 @@ export default class ActiveWidgetStore extends EventEmitter {
private persistentWidgetId: string | null = null; private persistentWidgetId: string | null = null;
private persistentRoomId: string | null = null; private persistentRoomId: string | null = null;
private dockedWidgetsByUid = new Map<string, number>(); private dockedWidgetsByUid = new Map<string, number>();
private matrixClient?: MatrixClient;
public static get instance(): ActiveWidgetStore { public static get instance(): ActiveWidgetStore {
if (!ActiveWidgetStore.internalInstance) { if (!ActiveWidgetStore.internalInstance) {
@@ -49,12 +49,13 @@ export default class ActiveWidgetStore extends EventEmitter {
return ActiveWidgetStore.internalInstance; return ActiveWidgetStore.internalInstance;
} }
public start(): void { public start(client: MatrixClient): void {
MatrixClientPeg.safeGet().on(RoomStateEvent.Events, this.onRoomStateEvents); this.matrixClient = client;
this.matrixClient.on(RoomStateEvent.Events, this.onRoomStateEvents);
} }
public stop(): void { public stop(): void {
MatrixClientPeg.get()?.removeListener(RoomStateEvent.Events, this.onRoomStateEvents); this.matrixClient?.removeListener(RoomStateEvent.Events, this.onRoomStateEvents);
} }
private onRoomStateEvents = (ev: MatrixEvent, { roomId }: RoomState): void => { private onRoomStateEvents = (ev: MatrixEvent, { roomId }: RoomState): void => {