diff --git a/src/conversationmodel.cpp b/src/conversationmodel.cpp index ac38251e420f06cd0c5cbd3bc52cef78f18a3ef1..5e2b3748ee83693fa8efb98b83c003c3ad82d246 100644 --- a/src/conversationmodel.cpp +++ b/src/conversationmodel.cpp @@ -725,20 +725,35 @@ ConversationModel::clearInteractionFromConversation(const std::string& convId, c return; auto erased_keys = 0; + bool lastInteractionUpdated = false; { std::lock_guard<std::mutex> lk(pimpl_->interactionsLocks[convId]); try { auto& conversation = pimpl_->conversations.at(conversationIdx); database::clearInteractionFromConversation(pimpl_->db, convId, interactionId); - erased_keys = conversation.interactions.erase(interactionId); + + if (conversation.lastMessageUid == interactionId) { + // Update lastMessageUid + auto newLastId = 0; + if (!conversation.interactions.empty()) + newLastId = conversation.interactions.rbegin()->first; + conversation.lastMessageUid = newLastId; + lastInteractionUpdated = true; + } + } catch (const std::out_of_range& e) { qDebug() << "can't clear interaction from conversation: " << e.what(); } } if (erased_keys > 0) emit interactionRemoved(convId, interactionId); + if (lastInteractionUpdated) { + // last interaction as changed, so the order can changes. + pimpl_->sortConversations(); + emit modelSorted(); + } } void diff --git a/test/conversationmodeltester.cpp b/test/conversationmodeltester.cpp index 336b8fccf882194dcc33f8aa954f917bc9582826..81216a8640110d8475fc8dbb1bb165ba8ff35df4 100644 --- a/test/conversationmodeltester.cpp +++ b/test/conversationmodeltester.cpp @@ -410,6 +410,59 @@ ConversationModelTester::testSendMessagesAndClearInteraction() CPPUNIT_ASSERT(conversationExists); } +void +ConversationModelTester::testSendMessagesAndClearLastInteraction() +{ + accInfo_.conversationModel->setFilter(""); + auto conversations = accInfo_.conversationModel->allFilteredConversations(); + CPPUNIT_ASSERT(conversations.size() != 0); + auto firstConversation = accInfo_.conversationModel->filteredConversation(0); + auto firstConversationUid = firstConversation.uid; + + // HACK reinit the conversation here (without these line, Hello World! will not be in interactions) + // FIXME + accInfo_.conversationModel->clearHistory(firstConversationUid); + firstConversation = accInfo_.conversationModel->filteredConversation(0); + + // Send 3 messages (will be added to conversation.interactions) + int baseInteractionsSize = firstConversation.interactions.size(); + accInfo_.conversationModel->sendMessage(firstConversationUid, "Hello World!"); + accInfo_.conversationModel->sendMessage(firstConversationUid, "It's been a long time"); + accInfo_.conversationModel->sendMessage(firstConversationUid, "How have you been?"); + + conversations = accInfo_.conversationModel->allFilteredConversations(); + auto conversationExists = false; + uint64_t lastInteractionId = {}; + uint64_t secondInterId = {}; + for (const auto& conversation : conversations) { + if (conversation.uid == firstConversationUid) { + conversationExists = true; + CPPUNIT_ASSERT_EQUAL((int)conversation.interactions.size(), baseInteractionsSize + 3); + auto it = conversation.interactions.rbegin(); + lastInteractionId = it->first; + it++; + secondInterId = it->first; + break; + } + } + CPPUNIT_ASSERT(conversationExists); + + accInfo_.conversationModel->clearInteractionFromConversation(firstConversationUid, lastInteractionId); + WaitForSignalHelper(*accInfo_.conversationModel, + SIGNAL(interactionRemoved(const std::string& convUid, uint64_t interactionId))).wait(1000); + conversations = accInfo_.conversationModel->allFilteredConversations(); + conversationExists = false; + for (const auto& conversation : conversations) { + if (conversation.uid == firstConversationUid) { + conversationExists = true; + // lastMessageUid should be equals to the new last interaction's id. + CPPUNIT_ASSERT_EQUAL(conversation.lastMessageUid, secondInterId); + break; + } + } + CPPUNIT_ASSERT(conversationExists); +} + void ConversationModelTester::testReceiveMessageAndSetRead() { diff --git a/test/conversationmodeltester.h b/test/conversationmodeltester.h index 2aac71ff3957e7abbcea432e4876f5717b686569..2bab3ee18a60e21873d0d4818e571dbfe4ad6961 100644 --- a/test/conversationmodeltester.h +++ b/test/conversationmodeltester.h @@ -47,6 +47,7 @@ class ConversationModelTester : public CppUnit::TestFixture { CPPUNIT_TEST(testFilterAndGetConversations); CPPUNIT_TEST(testSendMessageAndClearHistory); CPPUNIT_TEST(testSendMessagesAndClearInteraction); + CPPUNIT_TEST(testSendMessagesAndClearLastInteraction); CPPUNIT_TEST(testReceiveMessageAndSetRead); CPPUNIT_TEST(testPlaceCall); CPPUNIT_TEST(testCreateConference); @@ -99,6 +100,11 @@ public: * Send multiple messages to the first conversation and clear one interaction */ void testSendMessagesAndClearInteraction(); + /** + * Send multiple messages to the first conversation and clear the last interaction + * lastMessageUid should be updated + */ + void testSendMessagesAndClearLastInteraction(); /** * Receives a message from a conversation and set this message READ */