Skip to content
Snippets Groups Projects
Commit 6c0a79eb authored by Sébastien Blin's avatar Sébastien Blin
Browse files

conversationmodel: update lastMessageUid when removing last interaction


Actually, conversation.lastMessageUid still has the removed last
interaction uid. If this interaction is removed, this attribute must
be updated.

+ Add a unit test

Change-Id: Icfce0a54d7a88fdf71469127392460048f491ba8
Reviewed-by: default avatarAndreas Traczyk <andreas.traczyk@savoirfairelinux.com>
parent f30eca67
No related branches found
No related tags found
No related merge requests found
...@@ -725,20 +725,35 @@ ConversationModel::clearInteractionFromConversation(const std::string& convId, c ...@@ -725,20 +725,35 @@ ConversationModel::clearInteractionFromConversation(const std::string& convId, c
return; return;
auto erased_keys = 0; auto erased_keys = 0;
bool lastInteractionUpdated = false;
{ {
std::lock_guard<std::mutex> lk(pimpl_->interactionsLocks[convId]); std::lock_guard<std::mutex> lk(pimpl_->interactionsLocks[convId]);
try try
{ {
auto& conversation = pimpl_->conversations.at(conversationIdx); auto& conversation = pimpl_->conversations.at(conversationIdx);
database::clearInteractionFromConversation(pimpl_->db, convId, interactionId); database::clearInteractionFromConversation(pimpl_->db, convId, interactionId);
erased_keys = conversation.interactions.erase(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) { } catch (const std::out_of_range& e) {
qDebug() << "can't clear interaction from conversation: " << e.what(); qDebug() << "can't clear interaction from conversation: " << e.what();
} }
} }
if (erased_keys > 0) if (erased_keys > 0)
emit interactionRemoved(convId, interactionId); emit interactionRemoved(convId, interactionId);
if (lastInteractionUpdated) {
// last interaction as changed, so the order can changes.
pimpl_->sortConversations();
emit modelSorted();
}
} }
void void
......
...@@ -410,6 +410,59 @@ ConversationModelTester::testSendMessagesAndClearInteraction() ...@@ -410,6 +410,59 @@ ConversationModelTester::testSendMessagesAndClearInteraction()
CPPUNIT_ASSERT(conversationExists); 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 void
ConversationModelTester::testReceiveMessageAndSetRead() ConversationModelTester::testReceiveMessageAndSetRead()
{ {
......
...@@ -47,6 +47,7 @@ class ConversationModelTester : public CppUnit::TestFixture { ...@@ -47,6 +47,7 @@ class ConversationModelTester : public CppUnit::TestFixture {
CPPUNIT_TEST(testFilterAndGetConversations); CPPUNIT_TEST(testFilterAndGetConversations);
CPPUNIT_TEST(testSendMessageAndClearHistory); CPPUNIT_TEST(testSendMessageAndClearHistory);
CPPUNIT_TEST(testSendMessagesAndClearInteraction); CPPUNIT_TEST(testSendMessagesAndClearInteraction);
CPPUNIT_TEST(testSendMessagesAndClearLastInteraction);
CPPUNIT_TEST(testReceiveMessageAndSetRead); CPPUNIT_TEST(testReceiveMessageAndSetRead);
CPPUNIT_TEST(testPlaceCall); CPPUNIT_TEST(testPlaceCall);
CPPUNIT_TEST(testCreateConference); CPPUNIT_TEST(testCreateConference);
...@@ -99,6 +100,11 @@ public: ...@@ -99,6 +100,11 @@ public:
* Send multiple messages to the first conversation and clear one interaction * Send multiple messages to the first conversation and clear one interaction
*/ */
void testSendMessagesAndClearInteraction(); 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 * Receives a message from a conversation and set this message READ
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment