Skip to content

Commit

Permalink
Merge pull request #834 from matrix-org/riot_3126
Browse files Browse the repository at this point in the history
MXCrypto: Make trustLevelSummaryForUserIds async …
  • Loading branch information
manuroe committed Apr 21, 2020
2 parents 9b81ec2 + bfe07fa commit b98a709
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ Changes in Matrix iOS SDK in 0.16.1 (2020-04-xx)

Improvements:
* MXHTTPClient: Log HTTP requests methods.
* MXCrypto: Make trustLevelSummaryForUserIds async (vector-im/riot-ios/issues/3126).

Bug fix:
* Fix race condition in MXSecretShareManager (vector-im/riot-ios/issues/3123).
* Too much MXDeviceInfoTrustLevelDidChangeNotification and MXCrossSigningInfoTrustLevelDidChangeNotification (vector-im/riot-ios/issues/3121).

API break:
* MXCrypto: trustLevelSummaryForUserIds: is now async.

Changes in Matrix iOS SDK in 0.16.0 (2020-04-17)
================================================

Expand Down
4 changes: 2 additions & 2 deletions MatrixSDK/Crypto/MXCrypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ extern NSString *const MXDeviceListDidUpdateUsersDevicesNotification;
Get the stored summary of users trust level (trusted users and devices count).
@param userIds The user ids.
@return the trust summary.
@param onComplete the callback called once operation is done.
*/
- (MXUsersTrustLevelSummary *)trustLevelSummaryForUserIds:(NSArray<NSString*>*)userIds;
- (void)trustLevelSummaryForUserIds:(NSArray<NSString*>*)userIds onComplete:(void (^)(MXUsersTrustLevelSummary *trustLevelSummary))onComplete;


#pragma mark - Users keys
Expand Down
67 changes: 40 additions & 27 deletions MatrixSDK/Crypto/MXCrypto.m
Original file line number Diff line number Diff line change
Expand Up @@ -1049,45 +1049,58 @@ - (void)trustLevelSummaryForUserIds:(NSArray<NSString*>*)userIds

// Read data from the store
// It has been updated in the process of the downloadKeys response
success([self trustLevelSummaryForUserIds:userIds]);
[self trustLevelSummaryForUserIds:userIds onComplete:^(MXUsersTrustLevelSummary *trustLevelSummary) {
success(trustLevelSummary);
}];

} failure:failure];
}

- (MXUsersTrustLevelSummary *)trustLevelSummaryForUserIds:(NSArray<NSString*>*)userIds
- (void)trustLevelSummaryForUserIds:(NSArray<NSString*>*)userIds onComplete:(void (^)(MXUsersTrustLevelSummary *trustLevelSummary))onComplete;
{
NSUInteger usersCount = 0;
NSUInteger trustedUsersCount = 0;
NSUInteger devicesCount = 0;
NSUInteger trustedDevicesCount = 0;

for (NSString *userId in userIds)
{
usersCount++;
// Use cargoQueue for potential huge read requests from the store
MXWeakify(self);
dispatch_async(cargoQueue, ^{
MXStrongifyAndReturnIfNil(self);

MXUserTrustLevel *userTrustLevel = [self trustLevelForUser:userId];
if (userTrustLevel.isVerified)
NSUInteger usersCount = 0;
NSUInteger trustedUsersCount = 0;
NSUInteger devicesCount = 0;
NSUInteger trustedDevicesCount = 0;

for (NSString *userId in userIds)
{
trustedUsersCount++;

for (MXDeviceInfo *device in [self.store devicesForUser:userId].allValues)
usersCount++;

MXUserTrustLevel *userTrustLevel = [self trustLevelForUser:userId];
if (userTrustLevel.isVerified)
{
devicesCount++;
if (device.trustLevel.isVerified)
trustedUsersCount++;

for (MXDeviceInfo *device in [self.store devicesForUser:userId].allValues)
{
trustedDevicesCount++;
devicesCount++;
if (device.trustLevel.isVerified)
{
trustedDevicesCount++;
}
}
}
}
}

NSProgress *trustedUsersProgress = [NSProgress progressWithTotalUnitCount:usersCount];
trustedUsersProgress.completedUnitCount = trustedUsersCount;

NSProgress *trustedDevicesProgress = [NSProgress progressWithTotalUnitCount:devicesCount];
trustedDevicesProgress.completedUnitCount = trustedDevicesCount;

return [[MXUsersTrustLevelSummary alloc] initWithTrustedUsersProgress:trustedUsersProgress andTrustedDevicesProgress:trustedDevicesProgress];

NSProgress *trustedUsersProgress = [NSProgress progressWithTotalUnitCount:usersCount];
trustedUsersProgress.completedUnitCount = trustedUsersCount;

NSProgress *trustedDevicesProgress = [NSProgress progressWithTotalUnitCount:devicesCount];
trustedDevicesProgress.completedUnitCount = trustedDevicesCount;

MXUsersTrustLevelSummary *trustLevelSummary = [[MXUsersTrustLevelSummary alloc] initWithTrustedUsersProgress:trustedUsersProgress
andTrustedDevicesProgress:trustedDevicesProgress];

dispatch_async(dispatch_get_main_queue(), ^{
onComplete(trustLevelSummary);
});
});
}

#pragma mark - Users keys
Expand Down
4 changes: 3 additions & 1 deletion MatrixSDK/Data/MXRoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -3121,7 +3121,9 @@ - (void)membersTrustLevelSummaryWithForceDownload:(BOOL)forceDownload success:(v
}
else
{
success([crypto trustLevelSummaryForUserIds:memberIds]);
[crypto trustLevelSummaryForUserIds:memberIds onComplete:^(MXUsersTrustLevelSummary *trustLevelSummary) {
success(trustLevelSummary);
}];
}

} failure:failure];
Expand Down

0 comments on commit b98a709

Please sign in to comment.