From 98b798228d9096a402ec12a4cce938800ce5a978 Mon Sep 17 00:00:00 2001
From: Bruno Muniz Azevedo Filho <bruno@elixxir.io>
Date: Tue, 28 Jun 2022 01:25:50 -0300
Subject: [PATCH] Trying to fix group issue

---
 .../Integration/Session/Session+Chat.swift    |  8 +-
 .../Integration/Session/Session+Group.swift   | 87 +++++++++++++++----
 2 files changed, 78 insertions(+), 17 deletions(-)

diff --git a/Sources/Integration/Session/Session+Chat.swift b/Sources/Integration/Session/Session+Chat.swift
index 34af6aa2..3e3eb63a 100644
--- a/Sources/Integration/Session/Session+Chat.swift
+++ b/Sources/Integration/Session/Session+Chat.swift
@@ -124,12 +124,16 @@ extension Session {
             message.date = Date()
 
             if let message = try? dbManager.saveMessage(message) {
-                send(message: message)
+                if let recipientId = message.recipientId {
+                    send(message: message)
+                } else {
+                    send(groupMessage: message)
+                }
             }
         }
     }
 
-    private func send(message: Message) {
+    func send(message: Message) {
         var message = message
 
         var reply: Reply?
diff --git a/Sources/Integration/Session/Session+Group.swift b/Sources/Integration/Session/Session+Group.swift
index f6b604d7..a3cf6896 100644
--- a/Sources/Integration/Session/Session+Group.swift
+++ b/Sources/Integration/Session/Session+Group.swift
@@ -25,17 +25,49 @@ extension Session {
         members: [Contact],
         _ completion: @escaping (Result<GroupInfo, Error>) -> Void
     ) {
-        guard let manager = client.groupManager else { fatalError("A group manager was not created") }
-
-        let me = client.bindings.meMarshalled
-        let memberIds = members.map { $0.id }
+        guard let manager = client.groupManager else {
+            fatalError("A group manager was not created")
+        }
 
-        manager.create(me: me, name: name, welcome: welcome, with: memberIds) { [weak self] in
+        manager.create(
+            me: myId,
+            name: name,
+            welcome: welcome,
+            with: members.map { $0.id }) { [weak self] result in
             guard let self = self else { return }
 
-            switch $0 {
+            switch result {
             case .success(let group):
-                completion(.success(self.processGroupCreation(group, memberIds: memberIds, welcome: welcome)))
+                try! self.dbManager.saveGroup(group)
+
+                members
+                    .map { GroupMember(groupId: group.id, contactId: $0.id) }
+                    .forEach { try! self.dbManager.saveGroupMember($0) }
+
+                // TODO: Add saveBulkGroupMembers to the database
+
+                if let welcome = welcome {
+                    let message = Message(
+                        networkId: nil,
+                        senderId: self.myId,
+                        recipientId: nil,
+                        groupId: group.id,
+                        date: group.createdAt,
+                        status: .received,
+                        isUnread: false,
+                        text: welcome,
+                        replyMessageId: nil,
+                        roundURL: nil,
+                        fileTransferId: nil
+                    )
+
+                    try! self.dbManager.saveMessage(message)
+                }
+
+                let query = GroupInfo.Query(groupId: group.id)
+                let info = try! self.dbManager.fetchGroupInfos(query).first
+                completion(.success(info!))
+
             case .failure(let error):
                 completion(.failure(error))
             }
@@ -48,9 +80,35 @@ extension Session {
         ///
         _ = try! dbManager.saveGroup(group)
 
-        /// Save the members
+        /// Which of those members are not my friends?
         ///
-        memberIds.forEach { _ = try! dbManager.saveGroupMember(.init(groupId: group.id, contactId: $0)) }
+        let friendsParticipating = try! dbManager.fetchContacts(Contact.Query(id: Set(memberIds)))
+
+        /// Save the strangers as contacts
+        ///
+        let friendIds = friendsParticipating.map(\.id)
+        memberIds.forEach {
+            if !friendIds.contains($0) {
+                try! dbManager.saveContact(.init(
+                    id: $0,
+                    marshaled: nil,
+                    username: nil,
+                    email: nil,
+                    phone: nil,
+                    nickname: nil,
+                    photo: nil,
+                    authStatus: .stranger,
+                    isRecent: false,
+                    createdAt: Date()
+                ))
+            }
+        }
+
+        /// Save group members relation
+        ///
+        memberIds.forEach {
+            try! dbManager.saveGroupMember(.init(groupId: group.id, contactId: $0))
+        }
 
         /// Save the welcome message (if any)
         ///
@@ -70,9 +128,8 @@ extension Session {
             ))
         }
 
-        /// Buzz if the group was not created by me
-        ///
-        if group.leaderId != client.bindings.myId, inappnotifications {
+
+        if inappnotifications {
             DeviceFeedback.sound(.contactAdded)
             DeviceFeedback.shake(.notification)
         }
@@ -103,15 +160,15 @@ extension Session {
 
         do {
             message = try dbManager.saveMessage(message)
-            send(message: message)
+            send(groupMessage: message)
         } catch {
             log(string: error.localizedDescription, type: .error)
         }
     }
 
-    private func send(message: Message) {
+    func send(groupMessage: Message) {
         guard let manager = client.groupManager else { fatalError("A group manager was not created") }
-        var message = message
+        var message = groupMessage
 
         var reply: Reply?
         if let replyId = message.replyMessageId,
-- 
GitLab