0% found this document useful (0 votes)
3 views3 pages

Chat

The document contains a set of functions for managing chat applications using Firebase Firestore. It includes functionalities to get users, messages, and chats; send, delete, and update messages; create and manage chats; and handle chat participants. Each function is designed to handle asynchronous requests and return appropriate responses based on the success or failure of the operations.

Uploaded by

Biju aryal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Chat

The document contains a set of functions for managing chat applications using Firebase Firestore. It includes functionalities to get users, messages, and chats; send, delete, and update messages; create and manage chats; and handle chat participants. Each function is designed to handle asynchronous requests and return appropriate responses based on the success or failure of the operations.

Uploaded by

Biju aryal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

const admin = require("firebase-admin");

const db = admin.firestore();

// get all users for chat


exports.getUsers = async (req, res) => {

// get all messages for a chat


exports.getMessages = async (req, res) => {
const { chatId } = req.params;
try {
const messagesRef = db.collection("messages").where("chatId", "==",
chatId);
const snapshot = await messagesRef.get();
if (snapshot.empty) {
return res.status(404).json({ message: "No messages found" });
}
const messages = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
return res.status(200).json(messages);
} catch (error) {
console.error("Error getting messages: ", error);
return res.status(500).json({ message: "Internal server error" });
}
}
// send a message
exports.sendMessage = async (req, res) => {
const { chatId, senderId, receiverId, message } = req.body;
try {
const newMessage = {
chatId,
senderId,
receiverId,
message,
timestamp: admin.firestore.FieldValue.serverTimestamp()
};
const messageRef = await db.collection("messages").add(newMessage);
return res.status(201).json({ id: messageRef.id, ...newMessage });
} catch (error) {
console.error("Error sending message: ", error);
return res.status(500).json({ message: "Internal server error" });
}
}
// delete a message
exports.deleteMessage = async (req, res) => {
const { messageId } = req.params;
try {
await db.collection("messages").doc(messageId).delete();
return res.status(200).json({ message: "Message deleted successfully" });
} catch (error) {
console.error("Error deleting message: ", error);
return res.status(500).json({ message: "Internal server error" });
}
}
// update a message
exports.updateMessage = async (req, res) => {
const { messageId } = req.params;
const { message } = req.body;
try {
await db.collection("messages").doc(messageId).update({ message });
return res.status(200).json({ message: "Message updated successfully" });
} catch (error) {
console.error("Error updating message: ", error);
return res.status(500).json({ message: "Internal server error" });
}
}
// get all chats for a user
exports.getChats = async (req, res) => {
const { userId } = req.params;
try {
const chatsRef = db.collection("chats").where("participants", "array-
contains", userId);
const snapshot = await chatsRef.get();
if (snapshot.empty) {
return res.status(404).json({ message: "No chats found" });
}
const chats = snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
return res.status(200).json(chats);
} catch (error) {
console.error("Error getting chats: ", error);
return res.status(500).json({ message: "Internal server error" });
}
}
// create a chat
exports.createChat = async (req, res) => {
const { participants } = req.body;
try {
const newChat = {
participants,
timestamp: admin.firestore.FieldValue.serverTimestamp()
};
const chatRef = await db.collection("chats").add(newChat);
return res.status(201).json({ id: chatRef.id, ...newChat });
} catch (error) {
console.error("Error creating chat: ", error);
return res.status(500).json({ message: "Internal server error" });
}
}
// delete a chat
exports.deleteChat = async (req, res) => {
const { chatId } = req.params;
try {
await db.collection("chats").doc(chatId).delete();
return res.status(200).json({ message: "Chat deleted successfully" });
} catch (error) {
console.error("Error deleting chat: ", error);
return res.status(500).json({ message: "Internal server error" });
}
}
// update a chat
exports.updateChat = async (req, res) => {
const { chatId } = req.params;
const { participants } = req.body;
try {
await db.collection("chats").doc(chatId).update({ participants });
return res.status(200).json({ message: "Chat updated successfully" });
} catch (error) {
console.error("Error updating chat: ", error);
return res.status(500).json({ message: "Internal server error" });
}
}
// get chat details
exports.getChatDetails = async (req, res) => {
const { chatId } = req.params;
try {
const chatRef = db.collection("chats").doc(chatId);
const chatDoc = await chatRef.get();
if (!chatDoc.exists) {
return res.status(404).json({ message: "Chat not found" });
}
return res.status(200).json({ id: chatDoc.id, ...chatDoc.data() });
} catch (error) {
console.error("Error getting chat details: ", error);
return res.status(500).json({ message: "Internal server error" });
}
}
// get chat participants
exports.getChatParticipants = async (req, res) => {
const { chatId } = req.params;
try {
const chatRef = db.collection("chats").doc(chatId);
const chatDoc = await chatRef.get();
if (!chatDoc.exists) {
return res.status(404).json({ message: "Chat not found" });
}
const chatData = chatDoc.data();
return res.status(200).json(chatData.participants);
} catch (error) {
console.error("Error getting chat participants: ", error);
return res.status(500).json({ message: "Internal server error" });
}
}
// add participant to chat
exports.addParticipantToChat = async (req, res) => {
const { chatId } = req.params;
const { participantId } = req.body;
try {
const chatRef = db.collection("chats").doc(chatId);
await chatRef.update({
participants: admin.firestore.FieldValue.arrayUnion(participantId)
});
return res.status(200).json({ message: "Participant added successfully" });
} catch (error) {
console.error("Error adding participant to chat: ", error);
return res.status(500).json({ message: "Internal server error" });
}
}

You might also like