有一个搜索页面,我可以在其中搜索要聊天的用户,
如果我点击用户,如果没有找到以前的聊天记录,将会创建一个新窗口,
如果不是第一次聊天,将使用现有的聊天室
代码运行良好,但我想要一些实现,
如果我搜索一个用户并点击他,然后我不聊天就回去,创建的新房间应该被删除......所以我需要消息逻辑的数量......
如何实施才能实现
Future<ChatRoomModel?> getchatroom(UserModel targetuser) async {
ChatRoomModel? chatroom;
//here i feel something wrong as even if blocked chatroom, window should be open
QuerySnapshot querysnapshot = await FirebaseFirestore.instance
.collection("chatrooms")
.where("participants.${targetuser.uid}", isEqualTo: true)
.where("participants.${widget.userModel.uid}", isEqualTo: true)
.get();
if (querysnapshot.docs.length > 0) {
var docdata = querysnapshot.docs[0].data();
ChatRoomModel existingchatroom =
ChatRoomModel.fromMap(docdata as Map<String, dynamic>);
chatroom = existingchatroom;
} else {
//creating new chat room
ChatRoomModel newchatroommodel = ChatRoomModel(
chatroomid: DateTime.now().toString(),
participants: {
widget.userModel.uid.toString(): true,
targetuser.uid.toString(): true,
},
lastMessage: "Say Hi");
await FirebaseFirestore.instance
.collection("chatrooms")
.doc(newchatroommodel.chatroomid)
.set(newchatroommodel.toMap());
chatroom = newchatroommodel;
print("Created success");
}
return chatroom;
}
通过“
ChatRoomId
”删除整个聊天记录
FirebaseFirestore.instance
.collection("chatrooms/'your_chat_room_id'")
.delete()
.then((value_2) {
print('========> successfully deleted');
});
通过从“消息”集合中检索文档列表来计算消息数:
QuerySnapshot messages = await querysnapshot.docs[0].reference.collection("messages").get();
int messageCount = messages.size;