add read marker class

This commit is contained in:
Ajay Bura 2026-10-28 18:27:08 +05:30
commit 6f4f9c74b4
3 changed files with 74 additions and 0 deletions

69
src/client/ReadMarker.ts Normal file
View file

@ -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<void> {
await this.queue.enqueue(() => this.read(roomId, receiptType, eventId, threadId));
}
private async read(
roomId: string,
receiptType: ReceiptType,
eventId: string,
threadId?: string
): Promise<void> {
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<PostReceiptEventIdOK>(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);
}
}

View file

@ -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<UnreadNotificationsObject[]>;
threadNotifications: ObservableValue<UnreadThreadNotificationsObject[]>;
roomToTimeline: ObservableValue<Map<string, Timeline>>;
readMarker: ReadMarker;
pendingMemberRooms: ObservableValue<string[]>;
verificationRequest: Observable<InboundVerification | OutboundVerification>;

View file

@ -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<InitializeClie
const callSessions = new CallSessions(storage, syncObservable);
const roomToTimeline = createTimelines(rooms.join, syncObservable, requestAuthJSON, disposer);
const readMarker = new ReadMarker(storage, requestAuthJSON, session.userId);
const logoutObservable = createLogoutObservable(syncObservable, disposer);
const logoutManager = new LogoutManager(logoutObservable, requestAuthJSON, async () => {
@ -161,6 +163,7 @@ export const initializeClient = async (session: Session): Promise<InitializeClie
roomToCreators,
roomToVersion,
roomToTimeline,
readMarker,
pendingMemberRooms,
verificationRequest,