-
Notifications
You must be signed in to change notification settings - Fork 314
model: Add data models for typing events. #783
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ sealed class Event { | |
case 'remove': return UpdateMessageFlagsRemoveEvent.fromJson(json); | ||
default: return UnexpectedEvent.fromJson(json); | ||
} | ||
case 'typing': return TypingEvent.fromJson(json); | ||
case 'reaction': return ReactionEvent.fromJson(json); | ||
case 'heartbeat': return HeartbeatEvent.fromJson(json); | ||
// TODO add many more event types | ||
|
@@ -707,6 +708,9 @@ class DeleteMessageEvent extends Event { | |
|
||
final List<int> messageIds; | ||
// final int messageId; // Not present; we support the bulk_message_deletion capability | ||
// The server never actually sends "direct" here yet (it's "private" instead), | ||
// but we accept both forms for forward-compatibility. | ||
@MessageTypeConverter() | ||
final MessageType messageType; | ||
final int? streamId; | ||
final String? topic; | ||
|
@@ -733,12 +737,28 @@ class DeleteMessageEvent extends Event { | |
Map<String, dynamic> toJson() => _$DeleteMessageEventToJson(this); | ||
} | ||
|
||
/// As in [DeleteMessageEvent.messageType] | ||
/// or [UpdateMessageFlagsMessageDetail.type]. | ||
@JsonEnum(fieldRename: FieldRename.snake) | ||
/// As in [DeleteMessageEvent.messageType], | ||
/// [UpdateMessageFlagsMessageDetail.type], | ||
/// or [TypingEvent.messageType]. | ||
@JsonEnum(alwaysCreate: true) | ||
enum MessageType { | ||
stream, | ||
private; | ||
direct; | ||
} | ||
|
||
class MessageTypeConverter extends JsonConverter<MessageType, String> { | ||
const MessageTypeConverter(); | ||
|
||
@override | ||
MessageType fromJson(String json) { | ||
if (json == 'private') json = 'direct'; // TODO(server-future) | ||
return $enumDecode(_$MessageTypeEnumMap, json); | ||
} | ||
|
||
@override | ||
String toJson(MessageType object) { | ||
return _$MessageTypeEnumMap[object]!; | ||
} | ||
} | ||
|
||
/// A Zulip event of type `update_message_flags`. | ||
|
@@ -820,6 +840,9 @@ class UpdateMessageFlagsRemoveEvent extends UpdateMessageFlagsEvent { | |
/// As in [UpdateMessageFlagsRemoveEvent.messageDetails]. | ||
@JsonSerializable(fieldRename: FieldRename.snake) | ||
class UpdateMessageFlagsMessageDetail { | ||
// The server never actually sends "direct" here yet (it's "private" instead), | ||
// but we accept both forms for forward-compatibility. | ||
@MessageTypeConverter() | ||
final MessageType type; | ||
final bool? mentioned; | ||
final List<int>? userIds; | ||
|
@@ -841,7 +864,7 @@ class UpdateMessageFlagsMessageDetail { | |
case MessageType.stream: | ||
result.streamId as int; | ||
result.topic as String; | ||
case MessageType.private: | ||
case MessageType.direct: | ||
result.userIds as List<int>; | ||
} | ||
return result; | ||
|
@@ -850,6 +873,69 @@ class UpdateMessageFlagsMessageDetail { | |
Map<String, dynamic> toJson() => _$UpdateMessageFlagsMessageDetailToJson(this); | ||
} | ||
|
||
/// A Zulip event of type `typing`: | ||
/// https://zulip.com/api/get-events#typing-start | ||
/// https://zulip.com/api/get-events#typing-stop | ||
@JsonSerializable(fieldRename: FieldRename.snake) | ||
class TypingEvent extends Event { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit in commit message:
This commit defines the events, but doesn't yet handle them — that's the next commit, where (The separation of commits seems good, though, as there's plenty of complexity in that next commit.) |
||
@override | ||
@JsonKey(includeToJson: true) | ||
String get type => 'typing'; | ||
|
||
final TypingOp op; | ||
@MessageTypeConverter() | ||
final MessageType messageType; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is "stream" or "private", but in the API these events say "direct" on current servers. So we should handle that. The enum can still have two values, but we'll just want some additional code around that so that it accepts both names for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's fine to add that additional logic in a way that applies to all the places we use this enum, even if some of them don't yet ever say All of them should eventually say And, the important part for making it OK to add that logic in a way that applies to all of them: for anything in the Zulip API that would be reasonable to think of as a "message type", if it does ever start sending a value of OTOH if the most convenient way to add the logic winds up being separate for each place the enum is used, then this PR might as well stick to this one spot where it's actually needed, and we can handle the others later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I put the change that affected other events in a prep commit: 9410969 |
||
@JsonKey(readValue: _readSenderId) | ||
final int senderId; | ||
@JsonKey(name: 'recipients', fromJson: _recipientIdsFromJson) | ||
final List<int>? recipientIds; | ||
final int? streamId; | ||
final String? topic; | ||
|
||
TypingEvent({ | ||
required super.id, | ||
required this.op, | ||
required this.messageType, | ||
required this.senderId, | ||
required this.recipientIds, | ||
required this.streamId, | ||
required this.topic, | ||
}); | ||
|
||
static Object? _readSenderId(Map<Object?, Object?> json, String key) { | ||
return (json['sender'] as Map<String, dynamic>)['user_id']; | ||
} | ||
|
||
static List<int>? _recipientIdsFromJson(Object? json) { | ||
if (json == null) return null; | ||
return (json as List<Object?>).map( | ||
(item) => (item as Map<String, Object?>)['user_id'] as int).toList(); | ||
} | ||
|
||
factory TypingEvent.fromJson(Map<String, dynamic> json) { | ||
final result = _$TypingEventFromJson(json); | ||
// Crunchy-shell validation | ||
switch (result.messageType) { | ||
case MessageType.stream: | ||
result.streamId as int; | ||
result.topic as String; | ||
case MessageType.direct: | ||
result.recipientIds as List<int>; | ||
} | ||
return result; | ||
} | ||
|
||
@override | ||
Map<String, dynamic> toJson() => _$TypingEventToJson(this); | ||
} | ||
|
||
/// As in [TypingEvent.op]. | ||
@JsonEnum(fieldRename: FieldRename.snake) | ||
enum TypingOp { | ||
start, | ||
stop | ||
} | ||
|
||
/// A Zulip event of type `reaction`, with op `add` or `remove`. | ||
/// | ||
/// See: | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit of a mismatch with the existing API, so it deserves a short comment acknowledging that.
Similarly on UpdateMessageFlagsMessageDetail.