From e8c20ef8eec41611c8471fb5ee64665162c9ece4 Mon Sep 17 00:00:00 2001
From: Morgan Pretty <morgan.t.pretty@gmail.com>
Date: Mon, 20 Jan 2025 10:31:00 +1100
Subject: [PATCH] More QA Fixes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

• Fixed an issue where "this message was deleted locally" couldn't be overwritten with "this message was deleted"
• Backported a libSession fix for the current config being incorrectly removed from the swarm
---
 LibSession-Util                               |  2 +-
 Session.xcodeproj/project.pbxproj             |  4 +-
 .../Database/Models/Interaction.swift         | 10 +++--
 .../LibSession+SessionMessagingKit.swift      |  4 +-
 .../Shared Models/MessageViewModel.swift      | 43 ++++++++++---------
 5 files changed, 34 insertions(+), 29 deletions(-)

diff --git a/LibSession-Util b/LibSession-Util
index bf7c010b5..b57c8a560 160000
--- a/LibSession-Util
+++ b/LibSession-Util
@@ -1 +1 @@
-Subproject commit bf7c010b5e857e4ab35c4d8f819ccf90ac595fdb
+Subproject commit b57c8a5604185b0a7d17fff9b62fb21c7ca31719
diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj
index 31c7fc9ca..3f58b9267 100644
--- a/Session.xcodeproj/project.pbxproj
+++ b/Session.xcodeproj/project.pbxproj
@@ -7916,7 +7916,7 @@
 				CLANG_WARN__ARC_BRIDGE_CAST_NONARC = YES;
 				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
 				CODE_SIGN_IDENTITY = "iPhone Developer";
-				CURRENT_PROJECT_VERSION = 527;
+				CURRENT_PROJECT_VERSION = 528;
 				ENABLE_BITCODE = NO;
 				ENABLE_STRICT_OBJC_MSGSEND = YES;
 				ENABLE_TESTABILITY = YES;
@@ -7992,7 +7992,7 @@
 				CLANG_WARN__ARC_BRIDGE_CAST_NONARC = YES;
 				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
 				CODE_SIGN_IDENTITY = "iPhone Distribution";
-				CURRENT_PROJECT_VERSION = 527;
+				CURRENT_PROJECT_VERSION = 528;
 				ENABLE_BITCODE = NO;
 				ENABLE_MODULE_VERIFIER = YES;
 				ENABLE_STRICT_OBJC_MSGSEND = YES;
