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:
commit
39152bcfb1
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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) =>
|
||||
|
|
|
|||
Loading…
Reference in New Issue