From ff359aedff2ed299ee35c05986601cb5cbb2b943 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sun, 10 Jul 2022 13:32:33 +0200 Subject: [PATCH] refactor: Better fetch own profile This makes it possible to pass cache and getFromRooms variable in fetchOwnProfile. It also removes the never working and duplicated check from rooms. The Profiles can be duplicated in the Set because they are equal but not the same objects. In the get profile method, it now checks the cache first before the rooms as the cache might have the more accurate result. This makes displaying an avatar change much easier as it makes it possible to just disable cache then. --- lib/src/client.dart | 44 ++++++++++++++++----------------------- test/fake_matrix_api.dart | 4 +++- 2 files changed, 21 insertions(+), 27 deletions(-) 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) =>