From 9b54aff3d599596092678c53748645c6a4b712e0 Mon Sep 17 00:00:00 2001 From: James Graham Date: Tue, 9 Aug 2022 16:42:32 +0000 Subject: [PATCH] Mark unread messages in the room as read when all messages are visible This is an alternative to network/neochat!467. When discussing in the matrix channel this option seemed more popular so I implemented it. Mark the unread messages in the room as read when all messages are visible to the user after a short timer. This happens on entry and when new messages come in as long as Neochat is active. If neochat isn't active, the room hasn't loaded or the read marker hasn't loaded the timer is for this is reset until all 3 conditions are false. --- imports/NeoChat/Page/RoomPage.qml | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/imports/NeoChat/Page/RoomPage.qml b/imports/NeoChat/Page/RoomPage.qml index 7e3fc752d..6cd1fa57c 100644 --- a/imports/NeoChat/Page/RoomPage.qml +++ b/imports/NeoChat/Page/RoomPage.qml @@ -23,6 +23,7 @@ Kirigami.ScrollablePage { /// It's not readonly because of the seperate window view. property var currentRoom: RoomManager.currentRoom + property bool loading: page.currentRoom === null || (messageListView.count === 0 && !page.currentRoom.allHistoryLoaded && !page.currentRoom.isInvite) /// Used to determine if scrolling to the bottom should mark the message as unread property bool hasScrolledUpBefore: false; @@ -55,6 +56,25 @@ Kirigami.ScrollablePage { ChatBoxHelper.clearEditReply() } + Connections { + target: messageEventModel + function onRowsInserted() { + markReadIfVisibleTimer.restart() + } + } + + Timer { + id: markReadIfVisibleTimer + interval: 1000 + onTriggered: { + if (loading || !currentRoom.readMarkerLoaded || !applicationWindow().active) { + restart() + } else { + markReadIfVisible() + } + } + } + ActionsHandler { id: actionsHandler room: page.currentRoom @@ -123,7 +143,7 @@ Kirigami.ScrollablePage { Kirigami.PlaceholderMessage { id: loadingIndicator anchors.centerIn: parent - visible: page.currentRoom === null || (messageListView.count === 0 && !page.currentRoom.allHistoryLoaded && !page.currentRoom.isInvite) + visible: loading text: i18n("Loading…") QQC2.BusyIndicator { running: loadingIndicator.visible @@ -592,6 +612,14 @@ Kirigami.ScrollablePage { return index; } + // Mark all messages as read if all unread messages are visible to the user + function markReadIfVisible() { + let readMarkerRow = eventToIndex(currentRoom.readMarkerEventId) + if (readMarkerRow > 0 && readMarkerRow < firstVisibleIndex()) { + currentRoom.markAllMessagesAsRead() + } + } + /// Open message context dialog for file and videos function openFileContext(event, file) { const contextMenu = fileDelegateContextMenu.createObject(page, {