Skip to content

Commit

Permalink
Use safeGet everywhere, so that it will throw if client is not initia…
Browse files Browse the repository at this point in the history
…lised.
  • Loading branch information
estellecomment committed Apr 9, 2024
1 parent edc83a1 commit 4b693f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/tchap/lib/ExpiredAccountHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 10 additions & 7 deletions src/tchap/util/TchapUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand All @@ -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
Expand Down Expand Up @@ -148,7 +148,7 @@ export default class TchapUtils {
* @returns Promise<true> is cross signing is supported by home server or false
*/
static async isCrossSigningSupportedByServer(): Promise<boolean> {
const cli = MatrixClientPeg.get();
const cli = MatrixClientPeg.safeGet();
return cli.doesServerSupportUnstableFeature("org.matrix.e2e_cross_signing");
}

Expand All @@ -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;
Expand Down Expand Up @@ -201,7 +201,10 @@ export default class TchapUtils {
*/
static async requestNewExpiredAccountEmail(): Promise<boolean> {
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`;
Expand Down Expand Up @@ -230,7 +233,7 @@ export default class TchapUtils {
* @returns true if account is expired, false otherwise
*/
static async isAccountExpired(): Promise<boolean> {
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)
Expand Down

0 comments on commit 4b693f7

Please sign in to comment.