From c9b9104e8a18d75cfbcf9ee7d13aa29afd3dd245 Mon Sep 17 00:00:00 2001
From: Ryan Zhao <ryanzhaors@qq.com>
Date: Fri, 11 Mar 2022 15:56:37 +1100
Subject: [PATCH 1/4] bump up build number

---
 Session.xcodeproj/project.pbxproj | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj
index d9eb55811..0bab0b492 100644
--- a/Session.xcodeproj/project.pbxproj
+++ b/Session.xcodeproj/project.pbxproj
@@ -5185,7 +5185,7 @@
 				"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
 				CODE_SIGN_STYLE = Automatic;
 				COPY_PHASE_STRIP = NO;
-				CURRENT_PROJECT_VERSION = 324;
+				CURRENT_PROJECT_VERSION = 325;
 				DEBUG_INFORMATION_FORMAT = dwarf;
 				DEVELOPMENT_TEAM = SUQ8J2PCT7;
 				FRAMEWORK_SEARCH_PATHS = "$(inherited)";
@@ -5258,7 +5258,7 @@
 				"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
 				CODE_SIGN_STYLE = Automatic;
 				COPY_PHASE_STRIP = NO;
-				CURRENT_PROJECT_VERSION = 324;
+				CURRENT_PROJECT_VERSION = 325;
 				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
 				DEVELOPMENT_TEAM = SUQ8J2PCT7;
 				ENABLE_NS_ASSERTIONS = NO;
@@ -5324,7 +5324,7 @@
 				"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
 				CODE_SIGN_STYLE = Automatic;
 				COPY_PHASE_STRIP = NO;
-				CURRENT_PROJECT_VERSION = 324;
+				CURRENT_PROJECT_VERSION = 325;
 				DEBUG_INFORMATION_FORMAT = dwarf;
 				DEVELOPMENT_TEAM = SUQ8J2PCT7;
 				FRAMEWORK_SEARCH_PATHS = "$(inherited)";
@@ -5398,7 +5398,7 @@
 				"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
 				CODE_SIGN_STYLE = Automatic;
 				COPY_PHASE_STRIP = NO;
-				CURRENT_PROJECT_VERSION = 324;
+				CURRENT_PROJECT_VERSION = 325;
 				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
 				DEVELOPMENT_TEAM = SUQ8J2PCT7;
 				ENABLE_NS_ASSERTIONS = NO;
@@ -6334,7 +6334,7 @@
 				CODE_SIGN_ENTITLEMENTS = Session/Meta/Signal.entitlements;
 				CODE_SIGN_IDENTITY = "iPhone Developer";
 				"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
-				CURRENT_PROJECT_VERSION = 324;
+				CURRENT_PROJECT_VERSION = 325;
 				DEVELOPMENT_TEAM = SUQ8J2PCT7;
 				FRAMEWORK_SEARCH_PATHS = (
 					"$(inherited)",
@@ -6405,7 +6405,7 @@
 				CODE_SIGN_ENTITLEMENTS = Session/Meta/Signal.entitlements;
 				CODE_SIGN_IDENTITY = "iPhone Developer";
 				"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
-				CURRENT_PROJECT_VERSION = 324;
+				CURRENT_PROJECT_VERSION = 325;
 				DEVELOPMENT_TEAM = SUQ8J2PCT7;
 				FRAMEWORK_SEARCH_PATHS = (
 					"$(inherited)",

From b920dbcc10c481549343829598781d4933139066 Mon Sep 17 00:00:00 2001
From: Ryan Zhao <ryanzhaors@qq.com>
Date: Tue, 15 Mar 2022 14:00:36 +1100
Subject: [PATCH 2/4] check if the incoming message is already in database
 before persisting

---
 SessionMessagingKit/Database/Storage+Messaging.swift            | 1 +
 .../Sending & Receiving/MessageReceiver+Handling.swift          | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/SessionMessagingKit/Database/Storage+Messaging.swift b/SessionMessagingKit/Database/Storage+Messaging.swift
index 0035217f2..f06b42712 100644
--- a/SessionMessagingKit/Database/Storage+Messaging.swift
+++ b/SessionMessagingKit/Database/Storage+Messaging.swift
@@ -42,6 +42,7 @@ extension Storage {
             }
             tsMessage = tsOutgoingMessage
         } else {
+            if let _ = TSIncomingMessage.find(withAuthorId: message.sender!, timestamp: message.sentTimestamp!, transaction: transaction) { return nil }
             tsMessage = TSIncomingMessage.from(message, quotedMessage: quotedMessage, linkPreview: linkPreview, associatedWith: thread)
         }
         tsMessage.save(with: transaction)
diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift
index 4ef1d2895..ab23a33f0 100644
--- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift	
+++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift	
@@ -365,7 +365,7 @@ extension MessageReceiver {
         }
         // Persist the message
         guard let tsMessageID = storage.persist(message, quotedMessage: tsQuotedMessage, linkPreview: owsLinkPreview,
-            groupPublicKey: message.groupPublicKey, openGroupID: openGroupID, using: transaction) else { throw Error.noThread }
+            groupPublicKey: message.groupPublicKey, openGroupID: openGroupID, using: transaction) else { throw Error.duplicateMessage }
         message.threadID = threadID
         // Start attachment downloads if needed
         let isContactTrusted = Storage.shared.getContact(with: message.sender!)?.isTrusted ?? false

