Simplify deviceMessages method

no need for a callback as it's a sync method.

// FREEBIE
pull/1/head
Michael Kirk 9 years ago
parent 9fe0ca000a
commit 5ae2717872

@ -236,94 +236,89 @@ dispatch_queue_t sendingQueue() {
if (remainingAttempts > 0) { if (remainingAttempts > 0) {
remainingAttempts -= 1; remainingAttempts -= 1;
[self outgoingMessages:message NSArray<NSDictionary *> *deviceMessages = [self deviceMessages:message forRecipient:recipient inThread:thread];
toRecipient:recipient
inThread:thread TSSubmitMessageRequest *request = [[TSSubmitMessageRequest alloc] initWithRecipient:recipient.uniqueId
completion:^(NSArray *messages) { messages:deviceMessages
TSSubmitMessageRequest *request = relay:recipient.relay
[[TSSubmitMessageRequest alloc] initWithRecipient:recipient.uniqueId timeStamp:message.timestamp];
messages:messages
relay:recipient.relay [[TSNetworkManager sharedManager] makeRequest:request
timeStamp:message.timestamp]; success:^(NSURLSessionDataTask *task, id responseObject) {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[[TSNetworkManager sharedManager] makeRequest:request [recipient saveWithTransaction:transaction];
success:^(NSURLSessionDataTask *task, id responseObject) { }];
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self handleMessageSent:message];
[recipient saveWithTransaction:transaction]; BLOCK_SAFE_RUN(successBlock);
}]; }
[self handleMessageSent:message]; failure:^(NSURLSessionDataTask *task, NSError *error) {
BLOCK_SAFE_RUN(successBlock); NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
} long statuscode = response.statusCode;
failure:^(NSURLSessionDataTask *task, NSError *error) { NSData *responseData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
long statuscode = response.statusCode; switch (statuscode) {
NSData *responseData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey]; case 404: {
[self unregisteredRecipient:recipient message:message inThread:thread];
switch (statuscode) { BLOCK_SAFE_RUN(failureBlock);
case 404: { break;
[self unregisteredRecipient:recipient message:message inThread:thread]; }
BLOCK_SAFE_RUN(failureBlock); case 409: {
break; // Mismatched devices
} DDLogWarn(@"Mismatch Devices.");
case 409: {
// Mismatched devices NSError *e;
DDLogWarn(@"Mismatch Devices."); NSDictionary *serializedResponse =
[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&e];
NSError *e;
NSDictionary *serializedResponse = if (e) {
[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&e]; DDLogError(@"Failed to serialize response of mismatched devices: %@", e.description);
} else {
if (e) { [self handleMismatchedDevices:serializedResponse recipient:recipient];
DDLogError(@"Failed to serialize response of mismatched devices: %@", }
e.description);
} else { dispatch_async(sendingQueue(), ^{
[self handleMismatchedDevices:serializedResponse recipient:recipient]; [self sendMessage:message
} toRecipient:recipient
inThread:thread
dispatch_async(sendingQueue(), ^{ withAttemps:remainingAttempts
[self sendMessage:message success:successBlock
toRecipient:recipient failure:failureBlock];
inThread:thread });
withAttemps:remainingAttempts
success:successBlock break;
failure:failureBlock]; }
}); case 410: {
// staledevices
break; DDLogWarn(@"Stale devices");
}
case 410: { if (!responseData) {
// staledevices DDLogWarn(@"Stale devices but server didn't specify devices in response.");
DDLogWarn(@"Stale devices"); return;
}
if (!responseData) {
DDLogWarn(@"Stale devices but server didn't specify devices in response."); [self handleStaleDevicesWithResponse:responseData recipientId:recipient.uniqueId];
return;
} dispatch_async(sendingQueue(), ^{
[self sendMessage:message
[self handleStaleDevicesWithResponse:responseData recipientId:recipient.uniqueId]; toRecipient:recipient
inThread:thread
dispatch_async(sendingQueue(), ^{ withAttemps:remainingAttempts
[self sendMessage:message success:successBlock
toRecipient:recipient failure:failureBlock];
inThread:thread });
withAttemps:remainingAttempts
success:successBlock break;
failure:failureBlock]; }
}); default:
[self sendMessage:message
break; toRecipient:recipient
} inThread:thread
default: withAttemps:remainingAttempts
[self sendMessage:message success:successBlock
toRecipient:recipient failure:failureBlock];
inThread:thread break;
withAttemps:remainingAttempts }
success:successBlock }];
failure:failureBlock];
break;
}
}];
}];
} else { } else {
[self saveMessage:message withState:TSOutgoingMessageStateUnsent]; [self saveMessage:message withState:TSOutgoingMessageStateUnsent];
BLOCK_SAFE_RUN(failureBlock); BLOCK_SAFE_RUN(failureBlock);
@ -356,10 +351,10 @@ dispatch_queue_t sendingQueue() {
[self saveMessage:message withState:TSOutgoingMessageStateSent]; [self saveMessage:message withState:TSOutgoingMessageStateSent];
} }
- (void)outgoingMessages:(TSOutgoingMessage *)message - (NSArray<NSDictionary *> *)deviceMessages:(TSOutgoingMessage *)message
toRecipient:(SignalRecipient *)recipient forRecipient:(SignalRecipient *)recipient
inThread:(TSThread *)thread inThread:(TSThread *)thread
completion:(messagesQueue)sendMessages { {
NSMutableArray *messagesArray = [NSMutableArray arrayWithCapacity:recipient.devices.count]; NSMutableArray *messagesArray = [NSMutableArray arrayWithCapacity:recipient.devices.count];
TSStorageManager *storage = [TSStorageManager sharedManager]; TSStorageManager *storage = [TSStorageManager sharedManager];
NSData *plainText = [self plainTextForMessage:message inThread:thread]; NSData *plainText = [self plainTextForMessage:message inThread:thread];
@ -381,13 +376,13 @@ dispatch_queue_t sendingQueue() {
if ([exception.name isEqualToString:InvalidDeviceException]) { if ([exception.name isEqualToString:InvalidDeviceException]) {
[recipient removeDevices:[NSSet setWithObject:deviceNumber]]; [recipient removeDevices:[NSSet setWithObject:deviceNumber]];
} else { } else {
DDLogWarn(@"Failed building message for device: %@ withe error %@", deviceNumber, exception);
[self processException:exception outgoingMessage:message inThread:thread]; [self processException:exception outgoingMessage:message inThread:thread];
return;
} }
} }
} }
sendMessages(messagesArray); return [messagesArray copy];
} }
- (NSDictionary *)encryptedMessageWithPlaintext:(NSData *)plainText - (NSDictionary *)encryptedMessageWithPlaintext:(NSData *)plainText

Loading…
Cancel
Save