From b607da5b142a0eb810a0dcaf942bd8e1d94ac502 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 27 Jun 2022 15:19:33 -0600 Subject: [PATCH] Break import cycles by not directly depending on Lifecycle --- src/Lifecycle.ts | 7 +++++- src/dispatcher/actions.ts | 5 ++++ .../payloads/OverwriteLoginPayload.ts | 25 +++++++++++++++++++ src/modules/ProxiedModuleApi.ts | 13 ++++++---- 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 src/dispatcher/payloads/OverwriteLoginPayload.ts diff --git a/src/Lifecycle.ts b/src/Lifecycle.ts index cdc40bf35ef..915bc85dd72 100644 --- a/src/Lifecycle.ts +++ b/src/Lifecycle.ts @@ -63,6 +63,7 @@ import VideoChannelStore from "./stores/VideoChannelStore"; import { fixStuckDevices } from "./utils/VideoChannelUtils"; import { Action } from "./dispatcher/actions"; import AbstractLocalStorageSettingsHandler from "./settings/handlers/AbstractLocalStorageSettingsHandler"; +import { OverwriteLoginPayload } from "./dispatcher/payloads/OverwriteLoginPayload"; const HOMESERVER_URL_KEY = "mx_hs_url"; const ID_SERVER_URL_KEY = "mx_is_url"; @@ -71,6 +72,10 @@ dis.register((payload) => { if (payload.action === Action.TriggerLogout) { // noinspection JSIgnoredPromiseFromCall - we don't care if it fails onLoggedOut(); + } else if (payload.action === Action.OverwriteLogin) { + const typed = payload; + // noinspection JSIgnoredPromiseFromCall - we don't care if it fails + doSetLoggedIn(typed.credentials, true); } }); @@ -558,7 +563,7 @@ export async function hydrateSession(credentials: IMatrixClientCreds): Promise { diff --git a/src/dispatcher/actions.ts b/src/dispatcher/actions.ts index c400b175743..75205176be1 100644 --- a/src/dispatcher/actions.ts +++ b/src/dispatcher/actions.ts @@ -315,4 +315,9 @@ export enum Action { * Fired when the client was logged in. No additional payload information required. */ OnLoggedIn = "on_logged_in", + + /** + * Overwrites the existing login with fresh session credentials. Use with a OverwriteLoginPayload. + */ + OverwriteLogin = "overwrite_login", } diff --git a/src/dispatcher/payloads/OverwriteLoginPayload.ts b/src/dispatcher/payloads/OverwriteLoginPayload.ts new file mode 100644 index 00000000000..ec5b83c1de7 --- /dev/null +++ b/src/dispatcher/payloads/OverwriteLoginPayload.ts @@ -0,0 +1,25 @@ +/* +Copyright 2022 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import { ActionPayload } from "../payloads"; +import { Action } from "../actions"; +import { IMatrixClientCreds } from "../../MatrixClientPeg"; + +export interface OverwriteLoginPayload extends ActionPayload { + action: Action.OverwriteLogin; + + credentials: IMatrixClientCreds; +} diff --git a/src/modules/ProxiedModuleApi.ts b/src/modules/ProxiedModuleApi.ts index bba6e2cceac..93c194721c3 100644 --- a/src/modules/ProxiedModuleApi.ts +++ b/src/modules/ProxiedModuleApi.ts @@ -28,13 +28,13 @@ import { _t } from "../languageHandler"; import { ModuleUiDialog } from "../components/views/dialogs/ModuleUiDialog"; import SdkConfig from "../SdkConfig"; import PlatformPeg from "../PlatformPeg"; -import { doSetLoggedIn } from "../Lifecycle"; import dispatcher from "../dispatcher/dispatcher"; import { navigateToPermalink } from "../utils/permalinks/navigator"; import { parsePermalink } from "../utils/permalinks/Permalinks"; import { MatrixClientPeg } from "../MatrixClientPeg"; import { getCachedRoomIDForAlias } from "../RoomAliasCache"; import { Action } from "../dispatcher/actions"; +import { OverwriteLoginPayload } from "../dispatcher/payloads/OverwriteLoginPayload"; /** * Glue between the `ModuleApi` interface and the react-sdk. Anticipates one instance @@ -136,10 +136,13 @@ export class ProxiedModuleApi implements ModuleApi { * @override */ public async overwriteAccountAuth(accountInfo: AccountAuthInfo): Promise { - await doSetLoggedIn({ - ...accountInfo, - guest: false, - }, true); + dispatcher.dispatch({ + action: Action.OverwriteLogin, + credentials: { + ...accountInfo, + guest: false, + }, + }, true); // require to be sync to match inherited interface behaviour } /**