Solian/lib/models/message.dart

123 lines
3.3 KiB
Dart
Raw Normal View History

2024-04-17 15:00:53 +00:00
import 'package:solian/models/account.dart';
import 'package:solian/models/channel.dart';
import 'package:solian/models/post.dart';
class Message {
int id;
DateTime createdAt;
DateTime updatedAt;
DateTime? deletedAt;
String content;
Map<String, dynamic>? metadata;
2024-04-17 15:00:53 +00:00
int type;
List<Attachment>? attachments;
Channel? channel;
Sender sender;
int? replyId;
Message? replyTo;
int channelId;
int senderId;
bool isSending = false;
2024-04-17 15:00:53 +00:00
Message({
required this.id,
required this.createdAt,
required this.updatedAt,
this.deletedAt,
required this.content,
required this.metadata,
required this.type,
this.attachments,
this.channel,
required this.sender,
required this.replyId,
required this.replyTo,
required this.channelId,
required this.senderId,
});
factory Message.fromJson(Map<String, dynamic> json) => Message(
2024-05-01 16:49:38 +00:00
id: json['id'],
createdAt: DateTime.parse(json['created_at']),
updatedAt: DateTime.parse(json['updated_at']),
deletedAt: json['deleted_at'],
content: json['content'],
metadata: json['metadata'],
type: json['type'],
2024-05-01 09:37:34 +00:00
attachments: List<Attachment>.from(
2024-05-01 16:49:38 +00:00
json['attachments']?.map((x) => Attachment.fromJson(x)) ??
2024-05-01 09:37:34 +00:00
List.empty()),
2024-05-01 16:49:38 +00:00
channel: Channel.fromJson(json['channel']),
sender: Sender.fromJson(json['sender']),
replyId: json['reply_id'],
replyTo: json['reply_to'] != null
? Message.fromJson(json['reply_to'])
2024-05-01 09:37:34 +00:00
: null,
2024-05-01 16:49:38 +00:00
channelId: json['channel_id'],
senderId: json['sender_id'],
2024-04-17 15:00:53 +00:00
);
Map<String, dynamic> toJson() => {
2024-05-01 16:49:38 +00:00
'id': id,
'created_at': createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String(),
'deleted_at': deletedAt,
'content': content,
'metadata': metadata,
'type': type,
'attachments': List<dynamic>.from(
2024-05-01 09:37:34 +00:00
attachments?.map((x) => x.toJson()) ?? List.empty()),
2024-05-01 16:49:38 +00:00
'channel': channel?.toJson(),
'sender': sender.toJson(),
'reply_id': replyId,
'reply_to': replyTo?.toJson(),
'channel_id': channelId,
'sender_id': senderId,
2024-04-17 15:00:53 +00:00
};
}
class Sender {
int id;
DateTime createdAt;
DateTime updatedAt;
DateTime? deletedAt;
Account account;
int channelId;
int accountId;
int notify;
Sender({
required this.id,
required this.createdAt,
required this.updatedAt,
this.deletedAt,
required this.account,
required this.channelId,
required this.accountId,
required this.notify,
});
factory Sender.fromJson(Map<String, dynamic> json) => Sender(
2024-05-01 16:49:38 +00:00
id: json['id'],
createdAt: DateTime.parse(json['created_at']),
updatedAt: DateTime.parse(json['updated_at']),
deletedAt: json['deleted_at'],
account: Account.fromJson(json['account']),
channelId: json['channel_id'],
accountId: json['account_id'],
notify: json['notify'],
2024-04-17 15:00:53 +00:00
);
Map<String, dynamic> toJson() => {
2024-05-01 16:49:38 +00:00
'id': id,
'created_at': createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String(),
'deleted_at': deletedAt,
'account': account.toJson(),
'channel_id': channelId,
'account_id': accountId,
'notify': notify,
2024-04-17 15:00:53 +00:00
};
}