diff --git a/lib/src/client.dart b/lib/src/client.dart index 0ce927f3..68a57931 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -726,23 +726,15 @@ class Client extends MatrixApi { /// one user can have different displaynames and avatar urls in different rooms. So /// this endpoint first checks if the profile is the same in all rooms. If not, the /// profile will be requested from the homserver. - Future fetchOwnProfile() async { - if (rooms.isNotEmpty) { - final profileSet = {}; - for (final room in rooms) { - final user = await room.requestUser(userID!); - if (user != null) { - profileSet.add(Profile( - avatarUrl: user.avatarUrl, - displayName: user.displayName, - userId: user.id, - )); - } - } - if (profileSet.length == 1) return profileSet.single; - } - return getProfileFromUserId(userID!); - } + Future fetchOwnProfile({ + bool getFromRooms = true, + bool cache = true, + }) => + getProfileFromUserId( + userID!, + getFromRooms: getFromRooms, + cache: cache, + ); final Map _profileCache = {}; @@ -755,6 +747,14 @@ class Client extends MatrixApi { /// become outdated if the user changes the displayname or avatar in this session. Future getProfileFromUserId(String userId, {bool cache = true, bool getFromRooms = true}) async { + var profile = _profileCache[userId]; + if (cache && profile != null) { + return Profile( + userId: userId, + displayName: profile.displayname, + avatarUrl: profile.avatarUrl); + } + if (getFromRooms) { final room = rooms.firstWhereOrNull((Room room) => room.getParticipants().indexWhere((User user) => user.id == userId) != @@ -768,16 +768,8 @@ class Client extends MatrixApi { avatarUrl: user.avatarUrl); } } - - var profile = _profileCache[userId]; - if (cache && profile != null) { - return Profile( - userId: userId, - displayName: profile.displayname, - avatarUrl: profile.avatarUrl); - } profile = await getUserProfile(userId); - _profileCache[userId] = profile; + if (cache) _profileCache[userId] = profile; return Profile( userId: userId, displayName: profile.displayname, diff --git a/test/fake_matrix_api.dart b/test/fake_matrix_api.dart index 0497cf4f..4d44380e 100644 --- a/test/fake_matrix_api.dart +++ b/test/fake_matrix_api.dart @@ -68,7 +68,7 @@ class FakeMatrixApi extends MockClient { dynamic res = {}; var statusCode = 200; - //print('\$method request to $action with Data: $data'); + //print('$method request to $action with Data: $data'); // Sync requests with timeout if (data is Map && data['timeout'] is String) { @@ -1191,6 +1191,8 @@ class FakeMatrixApi extends MockClient { 'last_seen_ip': '1.2.3.4', 'last_seen_ts': 1474491775024 }, + '/client/v3/profile/%40test%3AfakeServer.notExisting': (var reqI) => + {'displayname': 'Some First Name Some Last Name'}, '/client/v3/profile/%40alice%3Aexample.com/displayname': (var reqI) => {'displayname': 'Alice M'}, '/client/v3/profile/%40alice%3Aexample.com/avatar_url': (var reqI) =>