|
|
|
@ -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 {
|
|
|
|
@ -20,9 +21,8 @@ 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<Void> {
|
|
|
|
|
/// 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<Void> {
|
|
|
|
|
let hexEncodedToken = token.toHexString()
|
|
|
|
|
let userDefaults = UserDefaults.standard
|
|
|
|
|
let oldToken = userDefaults[.deviceToken]
|
|
|
|
@ -34,10 +34,10 @@ public final class LokiPushNotificationManager : NSObject {
|
|
|
|
|
return Promise<Void> { $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.")
|
|
|
|
|
}
|
|
|
|
@ -59,14 +59,13 @@ 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 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<Void> {
|
|
|
|
|
let hexEncodedToken = token.toHexString()
|
|
|
|
@ -79,10 +78,10 @@ public final class LokiPushNotificationManager : NSObject {
|
|
|
|
|
return Promise<Void> { $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
|
|
|
|
|
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.")
|
|
|
|
|
}
|
|
|
|
@ -104,51 +103,58 @@ 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 {
|
|
|
|
|
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")!
|
|
|
|
|
static func performOperation(_ operation: ClosedGroupOperation, for closedGroupPublicKey: String, publicKey: String) -> Promise<Void> {
|
|
|
|
|
let isUsingFullAPNs = UserDefaults.standard[.isUsingFullAPNs]
|
|
|
|
|
guard isUsingFullAPNs else { return Promise<Void> { $0.fulfill(()) } }
|
|
|
|
|
let parameters = [ "closedGroupPublicKey" : closedGroupPublicKey, "pubKey" : publicKey]
|
|
|
|
|
let url = URL(string: "\(server)/\(operation.rawValue)")!
|
|
|
|
|
let request = TSRequest(url: url, method: "POST", parameters: parameters)
|
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
|
|
|
|
|
TSNetworkManager.shared().makeRequest(request, success: { _, 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 acknowledge delivery for message with hash: \(hash).")
|
|
|
|
|
return print("[Loki] Couldn't subscribe/unsubscribe closed group: \(closedGroupPublicKey).")
|
|
|
|
|
}
|
|
|
|
|
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").")
|
|
|
|
|
return print("[Loki] Couldn't subscribe/unsubscribe for closed group: \(closedGroupPublicKey) 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).")
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
promise.catch2 { error in
|
|
|
|
|
print("[Loki] Couldn't subscribe/unsubscribe closed group: \(closedGroupPublicKey).")
|
|
|
|
|
}
|
|
|
|
|
return promise
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func performOperation(_ operation: ClosedGroupOperation, for closedGroupPublicKey: String, publicKey: String) -> Promise<Void> {
|
|
|
|
|
let isUsingFullAPNs = UserDefaults.standard[.isUsingFullAPNs]
|
|
|
|
|
guard isUsingFullAPNs else { return Promise<Void> { $0.fulfill(()) } }
|
|
|
|
|
let parameters = [ "closedGroupPublicKey" : closedGroupPublicKey, "pubKey" : publicKey]
|
|
|
|
|
let url = URL(string: server + operation.rawValue)!
|
|
|
|
|
static func notify(for signalMessage: SignalMessage) -> Promise<Void> {
|
|
|
|
|
let message = LokiMessage.from(signalMessage: signalMessage)!
|
|
|
|
|
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 = 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 notify PN server.")
|
|
|
|
|
}
|
|
|
|
|
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 notify PN server 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 notify PN server.")
|
|
|
|
|
}
|
|
|
|
|
return promise
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc(notifyForMessage:)
|
|
|
|
|
static func objc_notify(for signalMessage: SignalMessage) -> AnyPromise {
|
|
|
|
|
return AnyPromise.from(notify(for: signalMessage))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|