From c8700b59d00d505c59de51d8dc259454073123e3 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 21 Oct 2016 12:51:01 +0900 Subject: Room::markMessagesAsRead correctly handles local user's messages now setLastReadEvent() is called in any case (read marks a stored in a hashmap so it's a constant time operation anyway); postReceipt() is now called for the nearest previous non-local message. --- room.cpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'room.cpp') diff --git a/room.cpp b/room.cpp index 5181fb2a..674f0343 100644 --- a/room.cpp +++ b/room.cpp @@ -169,11 +169,32 @@ void Room::setLastReadEvent(User* user, QString eventId) emit lastReadEventChanged(user); } -void Room::markMessageAsRead(Event* event) +void Room::markMessagesAsRead(Timeline::const_iterator end) { - setLastReadEvent(connection()->user(), event->id()); - if (event->senderId() != connection()->userId()) - d->connection->postReceipt(this, event); + if (messageEvents().empty()) + return; + + QString prevLastReadId = lastReadEvent(connection()->user()); + setLastReadEvent(connection()->user(), (*--end)->id()); + + // We shouldn't send read receipts for messages from the local user - so + // shift back (if necessary) to the nearest message not from the local user + // or the so far last read message, whichever comes first. + for (; (*end)->id() != prevLastReadId; --end) + { + if ((*end)->senderId() != connection()->userId()) + { + d->connection->postReceipt(this, (*end)); + break; + } + if (end == messageEvents().begin()) + break; + } +} + +void Room::markMessagesAsRead() +{ + markMessagesAsRead(messageEvents().end()); } QString Room::lastReadEvent(User* user) -- cgit v1.2.3