forked from opencodeiiita/Geek-Chat
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
189 lines (164 loc) · 4.88 KB
/
app.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const path = require("path");
const http = require("http");
const express = require("express");
const socketio = require("socket.io");
const cors = require("cors"); //make requests from one website to another website in the browser
const moment = require("moment");
const fileUpload = require('express-fileupload')
const indexRoutes= require("./routes/index")
const imageRoutes= require("./routes/image")
const app = express();
const server = http.createServer(app);
const io = socketio(server);
const compileSass = require('compile-sass');
const { sanitizeAndRenderMessage } = require("./utils/message");
const { formatMessage } = require("./models/message");
const { usersArr, newUser, currentUserData } = require("./utils/users");
const { roomMembersCount } = require("./utils/roomMembersCount");
// Json
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
app.use(
"/css/:cssName",
compileSass.setup({
sassFilePath: path.join(__dirname, "public/scss/"),
sassFileExt: "scss",
embedSrcMapInProd: true,
resolveTildes: true,
nodeSassOptions: {
errLogToConsole: true,
noCache: true,
force: true,
},
})
);
app.use(fileUpload())
app.use(cors());
app.use(express.static(path.join(__dirname, "public")));
//routes
app.use("/", indexRoutes);
app.use("/", imageRoutes);
const roomDetails = io.of("/roomMembers");
roomDetails.on("connection", (socket) => {
roomDetails.emit("roomMembersCount", roomMembersCount);
});
io.on("connection", (socket) => {
//Validating user
//Connecting the user to the room
//Greeting message in the room user has joined
let { usrnm, room, profilePhoto } = currentUserData;
if (usrnm != undefined && room != undefined) {
roomMembersCount[room]++;
roomDetails.emit("countUpdate", {
room,
count: roomMembersCount[room],
});
newUser(usrnm, room, socket.id, profilePhoto);
socket.join(room);
socket.emit("roomJoined", {
room,
username: usrnm
});
}
socket.emit(
"message",
formatMessage("", "GeekChat Bot", "*Welcome to GeekChat!*", "")
);
socket
.to(room)
.emit(
"message",
formatMessage("", "GeekChat Bot", `**${usrnm}** entered the chat!`, "", profilePhoto)
);
let userList = usersArr.filter((ob) => ob.room === room);
socket.emit("userList", userList);
socket.to(room).emit("userJoined", {
id: socket.id,
username: usrnm,
profilePhoto:profilePhoto
});
//When a user leaves the room
//Disconnecting the user from the room
//Message in the room user has disconnected
socket.on("disconnect", () => {
roomMembersCount[room]--;
roomDetails.emit("countUpdate", {
room,
count: roomMembersCount[room],
});
io.in(room).emit("userLeft", {
id: socket.id,
username: usrnm,
});
let userIndex = -1, user;
for (const [index, userObj] of usersArr.entries()) {
if (userObj.session_id === socket.id) {
userIndex = index;
user = userObj;
break;
}
}
if (userIndex !== -1) {
usersArr.splice(userIndex, 1);
io.in(user.room).emit(
"message",
formatMessage("", "GeekChat Bot", `**${user.name}** disconnected.`, "", user.profilePhoto)
);
}
});
socket.on("chatMessage", (messageObject) => {
// When a user sends a chat message
// An id is created for the message
// Validating user and sending the message in the room
let user = usersArr.find((ob) => ob.session_id === socket.id);
let messageID = user.name + "_" + new Date().getTime();
if (user) {
io.in(user.room).emit(
"message",
formatMessage(
messageID,
user.name,
messageObject.msg,
messageObject.userID,
user.profilePhoto,
)
);
}
});
socket.on("deleteChatMsg", (msgId) => {
// When a user deletes a message
// Message id is verified
// Message is deleted
if (msgId == null || msgId == undefined) {
return;
}
let user = usersArr.find((ob) => ob.session_id === socket.id);
if (user) {
io.in(user.room).emit("deleteMsgFromChat", msgId);
}
});
//Event listner for typing
socket.on("typing", (info) => {
if (info.name !== undefined || info.name !== null) {
socket.broadcast.to(info.room).emit("typing", info.name);
}
});
//event listner for edit msg
socket.on("edited-msg", (info) => {
//find user
let user = usersArr.find((ob) => ob.session_id === socket.id);
//check user
if (user != null || user != undefined) {
if (info.text !== undefined || info.id !== undefined) {
info = { id: info.id, text: sanitizeAndRenderMessage(info.text, true), time: moment().valueOf() };
io.in(user.room).emit("edit-msg", info);
}
}
});
});
const port = process.env.PORT || 3000;
server.listen(port, () => console.log(`Server running on port ${port}`));