diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj index c6ae0a2aa..b934a41cc 100644 --- a/Session.xcodeproj/project.pbxproj +++ b/Session.xcodeproj/project.pbxproj @@ -134,6 +134,8 @@ 76C87F19181EFCE600C4ACAB /* MediaPlayer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 76C87F18181EFCE600C4ACAB /* MediaPlayer.framework */; }; 76EB054018170B33006006FC /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 76EB03C318170B33006006FC /* AppDelegate.m */; }; 7B1581E2271E743B00848B49 /* OWSSounds.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B1581E1271E743B00848B49 /* OWSSounds.swift */; }; + 7B1D74AA27BCC16E0030B423 /* NSENotificationPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B1D74A927BCC16E0030B423 /* NSENotificationPresenter.swift */; }; + 7B1D74AC27BDE7510030B423 /* Promise+Timeout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B1D74AB27BDE7510030B423 /* Promise+Timeout.swift */; }; 7B4C75CB26B37E0F0000AC89 /* UnsendRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B4C75CA26B37E0F0000AC89 /* UnsendRequest.swift */; }; 7B4C75CD26BB92060000AC89 /* DeletedMessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B4C75CC26BB92060000AC89 /* DeletedMessageView.swift */; }; 7B7CB18B270591630079FF93 /* ShareLogsModal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B7CB18A270591630079FF93 /* ShareLogsModal.swift */; }; @@ -1113,6 +1115,8 @@ 76EB03C318170B33006006FC /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 7ABE4694B110C1BBCB0E46A2 /* Pods-GlobalDependencies-FrameworkAndExtensionDependencies-ExtendedDependencies-SignalUtilitiesKit.app store release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GlobalDependencies-FrameworkAndExtensionDependencies-ExtendedDependencies-SignalUtilitiesKit.app store release.xcconfig"; path = "Pods/Target Support Files/Pods-GlobalDependencies-FrameworkAndExtensionDependencies-ExtendedDependencies-SignalUtilitiesKit/Pods-GlobalDependencies-FrameworkAndExtensionDependencies-ExtendedDependencies-SignalUtilitiesKit.app store release.xcconfig"; sourceTree = ""; }; 7B1581E1271E743B00848B49 /* OWSSounds.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OWSSounds.swift; sourceTree = ""; }; + 7B1D74A927BCC16E0030B423 /* NSENotificationPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSENotificationPresenter.swift; sourceTree = ""; }; + 7B1D74AB27BDE7510030B423 /* Promise+Timeout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Promise+Timeout.swift"; sourceTree = ""; }; 7B2DB2AD26F1B0FF0035B509 /* si */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = si; path = si.lproj/Localizable.strings; sourceTree = ""; }; 7B4C75CA26B37E0F0000AC89 /* UnsendRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnsendRequest.swift; sourceTree = ""; }; 7B4C75CC26BB92060000AC89 /* DeletedMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeletedMessageView.swift; sourceTree = ""; }; @@ -2069,6 +2073,7 @@ C31C219B255BC92200EC2D66 /* Meta */, 7BDCFC07242186E700641C39 /* NotificationServiceExtensionContext.swift */, 7BC01A3D241F40AB00BC7C55 /* NotificationServiceExtension.swift */, + 7B1D74A927BCC16E0030B423 /* NSENotificationPresenter.swift */, ); path = SessionNotificationServiceExtension; sourceTree = ""; @@ -2281,6 +2286,7 @@ C3C2A5D32553860900C340D1 /* Promise+Delaying.swift */, C3A7225D2558C38D0043A11F /* Promise+Retaining.swift */, C3C2A5D62553860B00C340D1 /* Promise+Retrying.swift */, + 7B1D74AB27BDE7510030B423 /* Promise+Timeout.swift */, ); path = PromiseKit; sourceTree = ""; @@ -4412,6 +4418,7 @@ files = ( 7BDCFC08242186E700641C39 /* NotificationServiceExtensionContext.swift in Sources */, 7BC01A3E241F40AB00BC7C55 /* NotificationServiceExtension.swift in Sources */, + 7B1D74AA27BCC16E0030B423 /* NSENotificationPresenter.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -4605,6 +4612,7 @@ buildActionMask = 2147483647; files = ( C3AABDDF2553ECF00042FF4C /* Array+Description.swift in Sources */, + 7B1D74AC27BDE7510030B423 /* Promise+Timeout.swift in Sources */, C32C5A47256DB8F0003C73A2 /* ECKeyPair+Hexadecimal.swift in Sources */, C3D9E41525676C320040E4F3 /* Storage.swift in Sources */, C32C5D83256DD5B6003C73A2 /* SSKKeychainStorage.swift in Sources */, diff --git a/Session.xcodeproj/xcshareddata/xcschemes/Session.xcscheme b/Session.xcodeproj/xcshareddata/xcschemes/Session.xcscheme index 3426f0ce1..9c2c819e3 100644 --- a/Session.xcodeproj/xcshareddata/xcschemes/Session.xcscheme +++ b/Session.xcodeproj/xcshareddata/xcschemes/Session.xcscheme @@ -229,7 +229,7 @@ String { + var result = self + let regex = try! NSRegularExpression(pattern: "@[0-9a-fA-F]{66}", options: []) + var mentions: [(range: NSRange, publicKey: String)] = [] + var m0 = regex.firstMatch(in: result, options: .withoutAnchoringBounds, range: NSRange(location: 0, length: result.utf16.count)) + while let m1 = m0 { + let publicKey = String((result as NSString).substring(with: m1.range).dropFirst()) // Drop the @ + var matchEnd = m1.range.location + m1.range.length + let displayName = Storage.shared.getContact(with: publicKey, using: transaction)?.displayName(for: .regular) + if let displayName = displayName { + result = (result as NSString).replacingCharacters(in: m1.range, with: "@\(displayName)") + mentions.append((range: NSRange(location: m1.range.location, length: displayName.utf16.count + 1), publicKey: publicKey)) // + 1 to include the @ + matchEnd = m1.range.location + displayName.utf16.count + } + m0 = regex.firstMatch(in: result, options: .withoutAnchoringBounds, range: NSRange(location: matchEnd, length: result.utf16.count - matchEnd)) + } + return result + } +} + diff --git a/SessionNotificationServiceExtension/NotificationServiceExtension.swift b/SessionNotificationServiceExtension/NotificationServiceExtension.swift index a52e672ff..7ffb87888 100644 --- a/SessionNotificationServiceExtension/NotificationServiceExtension.swift +++ b/SessionNotificationServiceExtension/NotificationServiceExtension.swift @@ -1,6 +1,7 @@ import UserNotifications import SessionMessagingKit import SignalUtilitiesKit +import PromiseKit public final class NotificationServiceExtension : UNNotificationServiceExtension { private var didPerformSetup = false @@ -8,13 +9,14 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension private var contentHandler: ((UNNotificationContent) -> Void)? private var notificationContent: UNMutableNotificationContent? - private static let isFromRemoteKey = "remote" - private static let threadIdKey = "Signal.AppNotificationsUserInfoKey.threadId" + public static let isFromRemoteKey = "remote" + public static let threadIdKey = "Signal.AppNotificationsUserInfoKey.threadId" + // MARK: Did receive a remote push notification request + override public func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { self.contentHandler = contentHandler self.notificationContent = request.content.mutableCopy() as? UNMutableNotificationContent - let userPublicKey = SNGeneralUtilities.getUserPublicKey() // Abort if the main app is running var isMainAppAndActive = false @@ -28,6 +30,12 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension // Handle the push notification AppReadiness.runNowOrWhenAppDidBecomeReady { + let openGorupPollingPromises = self.pollForOpneGorups() + defer { + when(resolved: openGorupPollingPromises).done { _ in + self.completeSilenty() + } + } let notificationContent = self.notificationContent! guard let base64EncodedData = notificationContent.userInfo["ENCRYPTED_DATA"] as! String?, let data = Data(base64Encoded: base64EncodedData), let envelope = try? MessageWrapper.unwrap(data: data), let envelopeAsData = try? envelope.serializedData() else { @@ -36,37 +44,12 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension Storage.write { transaction in // Intentionally capture self do { let (message, proto) = try MessageReceiver.parse(envelopeAsData, openGroupMessageServerID: nil, using: transaction) - let senderPublicKey = message.sender! - var senderDisplayName = Storage.shared.getContact(with: senderPublicKey)?.displayName(for: .regular) ?? senderPublicKey - let snippet: String - var userInfo: [String:Any] = [ NotificationServiceExtension.isFromRemoteKey : true ] switch message { case let visibleMessage as VisibleMessage: - let tsIncomingMessageID = try MessageReceiver.handleVisibleMessage(visibleMessage, associatedWithProto: proto, openGroupID: nil, isBackgroundPoll: false, using: transaction) - guard let tsMessage = TSMessage.fetch(uniqueId: tsIncomingMessageID, transaction: transaction) else { - return self.completeSilenty() - } - let thread = tsMessage.thread(with: transaction) - let threadID = thread.uniqueId! - userInfo[NotificationServiceExtension.threadIdKey] = threadID - snippet = tsMessage.previewText(with: transaction).filterForDisplay?.replacingMentions(for: threadID, using: transaction) - ?? "You've got a new message" - if let tsIncomingMessage = tsMessage as? TSIncomingMessage { - if thread.isMuted { - // Ignore PNs if the thread is muted - return self.completeSilenty() - } - if let thread = TSThread.fetch(uniqueId: threadID, transaction: transaction), let group = thread as? TSGroupThread, - group.groupModel.groupType == .closedGroup { // Should always be true because we don't get PNs for open groups - senderDisplayName = String(format: NotificationStrings.incomingGroupMessageTitleFormat, senderDisplayName, group.groupModel.groupName ?? MessageStrings.newGroupDefaultTitle) - if group.isOnlyNotifyingForMentions && !tsIncomingMessage.isUserMentioned { - // Ignore PNs if the group is set to only notify for mentions - return self.completeSilenty() - } - } - // Store the notification ID for unsend requests to later cancel this notification - tsIncomingMessage.setNotificationIdentifier(request.identifier, transaction: transaction) - } else { + let tsMessageID = try MessageReceiver.handleVisibleMessage(visibleMessage, associatedWithProto: proto, openGroupID: nil, isBackgroundPoll: false, using: transaction) + + // Remove the notificaitons if there is an outgoing messages from a linked device + if let tsMessage = TSMessage.fetch(uniqueId: tsMessageID, transaction: transaction), tsMessage.isKind(of: TSOutgoingMessage.self), let threadID = tsMessage.thread(with: transaction).uniqueId { let semaphore = DispatchSemaphore(value: 0) let center = UNUserNotificationCenter.current() center.getDeliveredNotifications { notifications in @@ -77,51 +60,24 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension } semaphore.wait() } - notificationContent.sound = OWSSounds.notificationSound(for: thread).notificationSound(isQuiet: false) + case let unsendRequest as UnsendRequest: MessageReceiver.handleUnsendRequest(unsendRequest, using: transaction) - return self.completeSilenty() case let closedGroupControlMessage as ClosedGroupControlMessage: - // TODO: We could consider actually handling the update here. Not sure if there's enough time though, seeing as though - // in some cases we need to send messages (e.g. our sender key) to a number of other users. - switch closedGroupControlMessage.kind { - case .new(_, let name, _, _, _, _): snippet = "\(senderDisplayName) added you to \(name)" - default: return self.completeSilenty() - } - default: return self.completeSilenty() - } - if (senderPublicKey == userPublicKey) { - // Ignore PNs for messages sent by the current user - // after handling the message. Otherwise the closed - // group self-send messages won't show. - return self.completeSilenty() - } - notificationContent.userInfo = userInfo - notificationContent.badge = NSNumber(value: OWSMessageUtils.sharedManager().unreadMessagesCount() + 1) - let notificationsPreference = Environment.shared.preferences!.notificationPreviewType() - switch notificationsPreference { - case .namePreview: - notificationContent.title = senderDisplayName - notificationContent.body = snippet - case .nameNoPreview: - notificationContent.title = senderDisplayName - notificationContent.body = NotificationStrings.incomingMessageBody - case .noNameNoPreview: - notificationContent.title = "Session" - notificationContent.body = NotificationStrings.incomingMessageBody + MessageReceiver.handleClosedGroupControlMessage(closedGroupControlMessage, using: transaction) default: break } - self.handleSuccess(for: notificationContent) } catch { if let error = error as? MessageReceiver.Error, error.isRetryable { self.handleFailure(for: notificationContent) } - self.completeSilenty() } } } } + // MARK: Setup + private func setUpIfNecessary(completion: @escaping () -> Void) { AssertIsOnMainThread() @@ -148,7 +104,7 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension AppSetup.setupEnvironment( appSpecificSingletonBlock: { - SSKEnvironment.shared.notificationsManager = NoopNotificationsManager() + SSKEnvironment.shared.notificationsManager = NSENotificationPresenter() }, migrationCompletion: { [weak self] in self?.versionMigrationsDidComplete() @@ -159,18 +115,6 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension NotificationCenter.default.addObserver(self, selector: #selector(storageIsReady), name: .StorageIsReady, object: nil) } - override public func serviceExtensionTimeWillExpire() { - // Called just before the extension will be terminated by the system. - // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. - let userInfo: [String:Any] = [ NotificationServiceExtension.isFromRemoteKey : true ] - let notificationContent = self.notificationContent! - notificationContent.userInfo = userInfo - notificationContent.badge = 1 - notificationContent.title = "Session" - notificationContent.body = "You've got a new message" - handleSuccess(for: notificationContent) - } - @objc private func versionMigrationsDidComplete() { AssertIsOnMainThread() @@ -203,8 +147,17 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension AppReadiness.setAppIsReady() } + // MARK: Handle completion + + override public func serviceExtensionTimeWillExpire() { + // Called just before the extension will be terminated by the system. + // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. + completeSilenty() + } + private func completeSilenty() { - contentHandler!(.init()) + SNLog("Complete silenty") + self.contentHandler!(.init()) } private func handleSuccess(for content: UNMutableNotificationContent) { @@ -218,26 +171,20 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension content.userInfo = userInfo contentHandler!(content) } -} - -private extension String { - func replacingMentions(for threadID: String, using transaction: YapDatabaseReadWriteTransaction) -> String { - var result = self - let regex = try! NSRegularExpression(pattern: "@[0-9a-fA-F]{66}", options: []) - var mentions: [(range: NSRange, publicKey: String)] = [] - var m0 = regex.firstMatch(in: result, options: .withoutAnchoringBounds, range: NSRange(location: 0, length: result.utf16.count)) - while let m1 = m0 { - let publicKey = String((result as NSString).substring(with: m1.range).dropFirst()) // Drop the @ - var matchEnd = m1.range.location + m1.range.length - let displayName = Storage.shared.getContact(with: publicKey, using: transaction)?.displayName(for: .regular) - if let displayName = displayName { - result = (result as NSString).replacingCharacters(in: m1.range, with: "@\(displayName)") - mentions.append((range: NSRange(location: m1.range.location, length: displayName.utf16.count + 1), publicKey: publicKey)) // + 1 to include the @ - matchEnd = m1.range.location + displayName.utf16.count - } - m0 = regex.firstMatch(in: result, options: .withoutAnchoringBounds, range: NSRange(location: matchEnd, length: result.utf16.count - matchEnd)) + // MARK: Poll for open groups + private func pollForOpneGorups() -> [Promise] { + var promises: [Promise] = [] + let servers = Set(Storage.shared.getAllV2OpenGroups().values.map { $0.server }) + servers.forEach { server in + let poller = OpenGroupPollerV2(for: server) + let promise = poller.poll().timeout(seconds: 20, timeoutError: NotificationServiceError.timeout) + promises.append(promise) } - return result + return promises + } + + private enum NotificationServiceError: Error { + case timeout } } diff --git a/SessionUtilitiesKit/PromiseKit/Promise+Timeout.swift b/SessionUtilitiesKit/PromiseKit/Promise+Timeout.swift new file mode 100644 index 000000000..e4b843df6 --- /dev/null +++ b/SessionUtilitiesKit/PromiseKit/Promise+Timeout.swift @@ -0,0 +1,19 @@ +// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved. + +import PromiseKit + +public extension Promise { + + func timeout(seconds: TimeInterval, timeoutError: Error) -> Promise { + return Promise { seal in + after(seconds: seconds).done { + seal.reject(timeoutError) + } + self.done { result in + seal.fulfill(result) + }.catch { err in + seal.reject(err) + } + } + } +}