Skip to content

Commit

Permalink
Merge pull request #1177 from matrix-org/doug/4609_update_room_name_c…
Browse files Browse the repository at this point in the history
…omputation

Add excludingUserIDs parameter to displayname and avatar updater methods.
  • Loading branch information
pixlwave authored Aug 11, 2021
2 parents c6ddeda + a206b5a commit 4da919f
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 10 deletions.
24 changes: 24 additions & 0 deletions MatrixSDK/Data/MXRoomSummaryUpdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,28 @@
*/
@property (nonatomic) NSArray<NSString *> *showRoomTypeStrings;

/**
Update the room summary's display name on received summary update, with additional support for excluding specified user IDs.
@param summary the room summary.
@param session the session the room belongs to.
@param serverRoomSummary the homeserver side room summary, or nil if updating from state events.
@param roomState the current state of the room.
@param excludedUserIDs an array of user IDs to ignore when computing the room name.
@return YES if the room summary has changed.
*/
- (BOOL)updateSummaryDisplayname:(MXRoomSummary *)summary session:(MXSession *)session withServerRoomSummary:(MXRoomSyncSummary *)serverRoomSummary roomState:(MXRoomState *)roomState excludingUserIDs:(NSArray<NSString *> *)excludedUserIDs;

/**
Update the room summary's avatar on received summary update, with additional support for excluding specified user IDs..
@param summary the room summary.
@param session the session the room belongs to.
@param serverRoomSummary the homeserver side room summary, or nil if updating from state events.
@param roomState the current state of the room.
@param excludedUserIDs an array of user IDs to ignore when updating the avatar.
@return YES if the room summary has changed.
*/
- (BOOL)updateSummaryAvatar:(MXRoomSummary *)summary session:(MXSession *)session withServerRoomSummary:(MXRoomSyncSummary *)serverRoomSummary roomState:(MXRoomState *)roomState excludingUserIDs:(NSArray<NSString *> *)excludedUserIDs;

@end
87 changes: 77 additions & 10 deletions MatrixSDK/Data/MXRoomSummaryUpdater.m
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ - (BOOL)session:(MXSession *)session updateRoomSummary:(MXRoomSummary *)summary
}

- (BOOL)updateSummaryDisplayname:(MXRoomSummary *)summary session:(MXSession *)session withServerRoomSummary:(MXRoomSyncSummary *)serverRoomSummary roomState:(MXRoomState *)roomState
{
return [self updateSummaryDisplayname:summary session:session withServerRoomSummary:serverRoomSummary roomState:roomState excludingUserIDs:@[]];
}

