session-ios/SignalServiceKit/src/Loki/LokiAPI.swift

69 lines
2.8 KiB
Swift
Raw Normal View History

2019-04-30 06:27:39 +02:00
import PromiseKit
2019-05-08 01:29:07 +02:00
@objc public final class LokiAPI : NSObject {
2019-04-30 06:27:39 +02:00
2019-05-08 01:29:07 +02:00
private static let version = "v1"
public static let defaultMessageTTL: UInt64 = 4 * 24 * 60 * 60
2019-04-30 06:27:39 +02:00
// MARK: Types
private enum Method : String {
2019-05-07 08:03:57 +02:00
case getMessages = "retrieve"
2019-05-06 08:13:32 +02:00
case sendMessage = "store"
2019-05-07 08:03:57 +02:00
case getSwarm = "get_snodes_for_pubkey"
2019-04-30 06:27:39 +02:00
}
2019-05-08 01:29:07 +02:00
public struct Target : Hashable {
2019-05-07 07:03:54 +02:00
let address: String
let port: UInt16
}
2019-05-07 05:53:31 +02:00
public typealias RawResponse = Any
2019-04-30 06:27:39 +02:00
2019-05-07 02:10:15 +02:00
public enum Error : LocalizedError {
case proofOfWorkCalculationFailed
public var errorDescription: String? {
switch self {
case .proofOfWorkCalculationFailed: return NSLocalizedString("Failed to calculate proof of work.", comment: "")
}
}
}
2019-04-30 06:27:39 +02:00
// MARK: Lifecycle
2019-05-06 08:13:32 +02:00
override private init() { }
2019-04-30 06:27:39 +02:00
// MARK: API
2019-05-07 07:03:54 +02:00
private static func invoke(_ method: Method, on target: Target, with parameters: [String:String] = [:]) -> Promise<RawResponse> {
2019-05-08 01:29:07 +02:00
let url = URL(string: "\(target.address):\(target.port)/\(version)/storage_rpc")!
2019-04-30 06:27:39 +02:00
let request = TSRequest(url: url, method: "POST", parameters: [ "method" : method.rawValue, "params" : parameters ])
2019-05-07 05:53:31 +02:00
return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }
2019-04-30 06:27:39 +02:00
}
2019-05-07 02:29:44 +02:00
2019-05-07 08:03:57 +02:00
public static func getRandomSnode() -> Promise<Target> {
return Promise<Target> { seal in
seal.fulfill(Target(address: "http://13.238.53.205", port: 8080)) // TODO: Temporary
}
2019-04-30 06:27:39 +02:00
}
2019-05-07 08:03:57 +02:00
public static func getMessages() -> Promise<RawResponse> {
2019-04-30 07:12:15 +02:00
let parameters = [
"pubKey" : OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey,
2019-05-06 08:13:32 +02:00
"lastHash" : "" // TODO: Implement
2019-04-30 07:12:15 +02:00
]
2019-05-08 01:29:07 +02:00
return getRandomSnode().then { invoke(.getMessages, on: $0, with: parameters) } // TODO: Use getSwarm()
2019-05-07 08:03:57 +02:00
}
public static func sendMessage(_ lokiMessage: LokiMessage) -> Promise<RawResponse> {
2019-05-08 01:29:07 +02:00
return getRandomSnode().then { invoke(.sendMessage, on: $0, with: lokiMessage.toJSON()) } // TODO: Use getSwarm()
2019-05-07 08:03:57 +02:00
}
2019-05-08 01:29:07 +02:00
public static func getSwarm(for hexEncodedPublicKey: String) -> Promise<Set<Target>> {
return getRandomSnode().then { invoke(.getSwarm, on: $0, with: [ "pubKey" : hexEncodedPublicKey ]) }.map { rawResponse in return [] } // TODO: Parse targets from raw response
2019-04-30 06:27:39 +02:00
}
2019-05-07 02:29:44 +02:00
// MARK: Obj-C API
2019-05-07 06:29:18 +02:00
@objc public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, requiringPoW isPoWRequired: Bool, completionHandler: ((RawResponse?, NSError?) -> Void)? = nil) {
2019-05-07 08:03:57 +02:00
LokiMessage.fromSignalMessage(signalMessage, requiringPoW: isPoWRequired).then(sendMessage).done { completionHandler?($0, nil) }.catch { completionHandler?(nil, $0 as NSError) }
2019-05-07 02:29:44 +02:00
}
2019-04-30 06:27:39 +02:00
}