Support for Mismatched Devices.

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

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

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

@ -136,10 +136,28 @@ dispatch_queue_t sendingQueue() {
}]; }];
break; break;
} }
case 409: case 409:{
// Mismatched devices // 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; break;
}
case 410:{ case 410:{
// staledevices // staledevices
DDLogWarn(@"Stale devices"); 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 { - (void)handleMessageSent:(TSOutgoingMessage*)message {
[self saveMessage:message withState:TSOutgoingMessageStateSent]; [self saveMessage:message withState:TSOutgoingMessageStateSent];
} }

Loading…
Cancel
Save