Merge branch 'krille/calc-displayname-without-local-part' into 'master'

Implement calcDisplayname without local part

See merge request famedly/famedlysdk!373
This commit is contained in:
Christian Pauly 2020-06-29 08:40:16 +00:00
commit 4582574e5b
3 changed files with 31 additions and 7 deletions

View file

@ -1134,8 +1134,13 @@ class Room {
final Set<String> _requestingMatrixIds = {}; final Set<String> _requestingMatrixIds = {};
/// Requests a missing [User] for this room. Important for clients using /// Requests a missing [User] for this room. Important for clients using
/// lazy loading. /// lazy loading. If the user can't be found this method tries to fetch
Future<User> requestUser(String mxID, {bool ignoreErrors = false}) async { /// the displayname and avatar from the profile if [requestProfile] is true.
Future<User> requestUser(
String mxID, {
bool ignoreErrors = false,
bool requestProfile = true,
}) async {
if (getState(EventTypes.RoomMember, mxID) != null) { if (getState(EventTypes.RoomMember, mxID) != null) {
return getState(EventTypes.RoomMember, mxID).asUser; return getState(EventTypes.RoomMember, mxID).asUser;
} }
@ -1147,10 +1152,24 @@ class Room {
EventTypes.RoomMember, EventTypes.RoomMember,
mxID, mxID,
); );
} catch (exception) {
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) { } catch (exception) {
_requestingMatrixIds.remove(mxID); _requestingMatrixIds.remove(mxID);
if (!ignoreErrors) rethrow; if (!ignoreErrors) rethrow;
} }
}
if (resp == null) { if (resp == null) {
return null; return null;
} }

View file

@ -97,11 +97,14 @@ class User extends Event {
/// has no displayname. If [formatLocalpart] is true, then the localpart will /// has no displayname. If [formatLocalpart] is true, then the localpart will
/// be formatted in the way, that all "_" characters are becomming white spaces and /// be formatted in the way, that all "_" characters are becomming white spaces and
/// the first character of each word becomes uppercase. /// 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) { if (displayName?.isNotEmpty ?? false) {
return displayName; return displayName;
} }
if (stateKey != null) { if (stateKey != null && mxidLocalPartFallback) {
if (!formatLocalpart) { if (!formatLocalpart) {
return stateKey.localpart; return stateKey.localpart;
} }
@ -113,7 +116,7 @@ class User extends Event {
} }
return words.join(' '); return words.join(' ');
} }
return 'Unknown User'; return 'Unknown user';
} }
/// Call the Matrix API to kick this user from this room. /// Call the Matrix API to kick this user from this room.

View file

@ -81,6 +81,8 @@ void main() {
expect(user2.calcDisplayname(), 'SuperAlice'); expect(user2.calcDisplayname(), 'SuperAlice');
expect(user3.calcDisplayname(), 'Alice Mep'); expect(user3.calcDisplayname(), 'Alice Mep');
expect(user3.calcDisplayname(formatLocalpart: false), 'alice_mep'); expect(user3.calcDisplayname(formatLocalpart: false), 'alice_mep');
expect(
user3.calcDisplayname(mxidLocalPartFallback: false), 'Unknown user');
}); });
test('kick', () async { test('kick', () async {
await client.checkServer('https://fakeserver.notexisting'); await client.checkServer('https://fakeserver.notexisting');