From 8e55f34bc3a637728abf7f4ff8fbf998a470c9bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Wed, 25 Aug 2021 13:08:39 -0400
Subject: [PATCH] contactmodel: use added information when adding conversation

After a sync, contactAdded will be emitted. But lrc was generating
a new conversation without the "added" information stored by the
daemon. Causing a smartlist with incorrect informations.

Change-Id: I5f05a905e831ec06353002fed40be403e3c753da
GitLab: https://git.jami.net/savoirfairelinux/ring-project/-/issues/1282
---
 src/api/contactmodel.h          |  5 +++++
 src/authority/storagehelper.cpp |  7 +++++--
 src/authority/storagehelper.h   |  4 +++-
 src/contactmodel.cpp            | 11 +++++++++++
 src/conversationmodel.cpp       | 12 ++++++++++--
 5 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/src/api/contactmodel.h b/src/api/contactmodel.h
index de5f8c37..987e3374 100644
--- a/src/api/contactmodel.h
+++ b/src/api/contactmodel.h
@@ -86,6 +86,11 @@ public:
      */
     const contact::Info getContact(const QString& contactUri) const;
     ContactInfoMap getSearchResults() const;
+
+    /**
+     * Retrieve when a contact is added
+     */
+    time_t getAddedTs(const QString& contactUri) const;
     /**
      * get list of banned contacts.
      * @return list of banned contacts uris as string
diff --git a/src/authority/storagehelper.cpp b/src/authority/storagehelper.cpp
index 0ca2a5c1..3660d5f3 100644
--- a/src/authority/storagehelper.cpp
+++ b/src/authority/storagehelper.cpp
@@ -357,7 +357,10 @@ getConversationsBetween(Database& db, const QString& peer1_uri, const QString& p
 }
 
 QString
-beginConversationWithPeer(Database& db, const QString& peer_uri, const bool isOutgoing)
+beginConversationWithPeer(Database& db,
+                          const QString& peer_uri,
+                          const bool isOutgoing,
+                          time_t timestamp)
 {
     // Add conversation between account and profile
     auto newConversationsId = db.select("IFNULL(MAX(id), 0) + 1", "conversations", "1=1", {})
@@ -367,7 +370,7 @@ beginConversationWithPeer(Database& db, const QString& peer_uri, const bool isOu
                   {{":id", newConversationsId}, {":participant", peer_uri}});
     api::interaction::Info msg {isOutgoing ? "" : peer_uri,
                                 {},
-                                std::time(nullptr),
+                                timestamp ? timestamp : std::time(nullptr),
                                 0,
                                 api::interaction::Type::CONTACT,
                                 isOutgoing ? api::interaction::Status::SUCCESS
diff --git a/src/authority/storagehelper.h b/src/authority/storagehelper.h
index 8c70ecae..bf92431e 100644
--- a/src/authority/storagehelper.h
+++ b/src/authority/storagehelper.h
@@ -172,11 +172,13 @@ VectorString getConversationsBetween(Database& db,
  * @param db
  * @param peer_uri the URI of the peer
  * @param isOutgoing
+ * @param timestamp
  * @return conversation_id of the new conversation.
  */
 QString beginConversationWithPeer(Database& db,
                                   const QString& peer_uri,
-                                  const bool isOutgoing = true);
+                                  const bool isOutgoing = true,
+                                  time_t timestamp = 0);
 
 /**
  * Return interactions from a conversation
diff --git a/src/contactmodel.cpp b/src/contactmodel.cpp
index 1dee9092..26666d32 100644
--- a/src/contactmodel.cpp
+++ b/src/contactmodel.cpp
@@ -234,6 +234,17 @@ ContactModel::getAllContacts() const
     return pimpl_->contacts;
 }
 
+time_t
+ContactModel::getAddedTs(const QString& contactUri) const
+{
+    MapStringString details = ConfigurationManager::instance().getContactDetails(owner.id,
+                                                                                 contactUri);
+    auto itAdded = details.find("added");
+    if (itAdded == details.end())
+        return 0;
+    return itAdded.value().toUInt();
+}
+
 void
 ContactModel::addContact(contact::Info contactInfo)
 {
diff --git a/src/conversationmodel.cpp b/src/conversationmodel.cpp
index 62f9033d..ea3e100d 100644
--- a/src/conversationmodel.cpp
+++ b/src/conversationmodel.cpp
@@ -1932,7 +1932,11 @@ ConversationModelPimpl::initConversations()
                 addContactRequest(c.second.profileInfo.uri);
                 continue;
             }
-            conv.push_back(storage::beginConversationWithPeer(db, c.second.profileInfo.uri));
+            conv.push_back(storage::beginConversationWithPeer(db,
+                                                              c.second.profileInfo.uri,
+                                                              true,
+                                                              linked.owner.contactModel->getAddedTs(
+                                                                  c.second.profileInfo.uri)));
         }
         addConversationWith(conv[0], c.first, isRequest);
 
@@ -2570,7 +2574,11 @@ ConversationModelPimpl::slotContactAdded(const QString& contactUri)
          */
         addConversation = true;
         if (conv.empty()) {
-            conv.push_back(storage::beginConversationWithPeer(db, contactUri));
+            conv.push_back(storage::beginConversationWithPeer(db,
+                                                              contactUri,
+                                                              true,
+                                                              linked.owner.contactModel->getAddedTs(
+                                                                  contactUri)));
         }
     }
     if (addConversation) {
-- 
GitLab