Use mapped types around account data events
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
28
src/@types/matrix-js-sdk.d.ts
vendored
28
src/@types/matrix-js-sdk.d.ts
vendored
@@ -11,6 +11,10 @@ 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 { string } from "css-tree";
|
||||
import { DeviceClientInformation } from "../utils/device/clientInformation.ts";
|
||||
import { UserWidget } from "../utils/WidgetUtils.ts";
|
||||
|
||||
// Extend Matrix JS SDK types via Typescript declaration merging to support unspecced event fields and types
|
||||
declare module "matrix-js-sdk/src/types" {
|
||||
@@ -57,6 +61,30 @@ declare module "matrix-js-sdk/src/types" {
|
||||
};
|
||||
}
|
||||
|
||||
export interface AccountDataEvents {
|
||||
[PosthogAnalytics.ANALYTICS_EVENT_TYPE]: {
|
||||
id: string;
|
||||
pseudonymousAnalyticsOptIn?: boolean;
|
||||
};
|
||||
[key: `io.element.matrix_client_information.${string}`]: DeviceClientInformation;
|
||||
"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<string, any>;
|
||||
|
||||
"org.matrix.preview_urls": { disable: boolean };
|
||||
|
||||
// This is not yet in the Matrix spec yet is being used as if it was
|
||||
"m.widgets": {
|
||||
[widgetId: string]: UserWidget;
|
||||
};
|
||||
|
||||
// This is not in the Matrix spec yet seems to use an `m.` prefix
|
||||
"m.accepted_terms": {
|
||||
accepted: string[];
|
||||
};
|
||||
}
|
||||
|
||||
export interface AudioContent {
|
||||
// MSC1767 + Ideals of MSC2516 as MSC3245
|
||||
// https://github.com/matrix-org/matrix-doc/pull/3245
|
||||
|
||||
@@ -8,7 +8,7 @@ Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import React, { useContext, useMemo, useState } from "react";
|
||||
import { IContent, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
||||
import { AccountDataEvents, IContent, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import BaseTool, { DevtoolsContext, IDevtoolsProps } from "./BaseTool";
|
||||
import MatrixClientContext from "../../../../contexts/MatrixClientContext";
|
||||
@@ -21,7 +21,7 @@ export const AccountDataEventEditor: React.FC<IEditorProps> = ({ mxEvent, onBack
|
||||
|
||||
const fields = useMemo(() => [eventTypeField(mxEvent?.getType())], [mxEvent]);
|
||||
|
||||
const onSend = async ([eventType]: string[], content?: IContent): Promise<void> => {
|
||||
const onSend = async ([eventType]: Array<keyof AccountDataEvents>, content?: IContent): Promise<void> => {
|
||||
await cli.setAccountData(eventType, content || {});
|
||||
};
|
||||
|
||||
|
||||
@@ -7,7 +7,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 { ClientEvent, MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
||||
import { AccountDataEvents, ClientEvent, MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
||||
import { defer } from "matrix-js-sdk/src/utils";
|
||||
import { isEqual } from "lodash";
|
||||
|
||||
@@ -140,10 +140,10 @@ export default class AccountSettingsHandler extends MatrixClientBackedSettingsHa
|
||||
}
|
||||
|
||||
// helper function to set account data then await it being echoed back
|
||||
private async setAccountData(
|
||||
eventType: string,
|
||||
field: string,
|
||||
value: any,
|
||||
private async setAccountData<K extends keyof AccountDataEvents, F extends keyof AccountDataEvents[K]>(
|
||||
eventType: K,
|
||||
field: F,
|
||||
value: AccountDataEvents[K][F],
|
||||
legacyEventType?: string,
|
||||
): Promise<void> {
|
||||
let content = this.getSettings(eventType);
|
||||
@@ -161,7 +161,8 @@ export default class AccountSettingsHandler extends MatrixClientBackedSettingsHa
|
||||
// which race between different lines.
|
||||
const deferred = defer<void>();
|
||||
const handler = (event: MatrixEvent): void => {
|
||||
if (event.getType() !== eventType || !isEqual(event.getContent()[field], value)) return;
|
||||
if (event.getType() !== eventType || !isEqual(event.getContent<AccountDataEvents[K]>()[field], value))
|
||||
return;
|
||||
this.client.off(ClientEvent.AccountData, handler);
|
||||
deferred.resolve();
|
||||
};
|
||||
|
||||
@@ -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 { MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
import { AccountDataEvents, MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import BasePlatform from "../../BasePlatform";
|
||||
import { IConfigOptions } from "../../IConfigOptions";
|
||||
@@ -34,7 +34,8 @@ const formatUrl = (): string | undefined => {
|
||||
};
|
||||
|
||||
const clientInformationEventPrefix = "io.element.matrix_client_information.";
|
||||
export const getClientInformationEventType = (deviceId: string): string => `${clientInformationEventPrefix}${deviceId}`;
|
||||
export const getClientInformationEventType = (deviceId: string): `${typeof clientInformationEventPrefix}${string}` =>
|
||||
`${clientInformationEventPrefix}${deviceId}`;
|
||||
|
||||
/**
|
||||
* Record extra client information for the current device
|
||||
@@ -70,7 +71,7 @@ export const pruneClientInformation = (validDeviceIds: string[], matrixClient: M
|
||||
}
|
||||
const [, deviceId] = event.getType().split(clientInformationEventPrefix);
|
||||
if (deviceId && !validDeviceIds.includes(deviceId)) {
|
||||
matrixClient.deleteAccountData(event.getType());
|
||||
matrixClient.deleteAccountData(event.getType() as keyof AccountDataEvents);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -40,7 +40,9 @@ export const deviceNotificationSettingsKeys = [
|
||||
"audioNotificationsEnabled",
|
||||
];
|
||||
|
||||
export function getLocalNotificationAccountDataEventType(deviceId: string | null): string {
|
||||
export function getLocalNotificationAccountDataEventType(
|
||||
deviceId: string | null,
|
||||
): `${typeof LOCAL_NOTIFICATION_SETTINGS_PREFIX.name}.${string}` {
|
||||
return `${LOCAL_NOTIFICATION_SETTINGS_PREFIX.name}.${deviceId}`;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
IContent,
|
||||
MatrixEvent,
|
||||
SyncState,
|
||||
AccountDataEvents,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
import { waitFor } from "jest-matrix-react";
|
||||
import { CallMembership, MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc";
|
||||
@@ -69,7 +70,7 @@ describe("Notifier", () => {
|
||||
let MockPlatform: MockedObject<BasePlatform>;
|
||||
let mockClient: MockedObject<MatrixClient>;
|
||||
let testRoom: Room;
|
||||
let accountDataEventKey: string;
|
||||
let accountDataEventKey: keyof AccountDataEvents;
|
||||
let accountDataStore: Record<string, MatrixEvent | undefined> = {};
|
||||
|
||||
let mockSettings: Record<string, boolean> = {};
|
||||
|
||||
@@ -6,7 +6,14 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { MatrixEvent, NotificationCountType, Room, MatrixClient, ReceiptType } from "matrix-js-sdk/src/matrix";
|
||||
import {
|
||||
MatrixEvent,
|
||||
NotificationCountType,
|
||||
Room,
|
||||
MatrixClient,
|
||||
ReceiptType,
|
||||
AccountDataEvents,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
import { Mocked, mocked } from "jest-mock";
|
||||
|
||||
import {
|
||||
@@ -32,7 +39,7 @@ jest.mock("../../../src/settings/SettingsStore");
|
||||
describe("notifications", () => {
|
||||
let accountDataStore: Record<string, MatrixEvent> = {};
|
||||
let mockClient: Mocked<MatrixClient>;
|
||||
let accountDataEventKey: string;
|
||||
let accountDataEventKey: keyof AccountDataEvents;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
|
||||
Reference in New Issue
Block a user