From f703a3d251726bb7e02732b680ca478768fda115 Mon Sep 17 00:00:00 2001
From: Kateryna Kostiuk <katryna.kostiuk@savoirfairelinux.com>
Date: Thu, 13 Apr 2023 13:06:07 -0400
Subject: [PATCH] iOS: add flag to avoid conversation sync on register

When an account is registered from the notification extension,
it only needs to fetch commits for one contact and not make
connections with other contacts, since the extension has limits
for time and memory.

It also do not need bootstrap conversation.

Change-Id: Ia5815dfc47176989c8df10766613f063d3211bee
---
 src/jami/jami.h             |   3 +-
 src/jamidht/jamiaccount.cpp | 104 +++++++++++++++++++-----------------
 src/manager.cpp             |   2 +
 src/manager.h               |   2 +
 src/ring_api.cpp            |   2 +
 5 files changed, 62 insertions(+), 51 deletions(-)

diff --git a/src/jami/jami.h b/src/jami/jami.h
index 13dcf98ee7..6c88ae1d42 100644
--- a/src/jami/jami.h
+++ b/src/jami/jami.h
@@ -42,7 +42,8 @@ enum InitFlag {
     LIBJAMI_FLAG_IOS_EXTENSION = 1 << 4,
     LIBJAMI_FLAG_NO_LOCAL_AUDIO = 1 << 6,
     LIBJAMI_FLAG_NO_LOCAL_VIDEO = 1 << 7,
-    LIBJAMI_FLAG_NO_LOCAL_MEDIA = LIBJAMI_FLAG_NO_LOCAL_AUDIO | LIBJAMI_FLAG_NO_LOCAL_VIDEO
+    LIBJAMI_FLAG_NO_LOCAL_MEDIA = LIBJAMI_FLAG_NO_LOCAL_AUDIO | LIBJAMI_FLAG_NO_LOCAL_VIDEO,
+    LIBJAMI_FLAG_NO_AUTOSYNC = 1 << 8
 };
 
 /**
diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp
index f3033a17a6..ae9570523a 100644
--- a/src/jamidht/jamiaccount.cpp
+++ b/src/jamidht/jamiaccount.cpp
@@ -1087,16 +1087,18 @@ JamiAccount::loadAccount(const std::string& archive_password,
         [this](const std::string& uri, bool confirmed) {
             if (!id_.first)
                 return;
-            dht::ThreadPool::io().run([w=weak(), uri, confirmed] {
-                if (auto shared = w.lock()) {
-                    if (auto cm = shared->convModule()) {
-                        auto activeConv = cm->getOneToOneConversation(uri);
-                        if (!activeConv.empty())
-                            cm->bootstrap(activeConv);
+            if (jami::Manager::instance().syncOnRegister) {
+                dht::ThreadPool::io().run([w=weak(), uri, confirmed] {
+                    if (auto shared = w.lock()) {
+                        if (auto cm = shared->convModule()) {
+                            auto activeConv = cm->getOneToOneConversation(uri);
+                            if (!activeConv.empty())
+                                cm->bootstrap(activeConv);
+                        }
+                        emitSignal<libjami::ConfigurationSignal::ContactAdded>(shared->getAccountID(), uri, confirmed);
                     }
-                    emitSignal<libjami::ConfigurationSignal::ContactAdded>(shared->getAccountID(), uri, confirmed);
-                }
-            });
+                });
+            }
         },
         [this](const std::string& uri, bool banned) {
             if (!id_.first)
@@ -1930,47 +1932,49 @@ JamiAccount::doRegister_()
 
             setRegistrationState(state);
         };
-        context.identityAnnouncedCb = [this](bool ok) {
-            if (!ok)
-                return;
-            accountManager_->startSync(
-                [this](const std::shared_ptr<dht::crypto::Certificate>& crt) {
-                    if (!crt)
-                        return;
-                    auto deviceId = crt->getLongId().toString();
-                    if (accountManager_->getInfo()->deviceId == deviceId)
-                        return;
-
-                    std::unique_lock<std::mutex> lk(connManagerMtx_);
-                    initConnectionManager();
-                    channelHandlers_[Uri::Scheme::SYNC]
-                        ->connect(crt->getLongId(),
-                                  "",
-                                  [this](std::shared_ptr<ChannelSocket> socket,
-                                         const DeviceId& deviceId) {
-                                      if (socket)
-                                          syncModule()->syncWith(deviceId, socket);
-                                  });
-                    lk.unlock();
-                    requestSIPConnection(
-                        getUsername(),
-                        crt->getLongId(),
-                        "sync"); // For git notifications, will use the same socket as sync
-                },
-                [this] {
-                    deviceAnnounced_ = true;
-
-                    // Bootstrap at the end to avoid to be long to load.
-                    dht::ThreadPool::io().run([w = weak()] {
-                        if (auto shared = w.lock()) {
-                            std::lock_guard<std::recursive_mutex> lock(shared->configurationMutex_);
-                            shared->convModule()->bootstrap();
-                        }
-                    });
-                    emitSignal<libjami::ConfigurationSignal::VolatileDetailsChanged>(
-                        accountID_, getVolatileAccountDetails());
-                });
-        };
+        if (jami::Manager::instance().syncOnRegister) {
+            context.identityAnnouncedCb = [this](bool ok) {
+                if (!ok)
+                    return;
+                accountManager_->startSync(
+                                           [this](const std::shared_ptr<dht::crypto::Certificate>& crt) {
+                                               if (!crt)
+                                                   return;
+                                               auto deviceId = crt->getLongId().toString();
+                                               if (accountManager_->getInfo()->deviceId == deviceId)
+                                                   return;
+
+                                               std::unique_lock<std::mutex> lk(connManagerMtx_);
+                                               initConnectionManager();
+                                               channelHandlers_[Uri::Scheme::SYNC]
+                                               ->connect(crt->getLongId(),
+                                                         "",
+                                                         [this](std::shared_ptr<ChannelSocket> socket,
+                                                                const DeviceId& deviceId) {
+                                                   if (socket)
+                                                       syncModule()->syncWith(deviceId, socket);
+                                               });
+                                               lk.unlock();
+                                               requestSIPConnection(
+                                                                    getUsername(),
+                                                                    crt->getLongId(),
+                                                                    "sync"); // For git notifications, will use the same socket as sync
+                                           },
+                                           [this] {
+                                               deviceAnnounced_ = true;
+
+                                               // Bootstrap at the end to avoid to be long to load.
+                                               dht::ThreadPool::io().run([w = weak()] {
+                                                   if (auto shared = w.lock()) {
+                                                       std::lock_guard<std::recursive_mutex> lock(shared->configurationMutex_);
+                                                       shared->convModule()->bootstrap();
+                                                   }
+                                               });
+                                               emitSignal<libjami::ConfigurationSignal::VolatileDetailsChanged>(
+                                                                                                                accountID_, getVolatileAccountDetails());
+                                           });
+            };
+        }
 
         setRegistrationState(RegistrationState::TRYING);
         dht_->run(dhtPortUsed(), config, std::move(context));
diff --git a/src/manager.cpp b/src/manager.cpp
index c9e0af2727..c784db74cc 100644
--- a/src/manager.cpp
+++ b/src/manager.cpp
@@ -141,6 +141,8 @@ std::atomic_bool Manager::initialized = {false};
 bool Manager::isIOSExtension = {false};
 #endif
 
+bool Manager::syncOnRegister = {true};
+
 static void
 copy_over(const std::string& srcPath, const std::string& destPath)
 {
diff --git a/src/manager.h b/src/manager.h
index dc05be7505..d3de5ebbf6 100644
--- a/src/manager.h
+++ b/src/manager.h
@@ -119,6 +119,8 @@ public:
     static bool isIOSExtension;
 #endif
 
+    static bool syncOnRegister;
+
     /**
      * Initialisation of thread (sound) and map.
      * Init a new VoIPLink, audio codec and audio driver
diff --git a/src/ring_api.cpp b/src/ring_api.cpp
index 8afa994705..b51423c235 100644
--- a/src/ring_api.cpp
+++ b/src/ring_api.cpp
@@ -69,6 +69,8 @@ init(enum InitFlag flags) noexcept
         if (flags & LIBJAMI_FLAG_IOS_EXTENSION)
             manager.isIOSExtension = true;
 #endif
+        if (flags & LIBJAMI_FLAG_NO_AUTOSYNC)
+            manager.syncOnRegister = false;
 
         return true;
     } catch (...) {
-- 
GitLab