notification: load one account and one conversation

Change-Id: I73f59b8cf43e20076623a4b2d842966aa86afb9a
diff --git a/Ring/jamiNotificationExtension/Adapter.h b/Ring/jamiNotificationExtension/Adapter.h
index 32cb326..8dce354 100644
--- a/Ring/jamiNotificationExtension/Adapter.h
+++ b/Ring/jamiNotificationExtension/Adapter.h
@@ -26,8 +26,8 @@
 
 @property (class, nonatomic, weak) id <AdapterDelegate> delegate;
 
-- (void)stop;
-- (BOOL)start:(NSString*)accountId;
+- (void)stopForAccountId:(NSString*)accountId;
+- (BOOL)start:(NSString*)accountId convId:(NSString*)convId loadAll:(BOOL)loadAll;
 - (void)pushNotificationReceived:(NSString*)from message:(NSDictionary*)data;
 - (bool)downloadFileWithFileId:(NSString*)fileId
                      accountId:(NSString*)accountId
diff --git a/Ring/jamiNotificationExtension/Adapter.mm b/Ring/jamiNotificationExtension/Adapter.mm
index 356c7c9..968341e 100644
--- a/Ring/jamiNotificationExtension/Adapter.mm
+++ b/Ring/jamiNotificationExtension/Adapter.mm
@@ -172,18 +172,17 @@
                         std::string([filePath UTF8String]));
 }
 
-- (BOOL)start:(NSString*)accountId
+- (BOOL)start:(NSString*)accountId convId:(NSString*)convId loadAll:(BOOL)loadAll
 {
     [self registerSignals];
     if (initialized() == true) {
-        reloadConversationsAndRequests(std::string([accountId UTF8String]));
-        setAccountActive(std::string([accountId UTF8String]), true);
+        loadAccountAndConversation(std::string([accountId UTF8String]), loadAll, std::string([convId UTF8String]));
         return true;
     }
 #if DEBUG
-    int flag = LIBJAMI_FLAG_CONSOLE_LOG | LIBJAMI_FLAG_DEBUG | LIBJAMI_FLAG_IOS_EXTENSION | LIBJAMI_FLAG_NO_AUTOSYNC | LIBJAMI_FLAG_NO_LOCAL_AUDIO;
+    int flag = LIBJAMI_FLAG_CONSOLE_LOG | LIBJAMI_FLAG_DEBUG | LIBJAMI_FLAG_IOS_EXTENSION | LIBJAMI_FLAG_NO_AUTOSYNC | LIBJAMI_FLAG_NO_LOCAL_AUDIO | LIBJAMI_FLAG_NO_AUTOLOAD;
 #else
-    int flag = LIBJAMI_FLAG_IOS_EXTENSION | LIBJAMI_FLAG_NO_AUTOSYNC | LIBJAMI_FLAG_NO_LOCAL_AUDIO;
+    int flag = LIBJAMI_FLAG_IOS_EXTENSION | LIBJAMI_FLAG_NO_AUTOSYNC | LIBJAMI_FLAG_NO_LOCAL_AUDIO | LIBJAMI_FLAG_NO_AUTOLOAD;
 #endif
     if (![[NSThread currentThread] isMainThread]) {
         __block bool success;
@@ -194,20 +193,23 @@
                 success = false;
             }
         });
+        loadAccountAndConversation(std::string([accountId UTF8String]), loadAll, std::string([convId UTF8String]));
         return success;
     } else {
+        bool success = false;
         if (init(static_cast<InitFlag>(flag))) {
-            return start({});
+            success = start({});
         }
-        return false;
+        loadAccountAndConversation(std::string([accountId UTF8String]), loadAll, std::string([convId UTF8String]));
+        return success;
     }
 }
 
-- (void)stop
+- (void)stopForAccountId:(NSString*)accountId
 {
     unregisterSignalHandlers();
     confHandlers.clear();
-    [self setAccountsActive:false];
+    setAccountActive(std::string([accountId UTF8String]), false, true);
 }
 
 - (void)setAccountsActive:(BOOL)active
diff --git a/Ring/jamiNotificationExtension/AdapterService.swift b/Ring/jamiNotificationExtension/AdapterService.swift
index 9e4ad1b..888d325 100644
--- a/Ring/jamiNotificationExtension/AdapterService.swift
+++ b/Ring/jamiNotificationExtension/AdapterService.swift
@@ -55,7 +55,7 @@
 
     enum PeerConnectionRequestType {
         case call(peerId: String, isVideo: Bool)
-        case gitMessage
+        case gitMessage(convId: String)
         case clone
         case unknown
     }
@@ -94,13 +94,13 @@
         Adapter.delegate = self
     }
 
