1
1
import '../api/model/events.dart' ;
2
2
import '../api/model/initial_snapshot.dart' ;
3
3
import '../api/model/model.dart' ;
4
+ import 'algorithms.dart' ;
4
5
import 'localizations.dart' ;
6
+ import 'narrow.dart' ;
5
7
import 'store.dart' ;
6
8
7
9
/// The portion of [PerAccountStore] describing the users in the realm.
@@ -66,6 +68,24 @@ mixin UserStore on PerAccountStoreBase {
66
68
return getUser (message.senderId)? .fullName
67
69
?? message.senderFullName;
68
70
}
71
+
72
+ /// All the users muted by [selfUser] , sorted by [MutedUserItem.id] ascending.
73
+ List <MutedUserItem > get mutedUsers;
74
+
75
+ /// Whether the user with the given [id] is muted by [selfUser] .
76
+ ///
77
+ /// By default, looks for the user id in [UserStore.mutedUsers] unless
78
+ /// [mutedUsers] is non-null, in which case looks in the latter.
79
+ bool isUserMuted (int id, {List <MutedUserItem >? mutedUsers});
80
+
81
+ /// Whether all of the users corresponding to [DmNarrow.otherRecipientIds]
82
+ /// are muted by [selfUser] ;
83
+ ///
84
+ /// By default, looks for the recipients in [UserStore.mutedUsers] unless
85
+ /// [mutedUsers] is non-null, in which case looks in the latter.
86
+ bool allRecipientsMuted (DmNarrow narrow, {List <MutedUserItem >? mutedUsers}) {
87
+ return ! narrow.otherRecipientIds.any ((id) => ! isUserMuted (id, mutedUsers: mutedUsers));
88
+ }
69
89
}
70
90
71
91
/// The implementation of [UserStore] that does the work.
@@ -81,16 +101,31 @@ class UserStoreImpl extends PerAccountStoreBase with UserStore {
81
101
initialSnapshot.realmUsers
82
102
.followedBy (initialSnapshot.realmNonActiveUsers)
83
103
.followedBy (initialSnapshot.crossRealmBots)
84
- .map ((user) => MapEntry (user.userId, user)));
104
+ .map ((user) => MapEntry (user.userId, user))),
105
+ mutedUsers = _sortMutedUsers (initialSnapshot.mutedUsers);
85
106
86
107
final Map <int , User > _users;
87
108
109
+ @override
110
+ final List <MutedUserItem > mutedUsers;
111
+
88
112
@override
89
113
User ? getUser (int userId) => _users[userId];
90
114
91
115
@override
92
116
Iterable <User > get allUsers => _users.values;
93
117
118
+ @override
119
+ bool isUserMuted (int id, {List <MutedUserItem >? mutedUsers}) {
120
+ return binarySearchByKey (
121
+ mutedUsers == null ? this .mutedUsers : _sortMutedUsers (mutedUsers), id,
122
+ (item, id) => item.id.compareTo (id)) >= 0 ;
123
+ }
124
+
125
+ static List <MutedUserItem > _sortMutedUsers (List <MutedUserItem > mutedUsers) {
126
+ return mutedUsers..sort ((a, b) => a.id.compareTo (b.id));
127
+ }
128
+
94
129
void handleRealmUserEvent (RealmUserEvent event) {
95
130
switch (event) {
96
131
case RealmUserAddEvent ():
0 commit comments