Merge branch 'user-feature-format-displayname' into 'master'

[User] Format displayname

See merge request famedly/famedlysdk!258
This commit is contained in:
Christian Pauly 2020-04-23 09:46:10 +00:00
commit e8611217e4
3 changed files with 25 additions and 8 deletions

View file

@ -101,10 +101,27 @@ class User extends Event {
: MxContent(''); : MxContent('');
/// Returns the displayname or the local part of the Matrix ID if the user /// Returns the displayname or the local part of the Matrix ID if the user
/// has no displayname. /// has no displayname. If [formatLocalpart] is true, then the localpart will
String calcDisplayname() => (displayName == null || displayName.isEmpty) /// be formatted in the way, that all "_" characters are becomming white spaces and
? (stateKey != null ? stateKey.localpart : 'Unknown User') /// the first character of each word becomes uppercase.
: displayName; String calcDisplayname({bool formatLocalpart = true}) {
if (displayName?.isNotEmpty ?? false) {
return displayName;
}
if (stateKey != null) {
if (!formatLocalpart) {
return stateKey.localpart;
}
var words = stateKey.localpart.replaceAll('_', ' ').split(' ');
for (var i = 0; i < words.length; i++) {
if (words[i].isNotEmpty) {
words[i] = words[i][0].toUpperCase() + words[i].substring(1);
}
}
return words.join(' ');
}
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.
Future<void> kick() => room.kick(id); Future<void> kick() => room.kick(id);

View file

@ -109,7 +109,7 @@ void main() {
expect(room.mJoinedMemberCount, notificationCount); expect(room.mJoinedMemberCount, notificationCount);
expect(room.mInvitedMemberCount, notificationCount); expect(room.mInvitedMemberCount, notificationCount);
expect(room.mHeroes, heroes); expect(room.mHeroes, heroes);
expect(room.displayname, 'alice, bob, charley'); expect(room.displayname, 'Alice, Bob, Charley');
expect(room.getState('m.room.join_rules').content['join_rule'], 'public'); expect(room.getState('m.room.join_rules').content['join_rule'], 'public');
expect(room.roomAccountData['com.test.foo'].content['foo'], 'bar'); expect(room.roomAccountData['com.test.foo'].content['foo'], 'bar');

View file

@ -61,10 +61,10 @@ void main() {
test('calcDisplayname', () async { test('calcDisplayname', () async {
final user1 = User('@alice:example.com'); final user1 = User('@alice:example.com');
final user2 = User('@SuperAlice:example.com'); final user2 = User('@SuperAlice:example.com');
final user3 = User('@alice:example.com'); final user3 = User('@alice_mep:example.com');
expect(user1.calcDisplayname(), 'alice'); expect(user1.calcDisplayname(), 'Alice');
expect(user2.calcDisplayname(), 'SuperAlice'); expect(user2.calcDisplayname(), 'SuperAlice');
expect(user3.calcDisplayname(), 'alice'); expect(user3.calcDisplayname(), 'Alice Mep');
}); });
}); });
} }