Skip to content

Muted users prep #1530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified assets/icons/ZulipIcons.ttf
Binary file not shown.
3 changes: 3 additions & 0 deletions assets/icons/eye.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/icons/eye_off.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions assets/icons/person.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
19 changes: 19 additions & 0 deletions lib/api/model/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ sealed class Event {
}
// case 'muted_topics': … // TODO(#422) we ignore this feature on older servers
case 'user_topic': return UserTopicEvent.fromJson(json);
case 'muted_users': return MutedUsersEvent.fromJson(json);
case 'message': return MessageEvent.fromJson(json);
case 'update_message': return UpdateMessageEvent.fromJson(json);
case 'delete_message': return DeleteMessageEvent.fromJson(json);
Expand Down Expand Up @@ -733,6 +734,24 @@ class UserTopicEvent extends Event {
Map<String, dynamic> toJson() => _$UserTopicEventToJson(this);
}

/// A Zulip event of type `muted_users`: https://zulip.com/api/get-events#muted_users
@JsonSerializable(fieldRename: FieldRename.snake)
class MutedUsersEvent extends Event {
@override
@JsonKey(includeToJson: true)
String get type => 'muted_users';

final List<MutedUserItem> mutedUsers;

MutedUsersEvent({required super.id, required this.mutedUsers});

factory MutedUsersEvent.fromJson(Map<String, dynamic> json) =>
_$MutedUsersEventFromJson(json);

@override
Map<String, dynamic> toJson() => _$MutedUsersEventToJson(this);
}

