Skip to content

Commit bda46dd

Browse files
committed
user: Add UserStore.isUserMuted
1 parent 2078174 commit bda46dd

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/model/store.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,9 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
630630
@override
631631
Iterable<User> get allUsers => _users.allUsers;
632632

633+
@override
634+
bool isUserMuted(int id) => _users.isUserMuted(id);
635+
633636
final UserStoreImpl _users;
634637

635638
final TypingStatus typingStatus;

lib/model/user.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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';
56
import 'store.dart';
67

@@ -66,6 +67,9 @@ mixin UserStore on PerAccountStoreBase {
6667
return getUser(message.senderId)?.fullName
6768
?? message.senderFullName;
6869
}
70+
71+
/// Whether the user with the given [id] is muted by [selfUser].
72+
bool isUserMuted(int id);
6973
}
7074

7175
/// The implementation of [UserStore] that does the work.
@@ -81,16 +85,32 @@ class UserStoreImpl extends PerAccountStoreBase with UserStore {
8185
initialSnapshot.realmUsers
8286
.followedBy(initialSnapshot.realmNonActiveUsers)
8387
.followedBy(initialSnapshot.crossRealmBots)
84-
.map((user) => MapEntry(user.userId, user)));
88+
.map((user) => MapEntry(user.userId, user))),
89+
_mutedUsers = initialSnapshot.mutedUsers,
90+
_mutedUsersSorted = initialSnapshot.mutedUsers
91+
..sort((a, b) => a.id.compareTo(b.id));
8592

8693
final Map<int, User> _users;
8794

95+
// Currently we don't need this, but we would need it if we wanted to display
96+
// muted users in the order of server.
97+
// ignore: unused_field
98+
final List<MutedUserItem> _mutedUsers;
99+
100+
final List<MutedUserItem> _mutedUsersSorted;
101+
88102
@override
89103
User? getUser(int userId) => _users[userId];
90104

91105
@override
92106
Iterable<User> get allUsers => _users.values;
93107

108+
@override
109+
bool isUserMuted(int id) {
110+
return binarySearchByKey(_mutedUsersSorted, id,
111+
(item, id) => item.id.compareTo(id)) >= 0;
112+
}
113+
94114
void handleRealmUserEvent(RealmUserEvent event) {
95115
switch (event) {
96116
case RealmUserAddEvent():

0 commit comments

Comments
 (0)