-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
313 lines (300 loc) · 9.85 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
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
/**
* Created by Shubham on 07-03-2017.
*/
var express = require('express'),
app = express(),
http = require('http').createServer(app),
io = require('socket.io').listen(http),
path = require('path'),
assert= require('assert'),
MongoClient = require('mongodb').MongoClient,
bodyParser = require('body-parser'),
session=require('express-session'),
_=require('lodash'),
atob=require('atob');
var dburl = 'mongodb://localhost:27017/mydb',
crud= require('./dbutil/CRUD')(),
util=require('./server/util')(),
db;
MongoClient.connect(dburl, function(err, database) {
assert.equal(null, err);
db = database;
http.listen(6768);
});
app.use('/static', express.static('public'));
app.use('/node_modules', express.static('node_modules'));
app.use('/server', express.static('server'));
app.use(session({
secret:"S3cR3t",
resave:false,
saveUninitialized:true}));
app.use(bodyParser.json());
io.on('connection', function (socket) {//code in work
console.log("a connection established");
socket.on('chat message', function (data) {
console.log("in chat message event server side");
var room=util.getConversationId();
socket.join(room);
crud.storeMessage(db,'conversation',room,data, function(err){
console.log("storing message");
if(err==null)
io.to(room).emit('new message', data);
});
});
});
app.post('/restoreOldMessages',function(req,res){//code in work
util.checkOrSetConversationId(db,'conversation',req.session.user,req.body.user,function(err){
if(err!=null)
console.log("error");
var id=util.getConversationId();
crud.getMessages(db,'conversation',id,function(err,result){
console.log("getting messages");
if(result.length==1)
res.send(result[0].messages);
else
res.send([]);
});
/*crud.getMessages(db,'conversation',id,function(err,result){
});
something like that
aggregate or find
$match, $unwind, $match, $sort, limit(50), display only messages
*/
});
console.log("in restore")
});
app.get('/getSessionUser',function(req,res){
if(req.session.user)
res.send(req.session.user);
else
res.send(null);
});
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/public/main/index.html'));
});
app.get('/showPendingInvites', function(req,res){
crud.listPendingInvites(db,'invitation', req.session.user, function(err,result){
res.send(result);
})
});
app.get('/showSentInvites', function(req,res){
crud.listSentInvites(db,'invitation', req.session.user, function(err,result){
res.send(result);
})
});
app.get('/logout', function(req,res){
req.session.destroy();
console.log("Session destroyed");
res.send("success");
});
app.post('/isLoggedIn',function(req,res){ //to check user is logged in or not at routing(resolve)
var payload;
if(req.body.token){
payload = (req.body.token).split('.')[1];
payload = atob(payload);
payload = JSON.parse(payload);
}
console.log(payload);
console.log(req.session);
if(payload==undefined)
res.send({"isLoggedIn":false});
else{
if(payload.username==req.session.user)
res.send({"isLoggedIn":true});
else
res.send({"isLoggedIn":false});
}
});
app.post('/acceptInvitation', function(req,res){
crud.acceptRejectAgainInvitation(db, 'invitation', req.body.user, req.session.user, 'accepted', function(err){
var obj= util.sendResponse();
if (err!=null){
obj.error = true;
obj.success = false;
}
res.send(obj);
})
});
app.post('/rejectInvitation', function(req,res){
crud.acceptRejectAgainInvitation(db, 'invitation', req.body.user, req.session.user, 'rejected', function(err){
var obj= util.sendResponse();
if (err!=null){
obj.error = true;
obj.success = false;
}
res.send(obj);
})
});
app.post('/sendInvitationAgain', function(req,res){
crud.acceptRejectAgainInvitation(db, 'invitation', req.session.user, req.body.user, "pending", function(err){
var obj= util.sendResponse();
if (err!=null){
obj.error = true;
obj.success = false;
}
res.send(obj);
});
});
app.post('/cancelPending', function(req,res){
crud.cancelPending(db, 'invitation', req.body.user, req.session.user, function(err){
var obj= util.sendResponse();
if (err!=null){
obj.error = true;
obj.success = false;
}
res.send(obj);
})
});
app.post('/cancelSent', function(req,res){
crud.cancelSent(db, 'invitation', req.session.user, req.body.user, function(err){
var obj= util.sendResponse();
if (err!=null){
obj.error = true;
obj.success = false;
}
res.send(obj);
})
});
app.post('/withdraw', function(req,res){
crud.withdraw(db,'invitation',req.session.user,req.body.user,function(err){
var obj=util.sendResponse();
if(err!=null){
obj.error=true;
obj.success=false;
}
res.send(obj);
})
});
app.post('/addUser', function (req,res) {
crud.create(db, 'userAdd', req.body, function(err){
var obj= util.sendResponse();
if (err!=null){
obj.error = true;
obj.success = false;
}
else if(err==null) {
req.session.user = req.body.username;
obj.token=util.generatejwt(req.body.username);
console.log("Session created for "+ req.session.user);
console.log(obj.token);
}
res.send(obj);
});
});
app.post('/checkUser', function (req,res) {
crud.isPresent(db, 'userAdd', req.body, function(err,result){
var obj=util.sendResponse();
if(err!=null){
obj.error=true;
obj.success=false;
}
else {
if (result.length == 1) {
req.session.user=req.body.username;
obj.token=util.generatejwt(req.body.username);
console.log("Session created for "+ req.session.user);
obj.responseObj = result;
}
else{
obj.error=true;
obj.success=false;
}
}
res.send(obj);
})
});
app.post('/forgot',function(req,res){
crud.findUsername(db, 'userAdd', req.body.username, function(err, result){
res.send(result);
})
});
app.post('/matchAnswer',function(req,res){
crud.matchAnswer(db, 'userAdd', req.body).then(function (count){
var obj= util.sendResponse();
if(count!=1){
obj.error=true;
obj.success=false;
}
res.send(obj);
})
});
app.post('/updatePassword', function(req,res){
crud.updatePassword(db, 'userAdd', req.body, function(err){
var obj= util.sendResponse();
if (err!=null){
obj.error = true;
obj.success = false;
}
res.send(obj);
})
});
app.post('/createContact', function(req,res){
crud.create(db,'contact',req.body,function(err){
var obj=util.sendResponse();
if(err!=null){
obj.error=true;
obj.success=false;
}
res.send(obj);
})
});
app.post('/checkUsername', function (req,res) {
crud.isUsername(db, 'userAdd', req.body.username).then(function(count){
var obj= util.sendResponse();
if(count!=0) {
obj.error = true;
obj.success = false;
}
res.send(obj);
});
});
app.post('/search', function(req,res){
var result1=[],result2=[],result3=[],results=[];
crud.listUsers(db,'userAdd', req.body.search, req.session.user, function(err,result){
result1=result1.concat(result);
crud.listFriends(db, 'friends', req.session.user, function (err, result) {
result2 = result2.concat(result);
crud.listPendingOnes(db,'invitation',req.session.user,function(err,result){
result3=result3.concat(result);
for(var i=0;i<result1.length;i++){
if(result2.length!=0) {
for (var j = 0; j < result2[0].friends.length; j++) {
if (result1[i].username == result2[0].friends[j]) {
result1[i].isFriend = true;//chat option
results.push(result1[i]);
}
}
}
if(result3.length!=0) {
for (var j = 0; j < result3.length; j++) {
if (result1[i].username == result3[j].fromUser) {
result1[i].isFromUser = true;//accept invitation option
results.push(result1[i]);
}
if (result1[i].username == result3[j].toUser) {
result1[i].isToUser = true;//check circle option
results.push(result1[i]);
}
}
}
}
results=results.concat(result1);
results= _.uniqBy(results,'username');
res.send(results);
})
});
});
});
app.post('/sendInvitation', function (req,res) {
var invitationObj= util.declareInvitationObj();
invitationObj.fromUser= req.session.user;
invitationObj.toUser= req.body.user;
crud.create(db, 'invitation', invitationObj, function(err){
var obj= util.sendResponse();
if (err!=null){
obj.error = true;
obj.success = false;
}
res.send(obj);
});
});