-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.js
More file actions
475 lines (371 loc) · 10.2 KB
/
final.js
File metadata and controls
475 lines (371 loc) · 10.2 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
const express = require('express');
const bodyParser = require("body-parser");
const customId = require("custom-id");
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const ServerKey = 3000;
var idnames = [];
var idee = [];
app.set('view engine', 'ejs');
require('dotenv').config()
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get('/chat', (req, res) => {
var uniqueID = customId({
randomLength: 2
});
// io.emit('Custom-key',uniqueID);
res.render('pages/startpage',{
Unique : uniqueID
});
});
app.post('/', (req, res) => {
// var name = req.body.user;
res.redirect('/');
});
app.get('/',(req,res) => {
res.redirect('/chat');
});
app.get('/chat/:id/:harsh', (req,res) => {
var hp = req.params.harsh;
var ide = req.params.id;
if((hp=="")||(ide==""))
res.redirect('/chat');
find_connection(ide).then(friend => {
// console.log(friend);
res.render('pages/index', {
NAME : friend,
naam: hp,
check: ide
} );
})
});
//Socket Connections
io.on('connection', (socket) => {
// console.log('Finaly worked :'+ socket.id +' user connected');
socket.on('makeid', (naam,userID,secret) => {
// console.log(naam+" "+userID+" "+secret);
insertID(naam,userID,secret);
});
socket.on('Online I', (user, id) => {
let active = {
"Name": user,
"ID": id,
"Socket":socket.id
}
idnames.push(active);
idee.push(id);
// console.log(idnames);
// console.log(idee);
});
socket.on('Add_Connection', (fromid, toid) => {
// console.log("From : "+fromid+" , To : "+toid);
add_connection(fromid,toid);
});
socket.on("private message",(from,to,fromid,toid,message,time) => {
// console.log("From :"+from);
// console.log("To :"+to);
// console.log("Message :"+message);
// console.log("Time :"+time);
// console.log(fromid+" : "+toid);
if(toid==="Back End Developer"){
toid = process.env.AdminID
}
send_chat(from,to,fromid,toid,message,time);
let k;
let abc = [
{
"From" : from,
"Message" : message,
"Time" : time
}
];
// console.log(idnames);
// if(idee.includes(toid)){
// for(let i=0;i<idnames.length;i++){
// if(idnames[i]["ID"] === toid){
// // console.log(idnames[i].Socket);
// k = idnames[i].Socket;
// io.to(k).emit("send text", abc);
// }
// }
// }
let work = "send text";
socket_worker(work,toid,abc)
// console.log(k);
});
socket.on("get chat",(fromid,toid) => {
// console.log("From :"+from);
// console.log("To :"+to);
// console.log("Message :"+message);
// console.log("Time :"+time);
// console.log(fromid+" : "+toid);
if(toid==="Back End Developer"){
toid = process.env.AdminID
}
search_chat(fromid,toid).then( data => {
io.emit("send text",data);
});
// console.log(ab);
});
socket.on('disconnect', () => {
console.log("Disconnecttttt :"+socket.id);
for (let i = 0; i < idnames.length; i++) {
if((idnames[i]["Socket"] == socket.id)){
let k = idnames[i].ID;
if(idee.includes(k))
arrayRemove(idee,k);
idnames.splice(i,1);
break;
}
}
});
});
app.post('/',(req,res) => {
console.log("Posttt");
});
const {MongoClient} = require("mongodb");
const url = process.env.MongoURL;
const client = new MongoClient(url);
const dbName = "WebChat";
const dbChat = "Chatting";
const collect_IDs = "UserIDs";
const collect_connection = "Connections";
async function insertID(naam,user_ID,secret_){
try{
let result = await client.connect();
let db = result.db(dbName);
let collection = db.collection(collect_IDs);
// let query = { id: 6};
const Account = {
Name:naam,
UserIDs: user_ID,
SecretKey: secret_
};
const res = await collection.insertOne(Account);
console.log("Values are Inserted!!");
// console.log(secret_);
add_connection(process.env.AdminID,secret_);
}
finally{
await client.close();
}
}
async function find_friend(frie_id){ //Imported
try{
let result = await client.connect();
let db = result.db(dbName);
let collection = db.collection(collect_IDs);
let response = await collection.find({"SecretKey":frie_id}).toArray();
if(response.length)
return response[0].Name;
else
return 0;
}
finally{
await client.close();
}
}
async function check_connection(myid,toid){
try{
let result = await client.connect();
let db = result.db(dbName);
let collection = db.collection(collect_connection);
let query = {
$or: [
{ 'Connection': { $regex: `^${myid}:${toid}` } },
{ 'Connection': { $regex: `^${toid}:${myid}` } }
]
};
let check = await collection.find(query).toArray();
return check.length;
}
finally{
await client.close();
}
}
async function find_connection(myid){
try{
let result = await client.connect();
let db = result.db(dbName);
let collection = db.collection(collect_connection);
let query = {
$or: [
{ 'Connection': { $regex: `^${myid}:` } },
{ 'Connection': { $regex: `:${myid}` } }
]
};
let Connecter = [];
let friend = await collection.find(query).toArray();
// let response2 = await collection.find(Endquery).toArray();
for(let i=0;i<friend.length;i++){
let k;
let pusher;
if(friend[i].FromID === myid){
k=friend[i].ToID;
if(k === process.env.AdminID)
continue;
pusher = {
"Name" : friend[i].ToUser,
"ID" : k
}
}else{
k=friend[i].FromID;
if(k === process.env.AdminID)
continue;
pusher = {
"Name" : friend[i].FromUser,
"ID" : k
}
}
Connecter.push(pusher);
}
return Connecter;
}
finally{
await client.close();
}
}
async function add_connection(from_id,To_id){ //Imported - ADD CONNECT
try{
let result = await client.connect();
let db = result.db(dbName);
let collection = db.collection(collect_connection);
let from_name = await find_friend(from_id);
let Connecter = sorter(from_id,To_id);
let a = check_same(from_id,To_id);
if(a==0){
let To_name = await find_friend(To_id);
if(To_name!=0){
var Connect = {
"FromUser": from_name,
"FromID": from_id,
"ToUser": To_name,
"ToID": To_id,
"Connection": Connecter
};
let check = await check_connection(from_id,To_id);
if(check==0){
await client.connect();
let response = await collection.insertOne(Connect);
// console.log("Connection Built in "+from_id+" & "+To_id);
// console.log("Connection Built in "+from_name+" & "+To_name);
// console.log(Connect);
}
// else
// console.log('Already have Connection');
}
// else{
// console.log("Person don't exist!!!, wrong friend ID!!");
// }
}
// else{
// console.log("Same ID: thats why error!!!");
// }
}
finally{
await client.close();
}
}
//----------------For CHATTING--------------------
async function send_chat(from_name,To_name,from_id,to_id,message,time){
try{
let result = await client.connect();
let db = result.db(dbChat);
let coll = sorter(from_id,to_id);
let collection = db.collection(coll);
// console.log("Sending coll"+coll);
let mess = {
"FromUser": from_name,
"FromID": from_id,
"ToUser": To_name,
"ToID": to_id,
"Message": message,
"Time":time
};
let response = await collection.insertOne(mess);
// console.log("Message Sent from "+from_id+" to "+to_id);
}
finally{
await client.close();
}
}
async function search_chat(from_id,to_id){
try{
let result = await client.connect();
let db = result.db(dbChat);
let coll = sorter(from_id,to_id);
let collection = db.collection(coll);
// console.log("receiveing coll"+coll);
let ab = [];
let response = await collection.find().toArray();
// console.log("Search of chat is done :"+response.length);
if(response.length){
for(let i=0;i<response.length;i++){
let a = response[i].FromID;
let b = response[i].Message;
let c = response[i].Time;
let abc = {
"From" : a,
"Message" : b,
"Time" : c
}
ab.push(abc);
}
// console.log(ab);
return ab;
}
else
return response;
}
finally{
await client.close();
}
}
//Functionsss
function check_same(id1,ide2){
// let a=0;
if (id1 == ide2 ){
return 1;
}
else
return 0;
}
const customOrder = process.env.Order;
function sorter(one,two){
let result;
for (let i = 0; i < Math.min(one.length, two.length); i++) {
let charA = one[i];
let charB = two[i];
let indexA = customOrder.indexOf(charA);
let indexB = customOrder.indexOf(charB);
if (indexA !== indexB) {
result = Math.min(indexA,indexB);
result = indexA < indexB ? one+":"+two : two+":"+one ;
break;
}
}
return result;
}
function arrayRemove(arr, value) {
return arr.filter(function (geeks) {
return geeks != value;
});
}
function socket_worker(work,To,data){
if(idee.includes(To)){
let n = idnames.length;
for(let i=0;i<n;i++){
if(idnames[i]["ID"] === To){
// console.log(idnames[i].Socket);
k = idnames[i].Socket;
io.to(k).emit(work, data);
}
}
}
}
server.listen(ServerKey,()=>{
console.log("Server at port ",ServerKey," !!!");
});