diff --git a/lib/src/client.dart b/lib/src/client.dart index 2cf69f80..6a43a20b 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -919,7 +919,7 @@ class Client extends MatrixApi { /// 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 /// prior value from this function - @Deprecated('Use getUserProfile() instead') + @Deprecated('Use fetchOwnProfile() instead') Future fetchOwnProfileFromServer( {bool useServerCache = false}) async { try { @@ -943,19 +943,11 @@ class Client extends MatrixApi { /// one user can have different displaynames and avatar urls in different rooms. /// This returns the profile from the first room by default, override `getFromRooms` /// to false to fetch from homeserver. - @Deprecated('User `getUserProfile(userID)` instead') Future fetchOwnProfile({ - bool getFromRooms = true, - bool cache = true, + @Deprecated('No longer supported') bool getFromRooms = true, + @Deprecated('No longer supported') bool cache = true, }) => - getProfileFromUserId( - userID!, - getFromRooms: getFromRooms, - cache: cache, - ); - - final Map _profileRoomsCache = {}; - final Map _profileServerCache = {}; + getProfileFromUserId(userID!); /// Get the combined profile information for this user. First checks for a /// non outdated cached profile before requesting from the server. Cached @@ -1006,53 +998,32 @@ class Client extends MatrixApi { final CachedStreamController onUserProfileUpdate = CachedStreamController(); - /// Get the combined profile information for this user. - /// If [getFromRooms] is true then the profile will first be searched from the - /// room memberships. This is unstable if the given user makes use of different displaynames - /// and avatars per room, which is common for some bots and bridges. - /// If [cache] is true then - /// the profile get cached for this session. Please note that then the profile may - /// become outdated if the user changes the displayname or avatar in this session. - @Deprecated('User `getUserProfile(userID)` instead') - Future getProfileFromUserId(String userId, - {bool cache = true, bool getFromRooms = true}) async { - var profile = - getFromRooms ? _profileRoomsCache[userId] : _profileServerCache[userId]; - if (cache && profile != null) { - return Profile( - userId: userId, - displayName: profile.displayname, - avatarUrl: profile.avatarUrl, + /// Get the combined profile information for this user from the server or + /// from the cache depending on the cache value. Returns a `Profile` object + /// including the given userId but without information about how outdated + /// the profile is. If you need those, try using `getUserProfile()` instead. + Future getProfileFromUserId( + String userId, { + @Deprecated('No longer supported') bool? getFromRooms, + @Deprecated('No longer supported') bool? cache, + Duration timeout = const Duration(seconds: 30), + Duration maxCacheAge = const Duration(days: 1), + }) async { + CachedProfileInformation? cachedProfileInformation; + try { + cachedProfileInformation = await getUserProfile( + userId, + timeout: timeout, + 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( userId: userId, - displayName: profile.displayname, - avatarUrl: profile.avatarUrl, + displayName: cachedProfileInformation?.displayname, + avatarUrl: cachedProfileInformation?.avatarUrl, ); } diff --git a/test/client_test.dart b/test/client_test.dart index 0aaa7e5f..1ac4f568 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -623,12 +623,17 @@ void main() { }); test('getProfileFromUserId', () async { - final profile = await matrix.getUserProfile('@getme:example.com'); - expect(profile.outdated, false); + final cachedProfile = await matrix.getUserProfile('@getme:example.com'); + 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.displayname, 'You got me'); + expect(profile.displayName, 'You got me'); + final aliceProfile = await matrix.getUserProfile('@alice:example.com'); - expect(profile.outdated, false); + expect(aliceProfile.outdated, false); expect(aliceProfile.avatarUrl.toString(), 'mxc://test'); expect(aliceProfile.displayname, 'Alice M'); await matrix.handleSync( diff --git a/test_driver/matrixsdk_test.dart b/test_driver/matrixsdk_test.dart index 1ab27024..a70c38c3 100644 --- a/test_driver/matrixsdk_test.dart +++ b/test_driver/matrixsdk_test.dart @@ -490,6 +490,9 @@ void main() => group('Integration tests', () { if (testClientB.getRoomById(dmRoom) == null) { 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 ++++'); final dmRoomFromB =