CI / Build & Quality Checks (pull_request) Successful in 3m23s
CI / Trigger Desktop Build (pull_request) Skipped
CI / Docker image build & smoke test (pull_request) Skipped
CI / Secret scan (gitleaks) (pull_request) Successful in 17s
CI / Playwright smoke (e2e) (pull_request) Successful in 18m6s
- Developer tools (send event, state event editor, room account data) and setMarkedUnread go through the typed helpers in utils/room and utils/accountData (new setRoomAccountData) instead of casting at the call site; sendStateEvent accepts a developer-chosen type string. - SecretStorage passes the key description as the SDK's SecretStorageKeyDescription, CallEmbed replies with an IWidgetApiRequest, and fillMissingPowers writes through a Record instead of `as any`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017PCb59uY3UKqDqt7B6hzd5
116 lines
4.7 KiB
TypeScript
116 lines
4.7 KiB
TypeScript
import { atom, useSetAtom } from 'jotai';
|
|
import { MatrixClient, MatrixEvent, Room, RoomEvent, RoomEventHandlerMap } from 'matrix-js-sdk';
|
|
import { useEffect } from 'react';
|
|
import { AccountDataEvent } from '../../../types/matrix/accountData';
|
|
import { setRoomAccountData } from '../../utils/accountData';
|
|
|
|
// MSC2867 — "mark a room as unread". A per-room account-data flag `{ unread }`.
|
|
// Stable type `m.marked_unread`; servers/clients predating the stabilization use
|
|
// the unstable `com.famedly.marked_unread`. We read either and write both so the
|
|
// flag round-trips across the ecosystem.
|
|
const UNSTABLE_MARKED_UNREAD = 'com.famedly.marked_unread';
|
|
|
|
export const readMarkedUnread = (room: Room): boolean => {
|
|
const stable = room.getAccountData(AccountDataEvent.MarkedUnread)?.getContent()?.unread;
|
|
if (typeof stable === 'boolean') return stable;
|
|
return room.getAccountData(UNSTABLE_MARKED_UNREAD)?.getContent()?.unread === true;
|
|
};
|
|
|
|
/** Set of room ids the user has explicitly marked as unread. */
|
|
export const markedUnreadAtom = atom<Set<string>>(new Set<string>());
|
|
|
|
/** Write (or clear) the marked-unread flag on both the stable + unstable keys. */
|
|
export const setMarkedUnread = (
|
|
mx: MatrixClient,
|
|
roomId: string,
|
|
unread: boolean,
|
|
): Promise<unknown> =>
|
|
Promise.all([
|
|
setRoomAccountData(mx, roomId, AccountDataEvent.MarkedUnread, { unread }),
|
|
// Best-effort mirror for older servers; never fail the primary write on it.
|
|
setRoomAccountData(mx, roomId, UNSTABLE_MARKED_UNREAD, { unread }).catch(() => undefined),
|
|
]);
|
|
|
|
export const receiptIsMine = (event: MatrixEvent, userId: string): boolean => {
|
|
const content = event.getContent();
|
|
return Object.keys(content).some((eventId) =>
|
|
Object.keys(content[eventId] ?? {}).some(
|
|
(receiptType) => content[eventId][receiptType]?.[userId],
|
|
),
|
|
);
|
|
};
|
|
|
|
// True only when OUR receipt in this event is for the main timeline — either
|
|
// unthreaded (no thread_id) or thread_id "main". A receipt scoped to a specific
|
|
// thread (thread_id === <threadRootId>) must NOT clear the whole-room marked
|
|
// flag, since only that one thread was read.
|
|
export const myMainReceiptPresent = (event: MatrixEvent, userId: string): boolean => {
|
|
const content = event.getContent();
|
|
return Object.keys(content).some((eventId) =>
|
|
Object.keys(content[eventId] ?? {}).some((receiptType) => {
|
|
const receipt = content[eventId][receiptType]?.[userId];
|
|
if (!receipt) return false;
|
|
const threadId = (receipt as { thread_id?: string }).thread_id;
|
|
return threadId === undefined || threadId === 'main';
|
|
}),
|
|
);
|
|
};
|
|
|
|
export const useBindMarkedUnreadAtom = (mx: MatrixClient, anAtom: typeof markedUnreadAtom) => {
|
|
const setAtom = useSetAtom(anAtom);
|
|
|
|
useEffect(() => {
|
|
const seed = new Set<string>();
|
|
mx.getRooms().forEach((room) => {
|
|
if (readMarkedUnread(room)) seed.add(room.roomId);
|
|
});
|
|
setAtom(seed);
|
|
|
|
const syncRoom = (room: Room) => {
|
|
const marked = readMarkedUnread(room);
|
|
setAtom((prev) => {
|
|
if (marked === prev.has(room.roomId)) return prev;
|
|
const next = new Set(prev);
|
|
if (marked) next.add(room.roomId);
|
|
else next.delete(room.roomId);
|
|
return next;
|
|
});
|
|
};
|
|
|
|
const onAccountData: RoomEventHandlerMap[RoomEvent.AccountData] = (_event, room) => {
|
|
syncRoom(room);
|
|
};
|
|
// Reading a room clears its marked-unread flag (MSC2867): when our own
|
|
// MAIN-timeline read receipt lands for a room that's currently marked, clear
|
|
// it. Gated to main/unthreaded receipts so reading a single thread doesn't
|
|
// wipe the whole-room flag. (This also fires for receipts from our other
|
|
// devices; the local read path clears via markAsRead in notifications.ts.)
|
|
const onReceipt: RoomEventHandlerMap[RoomEvent.Receipt] = (event, room) => {
|
|
const myId = mx.getUserId();
|
|
if (!myId || !readMarkedUnread(room)) return;
|
|
if (myMainReceiptPresent(event, myId)) {
|
|
setMarkedUnread(mx, room.roomId, false).catch(() => undefined);
|
|
}
|
|
};
|
|
const onMembership: RoomEventHandlerMap[RoomEvent.MyMembership] = (room) => {
|
|
if (room.getMyMembership() !== 'join') {
|
|
setAtom((prev) => {
|
|
if (!prev.has(room.roomId)) return prev;
|
|
const next = new Set(prev);
|
|
next.delete(room.roomId);
|
|
return next;
|
|
});
|
|
}
|
|
};
|
|
|
|
mx.on(RoomEvent.AccountData, onAccountData);
|
|
mx.on(RoomEvent.Receipt, onReceipt);
|
|
mx.on(RoomEvent.MyMembership, onMembership);
|
|
return () => {
|
|
mx.removeListener(RoomEvent.AccountData, onAccountData);
|
|
mx.removeListener(RoomEvent.Receipt, onReceipt);
|
|
mx.removeListener(RoomEvent.MyMembership, onMembership);
|
|
};
|
|
}, [mx, setAtom]);
|
|
};
|