From 9dc2f825a388c92fe3d124b7fe88b084a2f2cc39 Mon Sep 17 00:00:00 2001 From: Krille Date: Wed, 13 Dec 2023 14:28:08 +0100 Subject: [PATCH] refactor: Add loadHeroUsers method This makes it possible to await loading the hero users to correctly calculate displayname and avatar. --- lib/src/room.dart | 23 +++++++++++++++++++++++ test/room_test.dart | 3 +++ 2 files changed, 26 insertions(+) diff --git a/lib/src/room.dart b/lib/src/room.dart index 1ff131eac..a27147105 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -223,6 +223,29 @@ class Room { return pinned is Iterable ? pinned.map((e) => e.toString()).toList() : []; } + /// Returns the heroes as `User` objects. + /// This is very useful if you want to make sure that all users are loaded + /// from the database, that you need to correctly calculate the displayname + /// and the avatar of the room. + Future> loadHeroUsers() async { + var heroes = summary.mHeroes; + if (heroes == null) { + final directChatMatrixID = this.directChatMatrixID; + if (directChatMatrixID != null) { + heroes = [directChatMatrixID]; + } + } + + if (heroes == null) return []; + + return await Future.wait(heroes.map((hero) async => + (await requestUser( + hero, + ignoreErrors: true, + )) ?? + User(hero, room: this))); + } + /// Returns a localized displayname for this server. If the room is a groupchat /// without a name, then it will return the localized version of 'Group with Alice' instead /// of just 'Alice' to make it different to a direct chat. diff --git a/test/room_test.dart b/test/room_test.dart index 00df688f9..1d204c6fe 100644 --- a/test/room_test.dart +++ b/test/room_test.dart @@ -81,6 +81,9 @@ void main() { stateKey: '', )); + final heroUsers = await room.loadHeroUsers(); + expect(heroUsers.length, 3); + expect(room.id, id); expect(room.membership, membership); expect(room.notificationCount, notificationCount);