From b8bf25e0f97e11056f427ed817bb4789f9d542d2 Mon Sep 17 00:00:00 2001
From: Leopold Chappuis <leopold.chappuis@savoirfairelinux.com>
Date: Thu, 21 Nov 2024 13:50:42 -0500
Subject: [PATCH] updateProfile: Add Filetype

To enhance the flexibility and scalability of this API, we can include the filetype parameter.

Change-Id: I0fcb69a1073ac2cb4576d7a701e5339c1c5db5de
---
 .../cx.ring.Ring.ConfigurationManager.xml     |  5 +++
 bin/dbus/dbusconfigurationmanager.hpp         |  4 +--
 bin/jni/configurationmanager.i                |  2 +-
 bin/nodejs/configurationmanager.i             |  2 +-
 src/account.h                                 |  2 +-
 src/client/configurationmanager.cpp           |  4 +--
 src/jami/configurationmanager_interface.h     |  2 +-
 src/jamidht/jamiaccount.cpp                   | 34 +++++++++++--------
 src/jamidht/jamiaccount.h                     |  2 +-
 src/sip/sipaccount.cpp                        | 30 +++++++++-------
 src/sip/sipaccount.h                          |  2 +-
 11 files changed, 51 insertions(+), 38 deletions(-)

diff --git a/bin/dbus/cx.ring.Ring.ConfigurationManager.xml b/bin/dbus/cx.ring.Ring.ConfigurationManager.xml
index d5e99a09c..ce48ee83a 100644
--- a/bin/dbus/cx.ring.Ring.ConfigurationManager.xml
+++ b/bin/dbus/cx.ring.Ring.ConfigurationManager.xml
@@ -579,6 +579,11 @@
             <arg type="s" name="accountId" direction="in"/>
             <arg type="s" name="displayName" direction="in"/>
             <arg type="s" name="avatar" direction="in"/>
+            <arg type="s" name="fileType" direction="in">
+                <tp:docstring>
+                The file type of the avatar, for now JPEG or PNG
+                </tp:docstring>
+            </arg>
             <arg type="i" name="flag" direction="in"/>
             <tp:docstring>
                Update the profile of the account and send it to peers.
diff --git a/bin/dbus/dbusconfigurationmanager.hpp b/bin/dbus/dbusconfigurationmanager.hpp
index b5d267a67..b418c7b9a 100644
--- a/bin/dbus/dbusconfigurationmanager.hpp
+++ b/bin/dbus/dbusconfigurationmanager.hpp
@@ -217,9 +217,9 @@ public:
     }
 
     void
-    updateProfile(const std::string& accountID,const std::string& displayName, const std::string& avatar, const int32_t& flag)
+    updateProfile(const std::string& accountID,const std::string& displayName, const std::string& avatar, const std::string& fileType, const int32_t& flag)
     {
-        libjami::updateProfile(accountID, displayName, avatar, flag);
+        libjami::updateProfile(accountID, displayName, avatar, fileType, flag);
     }
 
     auto
