chore: add useServerCache option to fetchOwnProfileFromServer and fix missing awaits
This commit is contained in:
parent
c26d29ee14
commit
f7c7cb82de
|
|
@ -757,21 +757,23 @@ class Client extends MatrixApi {
|
||||||
/// Returns the user's own displayname and avatar url. In Matrix it is possible that
|
/// Returns the user's own displayname and avatar url. In Matrix it is possible that
|
||||||
/// one user can have different displaynames and avatar urls in different rooms.
|
/// one user can have different displaynames and avatar urls in different rooms.
|
||||||
/// Tries to get the profile from homeserver first, if failed, falls back to a profile
|
/// Tries to get the profile from homeserver first, if failed, falls back to a profile
|
||||||
/// from a room where the user exists.
|
/// from a room where the user exists. Set `useServerCache` to true to get any
|
||||||
Future<Profile> fetchOwnProfileFromServer() async {
|
/// prior value from this function
|
||||||
|
Future<Profile> fetchOwnProfileFromServer(
|
||||||
|
{bool useServerCache = false}) async {
|
||||||
try {
|
try {
|
||||||
return getProfileFromUserId(
|
return await getProfileFromUserId(
|
||||||
userID!,
|
userID!,
|
||||||
getFromRooms: false,
|
getFromRooms: false,
|
||||||
cache: false,
|
cache: useServerCache,
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
Logs().w(
|
Logs().w(
|
||||||
'[Matrix] getting profile from homeserver failed, falling back to first room with required profile');
|
'[Matrix] getting profile from homeserver failed, falling back to first room with required profile');
|
||||||
return getProfileFromUserId(
|
return await getProfileFromUserId(
|
||||||
userID!,
|
userID!,
|
||||||
getFromRooms: true,
|
getFromRooms: true,
|
||||||
cache: false,
|
cache: true,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -790,7 +792,8 @@ class Client extends MatrixApi {
|
||||||
cache: cache,
|
cache: cache,
|
||||||
);
|
);
|
||||||
|
|
||||||
final Map<String, ProfileInformation> _profileCache = {};
|
final Map<String, ProfileInformation> _profileRoomsCache = {};
|
||||||
|
final Map<String, ProfileInformation> _profileServerCache = {};
|
||||||
|
|
||||||
/// Get the combined profile information for this user.
|
/// Get the combined profile information for this user.
|
||||||
/// If [getFromRooms] is true then the profile will first be searched from the
|
/// If [getFromRooms] is true then the profile will first be searched from the
|
||||||
|
|
@ -801,12 +804,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];
|
var profile =
|
||||||
|
getFromRooms ? _profileRoomsCache[userId] : _profileServerCache[userId];
|
||||||
if (cache && profile != null) {
|
if (cache && profile != null) {
|
||||||
return Profile(
|
return Profile(
|
||||||
userId: userId,
|
userId: userId,
|
||||||
displayName: profile.displayname,
|
displayName: profile.displayname,
|
||||||
avatarUrl: profile.avatarUrl);
|
avatarUrl: profile.avatarUrl,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getFromRooms) {
|
if (getFromRooms) {
|
||||||
|
|
@ -816,20 +821,27 @@ class Client extends MatrixApi {
|
||||||
if (room != null) {
|
if (room != null) {
|
||||||
final user =
|
final user =
|
||||||
room.getParticipants().firstWhere((User user) => user.id == userId);
|
room.getParticipants().firstWhere((User user) => user.id == userId);
|
||||||
return Profile(
|
final profileFromRooms = Profile(
|
||||||
userId: userId,
|
userId: userId,
|
||||||
displayName: user.displayName,
|
displayName: user.displayName,
|
||||||
avatarUrl: user.avatarUrl);
|
avatarUrl: user.avatarUrl,
|
||||||
|
);
|
||||||
|
_profileRoomsCache[userId] = ProfileInformation(
|
||||||
|
avatarUrl: profileFromRooms.avatarUrl,
|
||||||
|
displayname: profileFromRooms.displayName,
|
||||||
|
);
|
||||||
|
return profileFromRooms;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
profile = await getUserProfile(userId);
|
profile = await getUserProfile(userId);
|
||||||
if (cache || _profileCache.containsKey(userId)) {
|
if (cache || _profileServerCache.containsKey(userId)) {
|
||||||
_profileCache[userId] = profile;
|
_profileServerCache[userId] = profile;
|
||||||
}
|
}
|
||||||
return Profile(
|
return Profile(
|
||||||
userId: userId,
|
userId: userId,
|
||||||
displayName: profile.displayname,
|
displayName: profile.displayname,
|
||||||
avatarUrl: profile.avatarUrl);
|
avatarUrl: profile.avatarUrl,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final List<ArchivedRoom> _archivedRooms = [];
|
final List<ArchivedRoom> _archivedRooms = [];
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue