From 500eaf40002877b8658a211259a92828a6d0b2a8 Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Thu, 17 Sep 2020 10:09:16 +1000 Subject: [PATCH 1/3] refactor the process of push notification --- SignalServiceKit/src/Loki/API/SnodeAPI.swift | 5 +- .../LokiPushNotificationManager.swift | 57 +++++++++++-------- .../src/Messages/OWSMessageSender.m | 1 + 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/SnodeAPI.swift b/SignalServiceKit/src/Loki/API/SnodeAPI.swift index f7ed65bc4..e851f07fa 100644 --- a/SignalServiceKit/src/Loki/API/SnodeAPI.swift +++ b/SignalServiceKit/src/Loki/API/SnodeAPI.swift @@ -243,10 +243,7 @@ public final class SnodeAPI : NSObject { internal static func parseRawMessagesResponse(_ rawResponse: Any, from snode: Snode, associatedWith publicKey: String) -> [SSKProtoEnvelope] { guard let json = rawResponse as? JSON, let rawMessages = json["messages"] as? [JSON] else { return [] } - if let (lastHash, expirationDate) = updateLastMessageHashValueIfPossible(for: snode, associatedWith: publicKey, from: rawMessages), - UserDefaults.standard[.isUsingFullAPNs] { - LokiPushNotificationManager.acknowledgeDelivery(forMessageWithHash: lastHash, expiration: expirationDate, publicKey: getUserHexEncodedPublicKey()) - } + updateLastMessageHashValueIfPossible(for: snode, associatedWith: publicKey, from: rawMessages) let rawNewMessages = removeDuplicates(from: rawMessages, associatedWith: publicKey) let newMessages = parseProtoEnvelopes(from: rawNewMessages) return newMessages diff --git a/SignalServiceKit/src/Loki/Push Notifications/LokiPushNotificationManager.swift b/SignalServiceKit/src/Loki/Push Notifications/LokiPushNotificationManager.swift index d060eee2b..4f625d909 100644 --- a/SignalServiceKit/src/Loki/Push Notifications/LokiPushNotificationManager.swift +++ b/SignalServiceKit/src/Loki/Push Notifications/LokiPushNotificationManager.swift @@ -5,10 +5,11 @@ public final class LokiPushNotificationManager : NSObject { // MARK: Settings #if DEBUG - private static let server = "https://dev.apns.getsession.org/" + private static let server = "https://staging.apns.getsession.org" #else - private static let server = "https://live.apns.getsession.org/" + private static let server = "https://live.apns.getsession.org" #endif + internal static let PNServerPublicKey = "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049" private static let tokenExpirationInterval: TimeInterval = 12 * 60 * 60 public enum ClosedGroupOperation: String { @@ -34,7 +35,7 @@ public final class LokiPushNotificationManager : NSObject { return Promise { $0.fulfill(()) } } let parameters = [ "token" : hexEncodedToken ] - let url = URL(string: server + "register")! + let url = URL(string: "\(server)/register")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] let promise = TSNetworkManager.shared().makePromise(request: request).map2 { _, response in @@ -79,7 +80,7 @@ public final class LokiPushNotificationManager : NSObject { return Promise { $0.fulfill(()) } } let parameters = [ "token" : hexEncodedToken, "pubKey" : publicKey] - let url = URL(string: server + "register")! + let url = URL(string: "\(server)/register")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] let promise = TSNetworkManager.shared().makePromise(request: request).map2 { _, response in @@ -111,30 +112,11 @@ public final class LokiPushNotificationManager : NSObject { return AnyPromise.from(register(with: token, publicKey: publicKey, isForcedUpdate: isForcedUpdate)) } - @objc(acknowledgeDeliveryForMessageWithHash:expiration:hexEncodedPublicKey:) - static func acknowledgeDelivery(forMessageWithHash hash: String, expiration: UInt64, publicKey: String) { - guard UserDefaults.standard[.isUsingFullAPNs] else { return } - let parameters: JSON = [ "lastHash" : hash, "pubKey" : publicKey, "expiration" : expiration] - let url = URL(string: server + "acknowledge_message_delivery")! - let request = TSRequest(url: url, method: "POST", parameters: parameters) - request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] - TSNetworkManager.shared().makeRequest(request, success: { _, response in - guard let json = response as? JSON else { - return print("[Loki] Couldn't acknowledge delivery for message with hash: \(hash).") - } - guard json["code"] as? Int != 0 else { - return print("[Loki] Couldn't acknowledge delivery for message with hash: \(hash) due to error: \(json["message"] as? String ?? "nil").") - } - }, failure: { _, error in - print("[Loki] Couldn't acknowledge delivery for message with hash: \(hash) due to error: \(error).") - }) - } - static func performOperation(_ operation: ClosedGroupOperation, for closedGroupPublicKey: String, publicKey: String) -> Promise { let isUsingFullAPNs = UserDefaults.standard[.isUsingFullAPNs] guard isUsingFullAPNs else { return Promise { $0.fulfill(()) } } let parameters = [ "closedGroupPublicKey" : closedGroupPublicKey, "pubKey" : publicKey] - let url = URL(string: server + operation.rawValue)! + let url = URL(string: "\(server)/\(operation.rawValue)")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] let promise = TSNetworkManager.shared().makePromise(request: request).map2 { _, response in @@ -151,4 +133,31 @@ public final class LokiPushNotificationManager : NSObject { } return promise } + + @objc(notifyMessage:) + static func objc_notify(_ signalMessage: SignalMessage) -> AnyPromise { + return AnyPromise.from(notify(signalMessage)) + } + + static func notify(_ signalMessage: SignalMessage) -> Promise { + let message = LokiMessage.from(signalMessage: signalMessage)! + guard message.ttl == TTLUtilities.getTTL(for: .regular) else { return Promise { $0.fulfill(()) } } + let parameters = [ "data" : message.data.description, "send_to" : message.recipientPublicKey] + let url = URL(string: "\(server)/notify")! + let request = TSRequest(url: url, method: "POST", parameters: parameters) + request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] + let promise = OnionRequestAPI.sendOnionRequest(request, to: server, using: PNServerPublicKey).map2 { response in + guard let json = response as? JSON else { + return print("[Loki] Couldn't notify message.") + } + guard json["code"] as? Int != 0 else { + return print("[Loki] Couldn't notify message due to error: \(json["message"] as? String ?? "nil").") + } + return + } + promise.catch2 { error in + print("[Loki] Couldn't notify message.") + } + return promise + } } diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 2250ef271..121c553fb 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -1135,6 +1135,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; if (isSuccess) { return; } // Succeed as soon as the first promise succeeds [NSNotificationCenter.defaultCenter postNotificationName:NSNotification.messageSent object:[[NSNumber alloc] initWithUnsignedLongLong:signalMessage.timestamp]]; isSuccess = YES; + [LKPushNotificationManager notifyMessage:signalMessage]; [self messageSendDidSucceed:messageSend deviceMessages:deviceMessages wasSentByUD:messageSend.isUDSend wasSentByWebsocket:false]; }) .catchOn(OWSDispatch.sendingQueue, ^(NSError *error) { From 0ed4211548c8416a81398794a1a7f3945fc19b22 Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Fri, 18 Sep 2020 10:07:41 +1000 Subject: [PATCH 2/3] rename unregister and use onion routing for all PN related requests --- Signal/src/AppDelegate.m | 4 +-- .../src/Account/TSAccountManager.m | 2 +- .../LokiPushNotificationManager.swift | 30 +++++++++---------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index a24f729df..f712ce4a1 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -763,7 +763,7 @@ static NSTimeInterval launchStartedAt; if (isUsingFullAPNs) { __unused AnyPromise *promise = [LKPushNotificationManager registerWithToken:deviceToken hexEncodedPublicKey:self.tsAccountManager.localNumber isForcedUpdate:NO]; } else { - __unused AnyPromise *promise = [LKPushNotificationManager registerWithToken:deviceToken isForcedUpdate:NO]; + __unused AnyPromise *promise = [LKPushNotificationManager unregisterWithToken:deviceToken isForcedUpdate:NO]; } } @@ -949,7 +949,7 @@ static NSTimeInterval launchStartedAt; NSString *hexEncodedDeviceToken = [userDefaults stringForKey:@"deviceToken"]; if (isUsingFullAPNs && hexEncodedDeviceToken != nil) { NSData *deviceToken = [NSData dataFromHexString:hexEncodedDeviceToken]; - [[LKPushNotificationManager registerWithToken:deviceToken isForcedUpdate:YES] retainUntilComplete]; // This actually unregisters the user; we should rename the function + [[LKPushNotificationManager unregisterWithToken:deviceToken isForcedUpdate:YES] retainUntilComplete]; } [ThreadUtil deleteAllContent]; [SSKEnvironment.shared.messageSenderJobQueue clearAllJobs]; diff --git a/SignalServiceKit/src/Account/TSAccountManager.m b/SignalServiceKit/src/Account/TSAccountManager.m index 85af560d1..fc516ab00 100644 --- a/SignalServiceKit/src/Account/TSAccountManager.m +++ b/SignalServiceKit/src/Account/TSAccountManager.m @@ -302,7 +302,7 @@ NSString *const TSAccountManager_NeedsAccountAttributesUpdateKey = @"TSAccountMa BOOL isUsingFullAPNs = [NSUserDefaults.standardUserDefaults boolForKey:@"isUsingFullAPNs"]; NSData *pushTokenAsData = [NSData dataFromHexString:pushToken]; AnyPromise *promise = isUsingFullAPNs ? [LKPushNotificationManager registerWithToken:pushTokenAsData hexEncodedPublicKey:self.localNumber isForcedUpdate:isForcedUpdate] - : [LKPushNotificationManager registerWithToken:pushTokenAsData isForcedUpdate:isForcedUpdate]; + : [LKPushNotificationManager unregisterWithToken:pushTokenAsData isForcedUpdate:isForcedUpdate]; promise .then(^() { successHandler(); diff --git a/SignalServiceKit/src/Loki/Push Notifications/LokiPushNotificationManager.swift b/SignalServiceKit/src/Loki/Push Notifications/LokiPushNotificationManager.swift index 4f625d909..bc2dd817d 100644 --- a/SignalServiceKit/src/Loki/Push Notifications/LokiPushNotificationManager.swift +++ b/SignalServiceKit/src/Loki/Push Notifications/LokiPushNotificationManager.swift @@ -21,9 +21,9 @@ public final class LokiPushNotificationManager : NSObject { private override init() { } // MARK: Registration - /// Registers the user for silent push notifications (that then trigger the app - /// into fetching messages). Only the user's device token is needed for this. - static func register(with token: Data, isForcedUpdate: Bool) -> Promise { + /// Unregisters the user for push notifications. + /// Only the user's device token is needed for this. + static func unregister(with token: Data, isForcedUpdate: Bool) -> Promise { let hexEncodedToken = token.toHexString() let userDefaults = UserDefaults.standard let oldToken = userDefaults[.deviceToken] @@ -35,10 +35,10 @@ public final class LokiPushNotificationManager : NSObject { return Promise { $0.fulfill(()) } } let parameters = [ "token" : hexEncodedToken ] - let url = URL(string: "\(server)/register")! + let url = URL(string: "\(server)/unregister")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] - let promise = TSNetworkManager.shared().makePromise(request: request).map2 { _, response in + let promise = OnionRequestAPI.sendOnionRequest(request, to: server, using: PNServerPublicKey).map2 { response in guard let json = response as? JSON else { return print("[Loki] Couldn't register device token.") } @@ -60,11 +60,11 @@ public final class LokiPushNotificationManager : NSObject { return promise } - /// Registers the user for silent push notifications (that then trigger the app - /// into fetching messages). Only the user's device token is needed for this. - @objc(registerWithToken:isForcedUpdate:) - static func objc_register(with token: Data, isForcedUpdate: Bool) -> AnyPromise { - return AnyPromise.from(register(with: token, isForcedUpdate: isForcedUpdate)) + /// Unregisters the user for push notifications. + /// Only the user's device token is needed for this. + @objc(unregisterWithToken:isForcedUpdate:) + static func objc_unregister(with token: Data, isForcedUpdate: Bool) -> AnyPromise { + return AnyPromise.from(unregister(with: token, isForcedUpdate: isForcedUpdate)) } /// Registers the user for normal push notifications. Requires the user's device @@ -83,7 +83,7 @@ public final class LokiPushNotificationManager : NSObject { let url = URL(string: "\(server)/register")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] - let promise = TSNetworkManager.shared().makePromise(request: request).map2 { _, response in + let promise = OnionRequestAPI.sendOnionRequest(request, to: server, using: PNServerPublicKey).map2 { response in guard let json = response as? JSON else { return print("[Loki] Couldn't register device token.") } @@ -119,17 +119,17 @@ public final class LokiPushNotificationManager : NSObject { let url = URL(string: "\(server)/\(operation.rawValue)")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] - let promise = TSNetworkManager.shared().makePromise(request: request).map2 { _, response in + let promise = OnionRequestAPI.sendOnionRequest(request, to: server, using: PNServerPublicKey).map2 { response in guard let json = response as? JSON else { - return print("[Loki] Couldn't subscribe to PNs for closed group with ID: \(closedGroupPublicKey).") + return print("[Loki] Couldn't subscribe/unsubscribe closed group: \(closedGroupPublicKey).") } guard json["code"] as? Int != 0 else { - return print("[Loki] Couldn't subscribe to PNs for closed group with ID: \(closedGroupPublicKey) due to error: \(json["message"] as? String ?? "nil").") + return print("[Loki] Couldn't subscribe/unsubscribe for closed group: \(closedGroupPublicKey) due to error: \(json["message"] as? String ?? "nil").") } return } promise.catch2 { error in - print("[Loki] Couldn't subscribe to PNs for closed group with ID: \(closedGroupPublicKey).") + print("[Loki] Couldn't subscribe/unsubscribe closed group: \(closedGroupPublicKey).") } return promise } From 3be9f05cfb6942c8dd6bc889164d7e17d24213f2 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 18 Sep 2020 11:20:18 +1000 Subject: [PATCH 3/3] Move away from TTL based approach --- .../LokiPushNotificationManager.swift | 39 +++++++++---------- .../src/Messages/OWSMessageSender.m | 8 +++- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/SignalServiceKit/src/Loki/Push Notifications/LokiPushNotificationManager.swift b/SignalServiceKit/src/Loki/Push Notifications/LokiPushNotificationManager.swift index bc2dd817d..ce5ed26de 100644 --- a/SignalServiceKit/src/Loki/Push Notifications/LokiPushNotificationManager.swift +++ b/SignalServiceKit/src/Loki/Push Notifications/LokiPushNotificationManager.swift @@ -9,7 +9,7 @@ public final class LokiPushNotificationManager : NSObject { #else private static let server = "https://live.apns.getsession.org" #endif - internal static let PNServerPublicKey = "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049" + internal static let pnServerPublicKey = "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049" private static let tokenExpirationInterval: TimeInterval = 12 * 60 * 60 public enum ClosedGroupOperation: String { @@ -21,8 +21,7 @@ public final class LokiPushNotificationManager : NSObject { private override init() { } // MARK: Registration - /// Unregisters the user for push notifications. - /// Only the user's device token is needed for this. + /// Unregisters the user from push notifications. Only the user's device token is needed for this. static func unregister(with token: Data, isForcedUpdate: Bool) -> Promise { let hexEncodedToken = token.toHexString() let userDefaults = UserDefaults.standard @@ -38,7 +37,7 @@ public final class LokiPushNotificationManager : NSObject { let url = URL(string: "\(server)/unregister")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] - let promise = OnionRequestAPI.sendOnionRequest(request, to: server, using: PNServerPublicKey).map2 { response in + let promise = OnionRequestAPI.sendOnionRequest(request, to: server, using: pnServerPublicKey).map2 { response in guard let json = response as? JSON else { return print("[Loki] Couldn't register device token.") } @@ -60,14 +59,13 @@ public final class LokiPushNotificationManager : NSObject { return promise } - /// Unregisters the user for push notifications. - /// Only the user's device token is needed for this. + /// Unregisters the user from push notifications. Only the user's device token is needed for this. @objc(unregisterWithToken:isForcedUpdate:) static func objc_unregister(with token: Data, isForcedUpdate: Bool) -> AnyPromise { return AnyPromise.from(unregister(with: token, isForcedUpdate: isForcedUpdate)) } - /// Registers the user for normal push notifications. Requires the user's device + /// Registers the user for push notifications. Requires the user's device /// token and their Session ID. static func register(with token: Data, publicKey: String, isForcedUpdate: Bool) -> Promise { let hexEncodedToken = token.toHexString() @@ -83,7 +81,7 @@ public final class LokiPushNotificationManager : NSObject { let url = URL(string: "\(server)/register")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] - let promise = OnionRequestAPI.sendOnionRequest(request, to: server, using: PNServerPublicKey).map2 { response in + let promise = OnionRequestAPI.sendOnionRequest(request, to: server, using: pnServerPublicKey).map2 { response in guard let json = response as? JSON else { return print("[Loki] Couldn't register device token.") } @@ -105,7 +103,7 @@ public final class LokiPushNotificationManager : NSObject { return promise } - /// Registers the user for normal push notifications. Requires the user's device + /// Registers the user for push notifications. Requires the user's device /// token and their Session ID. @objc(registerWithToken:hexEncodedPublicKey:isForcedUpdate:) static func objc_register(with token: Data, publicKey: String, isForcedUpdate: Bool) -> AnyPromise { @@ -119,7 +117,7 @@ public final class LokiPushNotificationManager : NSObject { let url = URL(string: "\(server)/\(operation.rawValue)")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] - let promise = OnionRequestAPI.sendOnionRequest(request, to: server, using: PNServerPublicKey).map2 { response in + let promise = OnionRequestAPI.sendOnionRequest(request, to: server, using: pnServerPublicKey).map2 { response in guard let json = response as? JSON else { return print("[Loki] Couldn't subscribe/unsubscribe closed group: \(closedGroupPublicKey).") } @@ -134,30 +132,29 @@ public final class LokiPushNotificationManager : NSObject { return promise } - @objc(notifyMessage:) - static func objc_notify(_ signalMessage: SignalMessage) -> AnyPromise { - return AnyPromise.from(notify(signalMessage)) - } - - static func notify(_ signalMessage: SignalMessage) -> Promise { + static func notify(for signalMessage: SignalMessage) -> Promise { let message = LokiMessage.from(signalMessage: signalMessage)! - guard message.ttl == TTLUtilities.getTTL(for: .regular) else { return Promise { $0.fulfill(()) } } let parameters = [ "data" : message.data.description, "send_to" : message.recipientPublicKey] let url = URL(string: "\(server)/notify")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] - let promise = OnionRequestAPI.sendOnionRequest(request, to: server, using: PNServerPublicKey).map2 { response in + let promise = OnionRequestAPI.sendOnionRequest(request, to: server, using: pnServerPublicKey).map2 { response in guard let json = response as? JSON else { - return print("[Loki] Couldn't notify message.") + return print("[Loki] Couldn't notify PN server.") } guard json["code"] as? Int != 0 else { - return print("[Loki] Couldn't notify message due to error: \(json["message"] as? String ?? "nil").") + return print("[Loki] Couldn't notify PN server due to error: \(json["message"] as? String ?? "nil").") } return } promise.catch2 { error in - print("[Loki] Couldn't notify message.") + print("[Loki] Couldn't notify PN server.") } return promise } + + @objc(notifyForMessage:) + static func objc_notify(for signalMessage: SignalMessage) -> AnyPromise { + return AnyPromise.from(notify(for: signalMessage)) + } } diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 121c553fb..bf73525ba 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -900,7 +900,9 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; TSOutgoingMessage *message = messageSend.message; SignalRecipient *recipient = messageSend.recipient; - + + BOOL notifyPNServer = ((message.body != nil && message.body.length > 0) || message.hasAttachments); + OWSLogInfo(@"Attempting to send message: %@, timestamp: %llu, recipient: %@.", message.class, message.timestamp, @@ -1135,7 +1137,9 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; if (isSuccess) { return; } // Succeed as soon as the first promise succeeds [NSNotificationCenter.defaultCenter postNotificationName:NSNotification.messageSent object:[[NSNumber alloc] initWithUnsignedLongLong:signalMessage.timestamp]]; isSuccess = YES; - [LKPushNotificationManager notifyMessage:signalMessage]; + if (notifyPNServer) { + [LKPushNotificationManager notifyForMessage:signalMessage]; + } [self messageSendDidSucceed:messageSend deviceMessages:deviceMessages wasSentByUD:messageSend.isUDSend wasSentByWebsocket:false]; }) .catchOn(OWSDispatch.sendingQueue, ^(NSError *error) {