Line data Source code
1 : import 'package:chatwoot_client_sdk/data/remote/responses/chatwoot_event.dart'; 2 : import 'package:equatable/equatable.dart'; 3 : import 'package:hive/hive.dart'; 4 : import 'package:json_annotation/json_annotation.dart'; 5 : 6 : import '../local_storage.dart'; 7 : 8 : part 'chatwoot_message.g.dart'; 9 : 10 : /// {@category FlutterClientSdk} 11 : @JsonSerializable(explicitToJson: true) 12 : @HiveType(typeId: CHATWOOT_MESSAGE_HIVE_TYPE_ID) 13 : class ChatwootMessage extends Equatable { 14 : ///unique identifier of the message 15 : @JsonKey(fromJson: idFromJson) 16 : @HiveField(0) 17 : final int id; 18 : 19 : ///text content of the message 20 : @JsonKey() 21 : @HiveField(1) 22 : final String? content; 23 : 24 : ///type of message 25 : /// 26 : ///returns 1 if message belongs to contact making the request 27 : @JsonKey(name: "message_type", fromJson: messageTypeFromJson) 28 : @HiveField(2) 29 : final int? messageType; 30 : 31 : ///content type of message 32 : @JsonKey(name: "content_type") 33 : @HiveField(3) 34 : final String? contentType; 35 : 36 : @JsonKey(name: "content_attributes") 37 : @HiveField(4) 38 : final dynamic contentAttributes; 39 : 40 : ///date and time message was created 41 : @JsonKey(name: "created_at", fromJson: createdAtFromJson) 42 : @HiveField(5) 43 : final String createdAt; 44 : 45 : ///id of the conversation the message belongs to 46 : @JsonKey(name: "conversation_id", fromJson: idFromJson) 47 : @HiveField(6) 48 : final int? conversationId; 49 : 50 : ///list of media/doc/file attachment for message 51 : @JsonKey() 52 : @HiveField(7) 53 : final List<dynamic>? attachments; 54 : 55 : ///The user this message belongs to 56 : @JsonKey(name: "sender") 57 : @HiveField(8) 58 : final ChatwootEventMessageUser? sender; 59 : 60 : ///checks if message belongs to contact making the request 61 3 : bool get isMine => messageType != 1; 62 : 63 4 : ChatwootMessage( 64 : {required this.id, 65 : required this.content, 66 : required this.messageType, 67 : required this.contentType, 68 : required this.contentAttributes, 69 : required this.createdAt, 70 : required this.conversationId, 71 : required this.attachments, 72 : required this.sender}); 73 : 74 4 : factory ChatwootMessage.fromJson(Map<String, dynamic> json) => 75 4 : _$ChatwootMessageFromJson(json); 76 : 77 0 : Map<String, dynamic> toJson() => _$ChatwootMessageToJson(this); 78 : 79 1 : @override 80 1 : List<Object?> get props => [ 81 1 : id, 82 1 : content, 83 1 : messageType, 84 1 : contentType, 85 1 : contentAttributes, 86 1 : createdAt, 87 1 : conversationId, 88 1 : attachments, 89 1 : sender 90 : ]; 91 : } 92 : 93 4 : int idFromJson(value) { 94 4 : if (value is String) { 95 0 : return int.tryParse(value) ?? 0; 96 : } 97 : return value; 98 : } 99 : 100 4 : int messageTypeFromJson(value) { 101 4 : if (value is String) { 102 0 : return int.tryParse(value) ?? 0; 103 : } 104 : return value; 105 : } 106 : 107 4 : String createdAtFromJson(value) { 108 4 : if (value is int) { 109 12 : return DateTime.fromMillisecondsSinceEpoch(value * 1000).toString(); 110 : } 111 1 : return value.toString(); 112 : }