poll for open groups in NSE

This commit is contained in:
Ryan Zhao 2022-02-17 14:55:32 +11:00
parent 7f8c952c66
commit cc1b1e8c51
9 changed files with 188 additions and 101 deletions

View File

@ -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 = "<group>"; };
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 = "<group>"; };
7B1581E1271E743B00848B49 /* OWSSounds.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OWSSounds.swift; sourceTree = "<group>"; };
7B1D74A927BCC16E0030B423 /* NSENotificationPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSENotificationPresenter.swift; sourceTree = "<group>"; };
7B1D74AB27BDE7510030B423 /* Promise+Timeout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Promise+Timeout.swift"; sourceTree = "<group>"; };
7B2DB2AD26F1B0FF0035B509 /* si */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = si; path = si.lproj/Localizable.strings; sourceTree = "<group>"; };
7B4C75CA26B37E0F0000AC89 /* UnsendRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnsendRequest.swift; sourceTree = "<group>"; };
7B4C75CC26BB92060000AC89 /* DeletedMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeletedMessageView.swift; sourceTree = "<group>"; };
@ -2069,6 +2073,7 @@
C31C219B255BC92200EC2D66 /* Meta */,
7BDCFC07242186E700641C39 /* NotificationServiceExtensionContext.swift */,
7BC01A3D241F40AB00BC7C55 /* NotificationServiceExtension.swift */,
7B1D74A927BCC16E0030B423 /* NSENotificationPresenter.swift */,
);
path = SessionNotificationServiceExtension;
sourceTree = "<group>";
@ -2281,6 +2286,7 @@
C3C2A5D32553860900C340D1 /* Promise+Delaying.swift */,
C3A7225D2558C38D0043A11F /* Promise+Retaining.swift */,
C3C2A5D62553860B00C340D1 /* Promise+Retrying.swift */,
7B1D74AB27BDE7510030B423 /* Promise+Timeout.swift */,
);
path = PromiseKit;
sourceTree = "<group>";
@ -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 */,

View File

@ -229,7 +229,7 @@
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "App Store Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
enableUBSanitizer = "YES"

View File

@ -226,6 +226,9 @@ NSString *const ReportedApplicationStateDidChangeNotification = @"ReportedApplic
- (void)setMainAppBadgeNumber:(NSInteger)value
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:value];
NSUserDefaults *sharedUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.loki-project.loki-messenger"];
[sharedUserDefaults setInteger:value forKey:@"currentBadgeNumber"];
[sharedUserDefaults synchronize];
}
- (nullable UIViewController *)frontmostViewController

View File

