DP16: add typed get/setAccountData helpers, remove ~19 any-casts
Add centralized typed helpers in src/app/utils/accountData.ts: - getAccountData<T>(mx, eventType): T | undefined (returns content) - setAccountData<T>(mx, eventType, content): Promise<void> These wrap the single `as any` cast needed because matrix-js-sdk's typed overloads reject the fork's custom account-data event names. Every call site now stays fully typed on its content shape. Route all account-data reads/writes that previously used `(mx as any).getAccountData/setAccountData` or `mx.getAccountData(... as any)` through the helpers (or, where a MatrixEvent is needed, through the existing utils/room.ts getAccountData whose param is widened to accept string keys). No behavior change: same event types, same content shapes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { MatrixClient, MatrixEvent } from 'matrix-js-sdk';
|
||||
import { AccountDataEvent } from '../../types/matrix/accountData';
|
||||
|
||||
// Typed global account-data read/write helpers.
|
||||
//
|
||||
// matrix-js-sdk's `getAccountData`/`setAccountData` overloads only accept the
|
||||
// SDK's built-in AccountDataEvents union and reject the fork's custom event
|
||||
// names (the `io.lotus.*` / `in.cinny.*` values in the AccountDataEvent enum, as
|
||||
// well as a few dynamic keys used by developer tools). We centralize the single
|
||||
// `as any` cast here so every call site stays fully typed on its content shape.
|
||||
|
||||
export function getAccountData<T>(
|
||||
mx: MatrixClient,
|
||||
eventType: AccountDataEvent | string,
|
||||
): T | undefined {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const event = (mx as any).getAccountData(eventType) as MatrixEvent | undefined;
|
||||
return event?.getContent() as T | undefined;
|
||||
}
|
||||
|
||||
export function setAccountData<T>(
|
||||
mx: MatrixClient,
|
||||
eventType: AccountDataEvent | string,
|
||||
content: T,
|
||||
): Promise<void> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return (mx as any).setAccountData(eventType, content);
|
||||
}
|
||||
@@ -45,7 +45,7 @@ export const getStateEvents = (room: Room, eventType: StateEvent): MatrixEvent[]
|
||||
|
||||
export const getAccountData = (
|
||||
mx: MatrixClient,
|
||||
eventType: AccountDataEvent,
|
||||
eventType: AccountDataEvent | string,
|
||||
): MatrixEvent | undefined => mx.getAccountData(eventType as any);
|
||||
|
||||
export const getMDirects = (mDirectEvent: MatrixEvent): Set<string> => {
|
||||
|
||||
Reference in New Issue
Block a user