aboutsummaryrefslogtreecommitdiff
path: root/room.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2016-11-02 17:09:43 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2016-11-02 17:09:43 +0900
commit66be8322379b9821ae624ad07d2ecf478a34384c (patch)
treeec3d7640cb598301476bd9513a4cfc1962102b8b /room.cpp
parent416460b8da337bca3f12fb8bdc00371b2c56481d (diff)
downloadlibquotient-66be8322379b9821ae624ad07d2ecf478a34384c.tar.gz
libquotient-66be8322379b9821ae624ad07d2ecf478a34384c.zip
Room: make read marker a Q_PROPERTY; markMessagesAsRead now uses eventId again
The first change allows to use the read marker from QML (hint to Tensor). The second change is actually a fix for a case when markMessagesAsRead() is called with an iterator behind the last read event (in that case markMessagesAsRead() would post a receipt for that older event, which is not quite right).
Diffstat (limited to 'room.cpp')
-rw-r--r--room.cpp31
1 files changed, 20 insertions, 11 deletions
diff --git a/room.cpp b/room.cpp
index 85ab1720..366073c7 100644
--- a/room.cpp
+++ b/room.cpp
@@ -170,13 +170,14 @@ void Room::setLastReadEvent(User* user, QString eventId)
emit lastReadEventChanged(user);
}
-bool Room::promoteReadMarker(QString newLastReadEventId)
+Room::Timeline::const_iterator Room::promoteReadMarker(QString eventId)
{
User* localUser = connection()->user();
QString prevLastReadId = lastReadEvent(localUser);
int stillUnreadMessagesCount = 0;
+ auto it = d->messageEvents.end();
// Older Qt doesn't provide rbegin()/rend() for Qt containers
- for (auto it = messageEvents().end(); it != messageEvents().begin();)
+ while (it != d->messageEvents.begin())
{
--it;
// Check that the new read event is not before the previously set - only
@@ -186,9 +187,10 @@ bool Room::promoteReadMarker(QString newLastReadEventId)
// Found the message to mark as read; for the local user,
// if we don't have other notable events below this one, reset unreadMessages
- if (newLastReadEventId == (*it)->id())
+ if (eventId == (*it)->id())
{
- setLastReadEvent(localUser, newLastReadEventId);
+ setLastReadEvent(localUser, eventId);
+ emit readMarkerPromoted();
break;
}
@@ -205,15 +207,17 @@ bool Room::promoteReadMarker(QString newLastReadEventId)
if (stillUnreadMessagesCount > 0)
qDebug() << "Room" << displayName()
<< ": still" << stillUnreadMessagesCount << "unread message(s)";
- return newLastReadEventId.isEmpty() || lastReadEvent(localUser) == newLastReadEventId;
+ return it;
}
-void Room::markMessagesAsRead(Timeline::const_iterator last)
+void Room::markMessagesAsRead(QString uptoEventId)
{
- QString prevLastReadId = lastReadEvent(connection()->user());
- if ( !promoteReadMarker( (*last)->id()) )
+ if (d->messageEvents.empty())
return;
+ QString prevLastReadId = lastReadEvent(connection()->user());
+ auto last = promoteReadMarker(uptoEventId);
+
// 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.
@@ -221,7 +225,7 @@ void Room::markMessagesAsRead(Timeline::const_iterator last)
{
if ((*last)->senderId() != connection()->userId())
{
- d->connection->postReceipt(this, (*last));
+ d->connection->postReceipt(this, *last);
break;
}
if (last == messageEvents().begin())
@@ -232,7 +236,7 @@ void Room::markMessagesAsRead(Timeline::const_iterator last)
void Room::markMessagesAsRead()
{
if (!messageEvents().empty())
- markMessagesAsRead(messageEvents().end() - 1);
+ markMessagesAsRead(messageEvents().back()->id());
}
bool Room::hasUnreadMessages()
@@ -240,11 +244,16 @@ bool Room::hasUnreadMessages()
return d->unreadMessages;
}
-QString Room::lastReadEvent(User* user)
+QString Room::lastReadEvent(User* user) const
{
return d->lastReadEvent.value(user);
}
+QString Room::readMarkerEventId() const
+{
+ return lastReadEvent(d->connection->user());
+}
+
int Room::notificationCount() const
{
return d->notificationCount;