@ -194,7 +194,7 @@ public class NotificationPresenter: NSObject, NotificationsProtocol {
case is TSContactThread:
notificationTitle = senderName
case is TSGroupThread:
var groupName = thread.name()
var groupName = thread.name(with: transaction)
if groupName.count < 1 {
groupName = MessageStrings.newGroupDefaultTitle
}

View File

@ -340,7 +340,7 @@ extension MessageReceiver {
OWSReadReceiptManager.shared().markAsReadLocally(beforeSortId: tsOutgoingMessage.sortId, thread: thread)
}
// Notify the user if needed
guard (isMainAppAndActive || isBackgroundPoll), let tsIncomingMessage = TSMessage.fetch(uniqueId: tsMessageID, transaction: transaction) as? TSIncomingMessage,
guard let tsIncomingMessage = TSMessage.fetch(uniqueId: tsMessageID, transaction: transaction) as? TSIncomingMessage,
let thread = TSThread.fetch(uniqueId: threadID, transaction: transaction) else { return tsMessageID }
// Use the same identifier for notifications when in backgroud polling to prevent spam
let notificationIdentifier = isBackgroundPoll ? thread.uniqueId : UUID().uuidString
@ -404,7 +404,7 @@ extension MessageReceiver {
// MARK: - Closed Groups
private static func handleClosedGroupControlMessage(_ message: ClosedGroupControlMessage, using transaction: Any) {
public static func handleClosedGroupControlMessage(_ message: ClosedGroupControlMessage, using transaction: Any) {
switch message.kind! {
case .new: handleNewClosedGroup(message, using: transaction)
case .encryptionKeyPair: handleClosedGroupEncryptionKeyPair(message, using: transaction)

View File

@ -5,7 +5,7 @@ import PromiseKit
public final class PushNotificationAPI : NSObject {
// MARK: Settings
public static let server = "https://live.apns.getsession.org"
public static let server = "https://dev.apns.getsession.org"
public static let serverPublicKey = "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049"
private static let maxRetryCount: UInt = 4
private static let tokenExpirationInterval: TimeInterval = 12 * 60 * 60

View File

@ -0,0 +1,110 @@
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import SignalUtilitiesKit
import UserNotifications
public class NSENotificationPresenter: NSObject, NotificationsProtocol {
public func notifyUser(for incomingMessage: TSIncomingMessage, in thread: TSThread, transaction: YapDatabaseReadTransaction) {
guard !thread.isMuted else {
// Ignore PNs if the thread is muted
return
}
let senderPublicKey = incomingMessage.authorId
let userPublicKey = SNGeneralUtilities.getUserPublicKey()
guard senderPublicKey != userPublicKey else {
// Ignore PNs for messages sent by the current user
// after handling the message. Otherwise the closed
// group self-send messages won't show.
return
}
let context = Contact.context(for: thread)
let senderName = Storage.shared.getContact(with: senderPublicKey)?.displayName(for: context) ?? senderPublicKey
var notificationTitle = senderName
if let group = thread as? TSGroupThread {
if group.isOnlyNotifyingForMentions && !incomingMessage.isUserMentioned {
// Ignore PNs if the group is set to only notify for mentions
return
}
var groupName = thread.name(with: transaction)
if groupName.count < 1 {
groupName = MessageStrings.newGroupDefaultTitle
}
notificationTitle = String(format: NotificationStrings.incomingGroupMessageTitleFormat, senderName, groupName)
}
let threadID = thread.uniqueId!
let snippet = incomingMessage.previewText(with: transaction).filterForDisplay?.replacingMentions(for: threadID, using: transaction)
?? "APN_Message".localized()
var userInfo: [String:Any] = [ NotificationServiceExtension.isFromRemoteKey : true ]
userInfo[NotificationServiceExtension.threadIdKey] = threadID
let notificationContent = UNMutableNotificationContent()
notificationContent.userInfo = userInfo
notificationContent.sound = OWSSounds.notificationSound(for: thread).notificationSound(isQuiet: false)
if let sharedUserDefaults = UserDefaults(suiteName: "group.com.loki-project.loki-messenger") {
let newBadgeNumber = sharedUserDefaults.integer(forKey: "currentBadgeNumber") + 1
notificationContent.badge = NSNumber(value: newBadgeNumber)
sharedUserDefaults.set(newBadgeNumber, forKey: "currentBadgeNumber")
}
let notificationsPreference = Environment.shared.preferences!.notificationPreviewType()
switch notificationsPreference {
case .namePreview:
notificationContent.title = notificationTitle
notificationContent.body = snippet
case .nameNoPreview:
notificationContent.title = notificationTitle
notificationContent.body = NotificationStrings.incomingMessageBody
case .noNameNoPreview:
notificationContent.title = "Session"
notificationContent.body = NotificationStrings.incomingMessageBody
default: break
}
let identifier = incomingMessage.notificationIdentifier ?? UUID().uuidString
let request = UNNotificationRequest(identifier: identifier, content: notificationContent, trigger: nil)
SNLog("Add remote notification request")
UNUserNotificationCenter.current().add(request)
}
public func cancelNotification(_ identifier: String) {
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.removePendingNotificationRequests(withIdentifiers: [ identifier ])
notificationCenter.removeDeliveredNotifications(withIdentifiers: [ identifier ])
}
public func clearAllNotifications() {
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.removeAllPendingNotificationRequests()
notificationCenter.removeAllDeliveredNotifications()
}
}
private extension String {
func replacingMentions(for threadID: String, using transaction: YapDatabaseReadTransaction) -> 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
}
}

View File

@ -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<Void>] {
var promises: [Promise<Void>] = []
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
}
}

View File

@ -0,0 +1,19 @@
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import PromiseKit
public extension Promise {
func timeout(seconds: TimeInterval, timeoutError: Error) -> Promise<T> {
return Promise<T> { seal in
after(seconds: seconds).done {
seal.reject(timeoutError)
}
self.done { result in
seal.fulfill(result)
}.catch { err in
seal.reject(err)
}
}
}
}