Files
cinny/src/app/utils/markThreadsRead.ts
T

30 lines
1.1 KiB
TypeScript
Raw Normal View History

import { MatrixClient, NotificationCountType, ReceiptType, Room } from 'matrix-js-sdk';
/**
* [Gitea #165] Send a threaded read receipt for every thread in the room that
* has unread replies. Mirrors the per-thread half of `markAsRead`, including
* its rule: never fall back to the thread root (a root lives in the main
* timeline, so a receipt there would move the MAIN read marker backwards).
* Returns how many threads were marked.
*/
export async function markAllThreadsRead(
mx: MatrixClient,
room: Room,
privateReceipt: boolean,
): Promise<number> {
const receiptType = privateReceipt ? ReceiptType.ReadPrivate : ReceiptType.Read;
const sends = room
.getThreads()
.map((thread) => {
const unread =
room.getThreadUnreadNotificationCount(thread.id, NotificationCountType.Total) ?? 0;
if (unread <= 0) return undefined;
const lastReply = thread.lastReply();
if (!lastReply || lastReply.isSending()) return undefined;
return mx.sendReadReceipt(lastReply, receiptType, false).catch(() => undefined);
})
.filter((p): p is NonNullable<typeof p> => !!p);
await Promise.all(sends);
return sends.length;
}