Skip to content

Commit 8b172ba

Browse files
committed
user [nfc]: Introduce senderDisplayName
1 parent de98200 commit 8b172ba

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lib/model/user.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,26 @@ mixin UserStore {
4343
///
4444
/// This is the user's [User.fullName] if the user is known,
4545
/// and otherwise a translation of "(unknown user)".
46+
///
47+
/// When a [Message] is available which the user sent,
48+
/// use [senderDisplayName] instead for a better-informed fallback.
4649
String userDisplayName(int userId) {
4750
return users[userId]?.fullName
4851
?? GlobalLocalizations.zulipLocalizations.unknownUserName;
4952
}
53+
54+
/// The name to show for the given message's sender in the UI.
55+
///
56+
/// If the user is known (see [users]), this is their current [User.fullName].
57+
/// If unknown, this uses the fallback value conveniently provided on the
58+
/// [Message] object itself, namely [Message.senderFullName].
59+
///
60+
/// For a user who isn't the sender of some known message,
61+
/// see [userDisplayName].
62+
String senderDisplayName(Message message) {
63+
return users[message.senderId]?.fullName
64+
?? message.senderFullName;
65+
}
5066
}
5167

5268
/// The implementation of [UserStore] that does the work.

test/model/user_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,36 @@ void main() {
2222
});
2323
});
2424

25+
group('senderDisplayName', () {
26+
test('on a known user', () async {
27+
final store = eg.store();
28+
final user = eg.user(fullName: 'Old Name');
29+
await store.addUser(user);
30+
final message = eg.streamMessage(sender: user);
31+
await store.addMessage(message);
32+
check(store.senderDisplayName(message)).equals('Old Name');
33+
34+
// If the user's name changes, `store.senderDisplayName` should update...
35+
await store.handleEvent(RealmUserUpdateEvent(id: 1,
36+
userId: user.userId, fullName: 'New Name'));
37+
check(store.senderDisplayName(message)).equals('New Name');
38+
// ... even though the Message object itself still has the old name.
39+
check(store.messages[message.id]!).senderFullName.equals('Old Name');
40+
});
41+
42+
test('on an unknown user', () async {
43+
final store = eg.store();
44+
final message = eg.streamMessage(sender: eg.user(fullName: 'Some User'));
45+
await store.addMessage(message);
46+
// If the user is unknown, `store.senderDisplayName` should fall back
47+
// to the name in the message...
48+
check(store.senderDisplayName(message)).equals('Some User');
49+
// ... even though `store.userDisplayName` (with no message available
50+
// for fallback) only has a generic fallback name.
51+
check(store.userDisplayName(message.senderId)).equals('(unknown user)');
52+
});
53+
});
54+
2555
group('RealmUserUpdateEvent', () {
2656
// TODO write more tests for handling RealmUserUpdateEvent
2757

0 commit comments

Comments
 (0)