Skip to content

Commit

Permalink
Merge pull request #1953 from famedly/krille/cache-members-for-encryp…
Browse files Browse the repository at this point in the history
…ted-rooms

fix: BREAKING Cache members for encrypted rooms in database
  • Loading branch information
krille-chan authored Nov 11, 2024
2 parents 52381ce + f46e762 commit 2856338
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
53 changes: 48 additions & 5 deletions lib/fake_matrix_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1816,19 +1816,62 @@ class FakeMatrixApi extends BaseClient {
'/client/v3/rooms/!localpart%3Aserver.abc/members': (var req) => {
'chunk': [
{
'type': 'm.room.member',
'content': {'membership': 'join', 'displayname': 'YOU'},
'sender': '@test:fakeServer.notExisting',
'state_key': '@test:fakeServer.notExisting',
'room_id': '!localpart%3Aserver.abc',
'event_id': '§143273582443PhrSn2:example.org',
'origin_server_ts': 1432735824653,
'unsigned': {'age': 1234},
},
{
'type': 'm.room.member',
'content': {
'membership': 'join',
'avatar_url': 'mxc://example.org/SEsfnsuifSDFSSEF',
'displayname': 'Alice Margatroid',
},
'sender': '@alice:matrix.org',
'state_key': '@alice:matrix.org',
'room_id': '!localpart%3Aserver.abc',
'event_id': '§143273582443PhrSn3:example.org',
'origin_server_ts': 1432735824653,
'unsigned': {'age': 1234},
},
{
'type': 'm.room.member',
'event_id': '§143273582443PhrSn:example.org',
'room_id': '!636q39766251:example.com',
'sender': '@example:example.org',
'content': {'membership': 'invite', 'displayname': 'Bob'},
'sender': '@bob:example.com',
'state_key': '@bob:example.com',
'room_id': '!localpart%3Aserver.abc',
'event_id': '§143273582443PhrSn4:example.org',
'origin_server_ts': 1432735824653,
'unsigned': {'age': 1234},
},
{
'type': 'm.room.member',
'content': {'membership': 'invite', 'displayname': 'Charley'},
'sender': '@charley:example.org',
'state_key': '@charley:example.org',
'room_id': '!localpart%3Aserver.abc',
'event_id': '§143273582443PhrSn5:example.org',
'origin_server_ts': 1432735824653,
'unsigned': {'age': 1234},
},
{
'type': 'm.room.member',
'content': {
'membership': 'join',
'avatar_url': 'mxc://example.org/SEsfnsuifSDFSSEF',
'displayname': 'Alice Margatroid',
},
'sender': '@example:example.org',
'state_key': '@alice:example.org',
}
'room_id': '!localpart%3Aserver.abc',
'event_id': '§143273582443PhrSn6:example.org',
'origin_server_ts': 1432735824653,
'unsigned': {'age': 1234},
},
],
},
'/client/v3/pushrules/global/content/nocake': (var req) => {
Expand Down
14 changes: 13 additions & 1 deletion lib/src/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1607,14 +1607,16 @@ class Room {
/// [[Membership.join, Membership.invite, Membership.knock]]
/// Set [cache] to `false` if you do not want to cache the users in memory
/// for this session which is highly recommended for large public rooms.
/// By default users are only cached in encrypted rooms as encrypted rooms
/// need a full member list.
Future<List<User>> requestParticipants([
List<Membership> membershipFilter = const [
Membership.join,
Membership.invite,
Membership.knock,
],
bool suppressWarning = false,
bool cache = true,
bool? cache,
]) async {
if (!participantListComplete || partial) {
// we aren't fully loaded, maybe the users are in the database
Expand All @@ -1633,6 +1635,8 @@ class Room {
return getParticipants(membershipFilter);
}

cache ??= encrypted;

final memberCount = summary.mJoinedMemberCount;
if (!suppressWarning && cache && memberCount != null && memberCount > 100) {
Logs().w('''
Expand All @@ -1651,6 +1655,14 @@ class Room {
if (cache) {
for (final user in users) {
setState(user); // at *least* cache this in-memory
await client.database?.storeEventUpdate(
EventUpdate(
roomID: id,
type: EventUpdateType.state,
content: user.toJson(),
),
client,
);
}
}

Expand Down
27 changes: 15 additions & 12 deletions test/room_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -657,14 +657,17 @@ void main() {
});

test('requestParticipants', () async {
final participants = await room.requestParticipants();
expect(participants.length, 4);
final user = participants.singleWhere((u) => u.id == '@alice:matrix.org');
expect(user.id, '@alice:matrix.org');
expect(user.displayName, 'Alice Margatroid');
expect(user.membership, Membership.join);
//expect(user.avatarUrl.toString(), 'mxc://example.org/SEsfnsuifSDFSSEF');
expect(user.room.id, '!localpart:server.abc');
final oldParticipants = room.getParticipants();
expect(oldParticipants.length, 4);
room.summary.mJoinedMemberCount = 5;
final fetchedParticipants = await room.requestParticipants(
const [Membership.join, Membership.invite, Membership.knock],
true,
true,
);
final newParticipants = room.getParticipants();
expect(oldParticipants.length < newParticipants.length, true);
expect(fetchedParticipants.length, newParticipants.length);
});

test('calcEncryptionHealthState', () async {
Expand Down Expand Up @@ -823,7 +826,7 @@ void main() {

test('getParticipants', () async {
var userList = room.getParticipants();
expect(userList.length, 4);
expect(userList.length, 5);
// add new user
room.setState(
Event(
Expand All @@ -837,8 +840,8 @@ void main() {
),
);
userList = room.getParticipants();
expect(userList.length, 5);
expect(userList[4].displayName, 'alice');
expect(userList.length, 6);
expect(userList[5].displayName, 'alice');
});

test('addToDirectChat', () async {
Expand Down Expand Up @@ -1660,7 +1663,7 @@ void main() {
matrixToLink = await room.matrixToInviteLink();
expect(
matrixToLink.toString(),
'https://matrix.to/#/!localpart%3Aserver.abc?via=fakeServer.notExisting&via=matrix.org&via=test.abc',
'https://matrix.to/#/!localpart%3Aserver.abc?via=fakeServer.notExisting&via=matrix.org&via=example.org',
);
});

Expand Down

0 comments on commit 2856338

Please sign in to comment.