From e49431d2eb5f9c7996d31e069032a6d949a15f9c Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Mon, 15 Nov 2021 16:47:12 +1100 Subject: [PATCH 01/11] fix transaction crash --- SessionMessagingKit/Database/Storage+Messaging.swift | 1 + SessionMessagingKit/Sending & Receiving/MessageSender.swift | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/SessionMessagingKit/Database/Storage+Messaging.swift b/SessionMessagingKit/Database/Storage+Messaging.swift index e02363de7..0035217f2 100644 --- a/SessionMessagingKit/Database/Storage+Messaging.swift +++ b/SessionMessagingKit/Database/Storage+Messaging.swift @@ -28,6 +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 } let tsOutgoingMessage = TSOutgoingMessage.from(message, associatedWith: thread, using: transaction) var recipients: [String] = [] if let syncTarget = message.syncTarget { diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender.swift b/SessionMessagingKit/Sending & Receiving/MessageSender.swift index d32b07681..50dee4be5 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender.swift @@ -116,8 +116,6 @@ public final class MessageSender : NSObject { if message.sentTimestamp == nil { // Visible messages will already have their sent timestamp set message.sentTimestamp = NSDate.millisecondTimestamp() } - // Ignore future self-sends - Storage.shared.addReceivedMessageTimestamp(message.sentTimestamp!, using: transaction) message.sender = userPublicKey switch destination { case .contact(let publicKey): message.recipient = publicKey @@ -268,8 +266,6 @@ public final class MessageSender : NSObject { if message.sentTimestamp == nil { // Visible messages will already have their sent timestamp set message.sentTimestamp = NSDate.millisecondTimestamp() } - // Ignore future self-sends - Storage.shared.addReceivedMessageTimestamp(message.sentTimestamp!, using: transaction) message.sender = storage.getUserPublicKey() switch destination { case .contact(_): preconditionFailure() From 13b7b1edb68d42bb8f4dc7f314d6218f7666b570 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Tue, 16 Nov 2021 10:36:31 +1100 Subject: [PATCH 02/11] Keep screen on when recording voice message --- Session/Conversations/ConversationVC+Interaction.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Session/Conversations/ConversationVC+Interaction.swift b/Session/Conversations/ConversationVC+Interaction.swift index 90807940f..809d9f165 100644 --- a/Session/Conversations/ConversationVC+Interaction.swift +++ b/Session/Conversations/ConversationVC+Interaction.swift @@ -762,6 +762,8 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc requestMicrophonePermissionIfNeeded() { [weak self] in self?.cancelVoiceMessageRecording() } + // Keep screen on + UIApplication.shared.isIdleTimerDisabled = false guard AVAudioSession.sharedInstance().recordPermission == .granted else { return } // Cancel any current audio playback audioPlayer?.stop() @@ -810,6 +812,7 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc } func endVoiceMessageRecording() { + UIApplication.shared.isIdleTimerDisabled = true // Hide the UI snInputView.hideVoiceMessageUI() // Cancel the timer From 2cb006c3e485792d747bc0e3fbc5eb1bad5ff2c1 Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Wed, 17 Nov 2021 11:14:13 +1100 Subject: [PATCH 03/11] Fix image orientation --- Session/Media Viewing & Editing/PhotoCapture.swift | 6 +++++- .../Image Editing/ImageEditorCanvasView.swift | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Session/Media Viewing & Editing/PhotoCapture.swift b/Session/Media Viewing & Editing/PhotoCapture.swift index 6c0eda63b..64d7ed650 100644 --- a/Session/Media Viewing & Editing/PhotoCapture.swift +++ b/Session/Media Viewing & Editing/PhotoCapture.swift @@ -593,7 +593,11 @@ class PhotoCaptureOutputAdaptee: NSObject, ImageCaptureOutput { @available(iOS 11.0, *) func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { - let data = photo.fileDataRepresentation()! + var data = photo.fileDataRepresentation()! + // Call normalized here to fix the orientation + if let srcImage = UIImage(data: data) { + data = srcImage.normalized().jpegData(compressionQuality: 1.0)! + } DispatchQueue.main.async { self.delegate?.captureOutputDidFinishProcessing(photoData: data, error: error) } diff --git a/SignalUtilitiesKit/Media Viewing & Editing/Image Editing/ImageEditorCanvasView.swift b/SignalUtilitiesKit/Media Viewing & Editing/Image Editing/ImageEditorCanvasView.swift index 92e4f93c1..12fae43f5 100644 --- a/SignalUtilitiesKit/Media Viewing & Editing/Image Editing/ImageEditorCanvasView.swift +++ b/SignalUtilitiesKit/Media Viewing & Editing/Image Editing/ImageEditorCanvasView.swift @@ -166,7 +166,7 @@ public class ImageEditorCanvasView: UIView { // of code simplicity. We could modify the image layer's // transform to handle the normalization, which would // have perf benefits. - return srcImage.normalized() + return srcImage } // MARK: - Content From 67d9dcfa005d9c5f395af59e37ce7fbcabee28e5 Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Fri, 19 Nov 2021 16:54:26 +1100 Subject: [PATCH 04/11] update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 21c390b4b..36979ca27 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ DerivedData *.ipa *.xcuserstate Index/ +Session-Turn-Server # CocoaPods Pods From 3f8f4ca0604acdd71fed5db86144b27577d2bac1 Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Mon, 22 Nov 2021 10:49:44 +1100 Subject: [PATCH 05/11] clean --- Session/Meta/AppDelegate.m | 1 - 1 file changed, 1 deletion(-) diff --git a/Session/Meta/AppDelegate.m b/Session/Meta/AppDelegate.m index 413f90059..cc8ffc692 100644 --- a/Session/Meta/AppDelegate.m +++ b/Session/Meta/AppDelegate.m @@ -740,7 +740,6 @@ static NSTimeInterval launchStartedAt; - (LKAppMode)getCurrentAppMode { LKAppMode appMode = [self getAppModeOrSystemDefault]; - UIWindow *window = UIApplication.sharedApplication.keyWindow; return appMode; } From 5a5741f47bdb6135479b0314535c7b412b8fde73 Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Tue, 23 Nov 2021 11:05:04 +1100 Subject: [PATCH 06/11] Group notifications from background polling --- Session/Notifications/AppNotifications.swift | 13 +++++--- .../UserNotificationsAdaptee.swift | 33 ++++++++++++++----- .../MessageReceiver+Handling.swift | 4 ++- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/Session/Notifications/AppNotifications.swift b/Session/Notifications/AppNotifications.swift index 4b3b9f04b..fc80e8a71 100644 --- a/Session/Notifications/AppNotifications.swift +++ b/Session/Notifications/AppNotifications.swift @@ -37,6 +37,7 @@ struct AppNotificationUserInfoKey { static let threadId = "Signal.AppNotificationsUserInfoKey.threadId" static let callBackNumber = "Signal.AppNotificationsUserInfoKey.callBackNumber" static let localCallId = "Signal.AppNotificationsUserInfoKey.localCallId" + static let threadNotificationCounter = "Session.AppNotificationsUserInfoKey.threadNotificationCounter" } extension AppNotificationCategory { @@ -80,9 +81,9 @@ extension AppNotificationAction { } } -// Delay notification of incoming messages when it's likely to be read by a linked device to -// avoid notifying a user on their phone while a conversation is actively happening on desktop. -let kNotificationDelayForRemoteRead: TimeInterval = 5 +// Delay notification of incoming messages when it's a background polling to +// avoid too many notifications fired at the same time +let kNotificationDelayForBackgroumdPoll: TimeInterval = 5 let kAudioNotificationsThrottleCount = 2 let kAudioNotificationsThrottleInterval: TimeInterval = 5 @@ -201,6 +202,8 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { owsFailDebug("unexpected thread: \(thread)") return } + default: + notificationTitle = "Session" } var notificationBody: String? @@ -209,6 +212,8 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { notificationBody = NotificationStrings.incomingMessageBody case .namePreview: notificationBody = messageText + default: + notificationBody = NotificationStrings.incomingMessageBody } guard let threadId = thread.uniqueId else { @@ -220,7 +225,7 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { // Don't reply from lockscreen if anyone in this conversation is // "no longer verified". - var category = AppNotificationCategory.incomingMessage + let category = AppNotificationCategory.incomingMessage let userInfo = [ AppNotificationUserInfoKey.threadId: threadId diff --git a/Session/Notifications/UserNotificationsAdaptee.swift b/Session/Notifications/UserNotificationsAdaptee.swift index 13c648fe6..551224aaf 100644 --- a/Session/Notifications/UserNotificationsAdaptee.swift +++ b/Session/Notifications/UserNotificationsAdaptee.swift @@ -96,17 +96,18 @@ extension UserNotificationPresenterAdaptee: NotificationPresenterAdaptee { let content = UNMutableNotificationContent() content.categoryIdentifier = category.identifier content.userInfo = userInfo + let isReplacingNotification = replacingIdentifier != nil + var isBackgroudPoll = false + if let threadIdentifier = userInfo[AppNotificationUserInfoKey.threadId] as? String { + content.threadIdentifier = threadIdentifier + isBackgroudPoll = replacingIdentifier == threadIdentifier + } let isAppActive = UIApplication.shared.applicationState == .active if let sound = sound, sound != OWSSound.none { content.sound = sound.notificationSound(isQuiet: isAppActive) } - - var notificationIdentifier: String = UUID().uuidString - if let replacingIdentifier = replacingIdentifier { - notificationIdentifier = replacingIdentifier - Logger.debug("replacing notification with identifier: \(notificationIdentifier)") - cancelNotification(identifier: notificationIdentifier) - } + + let notificationIdentifier = isReplacingNotification ? replacingIdentifier! : UUID().uuidString if shouldPresentNotification(category: category, userInfo: userInfo) { if let displayableTitle = title?.filterForDisplay { @@ -119,10 +120,26 @@ extension UserNotificationPresenterAdaptee: NotificationPresenterAdaptee { // Play sound and vibrate, but without a `body` no banner will show. Logger.debug("supressing notification body") } + + let trigger: UNNotificationTrigger? + if isBackgroudPoll { + trigger = UNTimeIntervalNotificationTrigger(timeInterval: kNotificationDelayForBackgroumdPoll, repeats: false) + let numberOfNotifications: Int + if let lastRequest = notifications[notificationIdentifier], let counter = lastRequest.content.userInfo[AppNotificationUserInfoKey.threadNotificationCounter] as? Int { + numberOfNotifications = counter + 1 + } else { + numberOfNotifications = 1 + } + content.userInfo[AppNotificationUserInfoKey.threadNotificationCounter] = numberOfNotifications + content.body = "\(numberOfNotifications) new messages." + } else { + trigger = nil + } - let request = UNNotificationRequest(identifier: notificationIdentifier, content: content, trigger: nil) + let request = UNNotificationRequest(identifier: notificationIdentifier, content: content, trigger: trigger) Logger.debug("presenting notification with identifier: \(notificationIdentifier)") + if isReplacingNotification { cancelNotification(identifier: notificationIdentifier) } notificationCenter.add(request) notifications[notificationIdentifier] = request } diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift index 5e9f987d9..dd44b29b0 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift @@ -342,7 +342,9 @@ extension MessageReceiver { // Notify the user if needed guard (isMainAppAndActive || isBackgroundPoll), let tsIncomingMessage = TSMessage.fetch(uniqueId: tsMessageID, transaction: transaction) as? TSIncomingMessage, let thread = TSThread.fetch(uniqueId: threadID, transaction: transaction) else { return tsMessageID } - tsIncomingMessage.setNotificationIdentifier(UUID().uuidString, transaction: transaction) + // Use the same identifier for notifications when in backgroud polling to prevent spam + let notificationIdentifier = isBackgroundPoll ? thread.uniqueId : UUID().uuidString + tsIncomingMessage.setNotificationIdentifier(notificationIdentifier, transaction: transaction) DispatchQueue.main.async { Storage.read { transaction in SSKEnvironment.shared.notificationsManager!.notifyUser(for: tsIncomingMessage, in: thread, transaction: transaction) From 4f1093beca5ac5b00b2341b0791428ffe690c9db Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Tue, 23 Nov 2021 11:58:19 +1100 Subject: [PATCH 07/11] improve translation and notificaiton displaying --- Session/Meta/Translations/en.lproj/Localizable.strings | 3 ++- Session/Notifications/UserNotificationsAdaptee.swift | 2 +- SignalUtilitiesKit/Utilities/CommonStrings.swift | 3 +++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Session/Meta/Translations/en.lproj/Localizable.strings b/Session/Meta/Translations/en.lproj/Localizable.strings index 3e0abe961..beddf5196 100644 --- a/Session/Meta/Translations/en.lproj/Localizable.strings +++ b/Session/Meta/Translations/en.lproj/Localizable.strings @@ -572,7 +572,8 @@ "DISMISS_BUTTON_TEXT" = "Dismiss"; /* Button text which opens the settings app */ "OPEN_SETTINGS_BUTTON" = "Settings"; -"APN_Message" = "You've got a new message"; +"APN_Message" = "You've got a new message."; +"APN_Collapsed_Messages" = "You've got %@ new messages."; "system_mode_theme" = "System"; "dark_mode_theme" = "Dark"; "light_mode_theme" = "Light"; diff --git a/Session/Notifications/UserNotificationsAdaptee.swift b/Session/Notifications/UserNotificationsAdaptee.swift index 551224aaf..e5997f9a9 100644 --- a/Session/Notifications/UserNotificationsAdaptee.swift +++ b/Session/Notifications/UserNotificationsAdaptee.swift @@ -127,11 +127,11 @@ extension UserNotificationPresenterAdaptee: NotificationPresenterAdaptee { let numberOfNotifications: Int if let lastRequest = notifications[notificationIdentifier], let counter = lastRequest.content.userInfo[AppNotificationUserInfoKey.threadNotificationCounter] as? Int { numberOfNotifications = counter + 1 + content.body = String(format: NotificationStrings.incomingCollapsedMessagesBody, "\(numberOfNotifications)") } else { numberOfNotifications = 1 } content.userInfo[AppNotificationUserInfoKey.threadNotificationCounter] = numberOfNotifications - content.body = "\(numberOfNotifications) new messages." } else { trigger = nil } diff --git a/SignalUtilitiesKit/Utilities/CommonStrings.swift b/SignalUtilitiesKit/Utilities/CommonStrings.swift index a5e4649d3..2d7199593 100644 --- a/SignalUtilitiesKit/Utilities/CommonStrings.swift +++ b/SignalUtilitiesKit/Utilities/CommonStrings.swift @@ -50,6 +50,9 @@ public class NotificationStrings: NSObject { @objc static public let incomingMessageBody = NSLocalizedString("APN_Message", comment: "notification body") + + @objc + static public let incomingCollapsedMessagesBody = NSLocalizedString("APN_Collapsed_Messages", comment: "collapsed notification body for background polling") @objc static public let incomingGroupMessageTitleFormat = NSLocalizedString("NEW_GROUP_MESSAGE_NOTIFICATION_TITLE", comment: "notification title. Embeds {{author name}} and {{group name}}") From cfd7d67c9ac7c10ed563d95f9189cfd508716480 Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Thu, 25 Nov 2021 16:32:47 +1100 Subject: [PATCH 08/11] fix for background polling collapsed notification title --- Session/Notifications/AppNotifications.swift | 22 ++++++++------------ 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/Session/Notifications/AppNotifications.swift b/Session/Notifications/AppNotifications.swift index fc80e8a71..52eae7ed1 100644 --- a/Session/Notifications/AppNotifications.swift +++ b/Session/Notifications/AppNotifications.swift @@ -158,9 +158,12 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { public func notifyUser(for incomingMessage: TSIncomingMessage, in thread: TSThread, transaction: YapDatabaseReadTransaction) { - guard !thread.isMuted else { - return - } + guard !thread.isMuted else { return } + guard let threadId = thread.uniqueId else { return } + + let identifier: String = incomingMessage.notificationIdentifier ?? UUID().uuidString + + let isBackgroudPoll = identifier == threadId // While batch processing, some of the necessary changes have not been commited. let rawMessageText = incomingMessage.previewText(with: transaction) @@ -195,9 +198,7 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { if groupName.count < 1 { groupName = MessageStrings.newGroupDefaultTitle } - notificationTitle = String(format: NotificationStrings.incomingGroupMessageTitleFormat, - senderName, - groupName) + notificationTitle = isBackgroudPoll ? groupName : String(format: NotificationStrings.incomingGroupMessageTitleFormat, senderName, groupName) default: owsFailDebug("unexpected thread: \(thread)") return @@ -216,11 +217,6 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { notificationBody = NotificationStrings.incomingMessageBody } - guard let threadId = thread.uniqueId else { - owsFailDebug("threadId was unexpectedly nil") - return - } - assert((notificationBody ?? notificationTitle) != nil) // Don't reply from lockscreen if anyone in this conversation is @@ -230,8 +226,6 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { let userInfo = [ AppNotificationUserInfoKey.threadId: threadId ] - - let identifier: String = incomingMessage.notificationIdentifier ?? UUID().uuidString DispatchQueue.main.async { notificationBody = MentionUtilities.highlightMentions(in: notificationBody!, threadID: thread.uniqueId!) @@ -252,6 +246,8 @@ public class NotificationPresenter: NSObject, NotificationsProtocol { notificationTitle = nil case .nameNoPreview, .namePreview: notificationTitle = thread.name() + default: + notificationTitle = nil } let notificationBody = NotificationStrings.failedToSendBody From a6df6504c8a8862c05f7ece827b143c23a9186b4 Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Mon, 29 Nov 2021 17:02:23 +1100 Subject: [PATCH 09/11] Prevent the same ExpirationTimerUpdate to be handled twice --- SessionMessagingKit/Sending & Receiving/MessageSender.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender.swift b/SessionMessagingKit/Sending & Receiving/MessageSender.swift index 50dee4be5..e9042b81f 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender.swift @@ -362,7 +362,11 @@ public final class MessageSender : NSObject { let userPublicKey = getUserHexEncodedPublicKey() if case .contact(let publicKey) = destination, !isSyncMessage { if let message = message as? VisibleMessage { message.syncTarget = publicKey } - if let message = message as? ExpirationTimerUpdate { message.syncTarget = publicKey } + if let message = message as? ExpirationTimerUpdate { + message.syncTarget = publicKey + // Prevent the same ExpirationTimerUpdate to be handled twice + Storage.shared.addReceivedMessageTimestamp(message.sentTimestamp!, using: transaction) + } // FIXME: Make this a job sendToSnodeDestination(.contact(publicKey: userPublicKey), message: message, using: transaction, isSyncMessage: true).retainUntilComplete() } From 10aa3c7fee645707bc2dc6f570befa53d315ddf2 Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Wed, 1 Dec 2021 14:27:51 +1100 Subject: [PATCH 10/11] probably fix the theme not being correct issue --- Session/Shared/BaseVC.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Session/Shared/BaseVC.swift b/Session/Shared/BaseVC.swift index ff53996b3..75800f7b3 100644 --- a/Session/Shared/BaseVC.swift +++ b/Session/Shared/BaseVC.swift @@ -90,7 +90,7 @@ class BaseVC : UIViewController { } override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { - NotificationCenter.default.post(name: .appModeChanged, object: nil) + // NotificationCenter.default.post(name: .appModeChanged, object: nil) } @objc internal func handleAppModeChangedNotification(_ notification: Notification) { From 5a7fabf216aabee076df649d92fff96daddeb46f Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Wed, 1 Dec 2021 15:05:30 +1100 Subject: [PATCH 11/11] minor improvement on theme change --- Session/Shared/BaseVC.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Session/Shared/BaseVC.swift b/Session/Shared/BaseVC.swift index 75800f7b3..83f9c9262 100644 --- a/Session/Shared/BaseVC.swift +++ b/Session/Shared/BaseVC.swift @@ -90,7 +90,9 @@ class BaseVC : UIViewController { } override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { - // NotificationCenter.default.post(name: .appModeChanged, object: nil) + if LKAppModeUtilities.isSystemDefault { + NotificationCenter.default.post(name: .appModeChanged, object: nil) + } } @objc internal func handleAppModeChangedNotification(_ notification: Notification) {