From b7c6eafa87808eaf37d77233c6edceff28de8983 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Thu, 30 Sep 2021 14:24:04 +1000 Subject: [PATCH] fix notification preview type nested transaction crash --- Session/Notifications/AppNotifications.swift | 1 + .../Database/Storage+Contacts.swift | 2 +- SessionMessagingKit/Utilities/OWSPreferences.h | 5 +++++ SessionMessagingKit/Utilities/OWSPreferences.m | 15 +++++++++++---- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Session/Notifications/AppNotifications.swift b/Session/Notifications/AppNotifications.swift index c482720cd..8199d1b4c 100644 --- a/Session/Notifications/AppNotifications.swift +++ b/Session/Notifications/AppNotifications.swift @@ -181,6 +181,7 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { let senderName = Storage.shared.getContact(with: incomingMessage.authorId, using: transaction)?.displayName(for: context) ?? incomingMessage.authorId let notificationTitle: String? + let previewType = preferences.notificationPreviewType(with: transaction) switch previewType { case .noNameNoPreview: notificationTitle = nil diff --git a/SessionMessagingKit/Database/Storage+Contacts.swift b/SessionMessagingKit/Database/Storage+Contacts.swift index 5581de050..3591d4d5e 100644 --- a/SessionMessagingKit/Database/Storage+Contacts.swift +++ b/SessionMessagingKit/Database/Storage+Contacts.swift @@ -25,8 +25,8 @@ extension Storage { @objc(setContact:usingTransaction:) public func setContact(_ contact: Contact, using transaction: Any) { - let oldContact = getContact(with: contact.sessionID) let transaction = transaction as! YapDatabaseReadWriteTransaction + let oldContact = getContact(with: contact.sessionID, using: transaction) if contact.sessionID == getUserHexEncodedPublicKey() { contact.isTrusted = true // Always trust ourselves } diff --git a/SessionMessagingKit/Utilities/OWSPreferences.h b/SessionMessagingKit/Utilities/OWSPreferences.h index f69dbb286..8fdafd1b1 100644 --- a/SessionMessagingKit/Utilities/OWSPreferences.h +++ b/SessionMessagingKit/Utilities/OWSPreferences.h @@ -3,6 +3,10 @@ // #import +#import +#import +#import +#import NS_ASSUME_NONNULL_BEGIN @@ -47,6 +51,7 @@ extern NSString *const OWSPreferencesCallLoggingDidChangeNotification; - (void)setScreenSecurity:(BOOL)flag; - (NotificationType)notificationPreviewType; +- (NotificationType)notificationPreviewTypeWithTransaction:(YapDatabaseReadTransaction *)transaction; - (void)setNotificationPreviewType:(NotificationType)type; - (NSString *)nameForNotificationPreviewType:(NotificationType)notificationType; diff --git a/SessionMessagingKit/Utilities/OWSPreferences.m b/SessionMessagingKit/Utilities/OWSPreferences.m index 645b73a02..29def0003 100644 --- a/SessionMessagingKit/Utilities/OWSPreferences.m +++ b/SessionMessagingKit/Utilities/OWSPreferences.m @@ -3,10 +3,6 @@ // #import "OWSPreferences.h" -#import -#import -#import -#import NS_ASSUME_NONNULL_BEGIN @@ -384,6 +380,17 @@ NSString *const OWSPreferencesKeySystemCallLogEnabled = @"OWSPreferencesKeySyste } } +- (NotificationType)notificationPreviewTypeWithTransaction:(YapDatabaseReadTransaction *)transaction +{ + NSNumber *preference = [self tryGetValueForKey:OWSPreferencesKeyNotificationPreviewType transaction:transaction]; + + if (preference) { + return [preference unsignedIntegerValue]; + } else { + return NotificationNamePreview; + } +} + - (NSString *)nameForNotificationPreviewType:(NotificationType)notificationType { switch (notificationType) {