From 1a994173ec3b3a3518d36b93fde7bc62f19064d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Blin?= <sebastien.blin@savoirfairelinux.com> Date: Fri, 21 Oct 2022 09:33:36 -0400 Subject: [PATCH] conversationmodel: fix loading for some conversations If there was too much following merge commits, sometimes the conversation was not correctly loaded. Moreover, the lastMessageUID was not correctly calculated from time to time, leading to empty conversations in the smartlist. Change-Id: I1224269c5df72936ae51f34211ce3f63dbf606ff --- src/libclient/conversationmodel.cpp | 28 ++++++++++++++-------------- src/libclient/messagelistmodel.cpp | 12 ++++++++++++ src/libclient/messagelistmodel.h | 2 ++ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/libclient/conversationmodel.cpp b/src/libclient/conversationmodel.cpp index 2a3fab0ff..e897830df 100644 --- a/src/libclient/conversationmodel.cpp +++ b/src/libclient/conversationmodel.cpp @@ -2312,9 +2312,11 @@ ConversationModelPimpl::slotConversationLoaded(uint32_t requestId, try { auto& conversation = getConversationForUid(conversationId).get(); - QString oldLast; // Used to detect loading loops just in case. - if (conversation.interactions->size() != 0) + QString oldLast, oldBegin; // Used to detect loading loops just in case. + if (conversation.interactions->size() != 0) { + oldBegin = conversation.interactions->begin()->first; oldLast = conversation.interactions->rbegin()->first; + } for (const auto& message : messages) { if (message["type"].isEmpty()) { continue; @@ -2362,21 +2364,19 @@ ConversationModelPimpl::slotConversationLoaded(uint32_t requestId, } } - for (int j = conversation.interactions->size() - 1; j >= 0; j--) { - if (conversation.interactions->atIndex(j).second.type != interaction::Type::MERGE) { - conversation.lastMessageUid = conversation.interactions->atIndex(j).first; - break; - } - } + conversation.lastMessageUid = conversation.interactions->lastMessageUid(); if (conversation.lastMessageUid.isEmpty() && !conversation.allMessagesLoaded && messages.size() != 0) { if (conversation.interactions->size() > 0) { - QString newLast; - if (conversation.interactions->size() > 0) + QString newLast, newBegin; + if (conversation.interactions->size() > 0) { + newBegin = conversation.interactions->begin()->first; newLast = conversation.interactions->rbegin()->first; - if (newLast == oldLast && !newLast.isEmpty()) { // [[unlikely]] in c++20 - qCritical() << "Loading loop detected for " << conversationId << "(" << newLast - << ")"; + } + if (newLast == oldLast && !newLast.isEmpty() && newBegin == oldBegin + && !newBegin.isEmpty()) { // [[unlikely]] in c++20 + qCritical() << "Loading loop detected for " << conversationId << "(" << newBegin + << " ; " << newLast << ")"; return; } } @@ -2473,7 +2473,7 @@ ConversationModelPimpl::slotMessageReceived(const QString& accountId, invalidateModel(); return; } - conversation.lastMessageUid = msgId; + conversation.lastMessageUid = conversation.interactions->lastMessageUid(); invalidateModel(); if (!interaction::isOutgoing(msg)) { Q_EMIT behaviorController.newUnreadInteraction(linked.owner.id, diff --git a/src/libclient/messagelistmodel.cpp b/src/libclient/messagelistmodel.cpp index 8e3f0b1cb..339ec2895 100644 --- a/src/libclient/messagelistmodel.cpp +++ b/src/libclient/messagelistmodel.cpp @@ -541,4 +541,16 @@ MessageListModel::emitDataChanged(const QString& msgId, VectorInt roles) Q_EMIT dataChanged(modelIndex, modelIndex, roles); } +QString +MessageListModel::lastMessageUid() const +{ + for (auto it = interactions_.rbegin(); it != interactions_.rend(); ++it) { + auto lastType = it->second.type; + if (lastType != interaction::Type::MERGE and !it->second.body.isEmpty()) { + return it->first; + } + } + return {}; +} + } // namespace lrc diff --git a/src/libclient/messagelistmodel.h b/src/libclient/messagelistmodel.h index 2613276b7..b3d494ac7 100644 --- a/src/libclient/messagelistmodel.h +++ b/src/libclient/messagelistmodel.h @@ -130,6 +130,8 @@ public: Q_SIGNAL void timestampUpdate(); + QString lastMessageUid() const; + protected: using Role = MessageList::Role; -- GitLab