From 49a43fc1660bdbd14145966a36a208c22d8be8af Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 19 Dec 2024 14:08:32 +0000 Subject: [PATCH] Harden types for reading account data too Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- spec/integ/matrix-client-syncing.spec.ts | 7 +++++++ spec/integ/sliding-sync-sdk.spec.ts | 7 +++++++ spec/unit/rust-crypto/rust-crypto.spec.ts | 7 +++++-- spec/unit/secret-storage.spec.ts | 21 ++++++++++++++------- src/@types/event.ts | 11 +++++++++-- src/client.ts | 10 ++++++---- src/models/invites-ignorer-types.ts | 13 +++++++++++++ src/models/invites-ignorer.ts | 17 +++++++++++------ 8 files changed, 72 insertions(+), 21 deletions(-) diff --git a/spec/integ/matrix-client-syncing.spec.ts b/spec/integ/matrix-client-syncing.spec.ts index 2c0ab315bed..e8b9d6e52f2 100644 --- a/spec/integ/matrix-client-syncing.spec.ts +++ b/spec/integ/matrix-client-syncing.spec.ts @@ -50,6 +50,13 @@ import { THREAD_RELATION_TYPE } from "../../src/models/thread"; import { IActionsObject } from "../../src/pushprocessor"; import { KnownMembership } from "../../src/@types/membership"; +declare module "../../src/@types/event" { + interface AccountDataEvents { + a: {}; + b: {}; + } +} + describe("MatrixClient syncing", () => { const selfUserId = "@alice:localhost"; const selfAccessToken = "aseukfgwef"; diff --git a/spec/integ/sliding-sync-sdk.spec.ts b/spec/integ/sliding-sync-sdk.spec.ts index 7b080351a6f..643f4f7e1ba 100644 --- a/spec/integ/sliding-sync-sdk.spec.ts +++ b/spec/integ/sliding-sync-sdk.spec.ts @@ -45,6 +45,13 @@ import { emitPromise } from "../test-utils/test-utils"; import { defer } from "../../src/utils"; import { KnownMembership } from "../../src/@types/membership"; +declare module "../../src/@types/event" { + interface AccountDataEvents { + global_test: {}; + tester: {}; + } +} + describe("SlidingSyncSdk", () => { let client: MatrixClient | undefined; let httpBackend: MockHttpBackend | undefined; diff --git a/spec/unit/rust-crypto/rust-crypto.spec.ts b/spec/unit/rust-crypto/rust-crypto.spec.ts index 8e177097bfe..1b56dfccbc3 100644 --- a/spec/unit/rust-crypto/rust-crypto.spec.ts +++ b/spec/unit/rust-crypto/rust-crypto.spec.ts @@ -30,6 +30,7 @@ import fetchMock from "fetch-mock-jest"; import { RustCrypto } from "../../../src/rust-crypto/rust-crypto"; import { initRustCrypto } from "../../../src/rust-crypto"; import { + AccountDataEvents, Device, DeviceVerification, encodeBase64, @@ -1924,11 +1925,13 @@ class DummyAccountDataClient super(); } - public async getAccountDataFromServer>(eventType: string): Promise { + public async getAccountDataFromServer( + eventType: K, + ): Promise { const ret = this.storage.get(eventType); if (eventType) { - return ret as T; + return ret; } else { return null; } diff --git a/spec/unit/secret-storage.spec.ts b/spec/unit/secret-storage.spec.ts index 60c5fb19839..b1afcb1a575 100644 --- a/spec/unit/secret-storage.spec.ts +++ b/spec/unit/secret-storage.spec.ts @@ -28,6 +28,7 @@ import { } from "../../src/secret-storage"; import { randomString } from "../../src/randomstring"; import { SecretInfo } from "../../src/secret-storage.ts"; +import { AccountDataEvents } from "../../src"; declare module "../../src/@types/event" { interface AccountDataEvents { @@ -124,9 +125,11 @@ describe("ServerSideSecretStorageImpl", function () { const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {}); const storedKey = { iv: "iv", mac: "mac" } as SecretStorageKeyDescriptionAesV1; - async function mockGetAccountData>(eventType: string): Promise { + async function mockGetAccountData( + eventType: string, + ): Promise { if (eventType === "m.secret_storage.key.my_key") { - return storedKey as unknown as T; + return storedKey as any; } else { throw new Error(`unexpected eventType ${eventType}`); } @@ -142,11 +145,13 @@ describe("ServerSideSecretStorageImpl", function () { const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {}); const storedKey = { iv: "iv", mac: "mac" } as SecretStorageKeyDescriptionAesV1; - async function mockGetAccountData>(eventType: string): Promise { + async function mockGetAccountData( + eventType: string, + ): Promise { if (eventType === "m.secret_storage.default_key") { - return { key: "default_key_id" } as unknown as T; + return { key: "default_key_id" } as any; } else if (eventType === "m.secret_storage.key.default_key_id") { - return storedKey as unknown as T; + return storedKey as any; } else { throw new Error(`unexpected eventType ${eventType}`); } @@ -243,9 +248,11 @@ describe("ServerSideSecretStorageImpl", function () { // stub out getAccountData to return a key with an unknown algorithm const storedKey = { algorithm: "badalg" } as SecretStorageKeyDescriptionCommon; - async function mockGetAccountData>(eventType: string): Promise { + async function mockGetAccountData( + eventType: string, + ): Promise { if (eventType === "m.secret_storage.key.keyid") { - return storedKey as unknown as T; + return storedKey as any; } else { throw new Error(`unexpected eventType ${eventType}`); } diff --git a/src/@types/event.ts b/src/@types/event.ts index 9dd7c142ad5..12dbefb6f58 100644 --- a/src/@types/event.ts +++ b/src/@types/event.ts @@ -61,7 +61,11 @@ import { SessionMembershipData } from "../matrixrtc/CallMembership.ts"; import { LocalNotificationSettings } from "./local_notifications.ts"; import { IPushRules } from "./PushRules.ts"; import { SecretInfo, SecretStorageKeyDescription } from "../secret-storage.ts"; -import { POLICIES_ACCOUNT_EVENT_TYPE } from "../models/invites-ignorer-types.ts"; +import { + IgnoreInvitesContent, + POLICIES_ACCOUNT_EVENT_TYPE, + UnstableIgnoreInvitesContent, +} from "../models/invites-ignorer-types.ts"; export enum EventType { // Room state events @@ -381,7 +385,6 @@ export interface AccountDataEvents { [EventType.Direct]: { [userId: string]: string[] }; [EventType.IgnoredUserList]: { [userId: string]: {} }; "m.secret_storage.default_key": { key: string }; - [POLICIES_ACCOUNT_EVENT_TYPE.name]: { [key: string]: any }; "m.identity_server": { base_url: string | null }; [key: `${typeof LOCAL_NOTIFICATION_SETTINGS_PREFIX.name}.${string}`]: LocalNotificationSettings; [key: `m.secret_storage.key.${string}`]: SecretStorageKeyDescription; @@ -390,4 +393,8 @@ export interface AccountDataEvents { "m.cross_signing.self_signing": SecretInfo; "m.cross_signing.user_signing": SecretInfo; "org.matrix.msc3814": SecretInfo; + + // Invites-ignorer events + [POLICIES_ACCOUNT_EVENT_TYPE.name]: UnstableIgnoreInvitesContent; + [POLICIES_ACCOUNT_EVENT_TYPE.altName]: IgnoreInvitesContent; } diff --git a/src/client.ts b/src/client.ts index d09ecb0292a..b30b071ba46 100644 --- a/src/client.ts +++ b/src/client.ts @@ -4256,7 +4256,7 @@ export class MatrixClient extends TypedEventEmitter(eventType: K): MatrixEvent | undefined { return this.store.getAccountData(eventType); } @@ -4268,7 +4268,9 @@ export class MatrixClient extends TypedEventEmitter(eventType: string): Promise { + public async getAccountDataFromServer( + eventType: K, + ): Promise { if (this.isInitialSyncComplete()) { const event = this.store.getAccountData(eventType); if (!event) { @@ -4276,7 +4278,7 @@ export class MatrixClient extends TypedEventEmitter(); + return event.getContent(); } const path = utils.encodeUri("/user/$userId/account_data/$type", { $userId: this.credentials.userId!, @@ -4315,7 +4317,7 @@ export class MatrixClient extends TypedEventEmitter; + ignoreInvitesPolicies: Policies; } { - let policies: IContent = {}; + let policies: Partial = {}; for (const key of [POLICIES_ACCOUNT_EVENT_TYPE.name, POLICIES_ACCOUNT_EVENT_TYPE.altName]) { if (!key) { continue; } - const value = this.client.getAccountData(key)?.getContent(); + const value = this.client + .getAccountData(key) + ?.getContent(); if (value) { policies = value; break; } } - let ignoreInvitesPolicies = {}; + let ignoreInvitesPolicies: Policies = {}; let hasIgnoreInvitesPolicies = false; for (const key of [IGNORE_INVITES_ACCOUNT_EVENT_KEY.name, IGNORE_INVITES_ACCOUNT_EVENT_KEY.altName]) { if (!key) {