From c0e90152cecad3930ab283ed7a9256ee3e9bd93f Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Wed, 9 Mar 2022 17:43:38 +1100 Subject: [PATCH] Fixes for config message handling Updated the config message handling to try fetch the existing contact first before creating a new one Updated the OpenGroupManagerV2 to avoid joining an open group if the user already has a thread for it and is polling it --- .../Open Groups/OpenGroupManagerV2.swift | 11 ++++++++++- .../MessageReceiver+Handling.swift | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/SessionMessagingKit/Open Groups/OpenGroupManagerV2.swift b/SessionMessagingKit/Open Groups/OpenGroupManagerV2.swift index 852520d45..43c2133a1 100644 --- a/SessionMessagingKit/Open Groups/OpenGroupManagerV2.swift +++ b/SessionMessagingKit/Open Groups/OpenGroupManagerV2.swift @@ -30,6 +30,15 @@ public final class OpenGroupManagerV2 : NSObject { // MARK: Adding & Removing public func add(room: String, server: String, publicKey: String, using transaction: Any) -> Promise { + // If we are currently polling for this server and already have a TSGroupThread for this room the do nothing + let transaction = transaction as! YapDatabaseReadWriteTransaction + let groupId: Data = LKGroupUtilities.getEncodedOpenGroupIDAsData("\(server).\(room)") + + if OpenGroupManagerV2.shared.pollers[server] != nil && TSGroupThread.fetch(uniqueId: TSGroupThread.threadId(fromGroupId: groupId), transaction: transaction) != nil { + SNLog("Ignoring join open group attempt (already joined)") + return Promise.value(()) + } + let storage = Storage.shared // Clear any existing data if needed storage.removeLastMessageServerID(for: room, on: server, using: transaction) @@ -38,7 +47,7 @@ public final class OpenGroupManagerV2 : NSObject { // Store the public key storage.setOpenGroupPublicKey(for: server, to: publicKey, using: transaction) let (promise, seal) = Promise.pending() - let transaction = transaction as! YapDatabaseReadWriteTransaction + transaction.addCompletionQueue(DispatchQueue.global(qos: .userInitiated)) { // Get the group info OpenGroupAPIV2.getInfo(for: room, on: server).done(on: DispatchQueue.global(qos: .userInitiated)) { info in diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift index 04e93b22b..1077a648c 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift @@ -211,7 +211,7 @@ extension MessageReceiver { // Contacts for contactInfo in message.contacts { let sessionID = contactInfo.publicKey! - let contact = Contact(sessionID: sessionID) + let contact = (Storage.shared.getContact(with: sessionID, using: transaction) ?? Contact(sessionID: sessionID)) if let profileKey = contactInfo.profileKey { contact.profileEncryptionKey = OWSAES256Key(data: profileKey) } contact.profilePictureURL = contactInfo.profilePictureURL contact.name = contactInfo.displayName