editTextMessage method
Edits an existing text message and marks it as edited in metadata
Implementation
Future<void> editTextMessage({
required String roomId,
required String messageId,
required String newText,
}) async {
if (firebaseUser == null) return;
// First get the existing message to verify ownership
final messageDoc = await getFirebaseFirestore
.collection('${FireChatConst.roomsCollectionName}/$roomId/messages')
.doc(messageId)
.get();
if (!messageDoc.exists) throw Exception('Message not found');
if (messageDoc.data()?['authorId'] != firebaseUser!.uid) {
throw Exception('Only message author can edit');
}
// Update the message with new text and edited metadata
await getFirebaseFirestore
.collection('${FireChatConst.roomsCollectionName}/$roomId/messages')
.doc(messageId)
.update({
'text': newText,
"isEdited": true,
'updatedAt': FieldValue.serverTimestamp(),
'metadata': {
...messageDoc.data()?['metadata'] ?? {},
'edited': true,
'editedAt': FieldValue.serverTimestamp(),
},
});
}