From f074cf9dc171052eaf6756a558a9bbafd5e075dd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Fri, 22 Dec 2023 14:44:20 -0500
Subject: [PATCH] uri: move DATA_TRANSFER_SCHEME and cleanup

Change-Id: I3458251d232d70ae6c04ab19890c88e8880109b5
---
 src/jamidht/jamiaccount.cpp              | 20 +++++++++-----------
 src/jamidht/transfer_channel_handler.cpp |  5 +++--
 src/uri.cpp                              |  2 +-
 src/uri.h                                |  6 +++++-
 4 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp
index 015f54959f..e91d613e27 100644
--- a/src/jamidht/jamiaccount.cpp
+++ b/src/jamidht/jamiaccount.cpp
@@ -108,9 +108,6 @@ constexpr pj_str_t STR_MESSAGE_ID = jami::sip_utils::CONST_PJ_STR("Message-ID");
 static constexpr const char MIME_TYPE_IMDN[] {"message/imdn+xml"};
 static constexpr const char MIME_TYPE_IM_COMPOSING[] {"application/im-iscomposing+xml"};
 static constexpr const char MIME_TYPE_INVITE_JSON[] {"application/invite+json"};
-static constexpr const char FILE_URI[] {"file://"};
-static constexpr const char VCARD_URI[] {"vcard://"};
-static constexpr const char DATA_TRANSFER_URI[] {"data-transfer://"};
 static constexpr const char DEVICE_ID_PATH[] {"ring_device"};
 static constexpr std::chrono::steady_clock::duration COMPOSING_TIMEOUT {std::chrono::seconds(12)};
 static constexpr auto TREATED_PATH = "treatedImMessages";
@@ -3693,9 +3690,10 @@ JamiAccount::sendProfile(const std::string& convId,
                          const std::string& peerUri,
                          const std::string& deviceId)
 {
-    if (not std::filesystem::is_regular_file(profilePath()))
+    auto accProfilePath = profilePath();
+    if (not std::filesystem::is_regular_file(accProfilePath))
         return;
-    auto currentSha3 = fileutils::sha3File(profilePath());
+    auto currentSha3 = fileutils::sha3File(accProfilePath);
     // VCard sync for peerUri
     if (not needToSendProfile(peerUri, deviceId, currentSha3)) {
         JAMI_DEBUG("Peer {} already got an up-to-date vcard", peerUri);
@@ -3703,14 +3701,14 @@ JamiAccount::sendProfile(const std::string& convId,
     }
     // We need a new channel
     transferFile(convId,
-                 profilePath().string(),
+                 accProfilePath.string(),
                  deviceId,
                  "profile.vcf",
                  "",
                  0,
                  0,
                  currentSha3,
-                 fileutils::lastWriteTimeInSeconds(profilePath()),
+                 fileutils::lastWriteTimeInSeconds(accProfilePath),
                  [accId = getAccountID(), peerUri, deviceId]() {
                      // Mark the VCard as sent
                      auto sendDir = fileutils::get_cache_dir() / accId / "vcard" / peerUri;
@@ -4082,8 +4080,8 @@ JamiAccount::transferFile(const std::string& conversationId,
         fmt::format("profile.vcf?sha3={}{}", sha3Sum, modified) : fileId;
     auto channelName
         = conversationId.empty()
-              ? fmt::format("{}profile.vcf?sha3={}{}", DATA_TRANSFER_URI, sha3Sum, modified)
-              : fmt::format("{}{}/{}/{}", DATA_TRANSFER_URI, conversationId, currentDeviceId(), fid);
+              ? fmt::format("{}profile.vcf?sha3={}{}", DATA_TRANSFER_SCHEME, sha3Sum, modified)
+              : fmt::format("{}{}/{}/{}", DATA_TRANSFER_SCHEME, conversationId, currentDeviceId(), fid);
     std::lock_guard<std::mutex> lkCM(connManagerMtx_);
     if (!connectionManager_)
         return;
@@ -4137,7 +4135,7 @@ JamiAccount::askForFileChannel(const std::string& conversationId,
             return;
 
         auto channelName = fmt::format("{}{}/{}/{}",
-                                       DATA_TRANSFER_URI,
+                                       DATA_TRANSFER_SCHEME,
                                        conversationId,
                                        currentDeviceId(),
                                        fileId);
@@ -4196,7 +4194,7 @@ JamiAccount::askForProfile(const std::string& conversationId,
         return;
 
     auto channelName = fmt::format("{}{}/profile/{}.vcf",
-                                   DATA_TRANSFER_URI,
+                                   DATA_TRANSFER_SCHEME,
                                    conversationId,
                                    memberUri);
     // We can avoid to negotiate new sessions, as the file notif
diff --git a/src/jamidht/transfer_channel_handler.cpp b/src/jamidht/transfer_channel_handler.cpp
index bb5373d1eb..172fd47cfd 100644
--- a/src/jamidht/transfer_channel_handler.cpp
+++ b/src/jamidht/transfer_channel_handler.cpp
@@ -53,7 +53,7 @@ TransferChannelHandler::onRequest(const std::shared_ptr<dht::crypto::Certificate
         return false;
     auto uri = cert->issuer->getId().toString();
     // Else, check if it's a profile or file in a conversation.
-    auto idstr = std::string_view(name).substr(16);
+    auto idstr = std::string_view(name).substr(DATA_TRANSFER_SCHEME.size());
     // Remove arguments for now
     auto sep = idstr.find_last_of('?');
     idstr = idstr.substr(0, sep);
@@ -95,7 +95,8 @@ TransferChannelHandler::onReady(const std::shared_ptr<dht::crypto::Certificate>&
     if (!acc)
         return;
 
-    auto idstr = name.substr(16);
+    // Remove scheme
+    auto idstr = name.substr(DATA_TRANSFER_SCHEME.size());
     // Parse arguments
     auto sep = idstr.find_last_of('?');
     std::string arguments;
diff --git a/src/uri.cpp b/src/uri.cpp
index 2b2a4077a4..eb98d5966b 100644
--- a/src/uri.cpp
+++ b/src/uri.cpp
@@ -21,7 +21,7 @@
 
 namespace jami {
 
-Uri::Uri(const std::string_view& uri)
+Uri::Uri(std::string_view uri)
 {
     // TODO better handling of Uri, for now it's only used for
     // setMessageDisplayed to differentiate swarm:xxx
diff --git a/src/uri.h b/src/uri.h
index 118b952d7e..3027210ed8 100644
--- a/src/uri.h
+++ b/src/uri.h
@@ -24,6 +24,10 @@
 
 namespace jami {
 
+using namespace std::string_view_literals;
+
+static constexpr std::string_view DATA_TRANSFER_SCHEME = "data-transfer://"sv;
+
 class Uri
 {
 public:
@@ -38,7 +42,7 @@ public:
         UNRECOGNIZED   // Anything that doesn't fit in other categories
     };
 
-    Uri(const std::string_view& uri);
+    Uri(std::string_view uri);
 
     const std::string& authority() const;
     Scheme scheme() const;
-- 
GitLab