From 4b693f77a73af9681f5ca02ee277c9369e9d9b00 Mon Sep 17 00:00:00 2001 From: Estelle Comment Date: Tue, 9 Apr 2024 17:25:42 +0200 Subject: [PATCH] Use safeGet everywhere, so that it will throw if client is not initialised. --- src/tchap/lib/ExpiredAccountHandler.ts | 3 ++- src/tchap/util/TchapUtils.ts | 17 ++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/tchap/lib/ExpiredAccountHandler.ts b/src/tchap/lib/ExpiredAccountHandler.ts index c03dc71a88..0745c30b38 100644 --- a/src/tchap/lib/ExpiredAccountHandler.ts +++ b/src/tchap/lib/ExpiredAccountHandler.ts @@ -35,7 +35,8 @@ class ExpiredAccountHandler { const expiredRegistrationId = this.dispatcher.register((payload: ActionPayload) => { if (payload.action === "will_start_client") { console.log(":tchap: register a listener for HttpApiEvent.ORG_MATRIX_EXPIRED_ACCOUNT events"); // todo(estelle) logger - const cli = MatrixClientPeg.get(); + // safeGet will throw if client is not initialised yet. We don't handle it because we don't know when it would happen. + const cli = MatrixClientPeg.safeGet(); cli.on(HttpApiEvent.ORG_MATRIX_EXPIRED_ACCOUNT, this.boundOnExpiredAccountEvent); //unregister callback once the work is done this.dispatcher.unregister(expiredRegistrationId); diff --git a/src/tchap/util/TchapUtils.ts b/src/tchap/util/TchapUtils.ts index c698c1a256..8580eb3c4f 100644 --- a/src/tchap/util/TchapUtils.ts +++ b/src/tchap/util/TchapUtils.ts @@ -18,9 +18,9 @@ export default class TchapUtils { * @returns {string} The shortened value of getDomain(). */ static getShortDomain(): string { - const cli = MatrixClientPeg.get(); + const cli = MatrixClientPeg.safeGet(); const baseDomain = cli.getDomain(); - const domain = baseDomain.split(".tchap.gouv.fr")[0].split(".").reverse().filter(Boolean)[0]; + const domain = baseDomain?.split(".tchap.gouv.fr")[0].split(".").reverse().filter(Boolean)[0]; return this.capitalize(domain) || "Tchap"; } @@ -34,7 +34,7 @@ export default class TchapUtils { showForumFederationSwitch: boolean; forumFederationSwitchDefaultValue?: boolean; } { - const cli = MatrixClientPeg.get(); + const cli = MatrixClientPeg.safeGet(); const baseDomain = cli.getDomain(); // Only show the federate switch to defense users : it's difficult to understand, so we avoid @@ -148,7 +148,7 @@ export default class TchapUtils { * @returns Promise is cross signing is supported by home server or false */ static async isCrossSigningSupportedByServer(): Promise { - const cli = MatrixClientPeg.get(); + const cli = MatrixClientPeg.safeGet(); return cli.doesServerSupportUnstableFeature("org.matrix.e2e_cross_signing"); } @@ -157,7 +157,7 @@ export default class TchapUtils { * @returns true is a map tile server is present in config or wellknown. */ static isMapConfigured(): boolean { - const cli = MatrixClientPeg.get(); + const cli = MatrixClientPeg.safeGet(); try { findMapStyleUrl(cli); return true; @@ -201,7 +201,10 @@ export default class TchapUtils { */ static async requestNewExpiredAccountEmail(): Promise { console.debug(":tchap: Requesting an email to renew to account"); // todo(estelle) reuse logger class - const client = MatrixClientPeg.get(); // todo(estelle) what to do if client is null ? use safeGet or get ? + + // safeGet will throw if client is not initialised. We don't handle it because we don't know when this would happen. + const client = MatrixClientPeg.safeGet(); + const homeserverUrl = client.getHomeserverUrl(); const accessToken = client.getAccessToken(); //const url = `${homeserverUrl}/_matrix/client/unstable/account_validity/send_mail`; @@ -230,7 +233,7 @@ export default class TchapUtils { * @returns true if account is expired, false otherwise */ static async isAccountExpired(): Promise { - const client = MatrixClientPeg.safeGet(); // todo(estelle) throws if client is not logged in. Is that what we want ? + const client = MatrixClientPeg.safeGet(); const matrixId: string | null = client.credentials.userId; if (!matrixId) { // throw ? return ? todo(estelle)