From c2e13f02bc890a2f778edf0784fac6a12133c091 Mon Sep 17 00:00:00 2001
From: Morgan Pretty <morgan.t.pretty@gmail.com>
Date: Thu, 10 Mar 2022 16:50:48 +1100
Subject: [PATCH 1/2] Fixed a crash which could occur while processing message
 request notifications due to not using a DB transaction

---
 Session/Notifications/AppNotifications.swift             | 2 +-
 SessionMessagingKit/Threads/TSThread.h                   | 2 ++
 SessionMessagingKit/Threads/TSThread.m                   | 9 +++++++--
 .../NSENotificationPresenter.swift                       | 2 +-
 4 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/Session/Notifications/AppNotifications.swift b/Session/Notifications/AppNotifications.swift
index c4118e3a6..5b38771cd 100644
--- a/Session/Notifications/AppNotifications.swift
+++ b/Session/Notifications/AppNotifications.swift
@@ -175,7 +175,7 @@ public class NotificationPresenter: NSObject, NotificationsProtocol {
         }
         else if thread.isMessageRequest() && CurrentAppContext().appUserDefaults()[.hasHiddenMessageRequests] {
             // If there are other interactions on this thread already then don't show the notification
-            if thread.numberOfInteractions() > 1 { return }
+            if thread.numberOfInteractions(with: transaction) > 1 { return }
             
             CurrentAppContext().appUserDefaults()[.hasHiddenMessageRequests] = false
         }
diff --git a/SessionMessagingKit/Threads/TSThread.h b/SessionMessagingKit/Threads/TSThread.h
index 0542b390a..dbb38ed20 100644
--- a/SessionMessagingKit/Threads/TSThread.h
+++ b/SessionMessagingKit/Threads/TSThread.h
@@ -67,6 +67,8 @@ BOOL IsNoteToSelfEnabled(void);
  */
 - (NSUInteger)numberOfInteractions;
 
+- (NSUInteger)numberOfInteractionsWithTransaction:(YapDatabaseReadTransaction *)transaction;
+
 - (NSUInteger)unreadMessageCountWithTransaction:(YapDatabaseReadTransaction *)transaction
     NS_SWIFT_NAME(unreadMessageCount(transaction:));
 
diff --git a/SessionMessagingKit/Threads/TSThread.m b/SessionMessagingKit/Threads/TSThread.m
index a848527bc..0f9a5cc9a 100644
--- a/SessionMessagingKit/Threads/TSThread.m
+++ b/SessionMessagingKit/Threads/TSThread.m
@@ -233,12 +233,17 @@ BOOL IsNoteToSelfEnabled(void)
 {
     __block NSUInteger count;
     [[self dbReadConnection] readWithBlock:^(YapDatabaseReadTransaction *transaction) {
-        YapDatabaseViewTransaction *interactionsByThread = [transaction ext:TSMessageDatabaseViewExtensionName];
-        count = [interactionsByThread numberOfItemsInGroup:self.uniqueId];
+        count = [self numberOfInteractionsWithTransaction:transaction];
     }];
     return count;
 }
 
+- (NSUInteger)numberOfInteractionsWithTransaction:(YapDatabaseReadTransaction *)transaction
+{
+    YapDatabaseViewTransaction *interactionsByThread = [transaction ext:TSMessageDatabaseViewExtensionName];
+    return [interactionsByThread numberOfItemsInGroup:self.uniqueId];
+}
+
 - (NSArray<id<OWSReadTracking>> *)unseenMessagesWithTransaction:(YapDatabaseReadTransaction *)transaction
 {
     NSMutableArray<id<OWSReadTracking>> *messages = [NSMutableArray new];
diff --git a/SessionNotificationServiceExtension/NSENotificationPresenter.swift b/SessionNotificationServiceExtension/NSENotificationPresenter.swift
index 2d0b658f1..6e330b94a 100644
--- a/SessionNotificationServiceExtension/NSENotificationPresenter.swift
+++ b/SessionNotificationServiceExtension/NSENotificationPresenter.swift
@@ -22,7 +22,7 @@ public class NSENotificationPresenter: NSObject, NotificationsProtocol {
         }
         else if thread.isMessageRequest() && CurrentAppContext().appUserDefaults()[.hasHiddenMessageRequests] {
             // If there are other interactions on this thread already then don't show the notification
-            if thread.numberOfInteractions() > 1 { return }
+            if thread.numberOfInteractions(with: transaction) > 1 { return }
             
             CurrentAppContext().appUserDefaults()[.hasHiddenMessageRequests] = false
         }

From 0e15c63e6b7ccb9002d1079d2430cc1b02024ecd Mon Sep 17 00:00:00 2001
From: Morgan Pretty <morgan.t.pretty@gmail.com>
Date: Thu, 10 Mar 2022 16:58:08 +1100
Subject: [PATCH 2/2] Fixed a case where a the share extension wasn't using a
 transaction version of a method

---
 SessionMessagingKit/Database/TSDatabaseView.m | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/SessionMessagingKit/Database/TSDatabaseView.m b/SessionMessagingKit/Database/TSDatabaseView.m
index aa7ab2837..0b31a8631 100644
--- a/SessionMessagingKit/Database/TSDatabaseView.m
+++ b/SessionMessagingKit/Database/TSDatabaseView.m
@@ -287,7 +287,7 @@ NSString *const TSLazyRestoreAttachmentsGroup = @"TSLazyRestoreAttachmentsGroup"
         }
         TSThread *thread = (TSThread *)object;
 
-        if (thread.isMessageRequest) {
+        if ([thread isMessageRequestUsingTransaction:transaction]) {
             return nil;
         }
         else {