From 549aab514074296beeb9fadb333ebcdfc1bf20f7 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Thu, 24 Aug 2023 13:38:53 +0530 Subject: [PATCH] fix(llc): fix not able to update message with `type: reply`. Signed-off-by: Sahil Kumar --- .../lib/src/core/models/message.dart | 8 ++++ .../lib/src/core/models/message.g.dart | 38 ++++++++++++------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/packages/stream_chat/lib/src/core/models/message.dart b/packages/stream_chat/lib/src/core/models/message.dart index 37d9f840..93110722 100644 --- a/packages/stream_chat/lib/src/core/models/message.dart +++ b/packages/stream_chat/lib/src/core/models/message.dart @@ -168,8 +168,16 @@ class Message extends Equatable { late final MessageState state; /// The message type. + @JsonKey(includeIfNull: false, toJson: _typeToJson) final String type; + // We need to skip passing type if it's not regular or system as the API + // does not expect it. + static String? _typeToJson(String type) { + if (['regular', 'system'].contains(type)) return type; + return null; + } + /// The list of attachments, either provided by the user or generated from a /// command or as a result of URL scraping. @JsonKey(includeIfNull: false) diff --git a/packages/stream_chat/lib/src/core/models/message.g.dart b/packages/stream_chat/lib/src/core/models/message.g.dart index 2eed9660..883408d0 100644 --- a/packages/stream_chat/lib/src/core/models/message.g.dart +++ b/packages/stream_chat/lib/src/core/models/message.g.dart @@ -71,17 +71,27 @@ Message _$MessageFromJson(Map json) => Message( ), ); -Map _$MessageToJson(Message instance) => { - 'id': instance.id, - 'text': instance.text, - 'type': instance.type, - 'attachments': instance.attachments.map((e) => e.toJson()).toList(), - 'mentioned_users': User.toIds(instance.mentionedUsers), - 'parent_id': instance.parentId, - 'quoted_message_id': instance.quotedMessageId, - 'show_in_channel': instance.showInChannel, - 'silent': instance.silent, - 'pinned': instance.pinned, - 'pin_expires': instance.pinExpires?.toIso8601String(), - 'extra_data': instance.extraData, - }; +Map _$MessageToJson(Message instance) { + final val = { + 'id': instance.id, + 'text': instance.text, + }; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('type', Message._typeToJson(instance.type)); + val['attachments'] = instance.attachments.map((e) => e.toJson()).toList(); + val['mentioned_users'] = User.toIds(instance.mentionedUsers); + val['parent_id'] = instance.parentId; + val['quoted_message_id'] = instance.quotedMessageId; + val['show_in_channel'] = instance.showInChannel; + val['silent'] = instance.silent; + val['pinned'] = instance.pinned; + val['pin_expires'] = instance.pinExpires?.toIso8601String(); + val['extra_data'] = instance.extraData; + return val; +}