diff --git a/SessionMessagingKit/Database/Models/Interaction.swift b/SessionMessagingKit/Database/Models/Interaction.swift
index 174131d0d..8a13afb04 100644
--- a/SessionMessagingKit/Database/Models/Interaction.swift
+++ b/SessionMessagingKit/Database/Models/Interaction.swift
@@ -1424,9 +1424,12 @@ public extension Interaction {
         try interactionInfo.grouped(by: { $0.variant }).forEach { variant, info in
             let targetVariant: Interaction.Variant = {
                 switch (variant, localOnly) {
-                    case (.standardOutgoing, true), (.standardOutgoingDeletedLocally, _): return .standardOutgoingDeletedLocally
-                    case (.standardOutgoing, false), (.standardOutgoingDeleted, _): return .standardOutgoingDeleted
-                    case (_, true), (.standardIncomingDeletedLocally, _): return .standardIncomingDeletedLocally
+                    case (.standardOutgoing, true), (.standardOutgoingDeletedLocally, true):
+                        return .standardOutgoingDeletedLocally
+                    case (.standardOutgoing, false), (.standardOutgoingDeletedLocally, false), (.standardOutgoingDeleted, _):
+                        return .standardOutgoingDeleted
+                    case (.standardIncoming, true), (.standardIncomingDeletedLocally, true):
+                        return .standardIncomingDeletedLocally
                     default: return .standardIncomingDeleted
                 }
             }()
@@ -1435,7 +1438,6 @@ public extension Interaction {
                 .filter(ids: info.map { $0.id })
                 .updateAll(
                     db,
-                    Interaction.Columns.serverHash.set(to: nil),
                     Interaction.Columns.variant.set(to: targetVariant),
                     Interaction.Columns.body.set(to: nil),
                     Interaction.Columns.wasRead.set(to: true),
diff --git a/SessionMessagingKit/LibSession/LibSession+SessionMessagingKit.swift b/SessionMessagingKit/LibSession/LibSession+SessionMessagingKit.swift
index d8bd5d633..d95570097 100644
--- a/SessionMessagingKit/LibSession/LibSession+SessionMessagingKit.swift
+++ b/SessionMessagingKit/LibSession/LibSession+SessionMessagingKit.swift
@@ -671,14 +671,14 @@ public extension LibSession {
             
             try groupedMessages
                 .sorted { lhs, rhs in lhs.key.namespace.processingOrder < rhs.key.namespace.processingOrder }
-                .forEach { variant, message in
+                .forEach { variant, messages in
                     let sessionId: SessionId = SessionId(hex: swarmPublicKey, dumpVariant: variant)
                     let config: Config? = configStore[sessionId, variant]
                     
                     do {
                         // Merge the messages (if it doesn't merge anything then don't bother trying
                         // to handle the result)
-                        guard let latestServerTimestampMs: Int64 = try config?.merge(message) else { return }
+                        guard let latestServerTimestampMs: Int64 = try config?.merge(messages) else { return }
                         
                         // Apply the updated states to the database
                         switch variant {
diff --git a/SessionMessagingKit/Shared Models/MessageViewModel.swift b/SessionMessagingKit/Shared Models/MessageViewModel.swift
index 956df8ea0..46019939b 100644
--- a/SessionMessagingKit/Shared Models/MessageViewModel.swift	
+++ b/SessionMessagingKit/Shared Models/MessageViewModel.swift	
@@ -402,36 +402,39 @@ public struct MessageViewModel: FetchableRecordWithRowId, Decodable, Equatable,
             )
         }()
         let (positionInCluster, isOnlyMessageInCluster): (Position, Bool) = {
-            let isFirstInCluster: Bool = (
-                prevModel == nil ||
-                shouldShowDateBeforeThisModel || (
-                    self.variant == .standardOutgoing &&
-                    prevModel?.variant != .standardOutgoing
+            let isSenderVariantConsistent: Bool = (
+                (
+                    (
+                        self.variant == .standardOutgoing ||
+                        self.variant == .standardOutgoingDeleted ||
+                        self.variant == .standardOutgoingDeletedLocally
+                    ) && (
+                        prevModel?.variant != .standardOutgoing ||
+                        prevModel?.variant != .standardOutgoingDeleted ||
+                        prevModel?.variant != .standardOutgoingDeletedLocally
+                    )
                 ) || (
                     (
                         self.variant == .standardIncoming ||
-                        self.variant == .standardIncomingDeleted
+                        self.variant == .standardIncomingDeleted ||
+                        self.variant == .standardIncomingDeletedLocally
                     ) && (
                         prevModel?.variant != .standardIncoming &&
-                        prevModel?.variant != .standardIncomingDeleted
+                        prevModel?.variant != .standardIncomingDeleted &&
+                        prevModel?.variant != .standardIncomingDeletedLocally
                     )
-                ) ||
+                )
+            )
+            let isFirstInCluster: Bool = (
+                prevModel == nil ||
+                shouldShowDateBeforeThisModel ||
+                isSenderVariantConsistent ||
                 self.authorId != prevModel?.authorId
             )
             let isLastInCluster: Bool = (
                 nextModel == nil ||
-                shouldShowDateBeforeNextModel || (
-                    self.variant == .standardOutgoing &&
-                    nextModel?.variant != .standardOutgoing
-                ) || (
-                    (
-                        self.variant == .standardIncoming ||
-                        self.variant == .standardIncomingDeleted
-                    ) && (
-                        nextModel?.variant != .standardIncoming &&
-                        nextModel?.variant != .standardIncomingDeleted
-                    )
-                ) ||
+                shouldShowDateBeforeNextModel ||
+                isSenderVariantConsistent ||
                 self.authorId != nextModel?.authorId
             )