Support for Mismatched Devices.

pull/1/head
Frederic Jacobs 9 years ago
parent cd0fb8bc51
commit 912b617a11

@ -14,12 +14,11 @@
+ (instancetype)recipientWithTextSecureIdentifier:(NSString*)textSecureIdentifier withTransaction:(YapDatabaseReadTransaction*)transaction;
- (NSSet*)devices; //NSNumbers
- (void)addDevices:(NSSet *)set;
- (void)removeDevices:(NSSet *)set;
@property (nonatomic, readonly) NSString *relay;
@property (nonatomic, retain) NSMutableOrderedSet *devices;
@end

@ -9,12 +9,6 @@
#import "TSStorageManager+IdentityKeyStore.h"
#import "TSRecipient.h"
@interface TSRecipient ()
@property (nonatomic, retain) NSMutableSet *devices;
@end
@implementation TSRecipient
+ (NSString*)collection{
@ -25,7 +19,7 @@
self = [super initWithUniqueId:textSecureIdentifier];
if (self) {
_devices = [NSMutableSet setWithObject:[NSNumber numberWithInt:1]];
_devices = [NSMutableOrderedSet orderedSetWithObject:[NSNumber numberWithInt:1]];
_relay = relay;
}
@ -36,16 +30,24 @@
return [self fetchObjectWithUniqueID:textSecureIdentifier transaction:transaction];
}
- (NSSet*)devices{
- (NSMutableOrderedSet*)devices{
return [_devices copy];
}
- (void)addDevices:(NSSet *)set{
[self checkDevices];
[_devices unionSet:set];
}
- (void)removeDevices:(NSSet *)set{
[self checkDevices];
[_devices minusSet:set];
}
- (void)checkDevices {
if (_devices == nil || ![_devices isKindOfClass:[NSMutableOrderedSet class]]) {
_devices = [NSMutableOrderedSet orderedSetWithObject:[NSNumber numberWithInt:1]];
}
}
@end

@ -136,10 +136,28 @@ dispatch_queue_t sendingQueue() {
}];
break;
}
case 409:
case 409:{
// Mismatched devices
DDLogError(@"Missing some devices");
DDLogWarn(@"Mismatch Devices.");
NSError *e;
NSDictionary *serializedResponse = [NSJSONSerialization JSONObjectWithData:responseData
options:0
error:&e];
if (e) {
DDLogError(@"Failed to serialize response of mismatched devices: %@", e.description);
} else {
[self handleMismatchedDevices:serializedResponse
recipient:recipient];
}
dispatch_async(sendingQueue(), ^{
[self sendMessage:message toRecipient:recipient inThread:thread withAttemps:remainingAttempts];
});
break;
}
case 410:{
// staledevices
DDLogWarn(@"Stale devices");
@ -168,6 +186,27 @@ dispatch_queue_t sendingQueue() {
}
}
- (void)handleMismatchedDevices:(NSDictionary*)dictionary recipient:(TSRecipient*)recipient {
NSArray *extraDevices = [dictionary objectForKey:@"extraDevices"];
NSArray *missingDevices = [dictionary objectForKey:@"missingDevices"];
if (extraDevices && [extraDevices count] > 0) {
for (NSNumber *extraDeviceId in extraDevices) {
[[TSStorageManager sharedManager] deleteSessionForContact:recipient.uniqueId deviceId:[extraDeviceId intValue]];
}
[recipient removeDevices:[NSSet setWithArray:extraDevices]];
}
if (missingDevices && [missingDevices count] > 0) {
[recipient addDevices:[NSSet setWithArray:missingDevices]];
}
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[recipient saveWithTransaction:transaction];
}];
}
- (void)handleMessageSent:(TSOutgoingMessage*)message {
[self saveMessage:message withState:TSOutgoingMessageStateSent];
}

Loading…
Cancel
Save