reactToMessage method
Implementation
Future<void> reactToMessage({
required String roomId,
required String messageId,
required String emoji,
}) async {
if (firebaseUser == null) return;
final String userId = firebaseUser!.uid;
final messageRef = FirebaseFirestore.instance
.collection('${FireChatConst.roomsCollectionName}/$roomId/messages')
.doc(messageId);
final snapshot = await messageRef.get();
if (!snapshot.exists) {
if (kDebugMode) {
print('Message does not exist.');
}
return;
}
final data = snapshot.data();
final Map<String, dynamic> reactions =
Map<String, dynamic>.from(data?['reactions'] ?? {});
if (reactions[userId] == emoji) {
// If user reacted with the same emoji again, remove it
reactions.remove(userId);
} else {
// Add or update the user's reaction
reactions[userId] = emoji;
}
await messageRef.update({'reactions': reactions});
}