forked from PAN001/QuickNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
499 lines (449 loc) · 20.6 KB
/
Copy pathapp.js
File metadata and controls
499 lines (449 loc) · 20.6 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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
var port = Number(process.env.PORT || 3000);
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var expressMongoDb = require("express-mongo-db");
var app = express();
// make "node app.js" work on local machine, this use node's static file fetching
app.use('/', express.static ('./public'));
//general middleware
// app.use(bodyParser.urlencoded({ extended: true }));
// app.use(bodyParser.json()); // get information from html forms
// app.use(cookieParser()); // read cookies (needed for auth)
var server = http.createServer(app);
server.listen(3000, function(){
console.log('-----> SERVER STARTED ON PORT:', port, '<-----');
console.log('-----> PROCESS PID:', process.pid, '<-----');
});
app.use(expressMongoDb('mongodb://localhost:27017/NoteTakingApp'));
console.log("db starts");
app.use('/updateAll', bodyParser.text());
app.use('/register', bodyParser.text());
app.use('/logIn', bodyParser.text());
app.use('/share/addShareNotebook', bodyParser.text());
app.use('/share/addShareNote', bodyParser.text());
//app.use('/share/listShareNotes', bodyParser.text());
//app.use(bodyParser.json());
console.log("server starts");
app.use(function(req, res, next) {
console.log("run");
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
// res.header( 'Content-Type' ,'application/x-www-form-urlencoded; charset=UTF-8');
//res.header('Access-Control-Allow-Headers', 'X-Requested_With');
// console.log(req);
next();
});
app.post("/updateAll", function(req, res) {
// res.header('Access-Control-Allow-Origin', '*'); // implementation of CORS
console.log("updateAll activated");
// console.log(req);
console.log("Body is: " + req.body);
var parsedData = JSON.parse(req.body);
var UserId = parsedData.UserInfo.UserId;
console.log("The UserId is: " + parsedData.UserInfo.UserId);
//req.db.collection('allAppData').insert(parsedData, function(err, data) {
// if(err) console.log(err);
// else {
// console.log("data saved to db");
// }
//});
var query = {"UserInfo.UserId": UserId};
req.db.collection('allAppData').update(query, parsedData, {upsert: true}, function(err, data) {
if(err) {
console.log(err);
res.end('{"msg": "Synchronized failed", "status": "fail"}');
}
else {
console.log("data updated to db");
res.end('{"msg": "Synchronized successfully", "status": "success"}');
}
});
});
app.post("/register", function(req, res) {
// res.header('Access-Control-Allow-Origin', '*'); // implementation of CORS
console.log("register activated");
console.log("Body is: " + req.body);
var parsedData = JSON.parse(req.body);
//req.db.collection('allAppData').insert(parsedData, function(err, data) {
// if(err) console.log(err);
// else {
// console.log("data saved to db");
// }
//});
var findQuery = {"Email": parsedData.Email};
req.db.collection('registeredUsers').findOne(findQuery, function(err, data) {
if(err) {
console.log(err);
res.end('{"msg": "DB error", "status": "fail"}');
}
else {
if(data) { // if existent
res.end('{"msg": "Email has already been registered", "status": "fail"}');
}
else { // if not existent
req.db.collection('registeredUsers').insert(parsedData, function(err, data) {
if(err) {
console.log(err);
res.end('{"msg": "DB error", "status": "fail"}');
}
else {
console.log("data inserted to db");
res.end('{"msg": "Reistered successfully", "status": "success"}');
}
});
}
}
});
});
app.post("/logIn", function(req, res) {
// res.header('Access-Control-Allow-Origin', '*'); // implementation of CORS
console.log("logIn activated");
console.log("Body is: " + req.body);
var parsedData = JSON.parse(req.body);
var findQuery = {"Email": parsedData.Email};
req.db.collection('registeredUsers').findOne(findQuery, function(err, data) {
if(err) {
console.log(err);
res.end('{"msg": "DB error", "status": "fail"}');
}
else {
if(data) { // if existent
console.log("email found");
var findQuery2 = {"Email": parsedData.Email, "Password": parsedData.Password};
req.db.collection('registeredUsers').findOne(findQuery2, function(err, data) {
if(data) { // if existent
console.log("log in successfully");
// res.end('{"msg": "Log in successfully", "status": "success"}');
res.json(JSON.stringify(data));
}
else {
console.log("wrong password");
res.end('{"msg": "wrong password", "status": "fail"}');
}
});
}
else { // if not existent
console.log("not found email");
res.end('{"msg": "wrong email", "status": "fail"}');
}
}
});
});
app.post("/share/addShareNotebook", function(req, res) {
console.log("/share/addShareNotebook activated");
console.log("Body is: " + req.body);
var parsedData = JSON.parse(req.body);
var senderUserId = parsedData.SenderUserId;
var Email = parsedData.Email;
var NotebookId = parsedData.NotebookId;
var Perm = parsedData.Perm;
var query = {"UserInfo.UserId": senderUserId};
req.db.collection("allAppData").findOne(query, function(err, item) {
if(err) {
console.log("err is: " + err);
res.end('{"msg": "DB error", "status": "fail"}');
}
else {
if(item) {
console.log("Found sender");
var notebooks = item.notebooks;
var targetNotebook;
var senderUserInfo = item.UserInfo;
console.log("sender userId is " + senderUserInfo.UserId);
// var shareNotebooks = item.shareNotebooks;
var isFound = !1;
for(var i in notebooks) {
// console.log(i);
var curNotebook = notebooks[i];
if(curNotebook.NotebookId == NotebookId) {
console.log("Found notebook");
targetNotebook = curNotebook;
isFound = !0;
break;
}
}
if(!isFound) { // 没找到
console.log("Not found");
res.end('{"msg": "Not found notebook", "status": "fail"}');
}
else { // 找到了要分享的notebook
// 找receiver
var query = {"UserInfo.Email": Email};
req.db.collection("allAppData").findOne(query, function(err, item) {
if(err) {
console.log("err is: " + err);
res.end('{"msg": "DB error", "status": "fail"}');
}
else {
if(item) { // 找到了receiver
console.log("Found receiver");
var targetUserInfo = item.UserInfo;
var shareNotebooks = item.shareNotebooks;
var ToUserId = targetUserInfo.UserId;
console.log("receiver id is " + targetUserInfo.UserId);
// 更新目标ShareUserInfos
var anotherUserInfo = {};
anotherUserInfo.UserId = senderUserInfo.UserId;
anotherUserInfo.Email = senderUserInfo.Email;
anotherUserInfo.Verified = senderUserInfo.Verified;
anotherUserInfo.Username = senderUserInfo.Username;
anotherUserInfo.CreatedTime = senderUserInfo.CreatedTime;
anotherUserInfo.Logo = senderUserInfo.Logo;
anotherUserInfo.Theme = senderUserInfo.Theme;
anotherUserInfo.FromUserId = senderUserInfo.FromUserId;
anotherUserInfo.NoteCnt = senderUserInfo.NoteCnt;
anotherUserInfo.Usn = senderUserInfo.Usn;
console.log("anotherUserInfo email is " + anotherUserInfo.Email);
// 更新receiver的sharedUserInfos
var query = {"UserInfo.UserId": ToUserId};
req.db.collection('allAppData').update(query, {$addToSet:{"sharedUserInfos": anotherUserInfo}}, {upsert: true}, function(err, data) {
if(err) {
console.log(err);
res.end('{"msg": "DB error", "status": "fail"}');
}
else {
console.log("successfully update ShareUserInfos");
// 更新receiver的ShareNotebooks
var anotherNotebook = {};
anotherNotebook.ParentNotebookId = targetNotebook.ParentNotebookId;
anotherNotebook.Title = targetNotebook.Title;
anotherNotebook.UrlTitle = targetNotebook.UrlTitle;
anotherNotebook.NumberNotes = targetNotebook.NumberNotes;
anotherNotebook.IsBlog = targetNotebook.IsBlog;
anotherNotebook.UpdatedTime = targetNotebook.UpdatedTime;
anotherNotebook.Usn = targetNotebook.Usn;
anotherNotebook.IsDeleted = targetNotebook.IsDeleted;
anotherNotebook.ShareNotebookId = "";
anotherNotebook.ToUserId = ToUserId;
anotherNotebook.ToGroupId = "";
anotherNotebook.ToGroup = {};
anotherNotebook.Perm = Perm;
anotherNotebook.Subs = targetNotebook.Subs;
anotherNotebook.Seq = targetNotebook.Seq;
anotherNotebook.NotebookId = targetNotebook.NotebookId;
anotherNotebook.IsDefault = targetNotebook.IsDefault;
if(shareNotebooks.hasOwnProperty(senderUserId)) { // 如果receiver已经有过sender的share记录
shareNotebooks[senderUserId].push(anotherNotebook);
}
else {
console.log("new sender")
shareNotebooks[senderUserId] = [];
shareNotebooks[senderUserId].push(anotherNotebook);
}
var query = {"UserInfo.UserId": ToUserId};
req.db.collection('allAppData').update(query, {$set:{"shareNotebooks": shareNotebooks}}, {upsert: true}, function(err, data) {
if(err) {
console.log(err);
res.end('{"msg": "DB error", "status": "fail"}');
}
else {
console.log("successfully update ShareNotebooks");
res.end('{"msg": "Updated scuccessfully", "status": "success"}');
}
});
}
});
}
else {
console.log("Note found receiver");
res.end('{"msg": "receiver not found", "status": "fail"}');
}
}
});
}
}
else {
console.log("Note found sender");
res.end('{"msg": "sender not found", "status": "fail"}');
}
}
});
});
app.post("/share/addShareNote", function(req, res) {
console.log("/share/addShareNote activated");
console.log("Body is: " + req.body);
var parsedData = JSON.parse(req.body);
var senderUserId = parsedData.SenderUserId;
var Email = parsedData.Email;
var NoteId = parsedData.NotebookId;
var Perm = parsedData.Perm;
var query = {"UserInfo.UserId": senderUserId};
req.db.collection("allAppData").findOne(query, function(err, item) {
if(err) {
console.log("err is: " + err);
res.end('{"msg": "DB error", "status": "fail"}');
}
else {
if(item) {
console.log("Found sender");
var notebooks = item.notebooks;
var targetNotebook;
var senderUserInfo = item.UserInfo;
console.log("sender userId is " + senderUserInfo.UserId);
// var shareNotebooks = item.shareNotebooks;
var query = {"UserInfo.Email": Email};
req.db.collection("allAppData").findOne(query, function(err, item) {
if(err) {
console.log("err is: " + err);
res.end('{"msg": "DB error", "status": "fail"}');
}
else {
if(item) { // 找到了receiver
console.log("Found receiver");
var targetUserInfo = item.UserInfo;
var shareNotebooks = item.shareNotebooks;
var ToUserId = targetUserInfo.UserId;
console.log("receiver id is " + targetUserInfo.UserId);
// 更新目标ShareUserInfos
var anotherUserInfo = {};
anotherUserInfo.UserId = senderUserInfo.UserId;
anotherUserInfo.Email = senderUserInfo.Email;
anotherUserInfo.Verified = senderUserInfo.Verified;
anotherUserInfo.Username = senderUserInfo.Username;
anotherUserInfo.CreatedTime = senderUserInfo.CreatedTime;
anotherUserInfo.Logo = senderUserInfo.Logo;
anotherUserInfo.Theme = senderUserInfo.Theme;
anotherUserInfo.FromUserId = senderUserInfo.FromUserId;
anotherUserInfo.NoteCnt = senderUserInfo.NoteCnt;
anotherUserInfo.Usn = senderUserInfo.Usn;
console.log("anotherUserInfo email is " + anotherUserInfo.Email);
// 更新receiver的sharedUserInfos
var query = {"UserInfo.UserId": ToUserId};
req.db.collection('allAppData').update(query, {$addToSet:{"sharedUserInfos": anotherUserInfo}}, {upsert: true}, function(err, data) {
if(err) {
console.log(err);
res.end('{"msg": "DB error", "status": "fail"}');
}
else {
console.log("successfully update ShareUserInfos");
res.end('{"msg": "success", "status": "success", "ToUserId":' + '"' + ToUserId + '"' + '}');
// res.json(JSON.stringify(ToUserId));
}
});
}
else {
console.log("Note found receiver");
res.end('{"msg": "receiver not found", "status": "fail"}');
}
}
});
}
else {
console.log("Note found sender");
res.end('{"msg": "sender not found", "status": "fail"}');
}
}
});
});
app.get("/share/listShareNotes", function(req, res) {
console.log("/share/listShareNotes activated");
// var parsedData = JSON.parse(req.body);
// var UserId = parsedData.userId;
// var NotebookId = parsedData.notebookId;
var UserId = req.query.UserId;
var NotebookId = req.query.NotebookId;
var CurUserId = req.query.CurUserId; // 被share的user的id
// 分享的是一个整个笔记本
console.log("UserId is " + UserId);
console.log("NotebookId is " + NotebookId);
if(NotebookId && UserId) {
var query = {"UserInfo.UserId": UserId};
req.db.collection("allAppData").findOne(query, function(err, item) {
if(err) console.log("err is: " + err);
else {
if(item) {
console.log("Found User");
var allNotes = [];
if(NotebookId == "share0") { // if it is in default sharing notebook
console.log("this is single notes sharing")
var shareNotebookDefault = item.shareNotebookDefault;
var thisShareNotes = shareNotebookDefault[CurUserId];
if(thisShareNotes != undefined) {
var notes = item.notes;
for(var i in thisShareNotes) {
var curNoteId = thisShareNotes[i].NoteId;
for(var j in notes) {
if(curNoteId == notes[j].NoteId) {
allNotes.push(notes[j]);
}
}
}
}
else { // 不存在share给当前用户的笔记
console.log("Not found any note");
res.end('{"msg": "Not found any note", "status": "fail"}');
}
}
else {
var notebooks = item.notebooks;
var isFound = !1;
for(var i in notebooks) {
// console.log(i);
var curNotebook = notebooks[i];
if(curNotebook.NotebookId == NotebookId) {
console.log("Found notebook");
isFound = !0;
break;
}
}
if(!isFound) {
console.log("Not found notebook")
res.end('{"msg": "Not found notebook", "status": "fail"}');
}
else {
var notes = item.notes;
for(var i in notes) {
var curNote = notes[i];
if(curNote.NotebookId == NotebookId) {
allNotes.push(curNote);
}
}
}
}
res.json(JSON.stringify(allNotes));
// res.end('{"msg": "found notebook", "status": "success"}');
}
else {
console.log("Not found user");
res.end('{"msg": "Not found user", "status": "fail"}');
}
}
});
}
else {
res.end('{"msg": "wrong input", "status": "fail"}');
}
});
app.get("/getAll", function(req, res) {
// res.header('Access-Control-Allow-Origin', '*'); // implementation of CORS
console.log("get activated");
// console.log(req);
var UserId = req.query.UserId;
console.log("UserId is: " + UserId);
var query = {"UserInfo.UserId": UserId};
req.db.collection("allAppData").findOne(query, function(err, item) {
if(err) console.log("err is: " + err);
else {
if(item) {
console.log("Found data");
res.json(JSON.stringify(item));
}
else {
console.log("Not found data");
res.end('{"msg": "Not found data", "status": "fail"}');
}
}
});
//var cursor = req.db.collection("allAppData").find(query);
//cursor.each(function(err, doc) {
// if(err) console.log("err is: " + err);
// else {
// console.log(doc.UserInfo.UserId);
// break;
// }
//});
//res.json(JSON.stringify(cursor[0]));
});
app.listen(8000);