Streamline SignalRecipient.

pull/1/head
Matthew Chen 7 years ago
parent 10b21d10e4
commit ef3933bfaa

@ -208,8 +208,8 @@ NS_ASSUME_NONNULL_BEGIN
__block NSMutableArray *result = [NSMutableArray array]; __block NSMutableArray *result = [NSMutableArray array];
for (PhoneNumber *number in [self.parsedPhoneNumbers sortedArrayUsingSelector:@selector(compare:)]) { for (PhoneNumber *number in [self.parsedPhoneNumbers sortedArrayUsingSelector:@selector(compare:)]) {
SignalRecipient *signalRecipient = SignalRecipient *_Nullable signalRecipient =
[SignalRecipient recipientWithTextSecureIdentifier:number.toE164 withTransaction:transaction]; [SignalRecipient registeredRecipientForRecipientId:number.toE164 transaction:transaction];
if (signalRecipient) { if (signalRecipient) {
[result addObject:signalRecipient]; [result addObject:signalRecipient];
} }
@ -223,7 +223,7 @@ NS_ASSUME_NONNULL_BEGIN
[OWSPrimaryStorage.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [OWSPrimaryStorage.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
for (PhoneNumber *number in self.parsedPhoneNumbers) { for (PhoneNumber *number in self.parsedPhoneNumbers) {
if ([SignalRecipient recipientWithTextSecureIdentifier:number.toE164 withTransaction:transaction]) { if ([SignalRecipient isRegisteredSignalAccount:number.toE164 transaction:transaction]) {
[identifiers addObject:number.toE164]; [identifiers addObject:number.toE164];
} }
} }

@ -9,9 +9,7 @@ NS_ASSUME_NONNULL_BEGIN
// We hang the "known device list" for signal accounts on this entity. // We hang the "known device list" for signal accounts on this entity.
@interface SignalRecipient : TSYapDatabaseObject @interface SignalRecipient : TSYapDatabaseObject
@property (readonly) NSOrderedSet *devices; @property (nonatomic, readonly) NSOrderedSet *devices;
@property (nonatomic) BOOL mayBeUnregistered;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
@ -28,9 +26,9 @@ NS_ASSUME_NONNULL_BEGIN
transaction:(YapDatabaseReadWriteTransaction *)transaction; transaction:(YapDatabaseReadWriteTransaction *)transaction;
// TODO: Replace with cache of known signal account ids. // TODO: Replace with cache of known signal account ids.
+ (nullable instancetype)recipientWithTextSecureIdentifier:(NSString *)textSecureIdentifier; // TODO: Remove?
+ (nullable instancetype)recipientWithTextSecureIdentifier:(NSString *)textSecureIdentifier + (nullable instancetype)registeredRecipientForRecipientId:(NSString *)recipientId
withTransaction:(YapDatabaseReadTransaction *)transaction; transaction:(YapDatabaseReadTransaction *)transaction;
- (void)addDevices:(NSSet *)set; - (void)addDevices:(NSSet *)set;
- (void)removeDevices:(NSSet *)set; - (void)removeDevices:(NSSet *)set;
@ -39,6 +37,14 @@ NS_ASSUME_NONNULL_BEGIN
- (NSComparisonResult)compare:(SignalRecipient *)other; - (NSComparisonResult)compare:(SignalRecipient *)other;
// TODO: Replace with cache of known signal account ids.
+ (BOOL)isRegisteredSignalAccount:(NSString *)recipientId transaction:(YapDatabaseReadTransaction *)transaction;
+ (void)markAccountAsRegistered:(NSString *)recipientId transaction:(YapDatabaseReadWriteTransaction *)transaction;
+ (void)markAccountAsNotRegistered:(NSString *)recipientId transaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)markAccountAsNotRegisteredWithTransaction:(YapDatabaseReadWriteTransaction *)transaction;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -10,7 +10,9 @@ NS_ASSUME_NONNULL_BEGIN
@interface SignalRecipient () @interface SignalRecipient ()
@property NSOrderedSet *devices; @property (nonatomic) BOOL mayBeUnregistered;
@property (nonatomic) NSOrderedSet *devices;
@end @end
@ -43,8 +45,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (SignalRecipient *)ensureRecipientExistsWithRecipientId:(NSString *)recipientId + (SignalRecipient *)ensureRecipientExistsWithRecipientId:(NSString *)recipientId
transaction:(YapDatabaseReadWriteTransaction *)transaction transaction:(YapDatabaseReadWriteTransaction *)transaction
{ {
SignalRecipient *_Nullable recipient = SignalRecipient *_Nullable recipient = [self recipientForRecipientId:recipientId transaction:transaction];
[self recipientWithTextSecureIdentifier:recipientId withTransaction:transaction];
if (recipient) { if (recipient) {
return recipient; return recipient;
} }
@ -60,8 +61,7 @@ NS_ASSUME_NONNULL_BEGIN
deviceId:(UInt32)deviceId deviceId:(UInt32)deviceId
transaction:(YapDatabaseReadWriteTransaction *)transaction transaction:(YapDatabaseReadWriteTransaction *)transaction
{ {
SignalRecipient *_Nullable existingRecipient = SignalRecipient *_Nullable existingRecipient = [self recipientForRecipientId:recipientId transaction:transaction];
[self recipientWithTextSecureIdentifier:recipientId withTransaction:transaction];
if (!existingRecipient) { if (!existingRecipient) {
DDLogDebug(@"%@ in %s creating recipient: %@, with deviceId: %u", DDLogDebug(@"%@ in %s creating recipient: %@, with deviceId: %u",
self.logTag, self.logTag,
@ -132,17 +132,37 @@ NS_ASSUME_NONNULL_BEGIN
return self; return self;
} }
+ (nullable instancetype)recipientWithTextSecureIdentifier:(NSString *)textSecureIdentifier
withTransaction:(YapDatabaseReadTransaction *)transaction + (nullable instancetype)registeredRecipientForRecipientId:(NSString *)recipientId
transaction:(YapDatabaseReadTransaction *)transaction
{
OWSAssert(transaction);
OWSAssert(recipientId.length > 0);
SignalRecipient *_Nullable instance = [self recipientForRecipientId:recipientId transaction:transaction];
if (instance && !instance.mayBeUnregistered) {
return instance;
} else {
return nil;
}
}
+ (nullable instancetype)recipientForRecipientId:(NSString *)recipientId
transaction:(YapDatabaseReadTransaction *)transaction
{ {
return [self fetchObjectWithUniqueID:textSecureIdentifier transaction:transaction]; OWSAssert(transaction);
OWSAssert(recipientId.length > 0);
return [self fetchObjectWithUniqueID:recipientId transaction:transaction];
} }
+ (nullable instancetype)recipientWithTextSecureIdentifier:(NSString *)textSecureIdentifier + (nullable instancetype)recipientForRecipientId:(NSString *)recipientId
{ {
OWSAssert(recipientId.length > 0);
__block SignalRecipient *recipient; __block SignalRecipient *recipient;
[self.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { [self.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
recipient = [self recipientWithTextSecureIdentifier:textSecureIdentifier withTransaction:transaction]; recipient = [self recipientForRecipientId:recipientId transaction:transaction];
}]; }];
return recipient; return recipient;
} }
@ -150,7 +170,7 @@ NS_ASSUME_NONNULL_BEGIN
// TODO This method should probably live on the TSAccountManager rather than grabbing a global singleton. // TODO This method should probably live on the TSAccountManager rather than grabbing a global singleton.
+ (instancetype)selfRecipient + (instancetype)selfRecipient
{ {
SignalRecipient *myself = [self recipientWithTextSecureIdentifier:[TSAccountManager localNumber]]; SignalRecipient *myself = [self recipientForRecipientId:[TSAccountManager localNumber]];
if (!myself) { if (!myself) {
myself = [[self alloc] initWithTextSecureIdentifier:[TSAccountManager localNumber]]; myself = [[self alloc] initWithTextSecureIdentifier:[TSAccountManager localNumber]];
} }
@ -195,6 +215,67 @@ NS_ASSUME_NONNULL_BEGIN
DDLogVerbose(@"%@ saved signal recipient: %@", self.logTag, self.recipientId); DDLogVerbose(@"%@ saved signal recipient: %@", self.logTag, self.recipientId);
} }
+ (BOOL)isRegisteredSignalAccount:(NSString *)recipientId
{
__block BOOL result;
[self.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
result = [self isRegisteredSignalAccount:recipientId transaction:transaction];
}];
return result;
}
+ (BOOL)isRegisteredSignalAccount:(NSString *)recipientId transaction:(YapDatabaseReadTransaction *)transaction
{
SignalRecipient *_Nullable instance = [self recipientForRecipientId:recipientId transaction:transaction];
return (instance && !instance.mayBeUnregistered);
}
+ (void)markAccountAsRegistered:(NSString *)recipientId transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssert(transaction);
OWSAssert(recipientId.length > 0);
SignalRecipient *_Nullable instance = [self recipientForRecipientId:recipientId transaction:transaction];
if (!instance) {
DDLogDebug(@"%@ creating recipient: %@", self.logTag, recipientId);
instance = [[self alloc] initWithTextSecureIdentifier:recipientId];
[instance saveWithTransaction:transaction];
return;
}
if (!instance.mayBeUnregistered) {
return;
}
instance.mayBeUnregistered = NO;
[instance saveWithTransaction:transaction];
}
+ (void)markAccountAsNotRegistered:(NSString *)recipientId transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssert(transaction);
OWSAssert(recipientId.length > 0);
SignalRecipient *_Nullable instance = [self recipientForRecipientId:recipientId transaction:transaction];
if (!instance) {
return;
}
if (instance.mayBeUnregistered) {
return;
}
instance.mayBeUnregistered = YES;
[instance saveWithTransaction:transaction];
}
- (void)markAccountAsNotRegisteredWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssert(transaction);
self.mayBeUnregistered = YES;
[SignalRecipient markAccountAsNotRegistered:self.recipientId transaction:transaction];
}
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -712,11 +712,11 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[message updateWithSkippedRecipient:recipient.recipientId transaction:transaction]; [message updateWithSkippedRecipient:recipient.recipientId transaction:transaction];
} }
if (recipient.mayBeUnregistered) { if (![SignalRecipient isRegisteredSignalAccount:recipient.recipientId transaction:transaction]) {
return; return;
} }
recipient.mayBeUnregistered = YES;
[recipient saveWithTransaction:transaction]; [recipient markAccountAsNotRegisteredWithTransaction:transaction];
[[TSInfoMessage userNotRegisteredMessageInThread:thread recipientId:recipient.recipientId] [[TSInfoMessage userNotRegisteredMessageInThread:thread recipientId:recipient.recipientId]
saveWithTransaction:transaction]; saveWithTransaction:transaction];
@ -1016,10 +1016,9 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[recipient saveWithTransaction:transaction]; [recipient saveWithTransaction:transaction];
[message updateWithSentRecipient:recipient.uniqueId transaction:transaction]; [message updateWithSentRecipient:recipient.uniqueId transaction:transaction];
if (recipient.mayBeUnregistered) { // If we've just delivered a message to a user, we know they
recipient.mayBeUnregistered = NO; // have a valid Signal account.
[recipient saveWithTransaction:transaction]; [SignalRecipient markAccountAsRegistered:recipient.recipientId transaction:transaction];
}
}]; }];
[self handleMessageSentLocally:message]; [self handleMessageSentLocally:message];

Loading…
Cancel
Save