-    func startAccountsWithListener(accountId: String, listener: @escaping (EventType, EventData) -> Void) {
+    func startAccountsWithListener(accountId: String, convId: String, loadAll: Bool, listener: @escaping (EventType, EventData) -> Void) {
         self.eventHandler = listener
-        start(accountId: accountId)
+        start(accountId: accountId, convId: convId, loadAll: loadAll)
     }
 
-    func startAccount(accountId: String) {
-        start(accountId: accountId)
+    func startAccount(accountId: String, convId: String, loadAll: Bool) {
+        start(accountId: accountId, convId: convId, loadAll: loadAll)
     }
 
     func pushNotificationReceived(accountId: String, data: [String: String]) {
@@ -112,16 +112,23 @@
         guard let peerId = result?.keys.first,
               let type = result?.values.first else {
             return .unknown}
+        /*
+         Extracts the conversation ID from type formatted as "application/im-gitmessage-id/conversationId".
+         This type is used for connections requests for messages.
+         */
         if type.contains("application/im-gitmessage-id") {
-            return PeerConnectionRequestType.gitMessage
+            let components = type.components(separatedBy: "/")
+            if let last = components.last, components.count > 2 {
+                return PeerConnectionRequestType.gitMessage(convId: last)
+            }
+            // TODO: In the future, when all platforms send the convId, we can simply ignore cases where the convId is not set.
+            return PeerConnectionRequestType.gitMessage(convId: "")
         }
         switch type {
         case "videoCall":
             return PeerConnectionRequestType.call(peerId: peerId, isVideo: true)
         case "audioCall":
             return PeerConnectionRequestType.call(peerId: peerId, isVideo: false)
-        case "text/plain", "application/im-gitmessage-id":
-            return PeerConnectionRequestType.gitMessage
         case "application/clone":
             return PeerConnectionRequestType.clone
         default:
@@ -129,8 +136,8 @@
         }
     }
 
-    func start(accountId: String) {
-        self.adapter.start(accountId)
+    func start(accountId: String, convId: String, loadAll: Bool) {
+        self.adapter.start(accountId, convId: convId, loadAll: loadAll)
     }
 
     func removeDelegate() {
@@ -138,8 +145,8 @@
         self.adapter = nil
     }
 
-    func stop() {
-        self.adapter.stop()
+    func stop(accountId: String) {
+        self.adapter.stop(forAccountId: accountId)
         removeDelegate()
     }
 
diff --git a/Ring/jamiNotificationExtension/NotificationService.swift b/Ring/jamiNotificationExtension/NotificationService.swift
index 66e3ac6..32f3233 100644
--- a/Ring/jamiNotificationExtension/NotificationService.swift
+++ b/Ring/jamiNotificationExtension/NotificationService.swift
@@ -122,15 +122,15 @@
                         guard let self = self else {
                             return
                         }
-                        /// jami will be started. Set accounts to not active state
-                        if self.accountIsActive {
-                            self.accountIsActive = false
-                            self.adapterService.stop()
-                        }
                         var info = request.content.userInfo
                         info["peerId"] = peerId
                         info["hasVideo"] = hasVideo
                         let name = self.bestName(accountId: self.accountId, contactId: peerId)
+                        /// jami will be started. Set accounts to not active state
+                        if self.accountIsActive {
+                            self.accountIsActive = false
+                            self.adapterService.stop(accountId: self.accountId)
+                        }
                         if name.isEmpty {
                             info["displayName"] = peerId
                             self.pendingCalls[peerId] = info
@@ -144,12 +144,12 @@
                     case .call(let peerId, let hasVideo):
                         handleCall(peerId, "\(hasVideo)")
                         return
-                    case .gitMessage:
-                        self.handleGitMessage()
+                    case .gitMessage(let convId):
+                        self.handleGitMessage(convId: convId, loadAll: convId.isEmpty)
                     case .clone:
                         // Should start daemon and wait until clone completed
                         self.waitForCloning = true
-                        self.handleGitMessage()
+                        self.handleGitMessage(convId: "", loadAll: false)
                     case .unknown:
                         break
                     }
@@ -177,18 +177,18 @@
             return false
         }
         self.accountIsActive = true
-        self.adapterService.startAccount(accountId: accountId)
+        self.adapterService.startAccount(accountId: accountId, convId: "", loadAll: false)
         self.adapterService.pushNotificationReceived(accountId: accountId, data: data)
         // wait to proceed pushNotificationReceived
         sleep(5)
         return true
     }
 
-    private func handleGitMessage() {
+    private func handleGitMessage(convId: String, loadAll: Bool) {
         /// check if account already acive
         guard !self.accountIsActive else { return }
         self.accountIsActive = true
-        self.adapterService.startAccountsWithListener(accountId: self.accountId) { [weak self] event, eventData in
+        self.adapterService.startAccountsWithListener(accountId: self.accountId, convId: convId, loadAll: loadAll) { [weak self] event, eventData in
             guard let self = self else {
                 return
             }
@@ -244,7 +244,7 @@
     private func finish() {
         if self.accountIsActive {
             self.accountIsActive = false
-            self.adapterService.stop()
+            self.adapterService.stop(accountId: self.accountId)
         } else {
             self.adapterService.removeDelegate()
         }