Hook up new messaging API

pull/8/head
Niels Andriesse 6 years ago
parent 1c4c47ab33
commit 72cc345734

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

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

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

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

@ -1095,86 +1095,77 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
} }
// Update the state to show that proof of work is being calculated // Update the state to show that proof of work is being calculated
[self calculatingProofOfWorkForMessage:messageSend]; [self setIsCalculatingProofOfWorkForMessage:messageSend];
// 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]; // Convert the message to a Loki message and send it using the Loki messaging API
if (underlyingError) { NSDictionary *signalMessage = deviceMessages.firstObject;
responseData BOOL isPOWRequired = YES; // TODO: Base on message type
= underlyingError.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey]; [LokiMessagingAPI sendSignalMessage:signalMessage to:recipient.recipientId requiringPOW:isPOWRequired completionHandler:nil];
// 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 { } else {
OWSFailDebug(@"Missing underlying error: %@", error); OWSFailDebug(@"Unexpected error: %@", error);
} }
} else {
OWSFailDebug(@"Unexpected error: %@", error);
}
[self messageSendDidFail:messageSend [self messageSendDidFail:messageSend
deviceMessages:deviceMessages deviceMessages:deviceMessages
statusCode:statusCode statusCode:statusCode
error:error error:error
responseData:responseData]; responseData:responseData];
}); });
}) retainUntilComplete]; }) retainUntilComplete];
*/
} }
- (void)calculatingProofOfWorkForMessage:(OWSMessageSend *)messageSend - (void)setIsCalculatingProofOfWorkForMessage:(OWSMessageSend *)messageSend
{ {
OWSAssertDebug(messageSend); OWSAssertDebug(messageSend);
dispatch_async([OWSDispatch sendingQueue], ^{ dispatch_async([OWSDispatch sendingQueue], ^{

Loading…
Cancel
Save