diff --git a/src/app/hooks/useMarkAsRead.ts b/src/app/hooks/useMarkAsRead.ts new file mode 100644 index 00000000..2e2ebb83 --- /dev/null +++ b/src/app/hooks/useMarkAsRead.ts @@ -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; +}; diff --git a/src/app/state/room/roomToUnread.ts b/src/app/state/room/roomToUnread.ts index e957bcea..096462bd 100644 --- a/src/app/state/room/roomToUnread.ts +++ b/src/app/state/room/roomToUnread.ts @@ -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>(new Set()); @@ -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); diff --git a/src/client/ReadMarker.ts b/src/client/ReadMarker.ts index f8af6019..94650fa5 100644 --- a/src/client/ReadMarker.ts +++ b/src/client/ReadMarker.ts @@ -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, diff --git a/src/client/syncWriter/utils.ts b/src/client/syncWriter/utils.ts index 632f4d76..ba011f3b 100644 --- a/src/client/syncWriter/utils.ts +++ b/src/client/syncWriter/utils.ts @@ -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,