You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/SignalServiceKit/src/Loki/LokiAPI.swift

73 lines
3.1 KiB
Swift

import PromiseKit
@objc public final class LokiAPI : NSObject {
private static let version = "v1"
public static let defaultMessageTTL: UInt64 = 4 * 24 * 60 * 60
// MARK: Types
private enum Method : String {
case getMessages = "retrieve"
case sendMessage = "store"
case getSwarm = "get_snodes_for_pubkey"
}
public struct Target : Hashable {
6 years ago
let address: String
let port: UInt16
}
public typealias RawResponse = Any
public enum Error : LocalizedError {
case proofOfWorkCalculationFailed
public var errorDescription: String? {
switch self {
case .proofOfWorkCalculationFailed: return NSLocalizedString("Failed to calculate proof of work.", comment: "")
}
}
}
// MARK: Lifecycle
override private init() { }
// MARK: API
6 years ago
private static func invoke(_ method: Method, on target: Target, with parameters: [String:String] = [:]) -> Promise<RawResponse> {
let url = URL(string: "\(target.address):\(target.port)/\(version)/storage_rpc")!
let request = TSRequest(url: url, method: "POST", parameters: [ "method" : method.rawValue, "params" : parameters ])
return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }
}
6 years ago
public static func getRandomSnode() -> Promise<Target> {
return Promise<Target> { seal in
seal.fulfill(Target(address: "http://13.238.53.205", port: 8080)) // TODO: Temporary
}
}
public static func getMessages() -> Promise<RawResponse> {
let parameters = [
"pubKey" : OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey,
"lastHash" : "" // TODO: Implement
]
return getRandomSnode().then { invoke(.getMessages, on: $0, with: parameters) } // TODO: Use getSwarm()
}
public static func sendMessage(_ lokiMessage: LokiMessage) -> Promise<RawResponse> {
return getRandomSnode().then { invoke(.sendMessage, on: $0, with: lokiMessage.toJSON()) } // TODO: Use getSwarm()
}
public static func ping(_ hexEncodedPublicKey: String) -> Promise<RawResponse> {
return getRandomSnode().then { invoke(.sendMessage, on: $0, with: [ "destination" : hexEncodedPublicKey ]) } // TODO: Use getSwarm() and figure out correct parameters
}
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
}
6 years ago
// MARK: Obj-C API
6 years ago
@objc public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, requiringPoW isPoWRequired: Bool, completionHandler: ((RawResponse?, NSError?) -> Void)? = nil) {
LokiMessage.fromSignalMessage(signalMessage, requiringPoW: isPoWRequired).then(sendMessage).done { completionHandler?($0, nil) }.catch { completionHandler?(nil, $0 as NSError) }
6 years ago
}
}