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('');
/// Returns the displayname or the local part of the Matrix ID if the user
/// has no displayname.
String calcDisplayname() => (displayName == null || displayName.isEmpty)
? (stateKey != null ? stateKey.localpart : 'Unknown User')
: displayName;
/// 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 (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.
Future<void> kick() => room.kick(id);

View file

@ -109,7 +109,7 @@ void main() {
expect(room.mJoinedMemberCount, notificationCount);
expect(room.mInvitedMemberCount, notificationCount);
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.roomAccountData['com.test.foo'].content['foo'], 'bar');

View file

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