From a1e47419c8302ce1566620ebae7c99438e6363f9 Mon Sep 17 00:00:00 2001
From: Ryan Zhao <ryanzhaors@qq.com>
Date: Tue, 15 Mar 2022 14:03:17 +1100
Subject: [PATCH 3/4] minor refactor

---
 SessionMessagingKit/Open Groups/OpenGroupManagerV2.swift | 2 +-
 SessionMessagingKit/Threads/TSGroupThread.h              | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/SessionMessagingKit/Open Groups/OpenGroupManagerV2.swift b/SessionMessagingKit/Open Groups/OpenGroupManagerV2.swift
index 43c2133a1..1e57471bf 100644
--- a/SessionMessagingKit/Open Groups/OpenGroupManagerV2.swift	
+++ b/SessionMessagingKit/Open Groups/OpenGroupManagerV2.swift	
@@ -34,7 +34,7 @@ public final class OpenGroupManagerV2 : NSObject {
         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 {
+        if OpenGroupManagerV2.shared.pollers[server] != nil && TSGroupThread.fetch(groupId: groupId, transaction: transaction) != nil {
             SNLog("Ignoring join open group attempt (already joined)")
             return Promise.value(())
         }
diff --git a/SessionMessagingKit/Threads/TSGroupThread.h b/SessionMessagingKit/Threads/TSGroupThread.h
index 09f551854..1ed89a978 100644
--- a/SessionMessagingKit/Threads/TSGroupThread.h
+++ b/SessionMessagingKit/Threads/TSGroupThread.h
@@ -30,7 +30,8 @@ extern NSString *const TSGroupThread_NotificationKey_UniqueId;
                                    groupType:(GroupType) groupType
                                  transaction:(YapDatabaseReadWriteTransaction *)transaction;
 
-+ (nullable instancetype)threadWithGroupId:(NSData *)groupId transaction:(YapDatabaseReadTransaction *)transaction;
++ (nullable instancetype)threadWithGroupId:(NSData *)groupId transaction:(YapDatabaseReadTransaction *)transaction
+    NS_SWIFT_NAME(fetch(groupId:transaction:));
 
 + (NSString *)threadIdFromGroupId:(NSData *)groupId;
 

From 6f927a50036a2f05db7c8daa77c1f3eb4452ef96 Mon Sep 17 00:00:00 2001
From: Ryan Zhao <ryanzhaors@qq.com>
Date: Tue, 15 Mar 2022 14:27:11 +1100
Subject: [PATCH 4/4] minor refactor

---
 SessionMessagingKit/Database/Storage+Messaging.swift | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/SessionMessagingKit/Database/Storage+Messaging.swift b/SessionMessagingKit/Database/Storage+Messaging.swift
index f06b42712..76cf9cd4d 100644
--- a/SessionMessagingKit/Database/Storage+Messaging.swift
+++ b/SessionMessagingKit/Database/Storage+Messaging.swift
@@ -28,7 +28,7 @@ extension Storage {
             let thread = TSThread.fetch(uniqueId: threadID, transaction: transaction) else { return nil }
         let tsMessage: TSMessage
         if message.sender == getUserPublicKey() {
-            if let _ = TSOutgoingMessage.find(withTimestamp: message.sentTimestamp!) { return nil }
+            if TSOutgoingMessage.find(withTimestamp: message.sentTimestamp!) != nil { return nil }
             let tsOutgoingMessage = TSOutgoingMessage.from(message, associatedWith: thread, using: transaction)
             var recipients: [String] = []
             if let syncTarget = message.syncTarget {
@@ -42,7 +42,7 @@ extension Storage {
             }
             tsMessage = tsOutgoingMessage
         } else {
-            if let _ = TSIncomingMessage.find(withAuthorId: message.sender!, timestamp: message.sentTimestamp!, transaction: transaction) { return nil }
+            if TSIncomingMessage.find(withAuthorId: message.sender!, timestamp: message.sentTimestamp!, transaction: transaction) != nil { return nil }
             tsMessage = TSIncomingMessage.from(message, quotedMessage: quotedMessage, linkPreview: linkPreview, associatedWith: thread)
         }
         tsMessage.save(with: transaction)