Skip to content

Commit 0e1259d

Browse files
committed
user: Add UserStore.mutedUsers and helper methods
1 parent 451ec5a commit 0e1259d

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/model/store.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,13 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
644644
@override
645645
Iterable<User> get allUsers => _users.allUsers;
646646

647+
@override
648+
List<MutedUserItem> get mutedUsers => _users.mutedUsers;
649+
650+
@override
651+
bool isUserMuted(int id, {List<MutedUserItem>? mutedUsers}) =>
652+
_users.isUserMuted(id, mutedUsers: mutedUsers);
653+
647654
final UserStoreImpl _users;
648655

649656
final TypingStatus typingStatus;

lib/model/user.dart

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import '../api/model/events.dart';
22
import '../api/model/initial_snapshot.dart';
33
import '../api/model/model.dart';
4+
import 'algorithms.dart';
45
import 'localizations.dart';
6+
import 'narrow.dart';
57
import 'store.dart';
68

79
/// The portion of [PerAccountStore] describing the users in the realm.
@@ -66,6 +68,24 @@ mixin UserStore on PerAccountStoreBase {
6668
return getUser(message.senderId)?.fullName
6769
?? message.senderFullName;
6870
}
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+
}
6989
}
7090

7191
/// The implementation of [UserStore] that does the work.
@@ -81,16 +101,31 @@ class UserStoreImpl extends PerAccountStoreBase with UserStore {
81101
initialSnapshot.realmUsers
82102
.followedBy(initialSnapshot.realmNonActiveUsers)
83103
.followedBy(initialSnapshot.crossRealmBots)
84-
.map((user) => MapEntry(user.userId, user)));
104+
.map((user) => MapEntry(user.userId, user))),
105+
mutedUsers = _sortMutedUsers(initialSnapshot.mutedUsers);
85106

86107
final Map<int, User> _users;
87108

109+
@override
110+
final List<MutedUserItem> mutedUsers;
111+
88112
@override
89113
User? getUser(int userId) => _users[userId];
90114

91115
@override
92116
Iterable<User> get allUsers => _users.values;
93117

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+
94129
void handleRealmUserEvent(RealmUserEvent event) {
95130
switch (event) {
96131
case RealmUserAddEvent():

0 commit comments

Comments
 (0)