From 6f4f9c74b4abbdc8a9bca1ce98129763df0fdc03 Mon Sep 17 00:00:00 2001 From: Ajay Bura <32841439+ajbura@users.noreply.github.com> Date: Wed, 28 Oct 2026 18:27:08 +0530 Subject: [PATCH] add read marker class --- src/client/ReadMarker.ts | 69 ++++++++++++++++++++++++++++++++++++++++ src/client/context.ts | 2 ++ src/client/initialize.ts | 3 ++ 3 files changed, 74 insertions(+) create mode 100644 src/client/ReadMarker.ts diff --git a/src/client/ReadMarker.ts b/src/client/ReadMarker.ts new file mode 100644 index 00000000..f8af6019 --- /dev/null +++ b/src/client/ReadMarker.ts @@ -0,0 +1,69 @@ +import { MatrixRequest, parseMatrixResp } from 'neurone/lib/matrix-request'; +import { ReceiptType } from 'neurone/lib/api/types'; +import { postReceiptEventId, PostReceiptEventIdOK } from 'neurone/lib/api/matrix/client/v3/rooms'; +import { AsyncTaskQueue, MatrixRequestRetrier, sleep } from 'neurone/lib/utils'; +import { Storage } from './storage'; +import { getReadReceiptId, ReadReceiptObject } from './storage/collections'; +import { ThreadId } from '../types/matrix/room'; + +export class ReadMarker { + private readonly queue = new AsyncTaskQueue(); + + constructor( + private readonly storage: Storage, + private readonly request: MatrixRequest, + private readonly userId: string + ) {} + + public async markAsRead( + roomId: string, + receiptType: ReceiptType, + eventId: string, + threadId?: string + ): Promise { + await this.queue.enqueue(() => this.read(roomId, receiptType, eventId, threadId)); + } + + private async read( + roomId: string, + receiptType: ReceiptType, + eventId: string, + threadId?: string + ): Promise { + const retrier = new MatrixRequestRetrier(sleep); + + const endpoint = postReceiptEventId({ + pathParams: { roomId, receiptType, eventId }, + body: { + thread_id: threadId, + }, + }); + + const response = await retrier.execute(() => this.request(endpoint)); + const [err] = await parseMatrixResp(response); + if (err) { + throw err.data.error ?? err.data.errcode ?? 'Unknown Error'; + } + await this.updateStore(roomId, receiptType, eventId, threadId); + } + + private async updateStore( + roomId: string, + receiptType: ReceiptType, + eventId: string, + threadId?: string + ) { + const id = getReadReceiptId(roomId, this.userId, threadId ?? ThreadId.Main); + const receipt: ReadReceiptObject = { + id, + roomId, + userId: this.userId, + type: receiptType, + eventId, + ts: Date.now(), + threadId: threadId ?? ThreadId.Main, + }; + + await this.storage.readReceipt.write.put(receipt); + } +} diff --git a/src/client/context.ts b/src/client/context.ts index f127f09b..14c63543 100644 --- a/src/client/context.ts +++ b/src/client/context.ts @@ -35,6 +35,7 @@ import { DecryptionManager } from './decryption'; import { MegolmBackup } from './MegolmBackup'; import { CallSessions } from './call'; import { randomStr } from '../app/utils/common'; +import { ReadMarker } from './ReadMarker'; export type StateContext = { join: { @@ -127,6 +128,7 @@ export type ClientContext = { notifications: ObservableValue; threadNotifications: ObservableValue; roomToTimeline: ObservableValue>; + readMarker: ReadMarker; pendingMemberRooms: ObservableValue; verificationRequest: Observable; diff --git a/src/client/initialize.ts b/src/client/initialize.ts index 08280241..34852ef4 100644 --- a/src/client/initialize.ts +++ b/src/client/initialize.ts @@ -44,6 +44,7 @@ import { MegolmBackup } from './MegolmBackup'; import { clearUserLocalStorage } from './localStorage'; import { CallSessions } from './call'; import { randomStr } from '../app/utils/common'; +import { ReadMarker } from './ReadMarker'; export type InitializeClientResult = { ct: ClientContext; @@ -114,6 +115,7 @@ export const initializeClient = async (session: Session): Promise { @@ -161,6 +163,7 @@ export const initializeClient = async (session: Session): Promise