Skip to content

api: Add add-reaction and remove-reaction routes #272

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

Merged
merged 3 commits into from
Aug 15, 2023

Conversation

chrisbobbe
Copy link
Collaborator

@chrisbobbe chrisbobbe commented Aug 15, 2023

Related: #125

@chrisbobbe chrisbobbe added the a-api Implementing specific parts of the Zulip server API label Aug 15, 2023
@chrisbobbe chrisbobbe requested a review from gnprice August 15, 2023 19:07
Comment on lines 268 to 273
Future<AddReactionResult> addReaction(ApiConnection connection, {
required int messageId,
required ReactionType reactionType,
required String emojiCode,
required String emojiName,
}) {
Copy link
Collaborator Author

@chrisbobbe chrisbobbe Aug 15, 2023

Choose a reason for hiding this comment

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

Maybe addReaction and removeReaction could take a Reaction object instead of reactionType, emojiCode, and emojiName individually. Would that be better, I wonder?

lib/api/model/model.dart

/// As in [Message.reactions].
@JsonSerializable(fieldRename: FieldRename.snake)
class Reaction {
  final String emojiName;
  final String emojiCode;
  final ReactionType reactionType;
  final int userId;
  // final Map<String, dynamic> user; // deprecated; ignore

  Reaction({
    required this.emojiName,
    required this.emojiCode,
    required this.reactionType,
    required this.userId,
  });

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

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

  @override
  String toString() => 'Reaction(emojiName: $emojiName, emojiCode: $emojiCode, reactionType: $reactionType, userId: $userId)';
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe not better: the Reaction instance would be just a one-off wrapper for those three fields, and the new bindings don't need Reaction's userId field.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I agree that wouldn't be better, because the userId wouldn't belong.

Copy link
Member

@gnprice gnprice left a comment

Choose a reason for hiding this comment

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

Thanks! Generally looks good; comments below.

Also a commit-message nit:

ApiConnection: Implement `delete` method

I'd prefer to keep this under the "api" prefix (like previous changes found in git log lib/api/core.dart). So e.g. "api: Add ApiConnection.delete".

Comment on lines 277 to 280
'reaction_type': RawParameter(switch (reactionType) {
ReactionType.unicodeEmoji => 'unicode_emoji',
ReactionType.realmEmoji => 'realm_emoji',
ReactionType.zulipExtraEmoji => 'zulip_extra_emoji',
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, awkward to have to repeat this switch. Can we get json_serializable to do this for us?

Yeah, looks like we can say this:

  zulipExtraEmoji;

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

and then I think this can become RawParameter(reactionType.toJson()).

Comment on lines 293 to 281
}


/// https://zulip.com/api/remove-reaction
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
}
/// https://zulip.com/api/remove-reaction
}
/// https://zulip.com/api/remove-reaction

Comment on lines 286 to 287
class AddReactionResult {
AddReactionResult();
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, this class doesn't really do anything, does it :-)

I think when there's no content in the response, it's probably cleanest to have the route binding just return Future<void>. The fromJson callback can look like (_) {}.

Comment on lines 268 to 273
Future<AddReactionResult> addReaction(ApiConnection connection, {
required int messageId,
required ReactionType reactionType,
required String emojiCode,
required String emojiName,
}) {
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I agree that wouldn't be better, because the userId wouldn't belong.

@@ -195,6 +195,20 @@ Reaction unicodeEmojiReaction = Reaction(
userId: selfUser.userId,
);

Reaction realmEmoji = Reaction(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Reaction realmEmoji = Reaction(
Reaction realmEmojiReaction = Reaction(

This object isn't just an emoji, but an emoji reaction. In particular it specifies a user ID, which isn't part of specifying an emoji.

@chrisbobbe chrisbobbe force-pushed the pr-api-add-remove-reaction branch from ed3268c to c66b920 Compare August 15, 2023 22:55
@chrisbobbe
Copy link
Collaborator Author

Thanks for the review! Revision pushed.

Copy link
Member

@gnprice gnprice left a comment

Choose a reason for hiding this comment

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

Thanks! Looks good; merging.

'reaction_type': _$ReactionTypeEnumMap[instance.reactionType]!,
'reaction_type': instance.reactionType,
Copy link
Member

Choose a reason for hiding this comment

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

Hmm interesting. I guess that makes sense.

We'll call this directly soon, when we'll want reusable code to
translate an enum value into a 'snake_case' string, for API bindings
for the add- and remove-reaction endpoints.

With a `toJson` method, instances can now be handled by
`encodeJson`. That's nice, but not really a goal here; we don't have
an imminent need to pass an instance through `encodeJson`. Anyway,
it means that json_serializable's generated _$ReactionToJson and
_$ReactionEventToJson functions don't need the enum-to-string
conversion on their reactionType fields in order for jsonEncode to
accept those functions' output. And it looks like json_serializable
has removed that conversion in both functions. So, adjust our code
to not depend on it.
@gnprice gnprice force-pushed the pr-api-add-remove-reaction branch from c66b920 to 7e798bb Compare August 15, 2023 23:20
@gnprice gnprice merged commit 7e798bb into zulip:main Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-api Implementing specific parts of the Zulip server API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants