Merge branch 'krille/better-fetch-own-profile' into 'main'

refactor: Better fetch own profile

See merge request famedly/company/frontend/famedlysdk!1075
This commit is contained in:
Krille Fear 2022-07-11 06:40:34 +00:00
commit 39152bcfb1
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 /// 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 /// this endpoint first checks if the profile is the same in all rooms. If not, the
/// profile will be requested from the homserver. /// profile will be requested from the homserver.
Future<Profile> fetchOwnProfile() async { Future<Profile> fetchOwnProfile({
if (rooms.isNotEmpty) { bool getFromRooms = true,
final profileSet = <Profile>{}; bool cache = true,
for (final room in rooms) { }) =>
final user = await room.requestUser(userID!); getProfileFromUserId(
if (user != null) { userID!,
profileSet.add(Profile( getFromRooms: getFromRooms,
avatarUrl: user.avatarUrl, cache: cache,
displayName: user.displayName, );
userId: user.id,
));
}
}
if (profileSet.length == 1) return profileSet.single;
}
return getProfileFromUserId(userID!);
}
final Map<String, ProfileInformation> _profileCache = {}; 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. /// become outdated if the user changes the displayname or avatar in this session.
Future<Profile> getProfileFromUserId(String userId, Future<Profile> getProfileFromUserId(String userId,
{bool cache = true, bool getFromRooms = true}) async { {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) { if (getFromRooms) {
final room = rooms.firstWhereOrNull((Room room) => final room = rooms.firstWhereOrNull((Room room) =>
room.getParticipants().indexWhere((User user) => user.id == userId) != room.getParticipants().indexWhere((User user) => user.id == userId) !=
@ -768,16 +768,8 @@ class Client extends MatrixApi {
avatarUrl: user.avatarUrl); 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); profile = await getUserProfile(userId);
_profileCache[userId] = profile; if (cache) _profileCache[userId] = profile;
return Profile( return Profile(
userId: userId, userId: userId,
displayName: profile.displayname, displayName: profile.displayname,

View File

@ -68,7 +68,7 @@ class FakeMatrixApi extends MockClient {
dynamic res = {}; dynamic res = {};
var statusCode = 200; var statusCode = 200;
//print('\$method request to $action with Data: $data'); //print('$method request to $action with Data: $data');
// Sync requests with timeout // Sync requests with timeout
if (data is Map<String, dynamic> && data['timeout'] is String) { 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_ip': '1.2.3.4',
'last_seen_ts': 1474491775024 '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) => '/client/v3/profile/%40alice%3Aexample.com/displayname': (var reqI) =>
{'displayname': 'Alice M'}, {'displayname': 'Alice M'},
'/client/v3/profile/%40alice%3Aexample.com/avatar_url': (var reqI) => '/client/v3/profile/%40alice%3Aexample.com/avatar_url': (var reqI) =>