UD send via REST.

pull/1/head
Matthew Chen 7 years ago
parent d08479980d
commit 24b0eed1f6

@ -706,6 +706,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
} }
- (nullable NSArray<NSDictionary *> *)deviceMessagesForMessageSendSafe:(OWSMessageSend *)messageSend - (nullable NSArray<NSDictionary *> *)deviceMessagesForMessageSendSafe:(OWSMessageSend *)messageSend
isUDSend:(BOOL)isUDSend
error:(NSError **)errorHandle error:(NSError **)errorHandle
{ {
OWSAssertDebug(messageSend); OWSAssertDebug(messageSend);
@ -716,7 +717,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
NSArray<NSDictionary *> *deviceMessages; NSArray<NSDictionary *> *deviceMessages;
@try { @try {
deviceMessages = [self deviceMessagesForMessageSendUnsafe:messageSend]; deviceMessages = [self deviceMessagesForMessageSendUnsafe:messageSend isUDSend:isUDSend];
} @catch (NSException *exception) { } @catch (NSException *exception) {
if ([exception.name isEqualToString:UntrustedIdentityKeyException]) { if ([exception.name isEqualToString:UntrustedIdentityKeyException]) {
// This *can* happen under normal usage, but it should happen relatively rarely. // This *can* happen under normal usage, but it should happen relatively rarely.
@ -848,12 +849,15 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[error setIsRetryable:YES]; [error setIsRetryable:YES];
return failureHandler(error); return failureHandler(error);
} }
// Consume an attempt. // Consume an attempt.
messageSend.remainingAttempts = messageSend.remainingAttempts - 1; messageSend.remainingAttempts = messageSend.remainingAttempts - 1;
BOOL isUDSend = messageSend.canUseUD && messageSend.udAccessKey != nil;
NSError *deviceMessagesError; NSError *deviceMessagesError;
NSArray<NSDictionary *> *_Nullable deviceMessages = NSArray<NSDictionary *> *_Nullable deviceMessages =
[self deviceMessagesForMessageSendSafe:messageSend error:&deviceMessagesError]; [self deviceMessagesForMessageSendSafe:messageSend isUDSend:isUDSend error:&deviceMessagesError];
if (deviceMessagesError || !deviceMessages) { if (deviceMessagesError || !deviceMessages) {
OWSAssertDebug(deviceMessagesError); OWSAssertDebug(deviceMessagesError);
return failureHandler(deviceMessagesError); return failureHandler(deviceMessagesError);
@ -930,7 +934,6 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
messages:deviceMessages messages:deviceMessages
timeStamp:message.timestamp]; timeStamp:message.timestamp];
BOOL isUDSend = messageSend.canUseUD && messageSend.udAccessKey != nil;
if (isUDSend) { if (isUDSend) {
DDLogVerbose(@"UD send."); DDLogVerbose(@"UD send.");
request.shouldHaveAuthorizationHeaders = YES; request.shouldHaveAuthorizationHeaders = YES;
@ -1242,12 +1245,11 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
} }
// NOTE: This method uses exceptions for control flow. // NOTE: This method uses exceptions for control flow.
- (NSArray<NSDictionary *> *)deviceMessagesForMessageSendUnsafe:(OWSMessageSend *)messageSend - (NSArray<NSDictionary *> *)deviceMessagesForMessageSendUnsafe:(OWSMessageSend *)messageSend isUDSend:(BOOL)isUDSend
{ {
OWSAssertDebug(messageSend.message); OWSAssertDebug(messageSend.message);
OWSAssertDebug(messageSend.recipient); OWSAssertDebug(messageSend.recipient);
TSOutgoingMessage *message = messageSend.message;
SignalRecipient *recipient = messageSend.recipient; SignalRecipient *recipient = messageSend.recipient;
NSMutableArray *messagesArray = [NSMutableArray arrayWithCapacity:recipient.devices.count]; NSMutableArray *messagesArray = [NSMutableArray arrayWithCapacity:recipient.devices.count];
@ -1266,11 +1268,10 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[self.dbConnection [self.dbConnection
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
@try { @try {
messageDict = [self encryptedMessageWithPlaintext:plainText messageDict = [self encryptedMessageForMessageSend:messageSend
recipient:messageSend.recipient
deviceId:deviceNumber deviceId:deviceNumber
keyingStorage:self.primaryStorage plainText:plainText
isSilent:message.isSilent isUDSend:isUDSend
transaction:transaction]; transaction:transaction];
} @catch (NSException *exception) { } @catch (NSException *exception) {
encryptionException = exception; encryptionException = exception;
@ -1301,23 +1302,24 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
return [messagesArray copy]; return [messagesArray copy];
} }
- (NSDictionary *)encryptedMessageWithPlaintext:(NSData *)plainText - (NSDictionary *)encryptedMessageForMessageSend:(OWSMessageSend *)messageSend
recipient:(SignalRecipient *)recipient
deviceId:(NSNumber *)deviceNumber deviceId:(NSNumber *)deviceNumber
keyingStorage:(OWSPrimaryStorage *)storage plainText:(NSData *)plainText
isSilent:(BOOL)isSilent isUDSend:(BOOL)isUDSend
transaction:(YapDatabaseReadWriteTransaction *)transaction transaction:(YapDatabaseReadWriteTransaction *)transaction
{ {
OWSAssertDebug(plainText); OWSAssertDebug(messageSend);
OWSAssertDebug(recipient);
OWSAssertDebug(deviceNumber); OWSAssertDebug(deviceNumber);
OWSAssertDebug(storage); OWSAssertDebug(plainText);
OWSAssertDebug(transaction); OWSAssertDebug(transaction);
NSString *identifier = recipient.recipientId; OWSPrimaryStorage *storage = self.primaryStorage;
OWSAssertDebug(identifier.length > 0); TSOutgoingMessage *message = messageSend.message;
SignalRecipient *recipient = messageSend.recipient;
NSString *recipientId = recipient.recipientId;
OWSAssertDebug(recipientId.length > 0);
if (![storage containsSession:identifier deviceId:[deviceNumber intValue] protocolContext:transaction]) { if (![storage containsSession:recipientId deviceId:[deviceNumber intValue] protocolContext:transaction]) {
__block dispatch_semaphore_t sema = dispatch_semaphore_create(0); __block dispatch_semaphore_t sema = dispatch_semaphore_create(0);
__block PreKeyBundle *_Nullable bundle; __block PreKeyBundle *_Nullable bundle;
__block NSException *_Nullable exception; __block NSException *_Nullable exception;
@ -1326,7 +1328,8 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
// are called _off_ the main thread. Otherwise we'll deadlock if the main // are called _off_ the main thread. Otherwise we'll deadlock if the main
// thread is blocked on opening a transaction. // thread is blocked on opening a transaction.
TSRequest *request = TSRequest *request =
[OWSRequestFactory recipientPrekeyRequestWithRecipient:identifier deviceId:[deviceNumber stringValue]]; [OWSRequestFactory recipientPrekeyRequestWithRecipient:recipientId deviceId:[deviceNumber stringValue]];
[self.networkManager makeRequest:request [self.networkManager makeRequest:request
completionQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) completionQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
success:^(NSURLSessionDataTask *task, id responseObject) { success:^(NSURLSessionDataTask *task, id responseObject) {
@ -1367,14 +1370,14 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
preKeyStore:storage preKeyStore:storage
signedPreKeyStore:storage signedPreKeyStore:storage
identityKeyStore:[OWSIdentityManager sharedManager] identityKeyStore:[OWSIdentityManager sharedManager]
recipientId:identifier recipientId:recipientId
deviceId:[deviceNumber intValue]]; deviceId:[deviceNumber intValue]];
@try { @try {
[builder processPrekeyBundle:bundle protocolContext:transaction]; [builder processPrekeyBundle:bundle protocolContext:transaction];
} @catch (NSException *exception) { } @catch (NSException *exception) {
if ([exception.name isEqualToString:UntrustedIdentityKeyException]) { if ([exception.name isEqualToString:UntrustedIdentityKeyException]) {
OWSRaiseExceptionWithUserInfo(UntrustedIdentityKeyException, OWSRaiseExceptionWithUserInfo(UntrustedIdentityKeyException,
(@{ TSInvalidPreKeyBundleKey : bundle, TSInvalidRecipientKey : identifier }), (@{ TSInvalidPreKeyBundleKey : bundle, TSInvalidRecipientKey : recipientId }),
@""); @"");
} }
@throw exception; @throw exception;
@ -1386,7 +1389,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
preKeyStore:storage preKeyStore:storage
signedPreKeyStore:storage signedPreKeyStore:storage
identityKeyStore:[OWSIdentityManager sharedManager] identityKeyStore:[OWSIdentityManager sharedManager]
recipientId:identifier recipientId:recipientId
deviceId:[deviceNumber intValue]]; deviceId:[deviceNumber intValue]];
id<CipherMessage> encryptedMessage = id<CipherMessage> encryptedMessage =
@ -1396,9 +1399,10 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
NSData *serializedMessage = encryptedMessage.serialized; NSData *serializedMessage = encryptedMessage.serialized;
TSWhisperMessageType messageType = [self messageTypeForCipherMessage:encryptedMessage]; TSWhisperMessageType messageType = [self messageTypeForCipherMessage:encryptedMessage];
BOOL isSilent = message.isSilent;
OWSMessageServiceParams *messageParams = OWSMessageServiceParams *messageParams =
[[OWSMessageServiceParams alloc] initWithType:messageType [[OWSMessageServiceParams alloc] initWithType:messageType
recipientId:identifier recipientId:recipientId
device:[deviceNumber intValue] device:[deviceNumber intValue]
content:serializedMessage content:serializedMessage
isSilent:isSilent isSilent:isSilent

Loading…
Cancel
Save