I have created a custom action 'sendChatMessage' which takes 3 parameters, hostRef, GuestRef and message
```
Future sendChatMessage(
DocumentReference hostRef,
DocumentReference userRef,
String message,
) async {
var chatsCollection = FirebaseFirestore.instance.collection('chats');
// Check if a chat already exists between the two users.
var existingChatUserA =
await chatsCollection.where('user_a', whereIn: [hostRef, userRef]).get();
var existingChatUserB =
await chatsCollection.where('user_b', whereIn: [hostRef, userRef]).get();
var existingChat = [...existingChatUserA.docs, ...existingChatUserB.docs];
DocumentReference chatRef;
if (existingChat.isNotEmpty) {
// If a chat already exists, use the existing chat.
chatRef = existingChat.first.reference;
} else {
// If a chat does not exist, create a new chat.
var newChatData = {
'users': [hostRef, userRef],
'user_a': userRef,
'user_b': hostRef,
'last_message': message,
'last_message_time': DateTime.now(),
'last_message_sent_by': userRef,
'last_message_seen_by': [userRef],
};
chatRef = await chatsCollection.add(newChatData);
}
// Create a new chat message in the 'chat_messages' collection.
var chatMessagesCollection =
FirebaseFirestore.instance.collection('chat_messages');
var newMessageData = {
'chat': chatRef,
'text': message,
'user': userRef,
'timestamp': DateTime.now(),
};
await chatMessagesCollection.add(newMessageData);
// Update the chatRef with the lastMessage and lastMessageSentBy fields
await chatRef.update({
'lastMessage': message,
'lastMessageSentBy': userRef,
});
}
```
This took me quite a while to figure out as I wanted to be able to get or create the parent chat collection, however, there are some private parameters in `chat_record.dart` that I could not override