From ee08eae0ee5f3525336d3ceff06d163d4b287270 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Tue, 4 Apr 2023 11:17:04 -0400
Subject: [PATCH] contactmodel: avoid to re-write profile several time

Also, force the format of the vcard to always be the same to
avoid to change the shasum in a useless way.
Finally, only change if there is new details

https://git.jami.net/savoirfairelinux/jami-project/-/issues/1558

Change-Id: I95bc52e13c8d1020eae6ad64e77d4e58b1ee734b
---
 src/libclient/accountmodel.cpp   | 12 +++++++-----
 src/libclient/api/accountmodel.h |  4 ++--
 src/libclient/contactmodel.cpp   | 19 +++++++++----------
 3 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/src/libclient/accountmodel.cpp b/src/libclient/accountmodel.cpp
index 1dd57e88c..3bf692718 100644
--- a/src/libclient/accountmodel.cpp
+++ b/src/libclient/accountmodel.cpp
@@ -290,26 +290,28 @@ AccountModel::getAccountConfig(const QString& accountId) const
 }
 
 void
-AccountModel::setAlias(const QString& accountId, const QString& alias)
+AccountModel::setAlias(const QString& accountId, const QString& alias, bool save)
 {
     auto& accountInfo = pimpl_->getAccountInfo(accountId);
     if (accountInfo.profileInfo.alias == alias)
         return;
     accountInfo.profileInfo.alias = alias;
 
-    authority::storage::createOrUpdateProfile(accountInfo.id, accountInfo.profileInfo);
+    if (save)
+        authority::storage::createOrUpdateProfile(accountInfo.id, accountInfo.profileInfo);
+    Q_EMIT profileUpdated(accountId);
 }
 
 void
-AccountModel::setAvatar(const QString& accountId, const QString& avatar)
+AccountModel::setAvatar(const QString& accountId, const QString& avatar, bool save)
 {
     auto& accountInfo = pimpl_->getAccountInfo(accountId);
     if (accountInfo.profileInfo.avatar == avatar)
         return;
     accountInfo.profileInfo.avatar = avatar;
 
-    authority::storage::createOrUpdateProfile(accountInfo.id, accountInfo.profileInfo);
-
+    if (save)
+        authority::storage::createOrUpdateProfile(accountInfo.id, accountInfo.profileInfo);
     Q_EMIT profileUpdated(accountId);
 }
 
diff --git a/src/libclient/api/accountmodel.h b/src/libclient/api/accountmodel.h
index 07a7f4c4e..62f4c5453 100644
--- a/src/libclient/api/accountmodel.h
+++ b/src/libclient/api/accountmodel.h
@@ -140,14 +140,14 @@ public:
      * @param avatar
      * @throws out_of_range exception if account is not found
      */
-    void setAvatar(const QString& accountId, const QString& avatar);
+    void setAvatar(const QString& accountId, const QString& avatar, bool save = true);
     /**
      * Change the alias of an account
      * @param accountId
      * @param alias
      * @throws out_of_range exception if account is not found
      */
-    void setAlias(const QString& accountId, const QString& alias);
+    void setAlias(const QString& accountId, const QString& alias, bool save = true);
     /**
      * Try to register a name
      * @param accountId
diff --git a/src/libclient/contactmodel.cpp b/src/libclient/contactmodel.cpp
index b1eaf237d..b43c407b2 100644
--- a/src/libclient/contactmodel.cpp
+++ b/src/libclient/contactmodel.cpp
@@ -1211,16 +1211,15 @@ ContactModelPimpl::slotProfileReceived(const QString& accountId,
             profileInfo.alias = e.split(":")[1];
 
     if (peer == linked.owner.profileInfo.uri) {
-        if (!profileInfo.avatar.isEmpty()) {
-            auto dest = storage::getPath() + accountId + "/profile.vcf";
-            QFile oldvCard(dest);
-            if (oldvCard.exists())
-                oldvCard.remove();
-            vCardFile.rename(dest);
-            linked.owner.accountModel->setAlias(linked.owner.id, profileInfo.alias);
-            linked.owner.accountModel->setAvatar(linked.owner.id, profileInfo.avatar);
-            Q_EMIT linked.profileUpdated(peer);
-        }
+        auto avatarChanged = profileInfo.avatar != linked.owner.profileInfo.avatar;
+        auto aliasChanged = profileInfo.alias != linked.owner.profileInfo.alias;
+        if (profileInfo.avatar.isEmpty())
+            return; // In this case, probably a new device without avatar.
+        // Only save the new profile once
+        if (aliasChanged)
+            linked.owner.accountModel->setAlias(linked.owner.id, profileInfo.alias, !avatarChanged);
+        if (avatarChanged)
+            linked.owner.accountModel->setAvatar(linked.owner.id, profileInfo.avatar, true);
         return;
     }
     vCardFile.remove();
-- 
GitLab