-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
279 lines (236 loc) · 9.91 KB
/
server.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const port = 5000;
const express = require("express");
const http = require("http");
const app = express();
const server = http.createServer(app);
const { MongoClient } = require("mongodb");
const bodyParser = require("body-parser");
const session = require("express-session");
const socketio = require("socket.io");
const cookieParser = require("cookie-parser");
const io = socketio(server);
const secret = require("./secrets");
//connectin to the mongodb database...............................................................
const uri = secret.serverUrl; // Use your own database..
const client = new MongoClient(uri);
// ...............................................................................................
const activeUsers = new Set();
/* var savedMessages = [];
connectingDatabase("saved_messages",{}).then(
(result) => {
savedMessages = result;
console.log("First time looking at saved messages");
console.log(savedMessages);
}
).catch(console.log).finally(() => client.close());
var groupsAvailable = [];
connectingDatabase("room_details",{},{name:1,members:1,_id:0})
.then((result) => {
groupsAvailable = result
return groupsAvailable;
}).then(console.log)
.catch(console.log).finally(() => client.close()); */
app.use(express.static(__dirname + '/public'));
/* app.use(session({
name: session_name,
resave: false,
saveUninitialized: false,
secret: "killerboy123",
cookie:{
maxAge: 2536000000,
sameSite: true,
secure: false
}
})); */
// ........................Middlewares-for-Processing-Client-Connections......................................
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(cookieParser());
// #############################################################################################################
// >>>>>>>>................Setting-Up-Socket.io-For-Chat-Messages...................>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
io.on("connection",(socket) =>{
console.log("New socket connection");
var presenttime = new Date().toLocaleTimeString().replace(/(?!:\d\d:)(:\d\d)/,"");
socket.on("joinMessage",(message) =>{
activeUsers.add(message.user_name);
// for saved messages and for saving messages
/* savedMessages.forEach(mess =>{
if(mess.group == message.group && (mess.usersToGet.includes(message.user_name))){
var toSend = mess.message;
toSend.time = getPresentTime();
socket.emit("serveMessage",toSend);
var messId = mess._id;
var userToRemove = message.user_name;
updateItemsInDatabase("saved_messages",{"_id": messId},{$pull: {"usersToGet": userToRemove }})
.then(console.log).catch(console.log).finally(() => client.close());
}
});
connectingDatabase("saved_messages",{}).then(
(result) => {
savedMessages = result;
console.log("Second time looking at saved messages");
console.log(savedMessages);
}
).catch(console.log).finally(() => client.close());
// ................................................
console.log(activeUsers); */
console.log("Join message from client: " + message.message + message.group);
socket.join(message.group);
var initialMessage = {user_name: "Server",
time: presenttime ,message:`${message.user_name} joined the chat`}
console.log(initialMessage);
socket.broadcast.to(message.group).emit("initialmessage",initialMessage);
});
socket.on("message",(message) =>{
/* var availableMembers = Array.from(activeUsers);
var membersOfGroup = groupsAvailable.filter(groupThing => groupThing.name == message.group )[0].members;
var notAvailableMembers = membersOfGroup.filter(member => !availableMembers.includes(member));
console.log("Not available members are : " + notAvailableMembers); */
var presenttime = new Date().toLocaleTimeString().replace(/(?!:\d\d:)(:\d\d)/,"");
message.time = presenttime;
io.to(message.group).emit("serveMessage",message);
/* if(notAvailableMembers){
addItemsToDatabase("saved_messages",{"group": message.group,"usersToGet":notAvailableMembers,"message":message})
.then(console.log).catch(console.log).finally(() => client.close());
} */
});
var disconMessage = {user_name:"Server",
time:"", message:"Someone disconnected"};
socket.on("disconnect",() => {
io.emit("message",disconMessage);
});
});
// ......................................................................................................
// ..................................Functions-for-Database-connection..........................................................
async function connectingDatabase(collectionName,query,projection){
await client.connect();
console.log("connected successfully to the server");
const collection = client.db("appusers").collection(collectionName);
if(projection){
const result = await collection.find(query).project(projection).toArray();
return result;
} else{
const result = await collection.find(query).toArray();
return result;
}
}
async function addItemsToDatabase(collectionName,item){
await client.connect();
console.log("Connected to database for adding items");
const collection = client.db("appusers").collection(collectionName);
const result = await collection.insertOne(item);
return result;
}
async function updateItemsInDatabase(collectionName,item,update){
await client.connect();
const collection = client.db("appusers").collection(collectionName);
const result = await collection.updateOne(item,update);
return result;
}
// #############################################################################################################
// database connection and querying.............................................................................
/* connectingDatabase()
.then(console.log)
.catch(console.log)
.finally(() => client.close()); */
//.............................................................................................................
// ...........................ROUTING-TAKES-PLACE-HERE.........................................................
app.get("/",(req,res)=> {
res.sendFile("./views/index.html",{root: __dirname});
});
app.get("/messages",(req,res)=>{
// console.log(req.params['name']);
console.log(req.query);
console.log(req.cookies.username);
// console.log("user session " + req.session.userName)
// if(req.session.userName){
res.sendFile("./views/messages.html",{root: __dirname});
// }else{
// res.redirect(301,"/login");
//}
});
app.get("/login",(req,res)=>{
res.sendFile("./views/login.html",{root: __dirname});
});
app.get("/register",(req,res)=>{
// console.log(req.cookies);
res.sendFile("./views/register.html",{root: __dirname});
});
app.get("/rooms",(req,res) => {
res.sendFile("./views/roomjoin.html",{root: __dirname});
});
app.post("/room",(req,res) => {
console.log(req.body);
var groups = req.body
var password = groups.password;
var cookieUser = req.cookies.username;
var itemFound = "";
connectingDatabase("room_details",{name: groups.groupnames},{password:1,members:1,_id:0})
.then((result) => {
console.log(result);
if(result[0] != undefined){
result.forEach((item) => {
if(item.password === password){
res.cookie("roomname", groups.groupnames,{maxAge: 2592000000});
itemFound = item;
// ..................................................................
/* if(!itemFound.members.includes(cookieUser)){
updateItemsInDatabase("room_details",{"name":groups.groupnames},{$push:{"members": cookieUser}})
.then(console.log).catch(console.log).finally(() => client.close())
} */
res.redirect(301,`/messages?username=${cookieUser}&group=${groups.groupnames}`);
}else{
res.redirect(301,"/rooms");
}
})
}else{
res.redirect(301,"/login");
}
})
.catch(console.log).finally(() => client.close());
});
app.post("/submit-user",(req,res)=>{
var creds = req.body;
var userCreds = {
username: creds.username,
email: creds.email,
password: creds.password,
ip_addr: req.ip
};
// console.log(userCreds);
addItemsToDatabase("user_details",userCreds).then(console.log).catch(console.log).finally(() => client.close());
res.cookie("username",creds.username,{maxAge: 2592000000});
res.redirect(301,`/rooms`);
});
app.post("/confirm-user",(req,res) => {
var username = req.body.username;
var password = req.body.password;
connectingDatabase("user_details",{username: username},{password:1})
.then(result => {
console.log(result);
if(result[0] != undefined){
result.forEach((item) => {
if(item.password === password){
res.cookie("username",username,{maxAge: 2592000000});
res.redirect(301,"/rooms");
}else{
res.redirect(301,"/login");
}
})}else{
res.redirect(301,"/register");
}
}).catch(console.log);
});
app.get("/logout",(req, res) => {
res.clearCookie('username','roomname');
res.redirect(301,"/login");
});
// ...............................................................................................................
// listening on port 5000.........................................
server.listen(port,() =>{
console.log(`server is running on port ${port}`);
})
function getPresentTime(){
var currentTime = new Date().toLocaleTimeString().replace(/(?!:\d\d:)(:\d\d)/,"");
return currentTime;
}