Merge pull request #276 from loki-project/push-notifications

PN Migration
This commit is contained in:
Niels Andriesse 2020-09-21 11:33:23 +10:00 committed by GitHub
commit 7f3ea99521
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 8 deletions

View File

@ -243,7 +243,10 @@ 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 [] }
updateLastMessageHashValueIfPossible(for: snode, associatedWith: publicKey, from: rawMessages)
if let (lastHash, expirationDate) = updateLastMessageHashValueIfPossible(for: snode, associatedWith: publicKey, from: rawMessages),
UserDefaults.standard[.isUsingFullAPNs] {
LokiPushNotificationManager.acknowledgeDelivery(forMessageWithHash: lastHash, expiration: expirationDate, publicKey: getUserHexEncodedPublicKey())
}
let rawNewMessages = removeDuplicates(from: rawMessages, associatedWith: publicKey)
let newMessages = parseProtoEnvelopes(from: rawNewMessages)
return newMessages

View File

@ -28,14 +28,13 @@ 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: Promise<Void> = OnionRequestAPI.sendOnionRequest(request, to: server, using: pnServerPublicKey).map2 { response in
guard let json = response as? JSON else {
return print("[Loki] Couldn't unregister from push notifications.")
}
guard json["code"] as? Int != 0 else {
return print("[Loki] Couldn't unregister from push notifications due to error: \(json["message"] as? String ?? "nil").")
}
return
}
promise.catch2 { error in
print("[Loki] Couldn't unregister from push notifications.")
@ -59,7 +58,7 @@ public final class LokiPushNotificationManager : NSObject {
let hexEncodedToken = token.toHexString()
let userDefaults = UserDefaults.standard
let oldToken = userDefaults[.deviceToken]
let lastUploadTime = userDefaults[.lastDeviceTokenUpload2]
let lastUploadTime = userDefaults[.lastDeviceTokenUpload]
let now = Date().timeIntervalSince1970
guard isForcedUpdate || hexEncodedToken != oldToken || now - lastUploadTime > tokenExpirationInterval else {
print("[Loki] Device token hasn't changed or expired; no need to re-upload.")
@ -69,7 +68,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: Promise<Void> = 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.")
}
@ -77,9 +76,8 @@ public final class LokiPushNotificationManager : NSObject {
return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").")
}
userDefaults[.deviceToken] = hexEncodedToken
userDefaults[.lastDeviceTokenUpload2] = now
userDefaults[.lastDeviceTokenUpload] = now
userDefaults[.isUsingFullAPNs] = true
return
}
promise.catch2 { error in
print("[Loki] Couldn't register device token.")
@ -145,4 +143,21 @@ public final class LokiPushNotificationManager : NSObject {
static func objc_notify(for signalMessage: SignalMessage) -> AnyPromise {
return AnyPromise.from(notify(for: signalMessage))
}
static func acknowledgeDelivery(forMessageWithHash hash: String, expiration: UInt64, publicKey: String) {
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).")
})
}
}

View File

@ -21,7 +21,6 @@ public enum LKUserDefaults {
public enum Double : Swift.String {
/// - Note: Deprecated
case lastDeviceTokenUpload = "lastDeviceTokenUploadTime"
case lastDeviceTokenUpload2 = "lastDeviceTokenUpload2"
}
public enum Int: Swift.String {