Add protocol context to protocol kit.

pull/1/head
Matthew Chen 7 years ago
parent 39e353503f
commit 218bb15ea7

@ -27,7 +27,8 @@ NS_ASSUME_NONNULL_BEGIN
+ (NSString *)threadIdFromGroupId:(NSData *)groupId; + (NSString *)threadIdFromGroupId:(NSData *)groupId;
// all group threads containing recipient as a member // all group threads containing recipient as a member
+ (NSArray<TSGroupThread *> *)groupThreadsWithRecipientId:(NSString *)recipientId; + (NSArray<TSGroupThread *> *)groupThreadsWithRecipientId:(NSString *)recipientId
transaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)updateAvatarWithAttachmentStream:(TSAttachmentStream *)attachmentStream; - (void)updateAvatarWithAttachmentStream:(TSAttachmentStream *)attachmentStream;
- (void)updateAvatarWithAttachmentStream:(TSAttachmentStream *)attachmentStream - (void)updateAvatarWithAttachmentStream:(TSAttachmentStream *)attachmentStream

@ -144,28 +144,22 @@ NS_ASSUME_NONNULL_BEGIN
return [groupMemberIds copy]; return [groupMemberIds copy];
} }
// Group and Contact threads share a collection, this is a convenient way to enumerate *just* the group threads
+ (void)enumerateGroupThreadsUsingBlock:(void (^)(TSGroupThread *groupThread, BOOL *stop))block
{
[self enumerateCollectionObjectsUsingBlock:^(id obj, BOOL *stop) {
if ([obj isKindOfClass:[TSGroupThread class]]) {
block((TSGroupThread *)obj, stop);
}
}];
}
// @returns all threads to which the recipient is a member. // @returns all threads to which the recipient is a member.
// //
// @note If this becomes a hotspot we can extract into a YapDB View. // @note If this becomes a hotspot we can extract into a YapDB View.
// As is, the number of groups should be small (dozens, *maybe* hundreds), and we only enumerate them upon SN changes. // As is, the number of groups should be small (dozens, *maybe* hundreds), and we only enumerate them upon SN changes.
+ (NSArray<TSGroupThread *> *)groupThreadsWithRecipientId:(NSString *)recipientId + (NSArray<TSGroupThread *> *)groupThreadsWithRecipientId:(NSString *)recipientId
{ transaction:(YapDatabaseReadWriteTransaction *)transaction {
NSMutableArray<TSGroupThread *> *groupThreads = [NSMutableArray new]; NSMutableArray<TSGroupThread *> *groupThreads = [NSMutableArray new];
[self enumerateGroupThreadsUsingBlock:^(TSGroupThread *_Nonnull groupThread, BOOL *_Nonnull stop) { [self enumerateCollectionObjectsWithTransaction:transaction usingBlock:^(id obj, BOOL *stop) {
if ([obj isKindOfClass:[TSGroupThread class]]) {
TSGroupThread *groupThread = (TSGroupThread *)obj;
if ([groupThread.groupModel.groupMemberIds containsObject:recipientId]) { if ([groupThread.groupModel.groupMemberIds containsObject:recipientId]) {
[groupThreads addObject:groupThread]; [groupThreads addObject:groupThread];
} }
}
}]; }];
return [groupThreads copy]; return [groupThreads copy];

@ -471,7 +471,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
[TSErrorMessage nonblockingIdentityChangeInThread:contactThread recipientId:recipientId]; [TSErrorMessage nonblockingIdentityChangeInThread:contactThread recipientId:recipientId];
[messages addObject:errorMessage]; [messages addObject:errorMessage];
for (TSGroupThread *groupThread in [TSGroupThread groupThreadsWithRecipientId:recipientId]) { for (TSGroupThread *groupThread in [TSGroupThread groupThreadsWithRecipientId:recipientId transaction:transaction]) {
[messages addObject:[TSErrorMessage nonblockingIdentityChangeInThread:groupThread recipientId:recipientId]]; [messages addObject:[TSErrorMessage nonblockingIdentityChangeInThread:groupThread recipientId:recipientId]];
} }
@ -564,7 +564,6 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
}); });
} }
// TODO: Keep converting.
- (void)sendSyncVerificationStateMessage:(OWSVerificationStateSyncMessage *)message - (void)sendSyncVerificationStateMessage:(OWSVerificationStateSyncMessage *)message
{ {
OWSAssert(message); OWSAssert(message);
@ -585,7 +584,9 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
DDLogInfo(@"%@ Successfully sent verification state sync message", self.logTag); DDLogInfo(@"%@ Successfully sent verification state sync message", self.logTag);
// Record that this verification state was successfully synced. // Record that this verification state was successfully synced.
[self clearSyncMessageForRecipientId:message.verificationForRecipientId]; [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction * transaction) {
[self clearSyncMessageForRecipientId:message.verificationForRecipientId transaction:transaction];
}];
} }
failure:^(NSError *error) { failure:^(NSError *error) {
DDLogError(@"%@ Failed to send verification state sync message with error: %@", self.logTag, error); DDLogError(@"%@ Failed to send verification state sync message with error: %@", self.logTag, error);
@ -598,12 +599,13 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
self.logTag, self.logTag,
message.verificationForRecipientId); message.verificationForRecipientId);
// Otherwise this will fail forever. // Otherwise this will fail forever.
[self clearSyncMessageForRecipientId:message.verificationForRecipientId]; [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction * transaction) {
[self clearSyncMessageForRecipientId:message.verificationForRecipientId transaction:transaction];
}];
} }
}]; }];
} }
// TODO: Change signature of this method.
- (void)clearSyncMessageForRecipientId:(NSString *)recipientId - (void)clearSyncMessageForRecipientId:(NSString *)recipientId
transaction:(YapDatabaseReadWriteTransaction *)transaction transaction:(YapDatabaseReadWriteTransaction *)transaction
{ {
@ -630,18 +632,21 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
NSData *identityKey = [rawIdentityKey removeKeyType]; NSData *identityKey = [rawIdentityKey removeKeyType];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction * transaction) {
switch (verified.state) { switch (verified.state) {
case OWSSignalServiceProtosVerifiedStateDefault: case OWSSignalServiceProtosVerifiedStateDefault:
[self tryToApplyVerificationStateFromSyncMessage:OWSVerificationStateDefault [self tryToApplyVerificationStateFromSyncMessage:OWSVerificationStateDefault
recipientId:recipientId recipientId:recipientId
identityKey:identityKey identityKey:identityKey
overwriteOnConflict:NO]; overwriteOnConflict:NO
transaction:transaction];
break; break;
case OWSSignalServiceProtosVerifiedStateVerified: case OWSSignalServiceProtosVerifiedStateVerified:
[self tryToApplyVerificationStateFromSyncMessage:OWSVerificationStateVerified [self tryToApplyVerificationStateFromSyncMessage:OWSVerificationStateVerified
recipientId:recipientId recipientId:recipientId
identityKey:identityKey identityKey:identityKey
overwriteOnConflict:YES]; overwriteOnConflict:YES
transaction:transaction];
break; break;
case OWSSignalServiceProtosVerifiedStateUnverified: case OWSSignalServiceProtosVerifiedStateUnverified:
OWSFail(@"Verification state sync message for recipientId: %@ has unexpected value: %@.", OWSFail(@"Verification state sync message for recipientId: %@ has unexpected value: %@.",
@ -649,6 +654,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
OWSVerificationStateToString(OWSVerificationStateNoLongerVerified)); OWSVerificationStateToString(OWSVerificationStateNoLongerVerified));
return; return;
} }
}];
[self fireIdentityStateChangeNotification]; [self fireIdentityStateChangeNotification];
}); });
} }
@ -657,7 +663,11 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
recipientId:(NSString *)recipientId recipientId:(NSString *)recipientId
identityKey:(NSData *)identityKey identityKey:(NSData *)identityKey
overwriteOnConflict:(BOOL)overwriteOnConflict overwriteOnConflict:(BOOL)overwriteOnConflict
transaction:(YapDatabaseReadWriteTransaction *)transaction
{ {
OWSAssert(recipientId.length > 0);
OWSAssert(transaction);
if (recipientId.length < 1) { if (recipientId.length < 1) {
OWSFail(@"Verification state sync message missing recipientId."); OWSFail(@"Verification state sync message missing recipientId.");
return; return;
@ -668,9 +678,8 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
return; return;
} }
@synchronized(self) OWSRecipientIdentity *_Nullable recipientIdentity = [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId
{ transaction:transaction];
OWSRecipientIdentity *_Nullable recipientIdentity = [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId];
if (!recipientIdentity) { if (!recipientIdentity) {
// There's no existing recipient identity for this recipient. // There's no existing recipient identity for this recipient.
// We should probably create one. // We should probably create one.
@ -683,9 +692,10 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
// Ensure a remote identity exists for this key. We may be learning about // Ensure a remote identity exists for this key. We may be learning about
// it for the first time. // it for the first time.
[self saveRemoteIdentity:identityKey recipientId:recipientId protocolContext:protocolContext]; [self saveRemoteIdentity:identityKey recipientId:recipientId protocolContext:transaction];
recipientIdentity = [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId]; recipientIdentity = [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId
transaction:transaction];
if (recipientIdentity == nil) { if (recipientIdentity == nil) {
OWSFail(@"Missing expected identity: %@", recipientId); OWSFail(@"Missing expected identity: %@", recipientId);
@ -712,7 +722,8 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
OWSVerificationStateToString(recipientIdentity.verificationState), OWSVerificationStateToString(recipientIdentity.verificationState),
OWSVerificationStateToString(verificationState)); OWSVerificationStateToString(verificationState));
[recipientIdentity updateWithVerificationState:verificationState]; [recipientIdentity updateWithVerificationState:verificationState
transaction:transaction];
// No need to call [saveChangeMessagesForRecipientId:..] since this is // No need to call [saveChangeMessagesForRecipientId:..] since this is
// a new recipient. // a new recipient.
@ -734,9 +745,10 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
} }
DDLogWarn(@"recipientIdentity has non-matching identityKey; overwriting: %@", recipientId); DDLogWarn(@"recipientIdentity has non-matching identityKey; overwriting: %@", recipientId);
[self saveRemoteIdentity:identityKey recipientId:recipientId protocolContext:protocolContext]; [self saveRemoteIdentity:identityKey recipientId:recipientId protocolContext:transaction];
recipientIdentity = [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId]; recipientIdentity = [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId
transaction:transaction];
if (recipientIdentity == nil) { if (recipientIdentity == nil) {
OWSFail(@"Missing expected identity: %@", recipientId); OWSFail(@"Missing expected identity: %@", recipientId);
@ -758,14 +770,14 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
return; return;
} }
[recipientIdentity updateWithVerificationState:verificationState]; [recipientIdentity updateWithVerificationState:verificationState
transaction:transaction];
[self saveChangeMessagesForRecipientId:recipientId [self saveChangeMessagesForRecipientId:recipientId
verificationState:verificationState verificationState:verificationState
isLocalChange:NO isLocalChange:NO
transaction:transaction]; transaction:transaction];
} }
}
} }
// We only want to create change messages in response to user activity, // We only want to create change messages in response to user activity,

Loading…
Cancel
Save