add mark as read hook
This commit is contained in:
parent
72371de4cb
commit
24f5f7d679
4 changed files with 60 additions and 3 deletions
50
src/app/hooks/useMarkAsRead.ts
Normal file
50
src/app/hooks/useMarkAsRead.ts
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import { useCallback } from 'react';
|
||||
import { useClient } from './useClient';
|
||||
import { getReadReceiptId, getUnreadThreadNotificationId } from '../../client/storage/collections';
|
||||
import { ThreadId } from '../../types/matrix/room';
|
||||
import { ReadMarker } from '../../client/ReadMarker';
|
||||
|
||||
export const useMarkAsRead = (roomId: string, threadId?: string) => {
|
||||
const ct = useClient();
|
||||
|
||||
const clearNotificationCount = useCallback(async () => {
|
||||
if (threadId) {
|
||||
await ct.storage.unreadThreadNotifications.write.delete(
|
||||
getUnreadThreadNotificationId(roomId, threadId)
|
||||
);
|
||||
} else {
|
||||
await ct.storage.unreadNotifications.write.delete(roomId);
|
||||
}
|
||||
}, [ct, roomId, threadId]);
|
||||
|
||||
const markAsRead = useCallback(async () => {
|
||||
const latestEvent = ct.roomToLatestEvent.read.get(roomId);
|
||||
const notification = threadId
|
||||
? ct.threadNotifications.read.find(
|
||||
(n) => n.id === getUnreadThreadNotificationId(roomId, threadId)
|
||||
)
|
||||
: ct.notifications.read.find((n) => n.id === roomId);
|
||||
|
||||
const count = notification?.unread.notification_count ?? 0;
|
||||
|
||||
if (!latestEvent) {
|
||||
if (count > 0) await clearNotificationCount();
|
||||
return;
|
||||
}
|
||||
if (count > 0) {
|
||||
await ct.readMarker.markAsRead(roomId, 'm.read', latestEvent.event_id);
|
||||
await clearNotificationCount();
|
||||
}
|
||||
|
||||
const receiptId = getReadReceiptId(roomId, ct.session.userId, threadId ?? ThreadId.Main);
|
||||
const receipt = ct.userReadReceipt.read.find((r) => r.id === receiptId);
|
||||
|
||||
if (!receipt || !ReadMarker.hasUnread(receipt, latestEvent, ct.session.userId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
await ct.readMarker.markAsRead(roomId, 'm.read', latestEvent.event_id);
|
||||
}, [ct, roomId, threadId, clearNotificationCount]);
|
||||
|
||||
return markAsRead;
|
||||
};
|
||||
|
|
@ -4,6 +4,7 @@ import { ThreadId } from '../../../types/matrix/room';
|
|||
import { useObservableValue, useObservableValueListener } from '../../hooks/useObservableValue';
|
||||
import { getReadReceiptId } from '../../../client/storage/collections';
|
||||
import { ClientContext } from '../../../client/context';
|
||||
import { ReadMarker } from '../../../client/ReadMarker';
|
||||
|
||||
export const unreadRoomsAtom = atom<Set<string>>(new Set<string>());
|
||||
|
||||
|
|
@ -19,7 +20,7 @@ export const useBindUnreadRoomsAtom = (ct: ClientContext, unreadRooms: typeof un
|
|||
const receipt = userReadReceipts.find(
|
||||
(r) => getReadReceiptId(roomId, ct.session.userId, ThreadId.Main) === r.id
|
||||
);
|
||||
if (!receipt || receipt.ts > event.origin_server_ts || event.sender === ct.session.userId) {
|
||||
if (!receipt || !ReadMarker.hasUnread(receipt, event, ct.session.userId)) {
|
||||
return;
|
||||
}
|
||||
unreads.add(roomId);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { MatrixRequest, parseMatrixResp } from 'neurone/lib/matrix-request';
|
||||
import { ReceiptType } from 'neurone/lib/api/types';
|
||||
import { MessageEventWithoutRoomId, 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';
|
||||
|
|
@ -9,6 +9,12 @@ import { ThreadId } from '../types/matrix/room';
|
|||
export class ReadMarker {
|
||||
private readonly queue = new AsyncTaskQueue();
|
||||
|
||||
static hasUnread = (
|
||||
receipt: ReadReceiptObject,
|
||||
event: MessageEventWithoutRoomId,
|
||||
userId: string
|
||||
) => receipt && receipt.ts < event.origin_server_ts && event.sender !== userId;
|
||||
|
||||
constructor(
|
||||
private readonly storage: Storage,
|
||||
private readonly request: MatrixRequest,
|
||||
|
|
|
|||
|
|
@ -233,7 +233,7 @@ export const writeJoinRoomLatestEvent = async (
|
|||
|
||||
for (let i = events.length - 1; i >= 0; i -= 1) {
|
||||
const event = events[i];
|
||||
if (!('state_key' in event) && supportedTypes.includes(event.type)) {
|
||||
if (supportedTypes.includes(event.type)) {
|
||||
roomLatestEventObjects.push({
|
||||
id: roomId,
|
||||
event,
|
||||
|
|
|
|||
Loading…
Reference in a new issue