- (BOOL)updateSummaryDisplayname:(MXRoomSummary *)summary session:(MXSession *)session withServerRoomSummary:(MXRoomSyncSummary *)serverRoomSummary roomState:(MXRoomState *)roomState excludingUserIDs:(NSArray<NSString *> *)excludedUserIDs
{
NSString *displayname;

Expand Down Expand Up @@ -380,6 +385,11 @@ - (BOOL)updateSummaryDisplayname:(MXRoomSummary *)summary session:(MXSession *)s
memberNames = [NSMutableArray arrayWithCapacity:serverRoomSummary.heroes.count];
for (NSString *hero in serverRoomSummary.heroes)
{
if ([excludedUserIDs containsObject:hero])
{
continue;
}

NSString *memberName = [roomState.members memberName:hero];
if (!memberName)
{
Expand All @@ -399,6 +409,11 @@ - (BOOL)updateSummaryDisplayname:(MXRoomSummary *)summary session:(MXSession *)s
memberNames = [NSMutableArray arrayWithCapacity:otherMembers.count];
for (MXRoomMember *member in otherMembers)
{
if ([excludedUserIDs containsObject:member.userId])
{
continue;
}

NSString *memberName = [roomState.members memberName:member.userId];
if (memberName)
{
Expand Down Expand Up @@ -530,6 +545,11 @@ - (NSString*)fixUnexpectedEmptyRoomDisplayname:(NSUInteger)memberCount session:(
}

- (BOOL)updateSummaryAvatar:(MXRoomSummary *)summary session:(MXSession *)session withServerRoomSummary:(MXRoomSyncSummary *)serverRoomSummary roomState:(MXRoomState *)roomState
{
return [self updateSummaryAvatar:summary session:session withServerRoomSummary:serverRoomSummary roomState:roomState excludingUserIDs:@[]];
}

- (BOOL)updateSummaryAvatar:(MXRoomSummary *)summary session:(MXSession *)session withServerRoomSummary:(MXRoomSyncSummary *)serverRoomSummary roomState:(MXRoomState *)roomState excludingUserIDs:(NSArray<NSString *> *)excludedUserIDs
{
NSString *avatar;

Expand All @@ -539,17 +559,25 @@ - (BOOL)updateSummaryAvatar:(MXRoomSummary *)summary session:(MXSession *)sessio
avatar = roomState.avatar;
}
// Else, use Matrix room summaries and heroes
else if (serverRoomSummary.heroes.count == 1)
{
MXRoomMember *otherMember = [roomState.members memberWithUserId:serverRoomSummary.heroes.firstObject];
avatar = otherMember.avatarUrl;
}
// Or in case of non lazy loading or no server room summary,
// use the full room state
else if (roomState.membersCount.members == 2)
else
{
NSArray<MXRoomMember*> *otherMembers = [self sortedOtherMembersInRoomState:roomState withMatrixSession:session];
avatar = otherMembers.firstObject.avatarUrl;
NSArray<NSString *> *filteredHeroes = [self filteredHeroesFromServerRoomSummary:serverRoomSummary excludingUserIDs:excludedUserIDs];
if (filteredHeroes.count == 1)
{
MXRoomMember *otherMember = [roomState.members memberWithUserId:filteredHeroes.firstObject];
avatar = otherMember.avatarUrl;
}
// Or in case of non lazy loading or no server room summary,
// use the full room state
else
{
NSArray<MXRoomMember*> *otherMembers = [self sortedOtherMembersInRoomState:roomState withMatrixSession:session];
NSArray<MXRoomMember*> *filteredMembers = [self filteredMembersFromMembers:otherMembers excludingUserIDs:excludedUserIDs];
if (filteredMembers.count == 1)
{
avatar = filteredMembers.firstObject.avatarUrl;
}
}
}

if (avatar != summary.avatar || ![avatar isEqualToString:summary.avatar])
Expand All @@ -561,6 +589,45 @@ - (BOOL)updateSummaryAvatar:(MXRoomSummary *)summary session:(MXSession *)sessio
return NO;
}

/**
Returns the heroes from the serverRoomSummary, excluding any of the specified user IDs.
*/
- (NSArray<NSString *> *)filteredHeroesFromServerRoomSummary:(MXRoomSyncSummary *)serverRoomSummary excludingUserIDs:(NSArray<NSString *> *)excludedUserIDs
{
if (serverRoomSummary == nil)
{
return @[];
}

NSMutableArray<NSString*> *filteredHeroes = [NSMutableArray arrayWithCapacity:serverRoomSummary.heroes.count];
for (NSString *hero in serverRoomSummary.heroes)
{
if (![excludedUserIDs containsObject:hero])
{
[filteredHeroes addObject:hero];
}
}

return filteredHeroes;
}

/**
Returns the members array, excluding any members who match one of the specified user IDs.
*/
- (NSArray<MXRoomMember *> *)filteredMembersFromMembers:(NSArray<MXRoomMember *> *)members excludingUserIDs:(NSArray<NSString *> *)excludedUserIDs
{
NSMutableArray<MXRoomMember*> *filteredMembers = [NSMutableArray arrayWithCapacity:members.count];
for (MXRoomMember *member in members)
{
if (![excludedUserIDs containsObject:member.userId])
{
[filteredMembers addObject:member];
}
}

return filteredMembers;
}

- (BOOL)updateSummaryMemberCount:(MXRoomSummary *)summary session:(MXSession *)session withServerRoomSummary:(MXRoomSyncSummary *)serverRoomSummary roomState:(MXRoomState *)roomState
{

Expand Down
49 changes: 49 additions & 0 deletions MatrixSDKTests/MXRoomSummaryTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,55 @@ - (void)testDisplaynameUpdate
}];
}

- (void)testRoomDisplaynameExcludingUsers
{
[matrixSDKTestsData doMXSessionTestWithBobAndAliceInARoom:self readyToTest:^(MXSession *bobSession, MXRestClient *aliceRestClient, NSString *roomId, XCTestExpectation *expectation) {
MXRoom *room = [bobSession roomWithRoomId:roomId];
MXRoomSummary *summary = room.summary;
MXRoomSummaryUpdater *updater = (MXRoomSummaryUpdater*)bobSession.roomSummaryUpdateDelegate;

[room state:^(MXRoomState *roomState) {
// Given a room with two users.
XCTAssertEqualObjects(summary.displayname, @"mxAlice", @"A room with one other user should be given the name of that user.");

// When excluding the other user during a display name update.
[updater updateSummaryDisplayname:summary session:bobSession withServerRoomSummary:nil roomState:roomState excludingUserIDs: @[aliceRestClient.credentials.userId]];

// Then the name of the room should no longer include the other user.
XCTAssertEqualObjects(summary.displayname, @"Empty room", @"The name of the room should not include the other user when they are excluded.");
[expectation fulfill];
}];
}];
}

- (void)testRoomAvatarExcludingUsers
{
[matrixSDKTestsData doMXSessionTestWithBobAndAliceInARoom:self readyToTest:^(MXSession *bobSession, MXRestClient *aliceRestClient, NSString *roomId, XCTestExpectation *expectation) {
MXRoom *room = [bobSession roomWithRoomId:roomId];
MXRoomSummary *summary = room.summary;
MXRoomSummaryUpdater *updater = (MXRoomSummaryUpdater*)bobSession.roomSummaryUpdateDelegate;

NSString *avatarURL = @"http://matrix.org/matrix.png";

[aliceRestClient setAvatarUrl:avatarURL success:^{
[room state:^(MXRoomState *roomState) {
// Given a room with two users.
XCTAssertNotEqualObjects(summary.avatar, nil, @"A room with one other user who has set an avatar should have that same avatar.");

// When excluding the other user during an avatar update.
[updater updateSummaryAvatar:summary session:bobSession withServerRoomSummary:nil roomState:roomState excludingUserIDs: @[aliceRestClient.credentials.userId]];

// Then the room should no longer display that user's avatar.
XCTAssertEqualObjects(summary.avatar, nil, @"A room where the only other user is unimportant should not have an avatar");
[expectation fulfill];
}];
} failure:^(NSError *error) {
XCTFail(@"Cannot set up initial test conditions - error: %@", error);
[expectation fulfill];
}];
}];
}

// Check membership when:
// - the user is in the room
// - he has left it
Expand Down
1 change: 1 addition & 0 deletions changelog.d/4609.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MXRoomSummaryUpdater: Add variants of updateSummaryDisplayname and updateSummaryAvatar methods that can exclude specified user IDs.

0 comments on commit 4da919f

Please sign in to comment.