From 8f2b6d3f5afbc911a5cf8425203c8a60ee20f0d2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois-Simon=20Fauteux-Chapleau?=
 <francois-simon.fauteux-chapleau@savoirfairelinux.com>
Date: Thu, 15 Aug 2024 14:52:15 -0400
Subject: [PATCH] conversation_module: update removed contact's conversation
 exactly once

When removing a contact, we need to update the ID of its linked
conversation (to be empty, which is the convention used to signify that
a contact doesn't have a linked conversation).

This used to be done via the updateConvForContact function, but there
were two issues (which are fixed by this patch):
1) if the user had multiple one-to-one conversations with the removed
   contact, then the function would be called multiple times (once per
   conversation) even though the update only needs to be performed once;
2) more importantly, if the contact's linked conversation ID didn't
   match any of its one-to-one conversations, then the update would not
   be performed at all. (Every contact should normally be linked to a
   valid conversation, but this can fail in practice due to
   synchronization issues.) As a result, the contact would remain linked
   to a conversation that no longer exists.

https://git.jami.net/savoirfairelinux/jami-client-qt/-/issues/1673
https://git.jami.net/savoirfairelinux/jami-client-qt/-/issues/1804

Change-Id: Ia75c5b27a4120bc48da5f449a874354326c6d96a
---
 src/jamidht/conversation_module.cpp | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/src/jamidht/conversation_module.cpp b/src/jamidht/conversation_module.cpp
index ba6c1d7f8..37a215930 100644
--- a/src/jamidht/conversation_module.cpp
+++ b/src/jamidht/conversation_module.cpp
@@ -2851,20 +2851,20 @@ ConversationModule::removeContact(const std::string& uri, bool banned)
         pimpl_->withConversation(conversationId, [&](auto& conv) { conv.shutdownConnections(); });
         return; // Keep the conversation in banned model but stop connections
     }
-    // Remove related conversation
+
+    // Removed contacts should not be linked to any conversation
+    pimpl_->accountManager_->updateContactConversation(uri, "");
+
+    // Remove all one-to-one conversations with the removed contact
     auto isSelf = uri == pimpl_->username_;
     std::vector<std::string> toRm;
-    auto updateClient = [&](const auto& convId) {
-        pimpl_->updateConvForContact(uri, convId, "");
-        emitSignal<libjami::ConversationSignal::ConversationRemoved>(pimpl_->accountId_, convId);
-    };
     auto removeConvInfo = [&](const auto& conv, const auto& members) {
         if ((isSelf && members.size() == 1)
             || (!isSelf && std::find(members.begin(), members.end(), uri) != members.end())) {
             // Mark the conversation as removed if it wasn't already
             if (!conv->info.isRemoved()) {
                 conv->info.removed = std::time(nullptr);
-                updateClient(conv->info.id);
+                emitSignal<libjami::ConversationSignal::ConversationRemoved>(pimpl_->accountId_, conv->info.id);
                 pimpl_->addConvInfo(conv->info);
                 return true;
             }
@@ -2892,8 +2892,6 @@ ConversationModule::removeContact(const std::string& uri, bool banned)
             }
         }
     }
-    // Note, if we ban the device, we don't send the leave cause the other peer will just
-    // never got the notifications, so just erase the datas
     for (const auto& id : toRm)
         pimpl_->removeRepository(id, true, true);
 }
-- 
GitLab