From ec1a468aaba92cab94819122a667daef8edd2741 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Mon, 30 Mar 2020 17:07:25 +1100 Subject: [PATCH] move LKPushNotificationManager to SignalServiceKit & add lash hash sync with PN server --- Signal.xcodeproj/project.pbxproj | 4 --- SignalServiceKit/src/Loki/API/LokiAPI.swift | 3 ++ .../LokiPushNotificationManager.swift | 31 ++++++++++++++++--- 3 files changed, 29 insertions(+), 9 deletions(-) rename {Signal => SignalServiceKit}/src/Loki/Utilities/LokiPushNotificationManager.swift (65%) diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index 7774f995b..e95311b23 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -583,7 +583,6 @@ B83F2B88240CB75A000A54AB /* UIImage+Scaling.swift in Sources */ = {isa = PBXBuildFile; fileRef = B83F2B87240CB75A000A54AB /* UIImage+Scaling.swift */; }; B846365B22B7418B00AF1514 /* Identicon+ObjC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */; }; B84664F5235022F30083A1CD /* MentionUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B84664F4235022F30083A1CD /* MentionUtilities.swift */; }; - B847570323D5698100759540 /* LokiPushNotificationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = B847570223D5698100759540 /* LokiPushNotificationManager.swift */; }; B85357BF23A1AE0800AAF6CD /* SeedReminderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357BE23A1AE0800AAF6CD /* SeedReminderView.swift */; }; B85357C123A1B81900AAF6CD /* SeedReminderViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357C023A1B81900AAF6CD /* SeedReminderViewDelegate.swift */; }; B85357C323A1BD1200AAF6CD /* SeedVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357C223A1BD1200AAF6CD /* SeedVC.swift */; }; @@ -1448,7 +1447,6 @@ B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Identicon+ObjC.swift"; sourceTree = ""; }; B84664F4235022F30083A1CD /* MentionUtilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MentionUtilities.swift; sourceTree = ""; }; B847570023D568EB00759540 /* SignalServiceKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SignalServiceKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; - B847570223D5698100759540 /* LokiPushNotificationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LokiPushNotificationManager.swift; sourceTree = ""; }; B85357BE23A1AE0800AAF6CD /* SeedReminderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedReminderView.swift; sourceTree = ""; }; B85357C023A1B81900AAF6CD /* SeedReminderViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedReminderViewDelegate.swift; sourceTree = ""; }; B85357C223A1BD1200AAF6CD /* SeedVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedVC.swift; sourceTree = ""; }; @@ -2881,7 +2879,6 @@ isa = PBXGroup; children = ( B8544E3223D50E4900299F14 /* AppearanceUtilities.swift */, - B847570223D5698100759540 /* LokiPushNotificationManager.swift */, B84664F4235022F30083A1CD /* MentionUtilities.swift */, B886B4A82398BA1500211ABE /* QRCode.swift */, B8783E9D23EB948D00404FB8 /* UILabel+Interaction.swift */, @@ -4033,7 +4030,6 @@ 34D99CE4217509C2000AFB39 /* AppEnvironment.swift in Sources */, 348570A820F67575004FF32B /* OWSMessageHeaderView.m in Sources */, 450DF2091E0DD2C6003D14BE /* UserNotificationsAdaptee.swift in Sources */, - B847570323D5698100759540 /* LokiPushNotificationManager.swift in Sources */, 34B6A907218B5241007C4606 /* TypingIndicatorCell.swift in Sources */, 4CFD151D22415AA400F2450F /* CallVideoHintView.swift in Sources */, 34D1F0AB1F867BFC0066283D /* OWSContactOffersCell.m in Sources */, diff --git a/SignalServiceKit/src/Loki/API/LokiAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI.swift index d69c1abde..3f55bee45 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI.swift @@ -270,6 +270,9 @@ public final class LokiAPI : NSObject { private static func updateLastMessageHashValueIfPossible(for target: LokiAPITarget, from rawMessages: [JSON]) { if let lastMessage = rawMessages.last, let hashValue = lastMessage["hash"] as? String, let expirationDate = lastMessage["expiration"] as? Int { setLastMessageHashValue(for: target, hashValue: hashValue, expirationDate: UInt64(expirationDate)) + if UserDefaults.standard[.isUsingFullAPNs] { + LokiPushNotificationManager.acknowledgeDeliveryForMessage(with: hashValue, hexEncodedPublicKey: userHexEncodedPublicKey) + } } else if (!rawMessages.isEmpty) { print("[Loki] Failed to update last message hash value from: \(rawMessages).") } diff --git a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift b/SignalServiceKit/src/Loki/Utilities/LokiPushNotificationManager.swift similarity index 65% rename from Signal/src/Loki/Utilities/LokiPushNotificationManager.swift rename to SignalServiceKit/src/Loki/Utilities/LokiPushNotificationManager.swift index d37c71e7d..e339c5978 100644 --- a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift +++ b/SignalServiceKit/src/Loki/Utilities/LokiPushNotificationManager.swift @@ -1,14 +1,11 @@ - -// Ideally this should be in SignalServiceKit, but somehow linking fails when it is. - @objc(LKPushNotificationManager) final class LokiPushNotificationManager : NSObject { // MARK: Settings #if DEBUG - private static let url = URL(string: "https://dev.apns.getsession.org/register")! + private static let server = "https://dev.apns.getsession.org/" #else - private static let url = URL(string: "https://live.apns.getsession.org/register")! + private static let server = "https://live.apns.getsession.org/" #endif private static let tokenExpirationInterval: TimeInterval = 2 * 24 * 60 * 60 @@ -16,6 +13,8 @@ final class LokiPushNotificationManager : NSObject { private override init() { } // MARK: Registration + /** This method is for users to register for Silent Push Notification. + We only need the device token to make the SPN work.*/ @objc(registerWithToken:) static func register(with token: Data) { let hexEncodedToken = token.toHexString() @@ -31,6 +30,7 @@ final class LokiPushNotificationManager : NSObject { return print("[Loki] Using full APNs; no need to upload device token.") } let parameters = [ "token" : hexEncodedToken ] + let url = URL(string: server + "register")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] TSNetworkManager.shared().makeRequest(request, success: { _, response in @@ -48,12 +48,15 @@ final class LokiPushNotificationManager : NSObject { }) } + /** This method is for users to register for Normal Push Notification. + We need the device token and user's public key (session id) to make the NPN work.*/ @objc(registerWithToken:hexEncodedPublicKey:) static func register(with token: Data, hexEncodedPublicKey: String) { let hexEncodedToken = token.toHexString() let userDefaults = UserDefaults.standard let now = Date().timeIntervalSince1970 let parameters = [ "token" : hexEncodedToken, "pubKey" : hexEncodedPublicKey] + let url = URL(string: server + "register")! let request = TSRequest(url: url, method: "POST", parameters: parameters) request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] TSNetworkManager.shared().makeRequest(request, success: { _, response in @@ -70,4 +73,22 @@ final class LokiPushNotificationManager : NSObject { print("[Loki] Couldn't register device token.") }) } + + @objc(acknowledgeDeliveryForMessageWithHash:hexEncodedPublicKey:) + static func acknowledgeDeliveryForMessage(with hash: String, hexEncodedPublicKey: String) { + let parameters: [String : Any] = [ "lastHash" : hash, "pubKey" : hexEncodedPublicKey] + 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 the delivery for message with last hash: " + hash) + } + guard json["code"] as? Int != 0 else { + return print("[Loki] Couldn't acknowledge the delivery for message due to error: \(json["message"] as? String ?? "nil").") + } + }, failure: { _, error in + print("[Loki] Couldn't acknowledge the delivery for message with last hash: " + hash) + }) + } }