Skip to content

Track user status #1629

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 12 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
52 changes: 52 additions & 0 deletions lib/api/model/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ sealed class Event {
default: return UnexpectedEvent.fromJson(json);
}
// case 'muted_topics': … // TODO(#422) we ignore this feature on older servers
case 'user_status': return UserStatusEvent.fromJson(json);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api: Add user_status event

tools/check build_runner is failing at this commit; I think that can be fixed with tools/check build_runner --fix.

case 'user_topic': return UserTopicEvent.fromJson(json);
case 'muted_users': return MutedUsersEvent.fromJson(json);
case 'message': return MessageEvent.fromJson(json);
Expand Down Expand Up @@ -708,6 +709,57 @@ class SubscriptionPeerRemoveEvent extends SubscriptionEvent {
Map<String, dynamic> toJson() => _$SubscriptionPeerRemoveEventToJson(this);
}

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

final int userId;
// final bool away; // deprecated in server-6 (FL-148); ignore
final String? statusText;
final String? emojiName;
final String? emojiCode;
final UserStatusEventReactionType? reactionType;

UserStatusEvent({
required super.id,
required this.userId,
required this.statusText,
required this.emojiName,
required this.emojiCode,
required this.reactionType,
});

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

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

/// As in [UserStatusEvent.reactionType].
///
/// This is the same as [ReactionType], but with an additional value "empty"
/// representing an empty string value in [UserStatusEvent] where no status
/// emoji is selected.
@JsonEnum(fieldRename: FieldRename.snake)
enum UserStatusEventReactionType {
unicodeEmoji,
realmEmoji,
zulipExtraEmoji,
@JsonValue('')
empty;

String toJson() => _$UserStatusEventReactionTypeEnumMap[this]!;

static UserStatusEventReactionType fromApiValue(String value) => _byApiValue[value]!;

static final _byApiValue = _$UserStatusEventReactionTypeEnumMap
.map((key, value) => MapEntry(value, key));
}

/// A Zulip event of type `user_topic`: https://zulip.com/api/get-events#user_topic
@JsonSerializable(fieldRename: FieldRename.snake)
class UserTopicEvent extends Event {
Expand Down
31 changes: 31 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.

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

final List<ZulipStream> streams;

@JsonKey(name: 'user_status')
// In register-queue, the name of this field is the singular "user_status",
// even though it actually contains user status information for all the users
// that the self-user has access to. Therefore, we prefer to use the plural form.
final Map<int, UserStatus> userStatuses;

// Servers pre-5.0 don't have `user_settings`, and instead provide whatever
// user settings they support at toplevel in the initial snapshot. Since we're
// likely to desupport pre-5.0 servers before wide release, we prefer to
Expand Down Expand Up @@ -154,6 +160,7 @@ class InitialSnapshot {
required this.subscriptions,
required this.unreadMsgs,
required this.streams,
required this.userStatuses,
required this.userSettings,
required this.userTopics,
required this.realmWildcardMentionPolicy,
Expand Down
98 changes: 52 additions & 46 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.

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

/// A value in [InitialSnapshot.userStatuses].
///
/// For docs, search for "user_status:"
/// in <https://zulip.com/api/register-queue>.
@JsonSerializable(fieldRename: FieldRename.snake)
class UserStatus {
// final bool? away; // deprecated in server-6 (FL-148); ignore
final String? statusText;
final String? emojiName;
final String? emojiCode;
final ReactionType? reactionType;

UserStatus({
required this.statusText,
required this.emojiName,
required this.emojiCode,
required this.reactionType,
});

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

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

/// The name of a user setting that has a property in [UserSettings].
///
/// In Zulip event-handling code (for [UserSettingsUpdateEvent]),
Expand Down
24 changes: 24 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.

5 changes: 5 additions & 0 deletions lib/api/model/reaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,9 @@ enum ReactionType {
zulipExtraEmoji;

String toJson() => _$ReactionTypeEnumMap[this]!;

static ReactionType fromApiValue(String value) => _byApiValue[value]!;

static final _byApiValue = _$ReactionTypeEnumMap
.map((key, value) => MapEntry(value, key));
}
8 changes: 8 additions & 0 deletions lib/model/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,9 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
bool isUserMuted(int userId, {MutedUsersEvent? event}) =>
_users.isUserMuted(userId, event: event);

@override
UserStatus? getUserStatus(int userId) => _users.getUserStatus(userId);

final UserStoreImpl _users;

final TypingStatus typingStatus;
Expand Down Expand Up @@ -926,6 +929,11 @@ class PerAccountStore extends PerAccountStoreBase with ChangeNotifier, EmojiStor
_channels.handleSubscriptionEvent(event);
notifyListeners();

case UserStatusEvent():
assert(debugLog("server event: user_status"));
_users.handleUserStatusEvent(event);
notifyListeners();

case UserTopicEvent():
assert(debugLog("server event: user_topic"));
_messages.handleUserTopicEvent(event);
Expand Down
Loading