From 7e9c8f88f3f2966148a4d80e54a120e9271acfff Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Mon, 29 Jun 2020 08:40:16 +0000 Subject: [PATCH] Implement calcDisplayname without local part --- lib/src/room.dart | 27 +++++++++++++++++++++++---- lib/src/user.dart | 9 ++++++--- test/user_test.dart | 2 ++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/src/room.dart b/lib/src/room.dart index 3cfe836..617c4fe 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1134,8 +1134,13 @@ class Room { final Set _requestingMatrixIds = {}; /// Requests a missing [User] for this room. Important for clients using - /// lazy loading. - Future requestUser(String mxID, {bool ignoreErrors = false}) async { + /// lazy loading. If the user can't be found this method tries to fetch + /// the displayname and avatar from the profile if [requestProfile] is true. + Future requestUser( + String mxID, { + bool ignoreErrors = false, + bool requestProfile = true, + }) async { if (getState(EventTypes.RoomMember, mxID) != null) { return getState(EventTypes.RoomMember, mxID).asUser; } @@ -1148,8 +1153,22 @@ class Room { mxID, ); } catch (exception) { - _requestingMatrixIds.remove(mxID); - if (!ignoreErrors) rethrow; + if (!ignoreErrors) { + _requestingMatrixIds.remove(mxID); + rethrow; + } + } + if (resp == null && requestProfile) { + try { + final profile = await client.api.requestProfile(mxID); + resp = { + 'displayname': profile.displayname, + 'avatar_url': profile.avatarUrl, + }; + } catch (exception) { + _requestingMatrixIds.remove(mxID); + if (!ignoreErrors) rethrow; + } } if (resp == null) { return null; diff --git a/lib/src/user.dart b/lib/src/user.dart index 7dd9840..c87ebbc 100644 --- a/lib/src/user.dart +++ b/lib/src/user.dart @@ -97,11 +97,14 @@ class User extends Event { /// has no displayname. If [formatLocalpart] is true, then the localpart will /// be formatted in the way, that all "_" characters are becomming white spaces and /// the first character of each word becomes uppercase. - String calcDisplayname({bool formatLocalpart = true}) { + /// If [mxidLocalPartFallback] is true, then the local part of the mxid will be shown + /// if there is no other displayname available. If not then this will return "Unknown user". + String calcDisplayname( + {bool formatLocalpart = true, bool mxidLocalPartFallback = true}) { if (displayName?.isNotEmpty ?? false) { return displayName; } - if (stateKey != null) { + if (stateKey != null && mxidLocalPartFallback) { if (!formatLocalpart) { return stateKey.localpart; } @@ -113,7 +116,7 @@ class User extends Event { } return words.join(' '); } - return 'Unknown User'; + return 'Unknown user'; } /// Call the Matrix API to kick this user from this room. diff --git a/test/user_test.dart b/test/user_test.dart index 13e67b2..05e415b 100644 --- a/test/user_test.dart +++ b/test/user_test.dart @@ -81,6 +81,8 @@ void main() { expect(user2.calcDisplayname(), 'SuperAlice'); expect(user3.calcDisplayname(), 'Alice Mep'); expect(user3.calcDisplayname(formatLocalpart: false), 'alice_mep'); + expect( + user3.calcDisplayname(mxidLocalPartFallback: false), 'Unknown user'); }); test('kick', () async { await client.checkServer('https://fakeserver.notexisting');