87 lines
2.4 KiB
Dart
87 lines
2.4 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
|
|
class Message {
|
|
final String id;
|
|
final String text;
|
|
final DateTime timestamp;
|
|
final String senderId;
|
|
final String senderName;
|
|
final String groupId;
|
|
final Map<String, String> reactions; // userId -> emoji
|
|
final DateTime? editedAt;
|
|
final bool isEdited;
|
|
final bool isDeleted;
|
|
|
|
Message({
|
|
this.id = '',
|
|
required this.text,
|
|
required this.timestamp,
|
|
required this.senderId,
|
|
required this.senderName,
|
|
required this.groupId,
|
|
this.reactions = const {},
|
|
this.editedAt,
|
|
this.isEdited = false,
|
|
this.isDeleted = false,
|
|
});
|
|
|
|
factory Message.fromFirestore(DocumentSnapshot doc) {
|
|
final data = doc.data() as Map<String, dynamic>;
|
|
final timestamp = data['timestamp'] as Timestamp?;
|
|
final editedAtTimestamp = data['editedAt'] as Timestamp?;
|
|
final reactionsData = data['reactions'] as Map<String, dynamic>?;
|
|
|
|
return Message(
|
|
id: doc.id,
|
|
text: data['text'] ?? '',
|
|
timestamp: timestamp?.toDate() ?? DateTime.now(),
|
|
senderId: data['senderId'] ?? '',
|
|
senderName: data['senderName'] ?? 'Anonyme',
|
|
groupId: data['groupId'] ?? '',
|
|
reactions: reactionsData?.map((key, value) => MapEntry(key, value.toString())) ?? {},
|
|
editedAt: editedAtTimestamp?.toDate(),
|
|
isEdited: data['isEdited'] ?? false,
|
|
isDeleted: data['isDeleted'] ?? false,
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toFirestore() {
|
|
return {
|
|
'text': text,
|
|
'senderId': senderId,
|
|
'senderName': senderName,
|
|
'timestamp': Timestamp.fromDate(timestamp),
|
|
'groupId': groupId,
|
|
'reactions': reactions,
|
|
'editedAt': editedAt != null ? Timestamp.fromDate(editedAt!) : null,
|
|
'isEdited': isEdited,
|
|
'isDeleted': isDeleted,
|
|
};
|
|
}
|
|
|
|
Message copyWith({
|
|
String? id,
|
|
String? text,
|
|
DateTime? timestamp,
|
|
String? senderId,
|
|
String? senderName,
|
|
String? groupId,
|
|
Map<String, String>? reactions,
|
|
DateTime? editedAt,
|
|
bool? isEdited,
|
|
bool? isDeleted,
|
|
}) {
|
|
return Message(
|
|
id: id ?? this.id,
|
|
text: text ?? this.text,
|
|
timestamp: timestamp ?? this.timestamp,
|
|
senderId: senderId ?? this.senderId,
|
|
senderName: senderName ?? this.senderName,
|
|
groupId: groupId ?? this.groupId,
|
|
reactions: reactions ?? this.reactions,
|
|
editedAt: editedAt ?? this.editedAt,
|
|
isEdited: isEdited ?? this.isEdited,
|
|
isDeleted: isDeleted ?? this.isDeleted,
|
|
);
|
|
}
|
|
} |