Skip to content

Commit

Permalink
api: Add reaction/add and reaction/remove events (not yet handled)
Browse files Browse the repository at this point in the history
Ongoing discussion about how to handle these:
  https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/flutter.3A.20reaction.20events/near/1619282

But for now, at least we can represent the events in the API model,
before we handle them.

Related: #121
  • Loading branch information
chrisbobbe committed Aug 3, 2023
1 parent bdd2317 commit 80192ae
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
75 changes: 75 additions & 0 deletions lib/api/model/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ sealed class Event {
case 'message': return MessageEvent.fromJson(json);
case 'update_message': return UpdateMessageEvent.fromJson(json);
case 'delete_message': return DeleteMessageEvent.fromJson(json);
case 'reaction':
switch (json['op'] as String) {
case 'add': return ReactionAddEvent.fromJson(json);
case 'remove': return ReactionRemoveEvent.fromJson(json);
default: return UnexpectedEvent.fromJson(json);
}
case 'heartbeat': return HeartbeatEvent.fromJson(json);
// TODO add many more event types
default: return UnexpectedEvent.fromJson(json);
Expand Down Expand Up @@ -370,6 +376,75 @@ enum MessageType {
private;
}

/// A Zulip event of type `reaction`.
abstract class ReactionEvent extends Event {
@override
@JsonKey(includeToJson: true)
String get type => 'reaction';

String get op;

ReactionEvent({required super.id});
}

/// A [ReactionEvent] with op `add`: https://zulip.com/api/get-events#reaction-add
@JsonSerializable(fieldRename: FieldRename.snake)
class ReactionAddEvent extends ReactionEvent {
@override
String get op => 'add';

final String emojiName;
final String emojiCode;
final ReactionType reactionType;
final int userId;
// final Map<String, dynamic> user; // deprecated; ignore
final int messageId;

ReactionAddEvent({
required super.id,
required this.emojiName,
required this.emojiCode,
required this.reactionType,
required this.userId,
required this.messageId,
});

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

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

/// A [ReactionEvent] with op `remove`: https://zulip.com/api/get-events#reaction-remove
@JsonSerializable(fieldRename: FieldRename.snake)
class ReactionRemoveEvent extends ReactionEvent {
@override
String get op => 'remove';

final String emojiName;
final String emojiCode;
final ReactionType reactionType;
final int userId;
// final Map<String, dynamic> user; // deprecated; ignore
final int messageId;

ReactionRemoveEvent({
required super.id,
required this.emojiName,
required this.emojiCode,
required this.reactionType,
required this.userId,
required this.messageId,
});

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

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

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

6 changes: 6 additions & 0 deletions lib/model/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ class PerAccountStore extends ChangeNotifier {
} else if (event is DeleteMessageEvent) {
assert(debugLog("server event: delete_message ${event.messageIds}"));
// TODO handle
} else if (event is ReactionAddEvent) {
assert(debugLog("server event: reaction/add"));
// TODO handle
} else if (event is ReactionRemoveEvent) {
assert(debugLog("server event: reaction/remove"));
// TODO handle
} else if (event is UnexpectedEvent) {
assert(debugLog("server event: ${jsonEncode(event.toJson())}")); // TODO log better
} else {
Expand Down

0 comments on commit 80192ae

Please sign in to comment.