Skip to content

Commit e07a052

Browse files
committed
user [nfc]: Make the actual users Map private
This gives somewhat better encapsulation -- other code isn't expected to add or remove items from this map.
1 parent eeff380 commit e07a052

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

lib/model/store.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,10 @@ class PerAccountStore extends ChangeNotifier with EmojiStore, UserStore, Channel
438438
int get selfUserId => _users.selfUserId;
439439

440440
@override
441-
Map<int, User> get users => _users.users;
441+
User? getUser(int userId) => _users.getUser(userId);
442+
443+
@override
444+
Iterable<User> get allUsers => _users.allUsers;
442445

443446
final UserStoreImpl _users;
444447

lib/model/user.dart

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ mixin UserStore {
1313
/// For the corresponding [User] object, see [selfUser].
1414
int get selfUserId;
1515

16-
/// All known users in the realm, by [User.userId].
16+
/// The user with the given ID, if that user is known.
1717
///
18-
/// There may be other users not found in this map, for multiple reasons:
18+
/// There may be other users that are perfectly real but are
19+
/// not known to the app, for multiple reasons:
1920
///
2021
/// * The self-user may not have permission to see all the users in the
2122
/// realm, for example because the self-user is a guest.
@@ -27,33 +28,26 @@ mixin UserStore {
2728
/// Those may therefore refer to users for which we have yet to see the
2829
/// [RealmUserAddEvent], or have already handled a [RealmUserRemoveEvent].
2930
///
30-
/// Code that looks up a user in this map should therefore always handle
31+
/// Code that looks up a user here should therefore always handle
3132
/// the possibility that the user is not found (except
3233
/// where there is a specific reason to know the user should be found).
3334
/// Consider using [userDisplayName].
34-
Map<int, User> get users;
35-
36-
/// The [User] object for the "self-user",
37-
/// i.e. the account the person using this app is logged into.
38-
///
39-
/// When only the user ID is needed, see [selfUserId].
40-
User get selfUser => getUser(selfUserId)!;
41-
42-
/// The user with the given ID, if that user is known.
43-
///
44-
/// There may be perfectly real users that are not known,
45-
/// so callers must handle that possibility.
46-
/// For details, see [users].
47-
User? getUser(int userId) => users[userId];
35+
User? getUser(int userId);
4836

4937
/// All known users in the realm.
5038
///
5139
/// This may have a large number of elements, like tens of thousands.
5240
/// Consider [getUser] or other alternatives to iterating through this.
5341
///
5442
/// There may be perfectly real users which are not known
55-
/// and so are not found here. For details, see [users].
56-
Iterable<User> get allUsers => users.values;
43+
/// and so are not found here. For details, see [getUser].
44+
Iterable<User> get allUsers;
45+
46+
/// The [User] object for the "self-user",
47+
/// i.e. the account the person using this app is logged into.
48+
///
49+
/// When only the user ID is needed, see [selfUserId].
50+
User get selfUser => getUser(selfUserId)!;
5751

5852
/// The name to show the given user as in the UI, even for unknown users.
5953
///
@@ -69,7 +63,7 @@ mixin UserStore {
6963

7064
/// The name to show for the given message's sender in the UI.
7165
///
72-
/// If the user is known (see [users]), this is their current [User.fullName].
66+
/// If the user is known (see [getUser]), this is their current [User.fullName].
7367
/// If unknown, this uses the fallback value conveniently provided on the
7468
/// [Message] object itself, namely [Message.senderFullName].
7569
///
@@ -90,7 +84,7 @@ class UserStoreImpl with UserStore {
9084
UserStoreImpl({
9185
required this.selfUserId,
9286
required InitialSnapshot initialSnapshot,
93-
}) : users = Map.fromEntries(
87+
}) : _users = Map.fromEntries(
9488
initialSnapshot.realmUsers
9589
.followedBy(initialSnapshot.realmNonActiveUsers)
9690
.followedBy(initialSnapshot.crossRealmBots)
@@ -99,19 +93,24 @@ class UserStoreImpl with UserStore {
9993
@override
10094
final int selfUserId;
10195

96+
final Map<int, User> _users;
97+
98+
@override
99+
User? getUser(int userId) => _users[userId];
100+
102101
@override
103-
final Map<int, User> users;
102+
Iterable<User> get allUsers => _users.values;
104103

105104
void handleRealmUserEvent(RealmUserEvent event) {
106105
switch (event) {
107106
case RealmUserAddEvent():
108-
users[event.person.userId] = event.person;
107+
_users[event.person.userId] = event.person;
109108

110109
case RealmUserRemoveEvent():
111-
users.remove(event.userId);
110+
_users.remove(event.userId);
112111

113112
case RealmUserUpdateEvent():
114-
final user = users[event.userId];
113+
final user = _users[event.userId];
115114
if (user == null) {
116115
return; // TODO log
117116
}

test/model/store_checks.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ extension PerAccountStoreChecks on Subject<PerAccountStore> {
3939
Subject<Account> get account => has((x) => x.account, 'account');
4040
Subject<int> get selfUserId => has((x) => x.selfUserId, 'selfUserId');
4141
Subject<UserSettings?> get userSettings => has((x) => x.userSettings, 'userSettings');
42-
Subject<Map<int, User>> get users => has((x) => x.users, 'users');
4342
Subject<Map<int, ZulipStream>> get streams => has((x) => x.streams, 'streams');
4443
Subject<Map<String, ZulipStream>> get streamsByName => has((x) => x.streamsByName, 'streamsByName');
4544
Subject<Map<int, Subscription>> get subscriptions => has((x) => x.subscriptions, 'subscriptions');

0 commit comments

Comments
 (0)