Skip to content

Commit 8aedecc

Browse files
chrisbobbegnprice
authored andcommitted
model [nfc]: Implement ReactionType.toJson
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.
1 parent 99c17ef commit 8aedecc

File tree

5 files changed

+14
-11
lines changed

5 files changed

+14
-11
lines changed

lib/api/model/events.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/api/model/model.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,4 +475,6 @@ enum ReactionType {
475475
unicodeEmoji,
476476
realmEmoji,
477477
zulipExtraEmoji;
478+
479+
String toJson() => _$ReactionTypeEnumMap[this]!;
478480
}

lib/api/model/model.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/example_data.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ StreamMessage streamMessage({
147147
..._messagePropertiesFromContent(content, contentMarkdown),
148148
'display_recipient': effectiveStream.name,
149149
'stream_id': effectiveStream.streamId,
150-
'reactions': reactions?.map((r) => r.toJson()).toList() ?? [],
150+
'reactions': reactions?.map(
151+
(r) => r.toJson()..['reaction_type'] = r.reactionType.toJson(),
152+
).toList() ?? [],
151153
'flags': flags ?? [],
152154
'id': id ?? 1234567, // TODO generate example IDs
153155
'last_edit_timestamp': lastEditTimestamp,

test/model/message_list_test.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,23 +396,22 @@ void main() async {
396396

397397
test('remove reaction', () async {
398398
final eventReaction = Reaction(reactionType: ReactionType.unicodeEmoji,
399-
emojiName: 'wave', emojiCode: '1f44b', userId: 1);
399+
emojiName: 'wave', emojiCode: '1f44b', userId: 1);
400400

401401
// Same emoji, different user. Not to be removed.
402-
final reaction2 = Reaction.fromJson(eventReaction.toJson()
403-
..['user_id'] = 2);
402+
final reaction2 = Reaction(reactionType: ReactionType.unicodeEmoji,
403+
emojiName: 'wave', emojiCode: '1f44b', userId: 2);
404404

405405
// Same user, different emoji. Not to be removed.
406-
final reaction3 = Reaction.fromJson(eventReaction.toJson()
407-
..['emoji_code'] = '1f6e0'
408-
..['emoji_name'] = 'working_on_it');
406+
final reaction3 = Reaction(reactionType: ReactionType.unicodeEmoji,
407+
emojiName: 'working_on_it', emojiCode: '1f6e0', userId: 1);
409408

410409
// Same user, same emojiCode, different emojiName. To be removed: servers
411410
// key on user, message, reaction type, and emoji code, but not emoji name.
412411
// So we mimic that behavior; see discussion:
413412
// https://github.com/zulip/zulip-flutter/pull/256#discussion_r1284865099
414-
final reaction4 = Reaction.fromJson(eventReaction.toJson()
415-
..['emoji_name'] = 'hello');
413+
final reaction4 = Reaction(reactionType: ReactionType.unicodeEmoji,
414+
emojiName: 'hello', emojiCode: '1f44b', userId: 1);
416415

417416
final originalMessage = eg.streamMessage(
418417
reactions: [reaction2, reaction3, reaction4]);

0 commit comments

Comments
 (0)