Accommodate for PN migration period

pull/276/head
nielsandriesse 5 years ago
parent 39740ee6ff
commit b1a6e7f455

@ -243,7 +243,10 @@ public final class SnodeAPI : NSObject {
internal static func parseRawMessagesResponse(_ rawResponse: Any, from snode: Snode, associatedWith publicKey: String) -> [SSKProtoEnvelope] { 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 [] } 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 rawNewMessages = removeDuplicates(from: rawMessages, associatedWith: publicKey)
let newMessages = parseProtoEnvelopes(from: rawNewMessages) let newMessages = parseProtoEnvelopes(from: rawNewMessages)
return newMessages return newMessages

@ -28,14 +28,13 @@ public final class LokiPushNotificationManager : NSObject {
let url = URL(string: "\(server)/unregister")! let url = URL(string: "\(server)/unregister")!
let request = TSRequest(url: url, method: "POST", parameters: parameters) let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] 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 { guard let json = response as? JSON else {
return print("[Loki] Couldn't unregister from push notifications.") return print("[Loki] Couldn't unregister from push notifications.")
} }
guard json["code"] as? Int != 0 else { 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 print("[Loki] Couldn't unregister from push notifications due to error: \(json["message"] as? String ?? "nil").")
} }
return
} }
promise.catch2 { error in promise.catch2 { error in
print("[Loki] Couldn't unregister from push notifications.") print("[Loki] Couldn't unregister from push notifications.")
@ -59,7 +58,7 @@ public final class LokiPushNotificationManager : NSObject {
let hexEncodedToken = token.toHexString() let hexEncodedToken = token.toHexString()
let userDefaults = UserDefaults.standard let userDefaults = UserDefaults.standard
let oldToken = userDefaults[.deviceToken] let oldToken = userDefaults[.deviceToken]
let lastUploadTime = userDefaults[.lastDeviceTokenUpload2] let lastUploadTime = userDefaults[.lastDeviceTokenUpload]
let now = Date().timeIntervalSince1970 let now = Date().timeIntervalSince1970
guard isForcedUpdate || hexEncodedToken != oldToken || now - lastUploadTime > tokenExpirationInterval else { guard isForcedUpdate || hexEncodedToken != oldToken || now - lastUploadTime > tokenExpirationInterval else {
print("[Loki] Device token hasn't changed or expired; no need to re-upload.") 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 url = URL(string: "\(server)/register")!
let request = TSRequest(url: url, method: "POST", parameters: parameters) let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] 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 { guard let json = response as? JSON else {
return print("[Loki] Couldn't register device token.") 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").") return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").")
} }
userDefaults[.deviceToken] = hexEncodedToken userDefaults[.deviceToken] = hexEncodedToken
userDefaults[.lastDeviceTokenUpload2] = now userDefaults[.lastDeviceTokenUpload] = now
userDefaults[.isUsingFullAPNs] = true userDefaults[.isUsingFullAPNs] = true
return
} }
promise.catch2 { error in promise.catch2 { error in
print("[Loki] Couldn't register device token.") print("[Loki] Couldn't register device token.")
@ -145,4 +143,21 @@ public final class LokiPushNotificationManager : NSObject {
static func objc_notify(for signalMessage: SignalMessage) -> AnyPromise { static func objc_notify(for signalMessage: SignalMessage) -> AnyPromise {
return AnyPromise.from(notify(for: signalMessage)) 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).")
})
}
} }

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

Loading…
Cancel
Save