From b9b0b096a46c4eff8d5cf662cddefefc45131288 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 22 Mar 2023 09:22:51 +0000 Subject: [PATCH] Allow desktop app to expose recent rooms in UI integrations (#16940) --- src/vector/platform/ElectronPlatform.tsx | 28 ++++++++++++++++++- .../vector/platform/ElectronPlatform-test.ts | 18 +++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/vector/platform/ElectronPlatform.tsx b/src/vector/platform/ElectronPlatform.tsx index 2e181a5111..537ce4cc04 100644 --- a/src/vector/platform/ElectronPlatform.tsx +++ b/src/vector/platform/ElectronPlatform.tsx @@ -40,11 +40,21 @@ import ToastStore from "matrix-react-sdk/src/stores/ToastStore"; import GenericExpiringToast from "matrix-react-sdk/src/components/views/toasts/GenericExpiringToast"; import { logger } from "matrix-js-sdk/src/logger"; import { MatrixEvent } from "matrix-js-sdk/src/models/event"; +import { BreadcrumbsStore } from "matrix-react-sdk/src/stores/BreadcrumbsStore"; +import { UPDATE_EVENT } from "matrix-react-sdk/src/stores/AsyncStore"; +import { avatarUrlForRoom, getInitialLetter } from "matrix-react-sdk/src/Avatar"; import VectorBasePlatform from "./VectorBasePlatform"; import { SeshatIndexManager } from "./SeshatIndexManager"; import { IPCManager } from "./IPCManager"; +interface SquirrelUpdate { + releaseNotes: string; + releaseName: string; + releaseDate: Date; + updateURL: string; +} + const isMac = navigator.platform.toUpperCase().includes("MAC"); function platformFriendlyName(): string { @@ -150,13 +160,29 @@ export default class ElectronPlatform extends VectorBasePlatform { }); this.ipc.call("startSSOFlow", this.ssoID); + + BreadcrumbsStore.instance.on(UPDATE_EVENT, this.onBreadcrumbsUpdate); } public async getConfig(): Promise { return this.ipc.call("getConfig"); } - private onUpdateDownloaded = async (ev, { releaseNotes, releaseName }): Promise => { + private onBreadcrumbsUpdate = (): void => { + const rooms = BreadcrumbsStore.instance.rooms.slice(0, 7).map((r) => ({ + roomId: r.roomId, + avatarUrl: avatarUrlForRoom( + r, + Math.floor(60 * window.devicePixelRatio), + Math.floor(60 * window.devicePixelRatio), + "crop", + ), + initial: getInitialLetter(r.name), + })); + this.ipc.call("breadcrumbs", rooms); + }; + + private onUpdateDownloaded = async (ev: Event, { releaseNotes, releaseName }: SquirrelUpdate): Promise => { dis.dispatch({ action: Action.CheckUpdates, status: UpdateCheckStatus.Ready, diff --git a/test/unit-tests/vector/platform/ElectronPlatform-test.ts b/test/unit-tests/vector/platform/ElectronPlatform-test.ts index c1a4e4ce5c..24de2e180d 100644 --- a/test/unit-tests/vector/platform/ElectronPlatform-test.ts +++ b/test/unit-tests/vector/platform/ElectronPlatform-test.ts @@ -20,6 +20,7 @@ import { UpdateCheckStatus } from "matrix-react-sdk/src/BasePlatform"; import { Action } from "matrix-react-sdk/src/dispatcher/actions"; import dispatcher from "matrix-react-sdk/src/dispatcher/dispatcher"; import * as rageshake from "matrix-react-sdk/src/rageshake/rageshake"; +import { BreadcrumbsStore } from "matrix-react-sdk/src/stores/BreadcrumbsStore"; import ElectronPlatform from "../../../../src/vector/platform/ElectronPlatform"; @@ -43,7 +44,6 @@ describe("ElectronPlatform", () => { const userId = "@alice:server.org"; const deviceId = "device-id"; - window.electron = mockElectron; beforeEach(() => { window.electron = mockElectron; jest.clearAllMocks(); @@ -260,4 +260,20 @@ describe("ElectronPlatform", () => { expect(mockElectron.send).toHaveBeenCalledWith("install_update"); }); }); + + describe("breacrumbs", () => { + it("should send breadcrumb updates over the IPC", () => { + const spy = jest.spyOn(BreadcrumbsStore.instance, "on"); + new ElectronPlatform(); + const cb = spy.mock.calls[0][1]; + cb(); + + expect(mockElectron.send).toHaveBeenCalledWith( + "ipcCall", + expect.objectContaining({ + name: "breadcrumbs", + }), + ); + }); + }); });