-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
129 lines (110 loc) · 3.63 KB
/
server.js
File metadata and controls
129 lines (110 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const http = require("http");
const path = require("path");
const express = require("express");
const { Server } = require("socket.io");
const bodyParser = require("body-parser");
const loadConfig = require('./load_config');
const { initializeApp } = require("firebase/app");
const {
doc,
setDoc,
getDoc,
getDocs,
updateDoc,
Timestamp,
collection,
arrayUnion,
getFirestore,
} = require("firebase/firestore");
const { firebaseConfig, PORT } = loadConfig();
const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.use(bodyParser.json());
app.use(express.static("public"));
app.get("/", async (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
/**
* Endpoint to retrieve chat messages for a specific chat room.
* If the chat room doesn't exist, it creates the room and sends a success message.
*
* @param {string} req.params.chatRoomId - The ID of the chat room to retrieve messages for.
* @returns {Object} JSON object containing the chat room data.
*/
app.get("/getChatMessages/:chatRoomId", async (req, res) => {
const requestedChatRoomId = req.params.chatRoomId;
try {
const chatRoomRef = collection(db, requestedChatRoomId);
let chatRoomSnapshot = await getDocs(chatRoomRef);
if (chatRoomSnapshot.size === 0) {
// Initialize new room if the chat room doesn't exist
const newChatRoomDoc = doc(db, requestedChatRoomId, "server");
setDoc(newChatRoomDoc, {
messages: [
{
timeStamp: Timestamp.fromDate(new Date()),
message: `Room ${requestedChatRoomId} has been successfully created.`,
},
],
name: "server",
});
chatRoomSnapshot = await getDocs(chatRoomRef);
}
const chatRoomData = chatRoomSnapshot.docs.map((doc) => doc.data());
res.json(chatRoomData);
} catch (error) {
const errorData = { error: error.message };
res.status(500).json(errorData);
}
});
/**
* Endpoint to handle sending a message in the chat room.
* If the user exists, the message is added to their messages.
* If the user does not exist, a new user is created.
*
* @param {string} req.body.userName - The username of the sender.
* @param {string} req.body.message - The message to be sent.
* @param {string} req.body.chatRoomId - The ID of the chat room to send the message to.
*/
app.post("/sendMessage", async (req, res) => {
const { userName, message, chatRoomId } = req.body;
try {
const userDocRef = doc(db, chatRoomId, userName);
const userSnapshot = await getDoc(userDocRef);
if (userSnapshot.exists()) {
// Update existing user's messages
await updateDoc(userDocRef, {
messages: arrayUnion({
timeStamp: Timestamp.fromDate(new Date()),
message: message,
}),
});
} else {
// Create a new user with the provided message
await setDoc(userDocRef, {
messages: [{ timeStamp: Timestamp.fromDate(new Date()), message }],
name: userName,
});
}
res.status(200).send("Message sent successfully.");
} catch (error) {
res.status(500).send("Error sending message.");
}
});
server.listen(PORT, () => {
console.log(`\nServer is running on http://localhost:${PORT}`);
});
io.on("connection", (socket) => {
socket.on("joinRoom", (roomId) => {
socket.join(roomId);
});
socket.on("leaveRoom", (roomId) => {
socket.leave(roomId);
});
socket.on("sendMessageToRoom", ({ roomId, message }) => {
io.to(roomId).emit("broadcastMessageToRoom", { message });
});
});