cofounderella/lib/services/chat/chat_service.dart

106 lines
3.4 KiB
Dart
Raw Normal View History

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
2024-05-25 13:56:45 +02:00
import '../../constants.dart';
import '../../utils/helper.dart';
2024-05-25 13:56:45 +02:00
import '../../models/message.dart';
class ChatService {
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final FirebaseAuth _auth = FirebaseAuth.instance;
// send message
Future<void> sendMessage(String receiverID, message) async {
// get current user info
final String currentUserID = _auth.currentUser!.uid;
final String currentUserEmail = _auth.currentUser!.email!;
final Timestamp timestamp = Timestamp.now();
// create new message
Message newMessage = Message(
senderID: currentUserID,
senderEmail: currentUserEmail,
receiverID: receiverID,
message: message,
timestamp: timestamp,
);
2024-05-25 13:56:45 +02:00
// construct chat room ID for the two users
String chatRoomID = getCompoundId([currentUserID, receiverID]);
2024-06-03 16:35:33 +02:00
// create batch to add message and update last message details in one step
WriteBatch batch = _firestore.batch();
2024-05-05 10:26:49 +02:00
// add new message to database
2024-06-03 16:35:33 +02:00
var newMessageRef = _firestore
.collection(Constants.dbCollectionChatRooms)
.doc(chatRoomID)
.collection(Constants.dbCollectionMessages)
.doc(); // doc generates a random ID for the message
batch.set(newMessageRef, newMessage.toMap());
// update last message details in chat room
var chatRoomRef =
_firestore.collection(Constants.dbCollectionChatRooms).doc(chatRoomID);
// add to batch above
batch.set(
chatRoomRef,
{
Constants.dbFieldMessageSenderId: currentUserID,
Constants.dbFieldMessageSenderEmail: currentUserEmail,
Constants.dbFieldMessageTimestamp: timestamp,
},
SetOptions(merge: true));
// commit batch
await batch.commit();
}
/// Retrieves the last message of a chatroom.
Future<Message> getLastMessage(String chatRoomID) async {
String senderID = '';
String senderEmail = '';
String receiverID = '';
String message = '';
Timestamp timestamp = Timestamp.fromDate(DateTime.utc(1970, 01, 01));
QuerySnapshot messageSnapshot = await _firestore
2024-05-05 10:26:49 +02:00
.collection(Constants.dbCollectionChatRooms)
.doc(chatRoomID)
2024-05-05 10:26:49 +02:00
.collection(Constants.dbCollectionMessages)
2024-06-03 16:35:33 +02:00
.orderBy(Constants.dbFieldMessageTimestamp, descending: true)
.limit(1)
.get();
DocumentSnapshot lastMessageDoc = messageSnapshot.docs.first;
if (lastMessageDoc.exists) {
senderID = lastMessageDoc[Constants.dbFieldMessageSenderId];
senderEmail = lastMessageDoc[Constants.dbFieldMessageSenderEmail];
receiverID = lastMessageDoc[Constants.dbFieldMessageReceiverId];
message = lastMessageDoc[Constants.dbFieldMessageText];
timestamp = lastMessageDoc[Constants.dbFieldMessageTimestamp];
}
return Message(
senderID: senderID,
senderEmail: senderEmail,
receiverID: receiverID,
message: message,
timestamp: timestamp,
);
}
// get messages
Stream<QuerySnapshot> getMessages(String userID, otherUserID) {
2024-05-25 13:56:45 +02:00
String chatRoomID = getCompoundId([userID, otherUserID]);
return _firestore
2024-05-05 10:26:49 +02:00
.collection(Constants.dbCollectionChatRooms)
.doc(chatRoomID)
2024-05-05 10:26:49 +02:00
.collection(Constants.dbCollectionMessages)
2024-06-03 16:35:33 +02:00
.orderBy(Constants.dbFieldMessageTimestamp, descending: false)
.snapshots();
}
}