Skip to content

Commit

Permalink
MXRoomSummary: Add the trust property to indicate trust in other user…
Browse files Browse the repository at this point in the history
…s and devices in the room

element-hq/element-ios/issues/2906)
  • Loading branch information
manuroe committed Jan 31, 2020
1 parent 664ef3d commit 6d5fcd7
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 9,7 @@ Improvements:
* MXSession: Add createRoomWithParameters with a MXRoomCreationParameters model class.
* MXRoomCreationParameters: Support the initial_state parameter and allow e2e on room creation (vector-im/riot-ios/issues/2943).
* MXCrypto: Expose devicesForUser.
* MXRoomSummary: Add the trust property to indicate trust in other users and devices in the room (vector-im/riot-ios/issues/2906).

Bug fix:
* MXEventType: Fix Swift refinement.
Expand Down
6 changes: 6 additions & 0 deletions MatrixSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 413,8 @@
32D2CC0523422462002BD8CA /* MX3PidAddSession.h in Headers */ = {isa = PBXBuildFile; fileRef = 32D2CC0123422462002BD8CA /* MX3PidAddSession.h */; settings = {ATTRIBUTES = (Public, ); }; };
32D2CC0623422462002BD8CA /* MX3PidAddManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 32D2CC0223422462002BD8CA /* MX3PidAddManager.m */; };
32D2CC09234336D6002BD8CA /* MX3PidAddManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32D2CC08234336D6002BD8CA /* MX3PidAddManager.swift */; };
32D5D16323E400A600E3E37C /* MXRoomSummaryTrustTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 32D5D16223E400A600E3E37C /* MXRoomSummaryTrustTests.m */; };
32D5D16423E400A600E3E37C /* MXRoomSummaryTrustTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 32D5D16223E400A600E3E37C /* MXRoomSummaryTrustTests.m */; };
32D7767D1A27860600FC4AA2 /* MXMemoryStore.h in Headers */ = {isa = PBXBuildFile; fileRef = 32D7767B1A27860600FC4AA2 /* MXMemoryStore.h */; settings = {ATTRIBUTES = (Public, ); }; };
32D7767E1A27860600FC4AA2 /* MXMemoryStore.m in Sources */ = {isa = PBXBuildFile; fileRef = 32D7767C1A27860600FC4AA2 /* MXMemoryStore.m */; };
32D776811A27877300FC4AA2 /* MXMemoryRoomStore.h in Headers */ = {isa = PBXBuildFile; fileRef = 32D7767F1A27877300FC4AA2 /* MXMemoryRoomStore.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -1432,6 1434,7 @@
32D2CC0123422462002BD8CA /* MX3PidAddSession.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MX3PidAddSession.h; sourceTree = "<group>"; };
32D2CC0223422462002BD8CA /* MX3PidAddManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MX3PidAddManager.m; sourceTree = "<group>"; };
32D2CC08234336D6002BD8CA /* MX3PidAddManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MX3PidAddManager.swift; sourceTree = "<group>"; };
32D5D16223E400A600E3E37C /* MXRoomSummaryTrustTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXRoomSummaryTrustTests.m; sourceTree = "<group>"; };
32D7767B1A27860600FC4AA2 /* MXMemoryStore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXMemoryStore.h; sourceTree = "<group>"; };
32D7767C1A27860600FC4AA2 /* MXMemoryStore.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXMemoryStore.m; sourceTree = "<group>"; };
32D7767F1A27877300FC4AA2 /* MXMemoryRoomStore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXMemoryRoomStore.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2430,6 2433,7 @@
32C6F93919DD814400EA4E9C /* MatrixSDKTests */ = {
isa = PBXGroup;
children = (
32D5D16223E400A600E3E37C /* MXRoomSummaryTrustTests.m */,
B11BD45B22CB8ABC0064D8B0 /* MXReplyEventParserTests.m */,
32792BE02296C64200F4FC9D /* MXAggregatedEditsTests.m */,
32792BDE2296C59B00F4FC9D /* MXAggregatedReactionTests.m */,
Expand Down Expand Up @@ -3808,6 3812,7 @@
321809B919EEBF3000377451 /* MXEventTests.m in Sources */,
32A31BC120D3F4C4005916C7 /* MXFilterTests.m in Sources */,
32B0E3E423A384D40054FF1A /* MXAggregatedReferenceTests.m in Sources */,
32D5D16323E400A600E3E37C /* MXRoomSummaryTrustTests.m in Sources */,
C61A4AF41E5DD88400442158 /* Dummy.swift in Sources */,
32935F61216FA49D00A1BC24 /* MXCryptoBackupTests.m in Sources */,
328DDEC11A07E57E008C7DC8 /* MXJSONModelTests.m in Sources */,
Expand Down Expand Up @@ -4097,6 4102,7 @@
B1E09A292397FD080057C069 /* MXLoggerTests.m in Sources */,
B1E09A492398028D0057C069 /* MXSelfSignedHomeserverTests.m in Sources */,
32B0E3E523A384D40054FF1A /* MXAggregatedReferenceTests.m in Sources */,
32D5D16423E400A600E3E37C /* MXRoomSummaryTrustTests.m in Sources */,
B1E09A1B2397FCE90057C069 /* MXAggregatedReactionTests.m in Sources */,
B1E09A3F2397FD820057C069 /* MXNotificationCenterTests.m in Sources */,
B1E09A222397FCE90057C069 /* MXRoomSummaryTests.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion MatrixSDK/Crypto/Data/Trust/MXUsersTrustLevelSummary.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 21,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
A summary of the trust for a group of users and their devices
*/
@interface MXUsersTrustLevelSummary : NSObject
@interface MXUsersTrustLevelSummary : NSObject <NSCoding>

// The ratio of trusted users excluding our user
@property (nonatomic, strong, readonly) NSProgress *trustedUsersProgress;
Expand Down
37 changes: 34 additions & 3 deletions MatrixSDK/Crypto/Data/Trust/MXUsersTrustLevelSummary.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 18,53 @@

@interface MXUsersTrustLevelSummary()

@property (nonatomic, strong, readwrite) NSProgress *trustedMembersProgress;
@property (nonatomic, strong, readwrite) NSProgress *trustedUsersProgress;
@property (nonatomic, strong, readwrite) NSProgress *trustedDevicesProgress;

@end

@implementation MXUsersTrustLevelSummary

- (instancetype)initWithTrustedUsersProgress:(NSProgress*)trustedMembersProgress andTrustedDevicesProgress:(NSProgress*)trustedDevicesProgress
- (instancetype)initWithTrustedUsersProgress:(NSProgress*)trustedUsersProgress andTrustedDevicesProgress:(NSProgress*)trustedDevicesProgress
{
self = [super init];
if (self)
{
self.trustedMembersProgress = trustedMembersProgress;
self.trustedUsersProgress = trustedUsersProgress;
self.trustedDevicesProgress = trustedDevicesProgress;
}
return self;
}


#pragma mark - NSCoding

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [self init];
if (self)
{
NSUInteger usersCount = [aDecoder decodeIntegerForKey:@"usersCount"];
NSUInteger trustedUsersCount = [aDecoder decodeIntegerForKey:@"trustedUsersCount"];
NSUInteger devicesCount = [aDecoder decodeIntegerForKey:@"devicesCount"];
NSUInteger trustedDevicesCount = [aDecoder decodeIntegerForKey:@"trustedDevicesCount"];

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

self.trustedDevicesProgress = [NSProgress progressWithTotalUnitCount:devicesCount];
self.trustedDevicesProgress.completedUnitCount = trustedDevicesCount;
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInteger:self.trustedUsersProgress.totalUnitCount forKey:@"usersCount"];
[aCoder encodeInteger:self.trustedUsersProgress.completedUnitCount forKey:@"trustedUsersCount"];
[aCoder encodeInteger:self.trustedDevicesProgress.totalUnitCount forKey:@"devicesCount"];
[aCoder encodeInteger:self.trustedDevicesProgress.completedUnitCount forKey:@"trustedDevicesCount"];
}


@end
7 changes: 7 additions & 0 deletions MatrixSDK/Data/MXRoomSummary.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 22,7 @@
#import "MXHTTPOperation.h"
#import "MXRoomMembersCount.h"
#import "MXEnumConstants.h"
#import "MXUsersTrustLevelSummary.h"

@class MXSession, MXRoom, MXRoomState, MXEvent;
@protocol MXStore;
Expand Down Expand Up @@ -249,6 250,12 @@ FOUNDATION_EXPORT NSString *const kMXRoomSummaryDidChangeNotification;
*/
@property (nonatomic) BOOL isEncrypted;

/**
If the room is E2E encrypted, indicate global trust in other users and devices in the room.
Nil if not yet computed.
*/
@property (nonatomic) MXUsersTrustLevelSummary *trust;

/**
The number of unread events wrote in the store which have their type listed in the MXSession.unreadEventType.
Expand Down
109 changes: 109 additions & 0 deletions MatrixSDK/Data/MXRoomSummary.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 22,7 @@
#import "MXRoom.h"
#import "MXRoomState.h"
#import "MXSession.h"
#import "MXSDKOptions.h"
#import "MXTools.h"
#import "MXEventRelations.h"
#import "MXEventReplace.h"
Expand Down Expand Up @@ -442,6 443,102 @@ - (void)unregisterEventEditsListener
}


#pragma mark - Trust management

- (void)setIsEncrypted:(BOOL)isEncrypted
{
_isEncrypted = isEncrypted;
if (_isEncrypted && [MXSDKOptions sharedInstance].computeE2ERoomSummaryTrust)
{
// Bootstrap trust computation
[self registerTrustLevelDidChangeNotifications];

if (!self.trust)
{
[self triggerComputeTrust:YES];
}
}
}

- (void)setMembersCount:(MXRoomMembersCount *)membersCount
{
_membersCount = membersCount;
if (_isEncrypted && [MXSDKOptions sharedInstance].computeE2ERoomSummaryTrust)
{
[self triggerComputeTrust:YES];
}
}

- (void)registerTrustLevelDidChangeNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceInfoTrustLevelDidChange:) name:MXDeviceInfoTrustLevelDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(crossSigningInfoTrustLevelDidChange:) name:MXCrossSigningInfoTrustLevelDidChangeNotification object:nil];
}

- (void)deviceInfoTrustLevelDidChange:(NSNotification*)notification
{
MXDeviceInfo *deviceInfo = notification.object;

NSString *userId = deviceInfo.userId;
if (userId)
{
[self encryptionTrustLevelDidChangeRelatedToUserId:userId];
}
}

- (void)crossSigningInfoTrustLevelDidChange:(NSNotification*)notification
{
MXCrossSigningInfo *crossSigningInfo = notification.object;

NSString *userId = crossSigningInfo.userId;
if (userId)
{
[self encryptionTrustLevelDidChangeRelatedToUserId:userId];
}
}

- (void)encryptionTrustLevelDidChangeRelatedToUserId:(NSString*)userId
{
[self.room members:^(MXRoomMembers *roomMembers) {
MXRoomMember *roomMember = [roomMembers memberWithUserId:userId];

// If user belongs to the room refresh the trust level
if (roomMember)
{
[self triggerComputeTrust:NO];
}

} failure:^(NSError *error) {
NSLog(@"[MXRoomSummary] trustLevelDidChangeRelatedToUserId fails to retrieve room members");
}];
}

- (void)triggerComputeTrust:(BOOL)forceDownload
{
if (!_isEncrypted || ![MXSDKOptions sharedInstance].computeE2ERoomSummaryTrust)
{
return;
}

// To improve
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self computeTrust:forceDownload];
});
}

- (void)computeTrust:(BOOL)forceDownload
{
[self.room membersTrustLevelSummaryWithForceDownload:forceDownload success:^(MXUsersTrustLevelSummary *usersTrustLevelSummary) {

self.trust = usersTrustLevelSummary;
[self save:YES];

} failure:^(NSError *error) {
NSLog(@"[MXRoomSummary] trustLevelDidChangeRelatedToUserId fails to retrieve room members trusted progress");
}];
}


#pragma mark - Others
- (NSUInteger)localUnreadEventCount
{
Expand Down Expand Up @@ -615,6 712,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder

_others = [aDecoder decodeObjectForKey:@"others"];
_isEncrypted = [aDecoder decodeBoolForKey:@"isEncrypted"];
_trust = [aDecoder decodeObjectForKey:@"trust"];
_notificationCount = (NSUInteger)[aDecoder decodeIntegerForKey:@"notificationCount"];
_highlightCount = (NSUInteger)[aDecoder decodeIntegerForKey:@"highlightCount"];
_directUserId = [aDecoder decodeObjectForKey:@"directUserId"];
Expand All @@ -639,6 737,13 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder
_lastMessageOthers = lastMessageData[@"lastMessageOthers"];

_hiddenFromUser = [aDecoder decodeBoolForKey:@"hiddenFromUser"];

if (_isEncrypted && !_trust)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self triggerComputeTrust:YES];
});
}
}
return self;
}
Expand All @@ -657,6 762,10 @@ - (void)encodeWithCoder:(NSCoder *)aCoder

[aCoder encodeObject:_others forKey:@"others"];
[aCoder encodeBool:_isEncrypted forKey:@"isEncrypted"];
if (_trust)
{
[aCoder encodeObject:_trust forKey:@"trust"];
}
[aCoder encodeInteger:(NSInteger)_notificationCount forKey:@"notificationCount"];
[aCoder encodeInteger:(NSInteger)_highlightCount forKey:@"highlightCount"];
[aCoder encodeObject:_directUserId forKey:@"directUserId"];
Expand Down
7 changes: 7 additions & 0 deletions MatrixSDK/MXSDKOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 56,13 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic) BOOL enableCryptoWhenStartingMXSession;

/**
Compute and maintain MXRommSummary.trust value.
NO by default.
This requires to load all room members to compute it.
*/
@property (nonatomic) BOOL computeE2ERoomSummaryTrust;

/**
The delegate object to receive analytics events
Expand Down
Loading

0 comments on commit 6d5fcd7

Please sign in to comment.