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.
This commit is contained in:
Christian Pauly 2022-07-10 13:32:33 +02:00
parent 8217532ba7
commit ff359aedff
2 changed files with 21 additions and 27 deletions

View File

@ -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<Profile> fetchOwnProfile() async {
if (rooms.isNotEmpty) {
final profileSet = <Profile>{};
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<Profile> fetchOwnProfile({
bool getFromRooms = true,
bool cache = true,
}) =>
getProfileFromUserId(
userID!,
getFromRooms: getFromRooms,
cache: cache,
);
final Map<String, ProfileInformation> _profileCache = {};
@ -755,6 +747,14 @@ class Client extends MatrixApi {
/// become outdated if the user changes the displayname or avatar in this session.
Future<Profile> 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,

View File

@ -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<String, dynamic> && 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) =>