From b4d39a1e2c142e2e0227ab3d2094638cae05c929 Mon Sep 17 00:00:00 2001
From: Adrien Beraud <adrien.beraud@savoirfairelinux.com>
Date: Wed, 12 Jun 2024 14:46:20 -0400
Subject: [PATCH] JamiAccount: save registername as lowercase

Gitlab: #1010
Change-Id: Id7356b61b468bcb946861e34706ff372f8ba9161
---
 src/jamidht/archive_account_manager.cpp |  2 +-
 src/jamidht/jamiaccount.cpp             |  8 ++++----
 src/jamidht/namedirectory.cpp           | 24 ++++++++++++------------
 src/jamidht/namedirectory.h             |  2 +-
 src/jamidht/server_account_manager.cpp  |  4 ++--
 5 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/src/jamidht/archive_account_manager.cpp b/src/jamidht/archive_account_manager.cpp
index 0a44d6f784..32186189d0 100644
--- a/src/jamidht/archive_account_manager.cpp
+++ b/src/jamidht/archive_account_manager.cpp
@@ -839,7 +839,7 @@ ArchiveAccountManager::registerName(const std::string& name,
         ethAccount = dev::KeyPair(dev::Secret(archive.eth_key)).address().hex();
     } catch (const std::exception& e) {
         // JAMI_ERR("[Auth] can't export account: %s", e.what());
-        cb(NameDirectory::RegistrationResponse::invalidCredentials);
+        cb(NameDirectory::RegistrationResponse::invalidCredentials, name);
         return;
     }
 
diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp
index 4d7fa78080..c02a9af90f 100644
--- a/src/jamidht/jamiaccount.cpp
+++ b/src/jamidht/jamiaccount.cpp
@@ -1453,7 +1453,7 @@ JamiAccount::registerName(const std::string& name,
             name,
             scheme,
             password,
-            [acc = getAccountID(), name, w = weak()](NameDirectory::RegistrationResponse response) {
+            [acc = getAccountID(), name, w = weak()](NameDirectory::RegistrationResponse response, const std::string& regName) {
                 int res
                     = (response == NameDirectory::RegistrationResponse::success)
                           ? 0
@@ -1467,10 +1467,10 @@ JamiAccount::registerName(const std::string& name,
                                                : 4)));
                 if (response == NameDirectory::RegistrationResponse::success) {
                     if (auto this_ = w.lock()) {
-                        this_->registeredName_ = name;
-                        if (this_->config().registeredName != name)
+                        this_->registeredName_ = regName;
+                        if (this_->config().registeredName != regName)
                             this_->editConfig(
-                                [&](JamiAccountConfig& config) { config.registeredName = name; });
+                                [&](JamiAccountConfig& config) { config.registeredName = regName; });
                         emitSignal<libjami::ConfigurationSignal::VolatileDetailsChanged>(
                             this_->accountID_, this_->getVolatileAccountDetails());
                     }
diff --git a/src/jamidht/namedirectory.cpp b/src/jamidht/namedirectory.cpp
index bd1555a549..2b95521e51 100644
--- a/src/jamidht/namedirectory.cpp
+++ b/src/jamidht/namedirectory.cpp
@@ -344,16 +344,16 @@ NameDirectory::registerName(const std::string& addr,
 {
     std::string name {n};
     if (not validateName(name)) {
-        cb(RegistrationResponse::invalidName);
+        cb(RegistrationResponse::invalidName, name);
         return;
     }
     toLower(name);
     auto cacheResult = addrCache(name);
     if (not cacheResult.empty()) {
         if (cacheResult == addr)
-            cb(RegistrationResponse::success);
+            cb(RegistrationResponse::success, name);
         else
-            cb(RegistrationResponse::alreadyTaken);
+            cb(RegistrationResponse::alreadyTaken, name);
         return;
     }
     std::string body = fmt::format("{{\"addr\":\"{}\",\"owner\":\"{}\",\"signature\":\"{}\",\"publickey\":\"{}\"}}",
@@ -374,22 +374,22 @@ NameDirectory::registerName(const std::string& addr,
         request->add_on_done_callback(
             [this, name, addr, cb = std::move(cb)](const dht::http::Response& response) {
                 if (response.status_code == 400) {
-                    cb(RegistrationResponse::incompleteRequest);
+                    cb(RegistrationResponse::incompleteRequest, name);
                     JAMI_ERR("RegistrationResponse::incompleteRequest");
                 } else if (response.status_code == 401) {
-                    cb(RegistrationResponse::signatureVerificationFailed);
+                    cb(RegistrationResponse::signatureVerificationFailed, name);
                     JAMI_ERR("RegistrationResponse::signatureVerificationFailed");
                 } else if (response.status_code == 403) {
-                    cb(RegistrationResponse::alreadyTaken);
+                    cb(RegistrationResponse::alreadyTaken, name);
                     JAMI_ERR("RegistrationResponse::alreadyTaken");
                 } else if (response.status_code == 409) {
-                    cb(RegistrationResponse::alreadyTaken);
+                    cb(RegistrationResponse::alreadyTaken, name);
                     JAMI_ERR("RegistrationResponse::alreadyTaken");
                 } else if (response.status_code > 400 && response.status_code < 500) {
-                    cb(RegistrationResponse::alreadyTaken);
+                    cb(RegistrationResponse::alreadyTaken, name);
                     JAMI_ERR("RegistrationResponse::alreadyTaken");
                 } else if (response.status_code < 200 || response.status_code > 299) {
-                    cb(RegistrationResponse::error);
+                    cb(RegistrationResponse::error, name);
                     JAMI_ERR("RegistrationResponse::error");
                 } else {
                     Json::Value json;
@@ -401,7 +401,7 @@ NameDirectory::registerName(const std::string& addr,
                                        response.body.data() + response.body.size(),
                                        &json,
                                        &err)) {
-                        cb(RegistrationResponse::error);
+                        cb(RegistrationResponse::error, name);
                         return;
                     }
                     auto success = json["success"].asBool();
@@ -414,7 +414,7 @@ NameDirectory::registerName(const std::string& addr,
                         addrCache_.emplace(name, addr);
                         nameCache_.emplace(addr, name);
                     }
-                    cb(success ? RegistrationResponse::success : RegistrationResponse::error);
+                    cb(success ? RegistrationResponse::success : RegistrationResponse::error, name);
                 }
                 std::lock_guard lk(requestsMtx_);
                 if (auto req = response.request.lock())
@@ -427,7 +427,7 @@ NameDirectory::registerName(const std::string& addr,
         request->send();
     } catch (const std::exception& e) {
         JAMI_ERR("Error when performing name registration: %s", e.what());
-        cb(RegistrationResponse::error);
+        cb(RegistrationResponse::error, name);
         std::lock_guard lk(requestsMtx_);
         if (request)
             requests_.erase(request);
diff --git a/src/jamidht/namedirectory.h b/src/jamidht/namedirectory.h
index e7d3b40ec5..2d7a862ab5 100644
--- a/src/jamidht/namedirectory.h
+++ b/src/jamidht/namedirectory.h
@@ -69,7 +69,7 @@ public:
     using LookupCallback = std::function<void(const std::string& result, Response response)>;
     using SearchResult = std::vector<std::map<std::string, std::string>>;
     using SearchCallback = std::function<void(const SearchResult& result, Response response)>;
-    using RegistrationCallback = std::function<void(RegistrationResponse response)>;
+    using RegistrationCallback = std::function<void(RegistrationResponse response, const std::string& name)>;
 
     NameDirectory(const std::string& serverUrl, std::shared_ptr<dht::Logger> l = {});
     ~NameDirectory();
diff --git a/src/jamidht/server_account_manager.cpp b/src/jamidht/server_account_manager.cpp
index d708e2d1d9..d69e115fd8 100644
--- a/src/jamidht/server_account_manager.cpp
+++ b/src/jamidht/server_account_manager.cpp
@@ -627,9 +627,9 @@ ServerAccountManager::revokeDevice(const std::string& device,
 }
 
 void
-ServerAccountManager::registerName(const std::string&, std::string_view scheme, const std::string&, RegistrationCallback cb)
+ServerAccountManager::registerName(const std::string& name, std::string_view scheme, const std::string&, RegistrationCallback cb)
 {
-    cb(NameDirectory::RegistrationResponse::unsupported);
+    cb(NameDirectory::RegistrationResponse::unsupported, name);
 }
 
 bool
-- 
GitLab