From 809fe9e6214f3abc110563350432a137a9cff818 Mon Sep 17 00:00:00 2001 From: Henri Carnot Date: Thu, 24 Mar 2022 08:04:17 +0000 Subject: [PATCH] fix: room members loading States were used before being fetched from the database. Thus, room membership states weren't set, and so, user display names weren't be fetched from the database. --- lib/src/database/fluffybox_database.dart | 70 ++++++++++++------------ 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/lib/src/database/fluffybox_database.dart b/lib/src/database/fluffybox_database.dart index 720cf1a4..4aec0401 100644 --- a/lib/src/database/fluffybox_database.dart +++ b/lib/src/database/fluffybox_database.dart @@ -468,35 +468,10 @@ class FluffyBoxDatabase extends DatabaseApi { final getRoomStateRequests = >{}; final getRoomMembersRequests = >{}; + for (final raw in rawRooms.values) { // Get the room final room = Room.fromJson(copyMap(raw), client); - - final membersToPostload = {if (userID != null) userID}; - - // If the room is a direct chat, those IDs should be there too - if (room.isDirectChat) { - membersToPostload - .add(TupleKey(room.id, room.directChatMatrixID!).toString()); - } - // the lastEvent message preview might have an author we need to fetch, if it is a group chat - final lastEvent = room.getState(EventTypes.Message); - if (lastEvent != null && !room.isDirectChat) { - membersToPostload - .add(TupleKey(room.id, lastEvent.senderId).toString()); - } - // if the room has no name and no canonical alias, its name is calculated - // based on the heroes of the room - if (room.getState(EventTypes.RoomName) == null && - room.getState(EventTypes.RoomCanonicalAlias) == null) { - // we don't have a name and no canonical alias, so we'll need to - // post-load the heroes - final heroes = room.summary.mHeroes; - if (heroes != null) { - heroes.forEach((hero) => membersToPostload.add(hero)); - } - } - // Get the "important" room states. All other states will be loaded once // `getUnimportantRoomStates()` is called. final dbKeys = client.importantStateEvents @@ -506,14 +481,6 @@ class FluffyBoxDatabase extends DatabaseApi { dbKeys, ); - // Load members - final membersDbKeys = membersToPostload - .map((member) => TupleKey(room.id, member).toString()) - .toList(); - getRoomMembersRequests[room.id] = _roomMembersBox.getAll( - membersDbKeys, - ); - // Add to the list and continue. rooms[room.id] = room; } @@ -531,8 +498,41 @@ class FluffyBoxDatabase extends DatabaseApi { room.setState(state); } } - } + // now that we have the state we can continue + final membersToPostload = {if (userID != null) userID}; + // If the room is a direct chat, those IDs should be there too + if (room.isDirectChat) { + membersToPostload.add(room.directChatMatrixID!); + } + + // the lastEvent message preview might have an author we need to fetch, if it is a group chat + if (room.lastEvent != null && !room.isDirectChat) { + membersToPostload.add(room.lastEvent!.senderId); + } + + // if the room has no name and no canonical alias, its name is calculated + // based on the heroes of the room + if (room.getState(EventTypes.RoomName) == null && + room.getState(EventTypes.RoomCanonicalAlias) == null) { + // we don't have a name and no canonical alias, so we'll need to + // post-load the heroes + final heroes = room.summary.mHeroes; + if (heroes != null) { + heroes.forEach((hero) => membersToPostload.add(hero)); + } + } + // Load members + final membersDbKeys = membersToPostload + .map((member) => TupleKey(room.id, member).toString()) + .toList(); + getRoomMembersRequests[room.id] = _roomMembersBox.getAll( + membersDbKeys, + ); + } + } + + for (final room in rooms.values) { // Add members to the room final members = await getRoomMembersRequests[room.id]; if (members != null) {