Hook up Loki messaging API

This commit is contained in:
Niels Andriesse 2019-05-06 16:13:32 +10:00
parent b9d39f8758
commit ee2361b5cc
4 changed files with 19 additions and 41 deletions

View File

@ -96,17 +96,6 @@ final class OnboardingKeyPairViewController : OnboardingBaseViewController {
setUpViewHierarchy()
handleModeChanged() // Perform initial update
updateKeyPair()
// Test
// ================
let _ = LokiMessagingAPI.retrieveAllMessages().done { result in
print(result.task.originalRequest!)
print(result.task.response!)
}
let _ = LokiMessagingAPI.sendTestMessage().done { result in
print(result.task.originalRequest!)
print(result.task.response!)
}
// ================
}
private func setUpViewHierarchy() {

View File

@ -1,6 +1,6 @@
import PromiseKit
public struct LokiMessagingAPI {
@objc public final class LokiMessagingAPI : NSObject {
private static var baseURL: String { return textSecureServerURL }
private static var port: String { return "8080" }
@ -9,39 +9,30 @@ public struct LokiMessagingAPI {
// MARK: Types
private enum Method : String {
case retrieveAllMessages = "retrieve"
case send = "store"
case sendMessage = "store"
}
public typealias Response = TSNetworkManager.NetworkManagerResult
public typealias RawResponse = TSNetworkManager.NetworkManagerResult
// MARK: Lifecycle
private init() { }
override private init() { }
// MARK: API
private static func invoke(_ method: Method, parameters: [String:String] = [:]) -> Promise<Response> {
private static func invoke(_ method: Method, parameters: [String:String] = [:]) -> (request: TSRequest, promise: Promise<RawResponse>) {
let url = URL(string: "\(baseURL):\(port)/\(apiVersion)/storage_rpc")!
let request = TSRequest(url: url, method: "POST", parameters: [ "method" : method.rawValue, "params" : parameters ])
return TSNetworkManager.shared().makePromise(request: request)
return (request, TSNetworkManager.shared().makePromise(request: request))
}
@objc public static func sendMessage(_ message: [String:String]) -> TSRequest {
return invoke(.sendMessage, parameters: message).request
}
public static func sendTestMessage() -> Promise<Response> {
let hour = 60 * 60 * 1000
let ttl = String(4 * 24 * hour)
let parameters = [
"pubKey" : "0371e72be8dd42ff77105e474a3ac26a503d017fb4562409c639eaf5965f5b31", // TODO: Receiver's public key
"ttl" : ttl,
"nonce" : "AAAAAAAA5rs=", // TODO: Proof of work
"timestamp" : "1556259498201", // TODO: Message send time
"data" : "CAESvgMKA1BVVBIPL2FwaS92MS9tZXNzYWdlGqIDCGUSQjA1MDM3MWU3MmJlOGRkNDJmZjc3MTA1ZTQ3NGEzYWMyNmE1MDNkMDE3ZmI0NTYyNDA5YzYzOWVhZjU5NjVmNWIzYzgBKK3QrcKlLULQAlxclJTbzKeQjJPfPlvo0VdoNw+O6kmpAUAKz2Mmz0YDHnhIsFgdWlBIoudqxVDu7swq5Z4cUqMfcQ5Z0b03/dVjkmFYo79Hzv7wkmRlPsfqAOVLBgV06sLVl+C5d8EmDtfH+k2iT62HnD8fub8tIxHn2l0MCefB4kO8tbA4dl/n/IXlvRAFS7OPJiq3jLyykyZkauAW7SVdDBAO6exJlNyOHTgSaHF924V3a/s3BK0useVMbzJSun9cx68Jm3WGERMFqrd75X70PN933zUSHedBAmMFW1Mvecko1G854tfNPZllP7OO/o+6XrQm8hMoe0Zo3POelrXwRdX88jp9VSEio/Yugq9MMcBuMsU5G0ePK5ZJMNfGLwExGSLY4br3sYpJz5yO7slpq2GgPuO6t9hWwIfzWynvNIfVtDxBkLVSV5XZU7720p/KP6kqZWCGHyCsAQ==" // TODO: Encrypted content
]
return invoke(.send, parameters: parameters)
}
public static func retrieveAllMessages() -> Promise<Response> {
public static func retrieveAllMessages() -> Promise<RawResponse> {
let parameters = [
"pubKey" : OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey,
"lastHash" : ""
"lastHash" : "" // TODO: Implement
]
return invoke(.retrieveAllMessages, parameters: parameters)
return invoke(.retrieveAllMessages, parameters: parameters).promise
}
}

View File

@ -929,7 +929,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
NSString *destination = message[@"destination"];
NSString *data = message[@"content"];
NSString *_Nullable nonce = [ProofOfWork calculateWithData:data pubKey:destination timestamp:timestamp.unsignedIntegerValue ttl:ttl.integerValue];
NSString *nonce = [ProofOfWork calculateWithData:data pubKey:destination timestamp:timestamp.unsignedIntegerValue ttl:ttl.integerValue];
// Return our timestamp along with the nonce
// These will help us identify which nonce belongs to which message
@ -943,7 +943,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[promises addObject:promise];
}
// Wait for all the PoW Calculations to finish
// Wait for all the PoW calculations to finish
return PMKWhen(promises);
}
@ -1126,7 +1126,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
return messageSend.failure(error);
}
// TODO: Update message here to show the pow cog icon
// TODO: Update message here to show the POW cog icon
// Loki: Calculate the proof of work for each device message
NSNumber *ttl = [NSNumber numberWithInteger:@(4 * 24 * 60 * 60)];

View File

@ -366,7 +366,7 @@ NS_ASSUME_NONNULL_BEGIN
// Params for our message server
lokiMessage[@"pubKey"] = message[@"destination"];
lokiMessage[@"data"] = message[@"content"];
lokiMessage[@"ttl"] = ttl;
lokiMessage[@"ttl"] = [ttl stringValue];
NSDictionary *_Nullable nonce = [self getNonceFromArray:nonceArray forMessage:message];
if (nonce) {
@ -380,7 +380,6 @@ NS_ASSUME_NONNULL_BEGIN
return modifiedMessages;
}
+ (NSDictionary *_Nullable)getNonceFromArray:(NSArray *)nonceArray forMessage:(NSDictionary *)message {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"destination == %@ AND deviceId == %d", message[@"destination"], message[@"destinationDeviceId"]];
NSArray *filtered = [nonceArray filteredArrayUsingPredicate:predicate];
@ -405,9 +404,8 @@ NS_ASSUME_NONNULL_BEGIN
// Loki: Just send the first message
NSString *path = [textSecureMessagesAPI stringByAppendingString:recipientId];
NSDictionary *parameters = [lokiMessages objectAtIndex:0];
TSRequest *request = [TSRequest requestWithUrl:[NSURL URLWithString:path] method:@"PUT" parameters:parameters];
return request;
return [LokiMessagingAPI sendMessage:parameters];
}
+ (TSRequest *)submitMessageRequestWithRecipient:(NSString *)recipientId