Streamline SignalRecipient.

pull/1/head
Matthew Chen 7 years ago
parent ebe87348a2
commit 9618fc16c5

@ -38,6 +38,16 @@ NS_ASSUME_NONNULL_BEGIN
return self; return self;
} }
- (SignalRecipient *)signalRecipientForRegisteredRecipientId:(NSString *)recipientId
{
__block SignalRecipient *recipient;
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
recipient =
[SignalRecipient ensureRecipientExistsWithRegisteredRecipientId:recipientId transaction:transaction];
}];
return recipient;
}
- (void)lookupIdentifier:(NSString *)identifier - (void)lookupIdentifier:(NSString *)identifier
success:(void (^)(SignalRecipient *recipient))success success:(void (^)(SignalRecipient *recipient))success
failure:(void (^)(NSError *error))failure failure:(void (^)(NSError *error))failure
@ -52,7 +62,7 @@ NS_ASSUME_NONNULL_BEGIN
[self contactIntersectionWithSet:[NSSet setWithObject:identifier] [self contactIntersectionWithSet:[NSSet setWithObject:identifier]
success:^(NSSet<NSString *> *_Nonnull matchedIds) { success:^(NSSet<NSString *> *_Nonnull matchedIds) {
if (matchedIds.count == 1) { if (matchedIds.count == 1) {
success([SignalRecipient recipientWithTextSecureIdentifier:identifier]); success([self signalRecipientForRegisteredRecipientId:identifier]);
} else { } else {
failure(OWSErrorMakeNoSuchSignalRecipientError()); failure(OWSErrorMakeNoSuchSignalRecipientError());
} }
@ -75,7 +85,8 @@ NS_ASSUME_NONNULL_BEGIN
if (matchedIds.count > 0) { if (matchedIds.count > 0) {
NSMutableArray<SignalRecipient *> *recipients = [NSMutableArray new]; NSMutableArray<SignalRecipient *> *recipients = [NSMutableArray new];
for (NSString *identifier in matchedIds) { for (NSString *identifier in matchedIds) {
[recipients addObject:[SignalRecipient recipientWithTextSecureIdentifier:identifier]]; [recipients
addObject:[self signalRecipientForRegisteredRecipientId:identifier]];
} }
success([recipients copy]); success([recipients copy]);
} else { } else {
@ -109,18 +120,9 @@ NS_ASSUME_NONNULL_BEGIN
success:^(NSSet<NSString *> *matchedIds) { success:^(NSSet<NSString *> *matchedIds) {
[recipientIds minusSet:matchedIds]; [recipientIds minusSet:matchedIds];
// Cleaning up unregistered identifiers // TODO:
[OWSPrimaryStorage.dbReadWriteConnection //
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { // Update cache of registered identifiers.
for (NSString *identifier in recipientIds) {
SignalRecipient *recipient =
[SignalRecipient fetchObjectWithUniqueID:identifier
transaction:transaction];
[recipient removeWithTransaction:transaction];
}
}];
DDLogInfo(@"%@ successfully intersected contacts.", self.logTag); DDLogInfo(@"%@ successfully intersected contacts.", self.logTag);
success(); success();
} }
@ -165,7 +167,8 @@ NS_ASSUME_NONNULL_BEGIN
[OWSPrimaryStorage.dbReadWriteConnection [OWSPrimaryStorage.dbReadWriteConnection
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
for (NSString *identifier in identifiers) { for (NSString *identifier in identifiers) {
[SignalRecipient ensureRecipientExistsWithRecipientId:identifier transaction:transaction]; [SignalRecipient ensureRecipientExistsWithRegisteredRecipientId:identifier
transaction:transaction];
} }
}]; }];

@ -41,11 +41,6 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithRecipientId:(NSString *)recipientId; - (instancetype)initWithRecipientId:(NSString *)recipientId;
// In most cases this should be non-null. This should only
// be null in the case where the SignalRecipient was
// deleted before this property was accessed.
- (nullable SignalRecipient *)signalRecipientWithTransaction:(YapDatabaseReadTransaction *)transaction;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -36,15 +36,6 @@ NS_ASSUME_NONNULL_BEGIN
return self; return self;
} }
- (nullable SignalRecipient *)signalRecipientWithTransaction:(YapDatabaseReadTransaction *)transaction
{
OWSAssertIsOnMainThread();
OWSAssert(transaction);
OWSAssert(self.recipientId.length > 0);
return [SignalRecipient recipientWithTextSecureIdentifier:self.recipientId withTransaction:transaction];
}
- (nullable NSString *)uniqueId - (nullable NSString *)uniqueId
{ {
return _recipientId; return _recipientId;

@ -6,22 +6,20 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
// This class serves two purposes: // We hang the "known device list" for signal accounts on this entity.
//
// * We only _persist_ SignalRecipient instances when we know
// that it corresponds to an account on the Signal service.
// So SignalRecipient serves as a defacto cache of "known
// Signal users."
// * We hang the "known device list" for signal accounts on
// this entity.
@interface SignalRecipient : TSYapDatabaseObject @interface SignalRecipient : TSYapDatabaseObject
@property (readonly) NSOrderedSet *devices; @property (readonly) NSOrderedSet *devices;
@property (nonatomic) BOOL mayBeUnregistered;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
+ (instancetype)selfRecipient; + (instancetype)selfRecipient;
+ (SignalRecipient *)ensureRecipientExistsWithRegisteredRecipientId:(NSString *)recipientId
transaction:(YapDatabaseReadWriteTransaction *)transaction;
+ (SignalRecipient *)ensureRecipientExistsWithRecipientId:(NSString *)recipientId + (SignalRecipient *)ensureRecipientExistsWithRecipientId:(NSString *)recipientId
transaction:(YapDatabaseReadWriteTransaction *)transaction; transaction:(YapDatabaseReadWriteTransaction *)transaction;
@ -29,6 +27,7 @@ NS_ASSUME_NONNULL_BEGIN
deviceId:(UInt32)deviceId deviceId:(UInt32)deviceId
transaction:(YapDatabaseReadWriteTransaction *)transaction; transaction:(YapDatabaseReadWriteTransaction *)transaction;
// TODO: Replace with cache of known signal account ids.
+ (nullable instancetype)recipientWithTextSecureIdentifier:(NSString *)textSecureIdentifier; + (nullable instancetype)recipientWithTextSecureIdentifier:(NSString *)textSecureIdentifier;
+ (nullable instancetype)recipientWithTextSecureIdentifier:(NSString *)textSecureIdentifier + (nullable instancetype)recipientWithTextSecureIdentifier:(NSString *)textSecureIdentifier
withTransaction:(YapDatabaseReadTransaction *)transaction; withTransaction:(YapDatabaseReadTransaction *)transaction;

@ -22,6 +22,25 @@ NS_ASSUME_NONNULL_BEGIN
return @"SignalRecipient"; return @"SignalRecipient";
} }
- (void)removeWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSFail(@"%@ We should no longer remove SignalRecipients.", self.logTag);
[super removeWithTransaction:transaction];
}
+ (SignalRecipient *)ensureRecipientExistsWithRegisteredRecipientId:(NSString *)recipientId
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
SignalRecipient *recipient =
[self ensureRecipientExistsWithRegisteredRecipientId:recipient transaction:transaction];
if (recipient.mayBeUnregistered) {
recipient.mayBeUnregistered = NO;
[recipient saveWithTransaction:transaction];
}
return recipient;
}
+ (SignalRecipient *)ensureRecipientExistsWithRecipientId:(NSString *)recipientId + (SignalRecipient *)ensureRecipientExistsWithRecipientId:(NSString *)recipientId
transaction:(YapDatabaseReadWriteTransaction *)transaction transaction:(YapDatabaseReadWriteTransaction *)transaction
{ {

@ -712,7 +712,12 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[message updateWithSkippedRecipient:recipient.recipientId transaction:transaction]; [message updateWithSkippedRecipient:recipient.recipientId transaction:transaction];
} }
[recipient removeWithTransaction:transaction]; if (recipient.mayBeUnregistered) {
return;
}
recipient.mayBeUnregistered = YES;
[recipient saveWithTransaction:transaction];
[[TSInfoMessage userNotRegisteredMessageInThread:thread recipientId:recipient.recipientId] [[TSInfoMessage userNotRegisteredMessageInThread:thread recipientId:recipient.recipientId]
saveWithTransaction:transaction]; saveWithTransaction:transaction];
}]; }];
@ -1010,6 +1015,11 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[recipient saveWithTransaction:transaction]; [recipient saveWithTransaction:transaction];
[message updateWithSentRecipient:recipient.uniqueId transaction:transaction]; [message updateWithSentRecipient:recipient.uniqueId transaction:transaction];
if (recipient.mayBeUnregistered) {
recipient.mayBeUnregistered = NO;
[recipient saveWithTransaction:transaction];
}
}]; }];
[self handleMessageSentLocally:message]; [self handleMessageSentLocally:message];
@ -1075,6 +1085,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
DDLogWarn(@"%@ Unregistered recipient: %@", self.logTag, recipient.uniqueId); DDLogWarn(@"%@ Unregistered recipient: %@", self.logTag, recipient.uniqueId);
OWSAssert(thread); OWSAssert(thread);
[self unregisteredRecipient:recipient message:message thread:thread]; [self unregisteredRecipient:recipient message:message thread:thread];
NSError *error = OWSErrorMakeNoSuchSignalRecipientError(); NSError *error = OWSErrorMakeNoSuchSignalRecipientError();
// No need to retry if the recipient is not registered. // No need to retry if the recipient is not registered.

Loading…
Cancel
Save