From c3aa77a75a0b7a0de7fadc1e487d3a4f6367293f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Thu, 12 Oct 2023 11:16:45 -0400
Subject: [PATCH] account_manager: sync contacts if conversation is updated

This allow the JAMS server to know what is the correct
conversation to sync in 1:1
https://git.jami.net/savoirfairelinux/jami-jams/-/issues/125

Change-Id: Icfd50c643ef5f4389e08825591f6d70347ee63d2
---
 src/jamidht/account_manager.cpp | 22 ++++++++++++++++++++++
 src/jamidht/account_manager.h   |  1 +
 src/jamidht/jamiaccount.cpp     |  8 +-------
 3 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/src/jamidht/account_manager.cpp b/src/jamidht/account_manager.cpp
index b4adc0d512..c6f3ff7659 100644
--- a/src/jamidht/account_manager.cpp
+++ b/src/jamidht/account_manager.cpp
@@ -522,6 +522,28 @@ AccountManager::removeContactConversation(const std::string& uri)
         syncDevices();
 }
 
+void
+AccountManager::updateContactConversation(const std::string& uri, const std::string& convId)
+{
+    dht::InfoHash h(uri);
+    if (not h) {
+        JAMI_ERR("removeContact: invalid contact URI");
+        return;
+    }
+    if (not info_) {
+        JAMI_ERR("addContact(): account not loaded");
+        return;
+    }
+    info_->contacts->updateConversation(h, convId);
+    // Also decline trust request if there is one
+    auto req = info_->contacts->getTrustRequest(h);
+    if (req.find(libjami::Account::TrustRequest::CONVERSATIONID) != req.end()
+        && req.at(libjami::Account::TrustRequest::CONVERSATIONID) == convId) {
+        discardTrustRequest(uri);
+    }
+    syncDevices();
+}
+
 std::vector<std::map<std::string, std::string>>
 AccountManager::getContacts() const
 {
diff --git a/src/jamidht/account_manager.h b/src/jamidht/account_manager.h
index dfe97618e4..4e1250ac3f 100644
--- a/src/jamidht/account_manager.h
+++ b/src/jamidht/account_manager.h
@@ -220,6 +220,7 @@ public:
                     const std::string& conversationId = "");
     void removeContact(const std::string& uri, bool banned = true);
     void removeContactConversation(const std::string& uri); // for non swarm contacts
+    void updateContactConversation(const std::string& uri, const std::string& convId);
     std::vector<std::map<std::string, std::string>> getContacts() const;
 
     /** Obtain details about one account contact in serializable form. */
diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp
index 7228a311fc..6f6eb411ea 100644
--- a/src/jamidht/jamiaccount.cpp
+++ b/src/jamidht/jamiaccount.cpp
@@ -2826,19 +2826,13 @@ JamiAccount::updateConvForContact(const std::string& uri,
     if (newConv != oldConv) {
         std::lock_guard<std::recursive_mutex> lock(configurationMutex_);
         if (auto info = accountManager_->getInfo()) {
-            auto urih = dht::InfoHash(uri);
             auto details = getContactDetails(uri);
             auto itDetails = details.find(libjami::Account::TrustRequest::CONVERSATIONID);
             if (itDetails != details.end() && itDetails->second != oldConv) {
                 JAMI_DEBUG("Old conversation is not found in details {} - found: {}", oldConv, itDetails->second);
                 return false;
             }
-            info->contacts->updateConversation(urih, newConv);
-            // Also decline trust request if there is one
-            auto req = info->contacts->getTrustRequest(urih);
-            if (req.find(libjami::Account::TrustRequest::CONVERSATIONID) != req.end()
-                && req.at(libjami::Account::TrustRequest::CONVERSATIONID) == oldConv)
-                accountManager_->discardTrustRequest(uri);
+            accountManager_->updateContactConversation(uri, newConv);
         }
         return true;
     }
-- 
GitLab