/// A Zulip event of type `message`: https://zulip.com/api/get-events#message
@JsonSerializable(fieldRename: FieldRename.snake)
class MessageEvent extends Event {
Expand Down
16 changes: 16 additions & 0 deletions lib/api/model/events.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/api/model/initial_snapshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class InitialSnapshot {

// final List<…> mutedTopics; // TODO(#422) we ignore this feature on older servers

final List<MutedUserItem> mutedUsers;

final Map<String, RealmEmojiItem> realmEmoji;

final List<RecentDmConversation> recentPrivateConversations;
Expand Down Expand Up @@ -132,6 +134,7 @@ class InitialSnapshot {
required this.serverTypingStartedExpiryPeriodMilliseconds,
required this.serverTypingStoppedWaitPeriodMilliseconds,
required this.serverTypingStartedWaitPeriodMilliseconds,
required this.mutedUsers,
required this.realmEmoji,
required this.recentPrivateConversations,
required this.savedSnippets,
Expand Down
5 changes: 5 additions & 0 deletions lib/api/model/initial_snapshot.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions lib/api/model/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ class CustomProfileFieldExternalAccountData {
Map<String, dynamic> toJson() => _$CustomProfileFieldExternalAccountDataToJson(this);
}


/// An item in the [InitialSnapshot.mutedUsers] or [MutedUsersEvent].
///
/// For docs, search for "muted_users:"
/// in <https://zulip.com/api/register-queue>.
@JsonSerializable(fieldRename: FieldRename.snake)
class MutedUserItem {
final int id;
final int timestamp;

const MutedUserItem({required this.id, required this.timestamp});

factory MutedUserItem.fromJson(Map<String, dynamic> json) =>
_$MutedUserItemFromJson(json);

Map<String, dynamic> toJson() => _$MutedUserItemToJson(this);
}

/// An item in [InitialSnapshot.realmEmoji] or [RealmEmojiUpdateEvent].
///
/// For docs, search for "realm_emoji:"
Expand Down
9 changes: 9 additions & 0 deletions lib/api/model/model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions lib/model/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,13 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
@override
Iterable<User> get allUsers => _users.allUsers;

@override
List<MutedUserItem> get mutedUsers => _users.mutedUsers;

@override
bool isUserMuted(int id, {List<MutedUserItem>? mutedUsers}) =>
_users.isUserMuted(id, mutedUsers: mutedUsers);

final UserStoreImpl _users;

final TypingStatus typingStatus;
Expand Down Expand Up @@ -943,6 +950,11 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
assert(debugLog("server event: reaction/${event.op}"));
_messages.handleReactionEvent(event);

case MutedUsersEvent():
assert(debugLog("server event: muted_users"));
_users.handleMutedUsersEvent(event);
notifyListeners();

case UnexpectedEvent():
assert(debugLog("server event: ${jsonEncode(event.toJson())}")); // TODO log better
}
Expand Down
42 changes: 41 additions & 1 deletion lib/model/user.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import '../api/model/events.dart';
import '../api/model/initial_snapshot.dart';
import '../api/model/model.dart';
import 'algorithms.dart';
import 'localizations.dart';
import 'narrow.dart';
import 'store.dart';

/// The portion of [PerAccountStore] describing the users in the realm.
Expand Down Expand Up @@ -66,6 +68,24 @@ mixin UserStore on PerAccountStoreBase {
return getUser(message.senderId)?.fullName
?? message.senderFullName;
}

/// All the users muted by [selfUser], sorted by [MutedUserItem.id] ascending.
List<MutedUserItem> get mutedUsers;

/// Whether the user with the given [id] is muted by [selfUser].
///
/// By default, looks for the user id in [UserStore.mutedUsers] unless
/// [mutedUsers] is non-null, in which case looks in the latter.
bool isUserMuted(int id, {List<MutedUserItem>? mutedUsers});

/// Whether all of the users corresponding to [DmNarrow.otherRecipientIds]
/// are muted by [selfUser];
///
/// By default, looks for the recipients in [UserStore.mutedUsers] unless
/// [mutedUsers] is non-null, in which case looks in the latter.
bool allRecipientsMuted(DmNarrow narrow, {List<MutedUserItem>? mutedUsers}) {
return !narrow.otherRecipientIds.any((id) => !isUserMuted(id, mutedUsers: mutedUsers));
}
}

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

final Map<int, User> _users;

@override
final List<MutedUserItem> mutedUsers;

@override
User? getUser(int userId) => _users[userId];

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

@override
bool isUserMuted(int id, {List<MutedUserItem>? mutedUsers}) {
return binarySearchByKey(
mutedUsers == null ? this.mutedUsers : _sortMutedUsers(mutedUsers), id,
(item, id) => item.id.compareTo(id)) >= 0;
}

static List<MutedUserItem> _sortMutedUsers(List<MutedUserItem> mutedUsers) {
return mutedUsers..sort((a, b) => a.id.compareTo(b.id));
}

void handleRealmUserEvent(RealmUserEvent event) {
switch (event) {
case RealmUserAddEvent():
Expand Down Expand Up @@ -129,4 +164,9 @@ class UserStoreImpl extends PerAccountStoreBase with UserStore {
}
}
}

void handleMutedUsersEvent(MutedUsersEvent event) {
mutedUsers.clear();
mutedUsers.addAll(_sortMutedUsers(event.mutedUsers));
}
}
4 changes: 2 additions & 2 deletions lib/widgets/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class _HomePageState extends State<HomePage> {
narrow: const CombinedFeedNarrow()))),
button(_HomePageTab.channels, ZulipIcons.hash_italic),
// TODO(#1094): Users
button(_HomePageTab.directMessages, ZulipIcons.user),
button(_HomePageTab.directMessages, ZulipIcons.two_person),
_NavigationBarButton( icon: ZulipIcons.menu,
selected: false,
onPressed: () => _showMainMenu(context, tabNotifier: _tab)),
Expand Down Expand Up @@ -515,7 +515,7 @@ class _DirectMessagesButton extends _NavigationBarMenuButton {
const _DirectMessagesButton({required super.tabNotifier});

@override
IconData get icon => ZulipIcons.user;
IconData get icon => ZulipIcons.two_person;

@override
String label(ZulipLocalizations zulipLocalizations) {
Expand Down
Loading