fix: Synapse CI job failing because invite state not completely synced

This commit is contained in:
Krille 2024-07-23 09:41:06 +02:00
parent cabf357cf7
commit e7802bd20b
No known key found for this signature in database
GPG Key ID: E067ECD60F1A0652
3 changed files with 37 additions and 58 deletions

View File

@ -919,7 +919,7 @@ class Client extends MatrixApi {
/// 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. Set `useServerCache` to true to get any /// from a room where the user exists. Set `useServerCache` to true to get any
/// prior value from this function /// prior value from this function
@Deprecated('Use getUserProfile() instead') @Deprecated('Use fetchOwnProfile() instead')
Future<Profile> fetchOwnProfileFromServer( Future<Profile> fetchOwnProfileFromServer(
{bool useServerCache = false}) async { {bool useServerCache = false}) async {
try { try {
@ -943,19 +943,11 @@ class Client extends MatrixApi {
/// one user can have different displaynames and avatar urls in different rooms. /// one user can have different displaynames and avatar urls in different rooms.
/// This returns the profile from the first room by default, override `getFromRooms` /// This returns the profile from the first room by default, override `getFromRooms`
/// to false to fetch from homeserver. /// to false to fetch from homeserver.
@Deprecated('User `getUserProfile(userID)` instead')
Future<Profile> fetchOwnProfile({ Future<Profile> fetchOwnProfile({
bool getFromRooms = true, @Deprecated('No longer supported') bool getFromRooms = true,
bool cache = true, @Deprecated('No longer supported') bool cache = true,
}) => }) =>
getProfileFromUserId( getProfileFromUserId(userID!);
userID!,
getFromRooms: getFromRooms,
cache: cache,
);
final Map<String, ProfileInformation> _profileRoomsCache = {};
final Map<String, ProfileInformation> _profileServerCache = {};
/// Get the combined profile information for this user. First checks for a /// Get the combined profile information for this user. First checks for a
/// non outdated cached profile before requesting from the server. Cached /// non outdated cached profile before requesting from the server. Cached
@ -1006,53 +998,32 @@ class Client extends MatrixApi {
final CachedStreamController<String> onUserProfileUpdate = final CachedStreamController<String> onUserProfileUpdate =
CachedStreamController<String>(); CachedStreamController<String>();
/// Get the combined profile information for this user. /// Get the combined profile information for this user from the server or
/// If [getFromRooms] is true then the profile will first be searched from the /// from the cache depending on the cache value. Returns a `Profile` object
/// room memberships. This is unstable if the given user makes use of different displaynames /// including the given userId but without information about how outdated
/// and avatars per room, which is common for some bots and bridges. /// the profile is. If you need those, try using `getUserProfile()` instead.
/// If [cache] is true then Future<Profile> getProfileFromUserId(
/// the profile get cached for this session. Please note that then the profile may String userId, {
/// become outdated if the user changes the displayname or avatar in this session. @Deprecated('No longer supported') bool? getFromRooms,
@Deprecated('User `getUserProfile(userID)` instead') @Deprecated('No longer supported') bool? cache,
Future<Profile> getProfileFromUserId(String userId, Duration timeout = const Duration(seconds: 30),
{bool cache = true, bool getFromRooms = true}) async { Duration maxCacheAge = const Duration(days: 1),
var profile = }) async {
getFromRooms ? _profileRoomsCache[userId] : _profileServerCache[userId]; CachedProfileInformation? cachedProfileInformation;
if (cache && profile != null) { try {
return Profile( cachedProfileInformation = await getUserProfile(
userId: userId, userId,
displayName: profile.displayname, timeout: timeout,
avatarUrl: profile.avatarUrl, maxCacheAge: maxCacheAge,
); );
} catch (e) {
Logs().d('Unable to fetch profile for $userId', e);
} }
if (getFromRooms) {
final room = rooms.firstWhereOrNull((Room room) =>
room.getParticipants().indexWhere((User user) => user.id == userId) !=
-1);
if (room != null) {
final user =
room.getParticipants().firstWhere((User user) => user.id == userId);
final profileFromRooms = Profile(
userId: userId,
displayName: user.displayName,
avatarUrl: user.avatarUrl,
);
_profileRoomsCache[userId] = ProfileInformation(
avatarUrl: profileFromRooms.avatarUrl,
displayname: profileFromRooms.displayName,
);
return profileFromRooms;
}
}
profile = await super.getUserProfile(userId);
if (cache || _profileServerCache.containsKey(userId)) {
_profileServerCache[userId] = profile;
}
return Profile( return Profile(
userId: userId, userId: userId,
displayName: profile.displayname, displayName: cachedProfileInformation?.displayname,
avatarUrl: profile.avatarUrl, avatarUrl: cachedProfileInformation?.avatarUrl,
); );
} }

View File

@ -623,12 +623,17 @@ void main() {
}); });
test('getProfileFromUserId', () async { test('getProfileFromUserId', () async {
final profile = await matrix.getUserProfile('@getme:example.com'); final cachedProfile = await matrix.getUserProfile('@getme:example.com');
expect(profile.outdated, false); expect(cachedProfile.outdated, false);
expect(cachedProfile.avatarUrl.toString(), 'mxc://test');
expect(cachedProfile.displayname, 'You got me');
final profile = await matrix.getProfileFromUserId('@getme:example.com');
expect(profile.avatarUrl.toString(), 'mxc://test'); expect(profile.avatarUrl.toString(), 'mxc://test');
expect(profile.displayname, 'You got me'); expect(profile.displayName, 'You got me');
final aliceProfile = await matrix.getUserProfile('@alice:example.com'); final aliceProfile = await matrix.getUserProfile('@alice:example.com');
expect(profile.outdated, false); expect(aliceProfile.outdated, false);
expect(aliceProfile.avatarUrl.toString(), 'mxc://test'); expect(aliceProfile.avatarUrl.toString(), 'mxc://test');
expect(aliceProfile.displayname, 'Alice M'); expect(aliceProfile.displayname, 'Alice M');
await matrix.handleSync( await matrix.handleSync(

View File

@ -490,6 +490,9 @@ void main() => group('Integration tests', () {
if (testClientB.getRoomById(dmRoom) == null) { if (testClientB.getRoomById(dmRoom) == null) {
await testClientB.waitForRoomInSync(dmRoom, invite: true); await testClientB.waitForRoomInSync(dmRoom, invite: true);
} }
// Wait at least for one additional sync to make sure the invite landed
// correctly. Workaround for synapse CI job failing.
await testClientB.onSync.stream.first;
Logs().i('++++ (Bob) Create DM ++++'); Logs().i('++++ (Bob) Create DM ++++');
final dmRoomFromB = final dmRoomFromB =