setDeleteMessage method

Future<void> setDeleteMessage({
  1. required String roomId,
  2. required String messageId,
})

Edits an existing text message and marks it as edited in metadata

Implementation

Future<void> setDeleteMessage({
  required String roomId,
  required String messageId,
}) 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(
    {
      "isDeleted": true,
      'updatedAt': FieldValue.serverTimestamp(),
    },
  );
}