68 lines
2.1 KiB
Dart
68 lines
2.1 KiB
Dart
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
||
|
import 'package:cofounderella/models/message.dart';
|
||
|
import 'package:firebase_auth/firebase_auth.dart';
|
||
|
|
||
|
class ChatService {
|
||
|
// get instance of firestore and auth
|
||
|
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
||
|
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||
|
|
||
|
// get user stream
|
||
|
Stream<List<Map<String, dynamic>>> getUsersStream() {
|
||
|
return _firestore.collection("Users").snapshots().map((snapshot) {
|
||
|
return snapshot.docs.map((doc) {
|
||
|
// iterate each user
|
||
|
final user = doc.data();
|
||
|
|
||
|
//return user
|
||
|
return user;
|
||
|
}).toList();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// 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,
|
||
|
);
|
||
|
|
||
|
// construct chat room ID for the two users (sorted to ensure uniqueness)
|
||
|
List<String> ids = [currentUserID, receiverID];
|
||
|
ids.sort(); // sort to ensure the chatroomID is the same for any 2 users
|
||
|
String chatRoomID = ids.join('_');
|
||
|
|
||
|
// add new message to database
|
||
|
await _firestore
|
||
|
.collection("chat_rooms")
|
||
|
.doc(chatRoomID)
|
||
|
.collection("messages")
|
||
|
.add(newMessage.toMap());
|
||
|
}
|
||
|
|
||
|
// get messages
|
||
|
Stream<QuerySnapshot> getMessages(String userID, otherUserID) {
|
||
|
// TODO create chat room ID -- same code snippet as above
|
||
|
// construct chat room ID for the two users (sorted to ensure uniqueness)
|
||
|
List<String> ids = [userID, otherUserID];
|
||
|
ids.sort(); // sort to ensure the chatroomID is the same for any 2 users
|
||
|
String chatRoomID = ids.join('_');
|
||
|
|
||
|
return _firestore
|
||
|
.collection("chat_rooms")
|
||
|
.doc(chatRoomID)
|
||
|
.collection("messages")
|
||
|
.orderBy("timestamp", descending: false)
|
||
|
.snapshots();
|
||
|
}
|
||
|
}
|