Fix multiple keychange errors (#29)

* Don't attempt to send a message unless we've successfully built a deveice-messages
* Only process message exception when we're done with retries.

// FREEBIE
pull/1/head
Michael Kirk 9 years ago committed by GitHub
parent 9821e0c0d0
commit a0df56a68e

@ -131,6 +131,19 @@ NS_ASSUME_NONNULL_BEGIN
}];
}
/**
* Useful for tests and debugging. In production use an enumeration method.
*/
- (NSArray<TSInteraction *> *)allInteractions
{
NSMutableArray<TSInteraction *> *interactions = [NSMutableArray new];
[self enumerateInteractionsUsingBlock:^(TSInteraction *_Nonnull interaction) {
[interactions addObject:interaction];
}];
return [interactions copy];
}
- (NSArray<TSInvalidIdentityKeyReceivingErrorMessage *> *)receivedMessagesForInvalidKey:(NSData *)key
{
NSMutableArray *errorMessages = [NSMutableArray new];

@ -103,7 +103,7 @@ static const NSUInteger OWSMessageSchemaVersion = 2;
NSString *attachmentId = self.attachmentIds[0];
return [NSString stringWithFormat:@"Media Message with attachmentId:%@", attachmentId];
} else {
return [NSString stringWithFormat:@"Message with body:%@", self.body];
return [NSString stringWithFormat:@"%@ with body:%@", [self class], self.body];
}
}

@ -247,7 +247,33 @@ dispatch_queue_t sendingQueue() {
if (remainingAttempts > 0) {
remainingAttempts -= 1;
NSArray<NSDictionary *> *deviceMessages = [self deviceMessages:message forRecipient:recipient inThread:thread];
NSArray<NSDictionary *> *deviceMessages;
@try {
deviceMessages = [self deviceMessages:message forRecipient:recipient inThread:thread];
} @catch (NSException *exception) {
deviceMessages = @[];
if (remainingAttempts == 0) {
DDLogWarn(@"%@ Terminal failure to build any device messages. Giving up with exception:%@",
self.tag,
exception);
[self processException:exception outgoingMessage:message inThread:thread];
return;
}
}
if (deviceMessages.count == 0) {
DDLogWarn(@"%@ Failed to build any device messages. Not sending.", self.tag);
// Retrying incase we fixed our stale devices last time 'round.
dispatch_async(sendingQueue(), ^{
[self sendMessage:message
toRecipient:recipient
inThread:thread
withAttemps:remainingAttempts
success:successBlock
failure:failureBlock];
});
return;
}
TSSubmitMessageRequest *request = [[TSSubmitMessageRequest alloc] initWithRecipient:recipient.uniqueId
messages:deviceMessages
@ -275,14 +301,14 @@ dispatch_queue_t sendingQueue() {
}
case 409: {
// Mismatched devices
DDLogWarn(@"Mismatch Devices.");
DDLogWarn(@"%@ Mismatch Devices.", self.tag);
NSError *e;
NSDictionary *serializedResponse =
[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&e];
if (e) {
DDLogError(@"Failed to serialize response of mismatched devices: %@", e.description);
DDLogError(@"%@ Failed to serialize response of mismatched devices: %@", self.tag, e);
} else {
[self handleMismatchedDevices:serializedResponse recipient:recipient];
}
@ -412,8 +438,7 @@ dispatch_queue_t sendingQueue() {
if ([exception.name isEqualToString:InvalidDeviceException]) {
[recipient removeDevices:[NSSet setWithObject:deviceNumber]];
} else {
DDLogWarn(@"Failed building message for device: %@ withe error %@", deviceNumber, exception);
[self processException:exception outgoingMessage:message inThread:thread];
@throw exception;
}
}
}
@ -566,4 +591,15 @@ dispatch_queue_t sendingQueue() {
}
});
}
+ (NSString *)tag
{
return [NSString stringWithFormat:@"[%@]", self.class];
}
- (NSString *)tag
{
return self.class.tag;
}
@end

@ -478,26 +478,26 @@
DDLogWarn(@"Got exception: %@", exception.description);
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
TSErrorMessage *errorMessage;
if ([exception.name isEqualToString:UntrustedIdentityKeyException]) {
errorMessage = [TSInvalidIdentityKeySendingErrorMessage
untrustedKeyWithOutgoingMessage:message
inThread:thread
forRecipient:exception.userInfo[TSInvalidRecipientKey]
preKeyBundle:exception.userInfo[TSInvalidPreKeyBundleKey]
withTransaction:transaction];
message.messageState = TSOutgoingMessageStateUnsent;
[message saveWithTransaction:transaction];
} else if (message.groupMetaMessage == TSGroupMessageNone) {
// Only update this with exception if it is not a group message as group
// messages may except for one group
// send but not another and the UI doesn't know how to handle that
[message setMessageState:TSOutgoingMessageStateUnsent];
[message saveWithTransaction:transaction];
}
TSErrorMessage *errorMessage;
if ([exception.name isEqualToString:UntrustedIdentityKeyException]) {
errorMessage = [TSInvalidIdentityKeySendingErrorMessage
untrustedKeyWithOutgoingMessage:message
inThread:thread
forRecipient:exception.userInfo[TSInvalidRecipientKey]
preKeyBundle:exception.userInfo[TSInvalidPreKeyBundleKey]
withTransaction:transaction];
message.messageState = TSOutgoingMessageStateUnsent;
[message saveWithTransaction:transaction];
} else if (message.groupMetaMessage == TSGroupMessageNone) {
// Only update this with exception if it is not a group message as group
// messages may except for one group
// send but not another and the UI doesn't know how to handle that
[message setMessageState:TSOutgoingMessageStateUnsent];
[message saveWithTransaction:transaction];
}
[errorMessage saveWithTransaction:transaction];
[errorMessage saveWithTransaction:transaction];
}];
}

Loading…
Cancel
Save