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];
// 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 // Loki: Original code
NSNumber *ttl = [NSNumber numberWithInteger:@(4 * 24 * 60 * 60)]; /*
AnyPromise *powPromise = [self calculateProofOfWorkForDeviceMessages:deviceMessages ttl:ttl]; OWSRequestMaker *requestMaker = [[OWSRequestMaker alloc] initWithLabel:@"Message Send"
[powPromise requestFactoryBlock:^(SMKUDAccessKey *_Nullable udAccessKey) {
.thenOn([OWSDispatch sendingQueue], ^(NSArray *nonceArray) { return [OWSRequestFactory submitMessageRequestWithRecipient:recipient.recipientId
OWSRequestMaker *requestMaker = [[OWSRequestMaker alloc] initWithLabel:@"Message Send" messages:deviceMessages
requestFactoryBlock:^(SMKUDAccessKey *_Nullable udAccessKey) { timeStamp:message.timestamp
// Loki udAccessKey:udAccessKey];
// ======== }
return [OWSRequestFactory submitLokiMessageRequestWithRecipient:recipient.recipientId udAuthFailureBlock:^{
messages:deviceMessages // Note the UD auth failure so subsequent retries
nonceArray:nonceArray // to this recipient also use basic auth.
ttl:ttl]; [messageSend setHasUDAuthFailed];
// ======== }
/* Original code: websocketFailureBlock:^{
return [OWSRequestFactory submitMessageRequestWithRecipient:recipient.recipientId // Note the websocket failure so subsequent retries
messages:deviceMessages // to this recipient also use REST.
timeStamp:message.timestamp messageSend.hasWebsocketSendFailed = YES;
udAccessKey:udAccessKey]; }
*/ recipientId:recipient.recipientId
} udAccess:messageSend.udAccess
udAuthFailureBlock:^{ canFailoverUDAuth:NO];
// Note the UD auth failure so subsequent retries [[requestMaker makeRequestObjc]
// to this recipient also use basic auth. .then(^(OWSRequestMakerResult *result) {
[messageSend setHasUDAuthFailed]; dispatch_async([OWSDispatch sendingQueue], ^{
} [self messageSendDidSucceed:messageSend
websocketFailureBlock:^{ deviceMessages:deviceMessages
// Note the websocket failure so subsequent retries wasSentByUD:result.wasSentByUD
// to this recipient also use REST. wasSentByWebsocket:result.wasSentByWebsocket];
messageSend.hasWebsocketSendFailed = YES; });
} })
recipientId:recipient.recipientId .catch(^(NSError *error) {
udAccess:messageSend.udAccess dispatch_async([OWSDispatch sendingQueue], ^{
canFailoverUDAuth:NO]; NSUInteger statusCode = 0;
return requestMaker; NSData *_Nullable responseData = nil;
}) if ([error.domain isEqualToString:@"SignalServiceKit.RequestMakerUDAuthError"]) {
.thenOn([OWSDispatch sendingQueue], ^(OWSRequestMaker *requestMaker) { // Try again.
return [requestMaker makeRequestObjc]; OWSLogInfo(@"UD request auth failed; failing over to non-UD request.");
}).then(^(OWSRequestMakerResult *result) { [error setIsRetryable:YES];
dispatch_async([OWSDispatch sendingQueue], ^{ } else if ([error.domain isEqualToString:TSNetworkManagerErrorDomain]) {
[self messageSendDidSucceed:messageSend statusCode = error.code;
deviceMessages:deviceMessages
wasSentByUD:result.wasSentByUD NSError *_Nullable underlyingError = error.userInfo[NSUnderlyingErrorKey];
wasSentByWebsocket:result.wasSentByWebsocket]; if (underlyingError) {
}); responseData
}) = underlyingError.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
.catch(^(NSError *error) { } else {
dispatch_async([OWSDispatch sendingQueue], ^{ OWSFailDebug(@"Missing underlying error: %@", error);
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 { } else {
OWSFailDebug(@"Missing underlying error: %@", error); OWSFailDebug(@"Unexpected error: %@", error);
} }
} else {
OWSFailDebug(@"Unexpected error: %@", error); [self messageSendDidFail:messageSend
} deviceMessages:deviceMessages
statusCode:statusCode
[self messageSendDidFail:messageSend error:error
deviceMessages:deviceMessages responseData:responseData];
statusCode:statusCode });
error:error }) retainUntilComplete];
responseData:responseData]; */
});
}) retainUntilComplete];
} }
- (void)calculatingProofOfWorkForMessage:(OWSMessageSend *)messageSend - (void)setIsCalculatingProofOfWorkForMessage:(OWSMessageSend *)messageSend
{ {
OWSAssertDebug(messageSend); OWSAssertDebug(messageSend);
dispatch_async([OWSDispatch sendingQueue], ^{ dispatch_async([OWSDispatch sendingQueue], ^{
@ -1507,7 +1498,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
/* Original code: /* Original code:
NSMutableArray<NSNumber *> *deviceIds = [recipient.devices mutableCopy]; NSMutableArray<NSNumber *> *deviceIds = [recipient.devices mutableCopy];
*/ */
OWSAssertDebug(deviceIds); OWSAssertDebug(deviceIds);
if (messageSend.isLocalNumber) { if (messageSend.isLocalNumber) {

Loading…
Cancel
Save