-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.js
495 lines (431 loc) · 10.6 KB
/
functions.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
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
const { verifyMessage } = require("ed25519-keys");
const { nanoid } = require("nanoid");
const { CheckComment } = require("./checkSpam");
const { prisma } = require("./datebase");
const { VotesHandler } = require("./votes");
/**
* Delete Comment Handler
*/
const deleteComment = async (req, res, next) => {
let isOwn;
try {
isOwn = await prisma.comment.findFirst({
where: {
id: req.body.id,
},
include: {
User: {
select: {
authKey: true,
},
},
},
});
} catch (error) {
// Comment not found.
return res.send({ error: "E-1" });
}
if (isOwn && req.body.authKey === isOwn.User.authKey) {
let meta = await prisma.comment.update({
where: {
id: req.body.id,
},
data: {
comment: "",
deleted: 1,
},
select: {
parent_id: true,
},
});
meta.success = true;
meta.id = req.body.id;
return res.send(meta);
} else {
return res.send({ success: false });
}
return next(); // never
};
/**
* Check Login Handler
*/
const checkLogin = async (req, res, next) => {
let login = req.body.login.trim();
let fp = String(req.body.hash);
if (!fp) {
// Error. Please try again.
return res.send({ error: "E-2" });
}
if (login.length < 2 || login.length > 30) {
// Login can have min 2 and max 30 characters.
return res.send({ error: "E-3" });
}
const checkFp = await prisma.fingerprint.findFirst({
where: {
fp,
},
});
if (checkFp && checkFp.userLogin !== null) {
return res.send({
// Error. You have already one account:
error: "E-4", // checkFp.userLogin
});
}
const user = await prisma.user.count({
where: { login },
});
let rk = nanoid(20);
if (user === 0) {
const up = await prisma.fingerprint.upsert({
create: { fp, count: 1, rKey: rk },
update: {
count: {
increment: 1,
},
},
where: { fp },
});
res.send({ login, status: true, rk: up.rKey });
} else {
// Login exist, try another one.
res.send({ error: "E-5" });
}
return next();
};
/**
* Login User Handler
*/
const loginUser = async (req, res, next) => {
verifyMessage(req.body.sign, req.body.signature, req.body.publicKey).then(
async (verify) => {
if (verify) {
let upUser;
let publicKey = req.body.publicKey.trim();
try {
upUser = await prisma.user.findFirst({
where: { publicKey },
});
return res.send({
authKey: upUser.authKey,
login: upUser.login,
publicKey: upUser.publicKey,
});
} catch (error) {
// Error. Please try again.
return res.send({ error: "E-2" });
}
} else {
// Signature not match, please try again.
return res.send({ error: "E-6" });
}
return next(); // never
}
);
};
/**
* Register User Handler
*/
const registerUser = async (req, res, next) => {
let login = req.body.login.trim();
if (login.length < 2 || login.length > 30) {
// Login can have min 2 and max 30 characters.
return res.send({ error: "E-3" });
}
verifyMessage(req.body.login, req.body.signature, req.body.publicKey).then(
async (verify) => {
if (verify) {
const authKey = nanoid(20);
let login = req.body.login.trim();
let publicKey = req.body.publicKey.trim();
let rk = req.body.rk.trim();
let fp = String(req.body.hash);
try {
fpCheck = await prisma.fingerprint.findFirst({
where: { fp },
});
if (
!fpCheck ||
fpCheck.count <= 1 ||
fpCheck.rKey !== rk ||
fpCheck.rKey.length !== 20
) {
// Error. Please try again.
return res.send({ error: "E-2" });
}
let checkUser = await prisma.user.count({
where: {
publicKey,
},
});
if (checkUser !== 0) {
// Login exist, try another one.
return res.send({ error: "E-5" });
}
let upUser = await prisma.user.create({
data: { login, publicKey, authKey },
});
await prisma.fingerprint.update({
where: { fp },
data: { count: 0, userLogin: login, rKey: "" },
});
return res.send({
authKey: upUser.authKey,
login: upUser.login,
publicKey: upUser.publicKey,
});
} catch (error) {
// Error. Please try again.
return res.send({ error: "E-2" });
}
} else {
// Signature not match, please try again.
return res.send({ error: "E-6" });
}
return next(); // never
}
);
};
/**
* Post Comment Handler
*/
const postComments = async (req, res, next, type = "") => {
const commentMsg = req.body.comment.trim();
if (req.body.url.length < 1 || req.body.url.length > 2000) {
return res.send({
// Website URL to long. Max 2000 characters.
error: "E-7",
});
}
if (commentMsg.length < 2 || commentMsg.length > 3000) {
return res.send({
// Comment can have min 2 and max 3000 characters.
error: "E-8",
});
}
let user;
try {
user = await prisma.user.findFirst({
where: {
authKey: req.body.authKey,
},
});
} catch (error) {
// Please log in to post comment.
return res.send({ error: "E-9" });
}
if (user === null) {
// Please log in to post comment.
return res.send({ error: "E-9" });
}
if (type === "edit") {
// check if comment belongs to user and is not deleted
const comment = await prisma.comment.findFirst({
where: {
id: req.body.id,
},
});
if (comment.userLogin !== user.login || comment.deleted !== 0) {
// Error. Please try again.
return res.send({ error: "E-2" });
}
}
verifyMessage(req.body.comment, req.body.signature, user.publicKey).then(
async (verify) => {
if (verify) {
if (user.login) {
const url = req.body.url.trim();
const spam = await CheckComment(req, user.login);
if (spam) return res.send({ error: "Spam!" });
if (type !== "edit") {
await prisma.webpage.upsert({
where: {
url,
},
create: {
url,
count: 1,
},
update: {
count: {
increment: 1,
},
},
});
}
let comment;
if (type === "edit") {
comment = await prisma.comment.update({
where: {
id: req.body.id,
},
data: {
comment: commentMsg,
},
});
return res.send({ edit: comment });
} else {
comment = await prisma.comment.create({
data: {
webpageUrl: url,
comment: commentMsg,
userLogin: user.login,
parent_id: req.body.parent_id,
},
include: {
Children: {
include: {
Children: true,
},
},
},
});
return res.send({ meta: comment });
}
}
} else {
// Signature not match, please try again.
res.send({ error: "E-6" });
}
return next();
}
);
};
/**
* Get Comments Handler
*/
const getComments = async (req, res, next) => {
const user = await prisma.user.findFirst({
where: {
authKey: req.body.authKey,
},
});
if (!user || !user.login) {
// Please log in to see comments.
return res.send({ error: "E-10" });
}
let url = req.body.url;
if (url.includes("http://") || url.includes("https://")) {
url = url.match(/https?:\/\/(.*)/)[1];
}
const page = await prisma.webpage.findFirst({
where: {
url: {
endsWith: url,
},
},
select: {
count: true,
},
});
let pageInt = req.body.page ? Number(req.body.page) : 0;
let itemsPerPage = 30;
const comments = await prisma.comment.findMany({
skip: pageInt * itemsPerPage,
take: itemsPerPage,
where: {
webpageUrl: {
endsWith: url,
},
parent_id: null,
},
include: {
Children: {
include: {
Children: true,
votes: {
where: {
userLogin: user.login,
},
select: {
voteType: true,
},
},
},
},
votes: {
where: {
userLogin: user.login,
},
select: {
voteType: true,
},
},
},
orderBy: {
id: "desc",
},
});
res.send({
meta: comments.reverse(),
page: page === null ? { count: 0 } : page,
});
return next();
};
/**
* Vote Comment Handler
*/
// eslint-disable-next-line no-unused-vars
const voteComment = async (req, res, next) => {
let authKey = req.body.authKey;
let commentId = req.body.id;
let vote = req.body.vote;
const comment = await prisma.comment.findFirst({
where: {
id: commentId,
},
});
if (!comment || comment.deleted === 1) {
// Cannot vote on dead comments.
return res.send({ error: "E-11" });
}
const user = await prisma.user.findFirst({
where: { authKey },
});
if (!user) {
// Please log in to vote.
return res.send({ error: "E-12" });
}
if (comment.userLogin === user.login) {
// Can't vote on your own comments.
return res.send({ error: "E-15" });
}
const vreturn = await VotesHandler(user.login, commentId, vote);
vreturn.votes.success = true;
vreturn.votes.voteType = vreturn.voteType;
return res.send(vreturn.votes);
};
/**
* Get Comments Counter Handler
*/
// eslint-disable-next-line no-unused-vars
const getCount = async (req, res, next) => {
let urlPlain = JSON.parse(req.body);
let url = urlPlain.url;
try {
if (url.includes("http://") || url.includes("https://")) {
url = url.match(/https?:\/\/(.*)/)[1];
}
const count = await prisma.webpage.findFirst({
where: {
url: {
endsWith: url,
},
},
select: {
count: true,
},
});
res.send(count === null ? 0 : count);
} catch (error) {}
return next();
};
module.exports = {
getComments,
postComments,
checkLogin,
deleteComment,
loginUser,
registerUser,
voteComment,
getCount,
};