diff --git a/src/@types/global.d.ts b/src/@types/global.d.ts index d4ae6c897be..e31e34ae0c7 100644 --- a/src/@types/global.d.ts +++ b/src/@types/global.d.ts @@ -12,6 +12,7 @@ import "@types/modernizr"; import type { ModuleLoader } from "@element-hq/element-web-module-api"; import type { logger } from "matrix-js-sdk/src/logger"; +import type { CallState } from "matrix-js-sdk/src/webrtc/call"; import type ContentMessages from "../ContentMessages"; import { type IMatrixClientPeg } from "../MatrixClientPeg"; import type ToastStore from "../stores/ToastStore"; @@ -51,7 +52,6 @@ import type { RoomListStoreV3Class } from "../stores/room-list-v3/RoomListStoreV /* eslint-disable @typescript-eslint/naming-convention */ type ElectronChannel = - | "app_onAction" | "before-quit" | "check_updates" | "install_update" @@ -133,14 +133,18 @@ declare global { send(channel: ElectronChannel, ...args: any[]): void; // Initialisation initialise(): Promise<{ + version: string; protocol: string; sessionId: string; config: IConfigOptions; supportedSettings: Record; + canSelfUpdate: boolean; }>; // Settings setSettingValue(settingName: string, value: any): Promise; getSettingValue(settingName: string): Promise; + // Lifecycle + onCallState(callState: CallState): void; } interface DesktopCapturerSource { diff --git a/src/vector/platform/ElectronPlatform.tsx b/src/vector/platform/ElectronPlatform.tsx index 2bacbe337d3..9f0e47141a1 100644 --- a/src/vector/platform/ElectronPlatform.tsx +++ b/src/vector/platform/ElectronPlatform.tsx @@ -89,10 +89,7 @@ export default class ElectronPlatform extends BasePlatform { private readonly eventIndexManager: BaseEventIndexManager = new SeshatIndexManager(); private readonly initialised: Promise; private readonly electron: Electron; - private protocol!: string; - private sessionId!: string; - private config!: IConfigOptions; - private supportedSettings?: Record; + private parameters?: Awaited>; public constructor() { super(); @@ -189,21 +186,17 @@ export default class ElectronPlatform extends BasePlatform { super.onAction(payload); // Whitelist payload actions, no point sending most across if (["call_state"].includes(payload.action)) { - this.electron.send("app_onAction", payload); + this.electron.onCallState(payload.state); } } private async initialise(): Promise { - const { protocol, sessionId, config, supportedSettings } = await this.electron.initialise(); - this.protocol = protocol; - this.sessionId = sessionId; - this.config = config; - this.supportedSettings = supportedSettings; + this.parameters = await this.electron.initialise(); } public async getConfig(): Promise { await this.initialised; - return this.config; + return this.parameters?.config; } private onBreadcrumbsUpdate = (): void => { @@ -298,12 +291,13 @@ export default class ElectronPlatform extends BasePlatform { } public async getAppVersion(): Promise { - return this.ipc.call("getAppVersion"); + await this.initialised; + return this.parameters!.version; } public supportsSetting(settingName?: string): boolean { if (settingName === undefined) return true; - return this.supportedSettings?.[settingName] === true; + return this.parameters?.supportedSettings[settingName] === true; } public getSettingValue(settingName: string): Promise { @@ -315,8 +309,8 @@ export default class ElectronPlatform extends BasePlatform { } public async canSelfUpdate(): Promise { - const feedUrl = await this.ipc.call("getUpdateFeedUrl"); - return Boolean(feedUrl); + await this.initialised; + return this.parameters!.canSelfUpdate; } public startUpdateCheck(): void { @@ -352,7 +346,7 @@ export default class ElectronPlatform extends BasePlatform { } public async setLanguage(preferredLangs: string[]): Promise { - return this.ipc.call("setLanguage", preferredLangs); + return this.electron.setSettingValue("locale", preferredLangs); } public setSpellCheckEnabled(enabled: boolean): void { @@ -397,7 +391,7 @@ export default class ElectronPlatform extends BasePlatform { public getSSOCallbackUrl(fragmentAfterLogin?: string): URL { const url = super.getSSOCallbackUrl(fragmentAfterLogin); url.protocol = "element"; - url.searchParams.set(SSO_ID_KEY, this.sessionId); + url.searchParams.set(SSO_ID_KEY, this.parameters!.sessionId); return url; } @@ -475,7 +469,7 @@ export default class ElectronPlatform extends BasePlatform { } public getOidcClientState(): string { - return `:${SSO_ID_KEY}:${this.sessionId}`; + return `:${SSO_ID_KEY}:${this.parameters!.sessionId}`; } /** @@ -483,7 +477,7 @@ export default class ElectronPlatform extends BasePlatform { */ public getOidcCallbackUrl(): URL { const url = super.getOidcCallbackUrl(); - url.protocol = this.protocol; + url.protocol = this.parameters!.protocol; // Trim the double slash into a single slash to comply with https://datatracker.ietf.org/doc/html/rfc8252#section-7.1 if (url.href.startsWith(`${url.protocol}//`)) { url.href = url.href.replace("://", ":/"); diff --git a/test/unit-tests/vector/platform/ElectronPlatform-test.ts b/test/unit-tests/vector/platform/ElectronPlatform-test.ts index 40168231ac3..598689a3017 100644 --- a/test/unit-tests/vector/platform/ElectronPlatform-test.ts +++ b/test/unit-tests/vector/platform/ElectronPlatform-test.ts @@ -40,6 +40,7 @@ describe("ElectronPlatform", () => { }), setSettingValue: jest.fn().mockResolvedValue(undefined), getSettingValue: jest.fn().mockResolvedValue(undefined), + onCallState: jest.fn(), } as unknown as MockedObject; const dispatchSpy = jest.spyOn(dispatcher, "dispatch"); @@ -399,10 +400,7 @@ describe("ElectronPlatform", () => { true, ); - const ipcMessage = mockElectron.send.mock.calls.find((call) => call[0] === "app_onAction"); - expect(ipcMessage![1]).toEqual({ - action: "call_state", - state: "connected", - }); + const ipcMessage = mockElectron.onCallState.mock.calls[0]; + expect(ipcMessage![0]).toEqual("connected"); }); });