From 40d2c9bf2b6392d36939eb73436facc81bf7befb Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 17 Dec 2024 13:58:27 +0000 Subject: [PATCH] Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../get-openid-token.spec.ts | 3 +- .../e2e/integration-manager/kick.spec.ts | 3 +- .../integration-manager/read_events.spec.ts | 3 +- .../integration-manager/send_event.spec.ts | 3 +- playwright/e2e/room/room.spec.ts | 4 +-- playwright/e2e/widgets/stickers.spec.ts | 3 +- playwright/pages/client.ts | 6 +++- src/@types/matrix-js-sdk.d.ts | 11 ++++--- src/stores/WidgetStore.ts | 10 ++---- src/utils/WidgetUtils-types.ts | 32 +++++++++++++++++++ src/utils/WidgetUtils.ts | 14 ++------ src/utils/device/clientInformation.ts | 7 ++-- src/utils/device/types.ts | 13 ++++++++ 13 files changed, 75 insertions(+), 37 deletions(-) create mode 100644 src/utils/WidgetUtils-types.ts create mode 100644 src/utils/device/types.ts diff --git a/playwright/e2e/integration-manager/get-openid-token.spec.ts b/playwright/e2e/integration-manager/get-openid-token.spec.ts index 48d52d35fa..2bf00817ed 100644 --- a/playwright/e2e/integration-manager/get-openid-token.spec.ts +++ b/playwright/e2e/integration-manager/get-openid-token.spec.ts @@ -9,6 +9,7 @@ Please see LICENSE files in the repository root for full details. import type { Page } from "@playwright/test"; import { test, expect } from "../../element-web-test"; import { openIntegrationManager } from "./utils"; +import type { UserWidget } from "../../../src/utils/WidgetUtils-types.ts"; const ROOM_NAME = "Integration Manager Test"; @@ -92,7 +93,7 @@ test.describe("Integration Manager: Get OpenID Token", () => { }, }, id: "integration-manager", - }, + } as unknown as UserWidget, }); // Succeed when checking the token is valid diff --git a/playwright/e2e/integration-manager/kick.spec.ts b/playwright/e2e/integration-manager/kick.spec.ts index 59c2703a18..9d25d04934 100644 --- a/playwright/e2e/integration-manager/kick.spec.ts +++ b/playwright/e2e/integration-manager/kick.spec.ts @@ -9,6 +9,7 @@ Please see LICENSE files in the repository root for full details. import type { Page } from "@playwright/test"; import { test, expect } from "../../element-web-test"; import { openIntegrationManager } from "./utils"; +import type { UserWidget } from "../../../src/utils/WidgetUtils-types.ts"; const ROOM_NAME = "Integration Manager Test"; const USER_DISPLAY_NAME = "Alice"; @@ -136,7 +137,7 @@ test.describe("Integration Manager: Kick", () => { }, }, id: "integration-manager", - }, + } as unknown as UserWidget, }); // Succeed when checking the token is valid diff --git a/playwright/e2e/integration-manager/read_events.spec.ts b/playwright/e2e/integration-manager/read_events.spec.ts index 791d5bd725..8fc81d766f 100644 --- a/playwright/e2e/integration-manager/read_events.spec.ts +++ b/playwright/e2e/integration-manager/read_events.spec.ts @@ -9,6 +9,7 @@ Please see LICENSE files in the repository root for full details. import type { Page } from "@playwright/test"; import { test, expect } from "../../element-web-test"; import { openIntegrationManager } from "./utils"; +import type { UserWidget } from "../../../src/utils/WidgetUtils-types.ts"; const ROOM_NAME = "Integration Manager Test"; @@ -107,7 +108,7 @@ test.describe("Integration Manager: Read Events", () => { }, }, id: "integration-manager", - }, + } as unknown as UserWidget, }); // Succeed when checking the token is valid diff --git a/playwright/e2e/integration-manager/send_event.spec.ts b/playwright/e2e/integration-manager/send_event.spec.ts index 363719d8f1..2f6e903953 100644 --- a/playwright/e2e/integration-manager/send_event.spec.ts +++ b/playwright/e2e/integration-manager/send_event.spec.ts @@ -9,6 +9,7 @@ Please see LICENSE files in the repository root for full details. import type { Page } from "@playwright/test"; import { test, expect } from "../../element-web-test"; import { openIntegrationManager } from "./utils"; +import type { UserWidget } from "../../../src/utils/WidgetUtils-types.ts"; const ROOM_NAME = "Integration Manager Test"; @@ -113,7 +114,7 @@ test.describe("Integration Manager: Send Event", () => { }, }, id: "integration-manager", - }, + } as unknown as UserWidget, }); // Succeed when checking the token is valid diff --git a/playwright/e2e/room/room.spec.ts b/playwright/e2e/room/room.spec.ts index 76fa64a648..cd8a3ff793 100644 --- a/playwright/e2e/room/room.spec.ts +++ b/playwright/e2e/room/room.spec.ts @@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only Please see LICENSE files in the repository root for full details. */ -import type { EventType } from "matrix-js-sdk/src/matrix"; +import type { AccountDataEvents } from "matrix-js-sdk/src/matrix"; import { test, expect } from "../../element-web-test"; import { Bot } from "../../pages/bot"; @@ -28,7 +28,7 @@ test.describe("Room Directory", () => { const charlieRoom = await cli.createRoom({ is_direct: true }); await cli.invite(bobRoom.room_id, bob); await cli.invite(charlieRoom.room_id, charlie); - await cli.setAccountData("m.direct" as EventType, { + await cli.setAccountData("m.direct" as keyof AccountDataEvents, { [bob]: [bobRoom.room_id], [charlie]: [charlieRoom.room_id], }); diff --git a/playwright/e2e/widgets/stickers.spec.ts b/playwright/e2e/widgets/stickers.spec.ts index 318f712961..418e104037 100644 --- a/playwright/e2e/widgets/stickers.spec.ts +++ b/playwright/e2e/widgets/stickers.spec.ts @@ -12,6 +12,7 @@ import type { Page } from "@playwright/test"; import { test, expect } from "../../element-web-test"; import { ElementAppPage } from "../../pages/ElementAppPage"; import { Credentials } from "../../plugins/homeserver"; +import type { UserWidget } from "../../../src/utils/WidgetUtils-types.ts"; const STICKER_PICKER_WIDGET_ID = "fake-sticker-picker"; const STICKER_PICKER_WIDGET_NAME = "Fake Stickers"; @@ -123,7 +124,7 @@ async function setWidgetAccountData( state_key: STICKER_PICKER_WIDGET_ID, type: "m.widget", id: STICKER_PICKER_WIDGET_ID, - }, + } as unknown as UserWidget, }); } diff --git a/playwright/pages/client.ts b/playwright/pages/client.ts index 2dfe7484f5..7437ff967d 100644 --- a/playwright/pages/client.ts +++ b/playwright/pages/client.ts @@ -25,6 +25,7 @@ import type { Upload, StateEvents, TimelineEvents, + AccountDataEvents, } from "matrix-js-sdk/src/matrix"; import type { RoomMessageEventContent } from "matrix-js-sdk/src/types"; import { Credentials } from "../plugins/homeserver"; @@ -439,7 +440,10 @@ export class Client { * @param type The type of account data to set * @param content The content to set */ - public async setAccountData(type: string, content: IContent): Promise { + public async setAccountData( + type: T, + content: AccountDataEvents[T], + ): Promise { const client = await this.prepareClient(); return client.evaluate( async (client, { type, content }) => { diff --git a/src/@types/matrix-js-sdk.d.ts b/src/@types/matrix-js-sdk.d.ts index 8c42b989fb..b431de094a 100644 --- a/src/@types/matrix-js-sdk.d.ts +++ b/src/@types/matrix-js-sdk.d.ts @@ -11,9 +11,8 @@ import type { BLURHASH_FIELD } from "../utils/image-media"; import type { JitsiCallMemberEventType, JitsiCallMemberContent } from "../call-types"; import type { ILayoutStateEvent, WIDGET_LAYOUT_EVENT_TYPE } from "../stores/widgets/types"; import type { EncryptedFile } from "matrix-js-sdk/src/types"; -import { PosthogAnalytics } from "../PosthogAnalytics.ts"; -import { DeviceClientInformation } from "../utils/device/clientInformation.ts"; -import { UserWidget } from "../utils/WidgetUtils.ts"; +import type { DeviceClientInformation } from "../utils/device/types.ts"; +import type { UserWidget } from "../utils/WidgetUtils-types.ts"; // Extend Matrix JS SDK types via Typescript declaration merging to support unspecced event fields and types declare module "matrix-js-sdk/src/types" { @@ -61,16 +60,20 @@ declare module "matrix-js-sdk/src/types" { } export interface AccountDataEvents { - [PosthogAnalytics.ANALYTICS_EVENT_TYPE]: { + // Analytics account data event + "im.vector.analytics": { id: string; pseudonymousAnalyticsOptIn?: boolean; }; + // Device client information account data event [key: `io.element.matrix_client_information.${string}`]: DeviceClientInformation; + // Element settings account data events "im.vector.setting.breadcrumbs": { recent_rooms: string[] }; "io.element.recent_emoji": { recent_emoji: string[] }; "im.vector.setting.integration_provisioning": { enabled: boolean }; "im.vector.web.settings": Record; + // URL preview account data event "org.matrix.preview_urls": { disable: boolean }; // This is not yet in the Matrix spec yet is being used as if it was diff --git a/src/stores/WidgetStore.ts b/src/stores/WidgetStore.ts index cfb92360a0..62aac7a429 100644 --- a/src/stores/WidgetStore.ts +++ b/src/stores/WidgetStore.ts @@ -17,17 +17,11 @@ import WidgetEchoStore from "../stores/WidgetEchoStore"; import ActiveWidgetStore from "../stores/ActiveWidgetStore"; import WidgetUtils from "../utils/WidgetUtils"; import { UPDATE_EVENT } from "./AsyncStore"; +import { IApp } from "../utils/WidgetUtils-types"; interface IState {} -export interface IApp extends IWidget { - "roomId": string; - "eventId"?: string; // not present on virtual widgets - // eslint-disable-next-line camelcase - "avatar_url"?: string; // MSC2765 https://github.com/matrix-org/matrix-doc/pull/2765 - // Whether the widget was created from `widget_build_url` and thus is a call widget of some kind - "io.element.managed_hybrid"?: boolean; -} +export type { IApp }; export function isAppWidget(widget: IWidget | IApp): widget is IApp { return "roomId" in widget && typeof widget.roomId === "string"; diff --git a/src/utils/WidgetUtils-types.ts b/src/utils/WidgetUtils-types.ts new file mode 100644 index 0000000000..56515eeddb --- /dev/null +++ b/src/utils/WidgetUtils-types.ts @@ -0,0 +1,32 @@ +/* +Copyright 2024 New Vector Ltd. +Copyright 2017-2020 The Matrix.org Foundation C.I.C. +Copyright 2019 Travis Ralston + +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only +Please see LICENSE files in the repository root for full details. +*/ + +import { IWidget } from "matrix-widget-api"; + +export interface IApp extends IWidget { + "roomId": string; + "eventId"?: string; // not present on virtual widgets + // eslint-disable-next-line camelcase + "avatar_url"?: string; // MSC2765 https://github.com/matrix-org/matrix-doc/pull/2765 + // Whether the widget was created from `widget_build_url` and thus is a call widget of some kind + "io.element.managed_hybrid"?: boolean; +} + +export interface IWidgetEvent { + id: string; + type: string; + sender: string; + // eslint-disable-next-line camelcase + state_key: string; + content: IApp; +} + +export interface UserWidget extends Omit { + content: IWidget & Partial; +} diff --git a/src/utils/WidgetUtils.ts b/src/utils/WidgetUtils.ts index ad2ed63ba1..e82b8b632d 100644 --- a/src/utils/WidgetUtils.ts +++ b/src/utils/WidgetUtils.ts @@ -29,23 +29,13 @@ import WidgetStore, { IApp, isAppWidget } from "../stores/WidgetStore"; import { parseUrl } from "./UrlUtils"; import { useEventEmitter } from "../hooks/useEventEmitter"; import { WidgetLayoutStore } from "../stores/widgets/WidgetLayoutStore"; +import { IWidgetEvent, UserWidget } from "./WidgetUtils-types"; // How long we wait for the state event echo to come back from the server // before waitFor[Room/User]Widget rejects its promise const WIDGET_WAIT_TIME = 20000; -export interface IWidgetEvent { - id: string; - type: string; - sender: string; - // eslint-disable-next-line camelcase - state_key: string; - content: IApp; -} - -export interface UserWidget extends Omit { - content: IWidget & Partial; -} +export type { IWidgetEvent, UserWidget }; export default class WidgetUtils { /** diff --git a/src/utils/device/clientInformation.ts b/src/utils/device/clientInformation.ts index e88c6b2bc0..5e3f23018f 100644 --- a/src/utils/device/clientInformation.ts +++ b/src/utils/device/clientInformation.ts @@ -11,12 +11,9 @@ import { AccountDataEvents, MatrixClient } from "matrix-js-sdk/src/matrix"; import BasePlatform from "../../BasePlatform"; import { IConfigOptions } from "../../IConfigOptions"; import { DeepReadonly } from "../../@types/common"; +import { DeviceClientInformation } from "./types"; -export type DeviceClientInformation = { - name?: string; - version?: string; - url?: string; -}; +export type { DeviceClientInformation }; const formatUrl = (): string | undefined => { // don't record url for electron clients diff --git a/src/utils/device/types.ts b/src/utils/device/types.ts new file mode 100644 index 0000000000..1b652672a8 --- /dev/null +++ b/src/utils/device/types.ts @@ -0,0 +1,13 @@ +/* +Copyright 2024 New Vector Ltd. +Copyright 2022 The Matrix.org Foundation C.I.C. + +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only +Please see LICENSE files in the repository root for full details. +*/ + +export type DeviceClientInformation = { + name?: string; + version?: string; + url?: string; +};