Hook up new messaging API

This commit is contained in:
Niels Andriesse 2019-05-07 11:56:00 +10:00
parent 1c4c47ab33
commit 72cc345734
5 changed files with 72 additions and 83 deletions

View File

@ -604,8 +604,7 @@ class MessageDetailViewController: OWSViewController, MediaGalleryDataSourceDele
private func string(for messageReceiptStatus: MessageReceiptStatus) -> String {
switch messageReceiptStatus {
case .calculatingPoW:
return NSLocalizedString("Calculating proof of work",
comment: "Status label for messages which are calculating proof of work.")
return NSLocalizedString("Calculating proof of work", comment: "")
case .uploading:
return NSLocalizedString("MESSAGE_METADATA_VIEW_MESSAGE_STATUS_UPLOADING",
comment: "Status label for messages which are uploading.")

View File

@ -111,8 +111,7 @@ public class MessageRecipientStatusUtils: NSObject {
return (.failed, NSLocalizedString("MESSAGE_STATUS_FAILED", comment: "status message for failed messages"))
case .sending:
if outgoingMessage.isCalculatingPoW {
return (.calculatingPoW, NSLocalizedString("Calculating proof of work",
comment: "message status while calculating proof of work."))
return (.calculatingPoW, NSLocalizedString("Calculating proof of work", comment: ""))
}
if outgoingMessage.hasAttachments() {

View File

@ -52,7 +52,7 @@ import PromiseKit
}
// MARK: Obj-C API
@objc public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, requiringPOW isPOWRequired: Bool, completionHandler: @escaping (Any?, NSError?) -> Void) {
sendSignalMessage(signalMessage, to: destination, requiringPOW: isPOWRequired).done { completionHandler($0.responseObject, nil) }.catch { completionHandler(nil, $0 as NSError) }
@objc public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, requiringPOW isPOWRequired: Bool, completionHandler: ((Any?, NSError?) -> Void)? = nil) {
sendSignalMessage(signalMessage, to: destination, requiringPOW: isPOWRequired).done { completionHandler?($0.responseObject, nil) }.catch { completionHandler?(nil, $0 as NSError) }
}
}

View File

@ -43,7 +43,7 @@ private extension MutableCollection where Element == UInt8, Index == Int {
* This was copied from the desktop messenger.
* Ref: libloki/proof-of-work.js
*/
@objc public class ProofOfWork : NSObject {
@objc public final class ProofOfWork : NSObject {
// If this changes then we also have to use something other than UInt64 to support the new length
private static let nonceLength = 8

View File

@ -1095,86 +1095,77 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
}
// Update the state to show that proof of work is being calculated
[self calculatingProofOfWorkForMessage:messageSend];
[self setIsCalculatingProofOfWorkForMessage:messageSend];
// Convert the message to a Loki message and send it using the Loki messaging API
NSDictionary *signalMessage = deviceMessages.firstObject;
BOOL isPOWRequired = YES; // TODO: Base on message type
[LokiMessagingAPI sendSignalMessage:signalMessage to:recipient.recipientId requiringPOW:isPOWRequired completionHandler:nil];
// Loki: Calculate the proof of work for each device message
NSNumber *ttl = [NSNumber numberWithInteger:@(4 * 24 * 60 * 60)];
AnyPromise *powPromise = [self calculateProofOfWorkForDeviceMessages:deviceMessages ttl:ttl];
[powPromise
.thenOn([OWSDispatch sendingQueue], ^(NSArray *nonceArray) {
OWSRequestMaker *requestMaker = [[OWSRequestMaker alloc] initWithLabel:@"Message Send"
requestFactoryBlock:^(SMKUDAccessKey *_Nullable udAccessKey) {
// Loki
// ========
return [OWSRequestFactory submitLokiMessageRequestWithRecipient:recipient.recipientId
messages:deviceMessages
nonceArray:nonceArray
ttl:ttl];
// ========
/* Original code:
return [OWSRequestFactory submitMessageRequestWithRecipient:recipient.recipientId
messages:deviceMessages
timeStamp:message.timestamp
udAccessKey:udAccessKey];
*/
}
udAuthFailureBlock:^{
// Note the UD auth failure so subsequent retries
// to this recipient also use basic auth.
[messageSend setHasUDAuthFailed];
}
websocketFailureBlock:^{
// Note the websocket failure so subsequent retries
// to this recipient also use REST.
messageSend.hasWebsocketSendFailed = YES;
}
recipientId:recipient.recipientId
udAccess:messageSend.udAccess
canFailoverUDAuth:NO];
return requestMaker;
})
.thenOn([OWSDispatch sendingQueue], ^(OWSRequestMaker *requestMaker) {
return [requestMaker makeRequestObjc];
}).then(^(OWSRequestMakerResult *result) {
dispatch_async([OWSDispatch sendingQueue], ^{
[self messageSendDidSucceed:messageSend
deviceMessages:deviceMessages
wasSentByUD:result.wasSentByUD
wasSentByWebsocket:result.wasSentByWebsocket];
});
})
.catch(^(NSError *error) {
dispatch_async([OWSDispatch sendingQueue], ^{
NSUInteger statusCode = 0;
NSData *_Nullable responseData = nil;
if ([error.domain isEqualToString:@"SignalServiceKit.RequestMakerUDAuthError"]) {
// Try again.
OWSLogInfo(@"UD request auth failed; failing over to non-UD request.");
[error setIsRetryable:YES];
} else if ([error.domain isEqualToString:TSNetworkManagerErrorDomain]) {
statusCode = error.code;
NSError *_Nullable underlyingError = error.userInfo[NSUnderlyingErrorKey];
if (underlyingError) {
responseData
= underlyingError.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
// Loki: Original code
/*
OWSRequestMaker *requestMaker = [[OWSRequestMaker alloc] initWithLabel:@"Message Send"
requestFactoryBlock:^(SMKUDAccessKey *_Nullable udAccessKey) {
return [OWSRequestFactory submitMessageRequestWithRecipient:recipient.recipientId
messages:deviceMessages
timeStamp:message.timestamp
udAccessKey:udAccessKey];
}
udAuthFailureBlock:^{
// Note the UD auth failure so subsequent retries
// to this recipient also use basic auth.
[messageSend setHasUDAuthFailed];
}
websocketFailureBlock:^{
// Note the websocket failure so subsequent retries
// to this recipient also use REST.
messageSend.hasWebsocketSendFailed = YES;
}
recipientId:recipient.recipientId
udAccess:messageSend.udAccess
canFailoverUDAuth:NO];
[[requestMaker makeRequestObjc]
.then(^(OWSRequestMakerResult *result) {
dispatch_async([OWSDispatch sendingQueue], ^{
[self messageSendDidSucceed:messageSend
deviceMessages:deviceMessages
wasSentByUD:result.wasSentByUD
wasSentByWebsocket:result.wasSentByWebsocket];
});
})
.catch(^(NSError *error) {
dispatch_async([OWSDispatch sendingQueue], ^{
NSUInteger statusCode = 0;
NSData *_Nullable responseData = nil;
if ([error.domain isEqualToString:@"SignalServiceKit.RequestMakerUDAuthError"]) {
// Try again.
OWSLogInfo(@"UD request auth failed; failing over to non-UD request.");
[error setIsRetryable:YES];
} else if ([error.domain isEqualToString:TSNetworkManagerErrorDomain]) {
statusCode = error.code;
NSError *_Nullable underlyingError = error.userInfo[NSUnderlyingErrorKey];
if (underlyingError) {
responseData
= underlyingError.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
} else {
OWSFailDebug(@"Missing underlying error: %@", error);
}
} else {
OWSFailDebug(@"Missing underlying error: %@", error);
OWSFailDebug(@"Unexpected error: %@", error);
}
} else {
OWSFailDebug(@"Unexpected error: %@", error);
}
[self messageSendDidFail:messageSend
deviceMessages:deviceMessages
statusCode:statusCode
error:error
responseData:responseData];
});
}) retainUntilComplete];
[self messageSendDidFail:messageSend
deviceMessages:deviceMessages
statusCode:statusCode
error:error
responseData:responseData];
});
}) retainUntilComplete];
*/
}
- (void)calculatingProofOfWorkForMessage:(OWSMessageSend *)messageSend
- (void)setIsCalculatingProofOfWorkForMessage:(OWSMessageSend *)messageSend
{
OWSAssertDebug(messageSend);
dispatch_async([OWSDispatch sendingQueue], ^{
@ -1507,7 +1498,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
/* Original code:
NSMutableArray<NSNumber *> *deviceIds = [recipient.devices mutableCopy];
*/
OWSAssertDebug(deviceIds);
if (messageSend.isLocalNumber) {