diff --git a/bin/jni/configurationmanager.i b/bin/jni/configurationmanager.i
index af6e2a87a..2fca6883a 100644
--- a/bin/jni/configurationmanager.i
+++ b/bin/jni/configurationmanager.i
@@ -90,7 +90,7 @@ std::vector<std::map<std::string, std::string>> getConnectionList(const std::str
 std::vector<std::map<std::string, std::string>> getChannelList(const std::string& accountId, const std::string& connectionId);
 std::string addAccount(const std::map<std::string, std::string>& details);
 void removeAccount(const std::string& accountId);
-void updateProfile(const std::string& accountId,const std::string& displayName, const std::string& avatar, int32_t flag);
+void updateProfile(const std::string& accountId,const std::string& displayName, const std::string& avatar,const std::string& fileType, int32_t flag);
 std::vector<std::string> getAccountList();
 void sendRegister(const std::string& accountId, bool enable);
 void registerAllAccounts(void);
diff --git a/bin/nodejs/configurationmanager.i b/bin/nodejs/configurationmanager.i
index 8cc261ee5..1b09d14ea 100644
--- a/bin/nodejs/configurationmanager.i
+++ b/bin/nodejs/configurationmanager.i
@@ -86,7 +86,7 @@ std::vector<std::map<std::string, std::string>> getConnectionList(const std::str
 std::vector<std::map<std::string, std::string>> getChannelList(const std::string& accountId, const std::string& connectionId);
 std::string addAccount(const std::map<std::string, std::string>& details);
 void removeAccount(const std::string& accountId);
-void updateProfile(const std::string& accountId,const std::string& displayName, const std::string& avatar, int32_t flag);
+void updateProfile(const std::string& accountId,const std::string& displayName, const std::string& avatar,const std::string& fileType, int32_t flag);
 std::vector<std::string> getAccountList();
 void sendRegister(const std::string& accountId, bool enable);
 void registerAllAccounts(void);
diff --git a/src/account.h b/src/account.h
index c38ab4fa4..1d6fccae3 100644
--- a/src/account.h
+++ b/src/account.h
@@ -210,7 +210,7 @@ public:
 
     virtual std::map<std::string, std::string> getNearbyPeers() const { return {}; }
 
-    virtual void updateProfile(const std::string& /*displayName*/,  const std::string& /*avatar*/, int32_t /*flag*/) = 0;
+    virtual void updateProfile(const std::string& /*displayName*/,  const std::string& /*avatar*/, const std::string& /*fileType*/, int32_t /*flag*/) = 0;
 
     std::map<std::string, std::string> getProfileVcard() const;
 
diff --git a/src/client/configurationmanager.cpp b/src/client/configurationmanager.cpp
index e46ca3db1..a48a689f5 100644
--- a/src/client/configurationmanager.cpp
+++ b/src/client/configurationmanager.cpp
@@ -294,10 +294,10 @@ getNearbyPeers(const std::string& accountId)
 }
 
 void
-updateProfile(const std::string& accountId,const std::string& displayName, const std::string& avatar, int32_t flag)
+updateProfile(const std::string& accountId,const std::string& displayName, const std::string& avatar, const std::string& fileType, int32_t flag)
 {
     if (const auto acc = jami::Manager::instance().getAccount(accountId)){
-        acc->updateProfile(displayName, avatar, flag);
+        acc->updateProfile(displayName, avatar, fileType, flag);
     }
 }
 
diff --git a/src/jami/configurationmanager_interface.h b/src/jami/configurationmanager_interface.h
index 31a08cff6..239f1e5f2 100644
--- a/src/jami/configurationmanager_interface.h
+++ b/src/jami/configurationmanager_interface.h
@@ -104,7 +104,7 @@ LIBJAMI_PUBLIC bool cancelMessage(const std::string& accountId, uint64_t message
 LIBJAMI_PUBLIC std::vector<Message> getLastMessages(const std::string& accountId,
                                                     const uint64_t& base_timestamp);
 LIBJAMI_PUBLIC std::map<std::string, std::string> getNearbyPeers(const std::string& accountId);
-LIBJAMI_PUBLIC void updateProfile(const std::string& accountId,const std::string& displayName, const std::string& avatar, int32_t flag);
+LIBJAMI_PUBLIC void updateProfile(const std::string& accountId,const std::string& displayName, const std::string& avatar,const std::string& fileType, int32_t flag);
 LIBJAMI_PUBLIC int getMessageStatus(uint64_t id);
 LIBJAMI_PUBLIC int getMessageStatus(const std::string& accountId, uint64_t id);
 LIBJAMI_PUBLIC void setIsComposing(const std::string& accountId,
diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp
index 64be3b823..32e06a0a0 100644
--- a/src/jamidht/jamiaccount.cpp
+++ b/src/jamidht/jamiaccount.cpp
@@ -3411,8 +3411,11 @@ JamiAccount::sendProfileToPeers()
 void
 JamiAccount::updateProfile(const std::string& displayName,
                            const std::string& avatar,
+                           const std::string& fileType,
                            int32_t flag)
 {
+    // if the fileType is empty then only the display name will be upated
+
     const auto& accountUri = accountManager_->getInfo()->accountId;
     const auto& path = profilePath();
     const auto& profiles = idPath_ / "profiles";
@@ -3437,24 +3440,25 @@ JamiAccount::updateProfile(const std::string& displayName,
     editConfig([&](JamiAccountConfig& config) { config.displayName = displayName; });
     emitSignal<libjami::ConfigurationSignal::AccountDetailsChanged>(getAccountID(),
                                                                     getAccountDetails());
-    if (flag == 0) {
-        vCard::utils::removeByKey(profile, "PHOTO");
-        const auto& avatarPath = std::filesystem::path(avatar);
-        if (std::filesystem::exists(avatarPath)) {
-            try {
-                const auto& base64 = jami::base64::encode(fileutils::loadFile(avatarPath));
-                profile["PHOTO;ENCODING=BASE64;TYPE=PNG"] = base64;
-            } catch (const std::exception& e) {
-                JAMI_ERROR("Failed to load avatar: {}", e.what());
+
+    if (!fileType.empty()) {
+        const std::string& key = "PHOTO;ENCODING=BASE64;TYPE=" + fileType;
+        if (flag == 0) {
+            vCard::utils::removeByKey(profile, "PHOTO");
+            const auto& avatarPath = std::filesystem::path(avatar);
+            if (std::filesystem::exists(avatarPath)) {
+                try {
+                    const auto& base64 = jami::base64::encode(fileutils::loadFile(avatarPath));
+                    profile[key] = base64;
+                } catch (const std::exception& e) {
+                    JAMI_ERROR("Failed to load avatar: {}", e.what());
+                }
             }
+        } else if (flag == 1) {
+            vCard::utils::removeByKey(profile, "PHOTO");
+            profile[key] = avatar;
         }
-    } else if (flag == 1) {
-        vCard::utils::removeByKey(profile, "PHOTO");
-        profile["PHOTO;ENCODING=BASE64;TYPE=PNG"] = avatar;
     }
-
-    // nothing happens to the profile photo if the avatarPath is invalid
-    // and not empty. So far it seems to be the best default behavior.
     try {
         std::filesystem::path tmpPath = vCardPath.string() + ".tmp";
         std::ofstream file(tmpPath);
diff --git a/src/jamidht/jamiaccount.h b/src/jamidht/jamiaccount.h
index d23221611..a9e6e6a26 100644
--- a/src/jamidht/jamiaccount.h
+++ b/src/jamidht/jamiaccount.h
@@ -426,7 +426,7 @@ public:
      * @param avatar Current or new avatar
      * @param flag  0 for path to avatar, 1 for base64 avatar
      */
-    void updateProfile(const std::string& displayName, const std::string& avatar, int32_t flag) override;
+    void updateProfile(const std::string& displayName, const std::string& avatar, const std::string& fileType, int32_t flag) override;
 
 #ifdef LIBJAMI_TESTABLE
     dhtnet::ConnectionManager& connectionManager() { return *connectionManager_; }
diff --git a/src/sip/sipaccount.cpp b/src/sip/sipaccount.cpp
index 735138bc5..8b543c109 100644
--- a/src/sip/sipaccount.cpp
+++ b/src/sip/sipaccount.cpp
@@ -148,6 +148,7 @@ SIPAccount::~SIPAccount() noexcept
 void
 SIPAccount::updateProfile(const std::string& displayName,
                           const std::string& avatar,
+                          const std::string& fileType,
                           int32_t flag)
 {
     auto vCardPath = idPath_ / "profile.vcf";
@@ -158,21 +159,24 @@ SIPAccount::updateProfile(const std::string& displayName,
     }
     profile["FN"] = displayName;
 
-    if (flag == 0) {
-        vCard::utils::removeByKey(profile, "PHOTO");
-        const auto& avatarPath = std::filesystem::path(avatar);
-        if (std::filesystem::exists(avatarPath)) {
-            try {
-                profile["PHOTO;ENCODING=BASE64;TYPE=PNG"] = base64::encode(fileutils::loadFile(avatarPath));
-            } catch (const std::exception& e) {
-                JAMI_ERROR("Failed to load avatar: {}", e.what());
-            }
-        } else if (avatarPath.empty()) {
+    if (!fileType.empty()) {
+        const std::string& key = "PHOTO;ENCODING=BASE64;TYPE=" + fileType;
+        if (flag == 0) {
             vCard::utils::removeByKey(profile, "PHOTO");
-            profile["PHOTO;ENCODING=BASE64;TYPE=PNG"] = "";
+            const auto& avatarPath = std::filesystem::path(avatar);
+            if (std::filesystem::exists(avatarPath)) {
+                try {
+                    profile[key] = base64::encode(fileutils::loadFile(avatarPath));
+                } catch (const std::exception& e) {
+                    JAMI_ERROR("Failed to load avatar: {}", e.what());
+                }
+            } else if (avatarPath.empty()) {
+                vCard::utils::removeByKey(profile, "PHOTO");
+                profile[key] = "";
+            }
+        } else if (flag == 1) {
+            profile[key] = avatar;
         }
-    } else if (flag == 1) {
-        profile["PHOTO;ENCODING=BASE64;TYPE=PNG"] = avatar;
     }
 
     // nothing happens to the profile photo if the avatarPath is invalid
diff --git a/src/sip/sipaccount.h b/src/sip/sipaccount.h
index ff7927be0..bdf7ea4f1 100644
--- a/src/sip/sipaccount.h
+++ b/src/sip/sipaccount.h
@@ -130,7 +130,7 @@ public:
     /**
      * updates SIP account profile
      */
-    void updateProfile(const std::string& displayName, const std::string& avatar, int32_t flag) override;
+    void updateProfile(const std::string& displayName, const std::string& avatar, const std::string& fileType, int32_t flag) override;
 
     /**
      * Initialize the SIP voip link with the account parameters and send registration
-- 
GitLab