diff options
Diffstat (limited to 'server/middlewares/validators/video-comments.ts')
-rw-r--r-- | server/middlewares/validators/video-comments.ts | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/server/middlewares/validators/video-comments.ts b/server/middlewares/validators/video-comments.ts new file mode 100644 index 000000000..5e1be00f2 --- /dev/null +++ b/server/middlewares/validators/video-comments.ts | |||
@@ -0,0 +1,131 @@ | |||
1 | import * as express from 'express' | ||
2 | import { body, param } from 'express-validator/check' | ||
3 | import { logger } from '../../helpers' | ||
4 | import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' | ||
5 | import { isValidVideoCommentText } from '../../helpers/custom-validators/video-comments' | ||
6 | import { isVideoExist } from '../../helpers/custom-validators/videos' | ||
7 | import { VideoCommentModel } from '../../models/video/video-comment' | ||
8 | import { areValidationErrors } from './utils' | ||
9 | |||
10 | const listVideoCommentThreadsValidator = [ | ||
11 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | ||
12 | |||
13 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
14 | logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) | ||
15 | |||
16 | if (areValidationErrors(req, res)) return | ||
17 | if (!await isVideoExist(req.params.videoId, res)) return | ||
18 | |||
19 | return next() | ||
20 | } | ||
21 | ] | ||
22 | |||
23 | const listVideoThreadCommentsValidator = [ | ||
24 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | ||
25 | param('threadId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid threadId'), | ||
26 | |||
27 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
28 | logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) | ||
29 | |||
30 | if (areValidationErrors(req, res)) return | ||
31 | if (!await isVideoExist(req.params.videoId, res)) return | ||
32 | if (!await isVideoCommentThreadExist(req.params.threadId, req.params.videoId, res)) return | ||
33 | |||
34 | return next() | ||
35 | } | ||
36 | ] | ||
37 | |||
38 | const addVideoCommentThreadValidator = [ | ||
39 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | ||
40 | body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'), | ||
41 | |||
42 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
43 | logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) | ||
44 | |||
45 | if (areValidationErrors(req, res)) return | ||
46 | if (!await isVideoExist(req.params.videoId, res)) return | ||
47 | |||
48 | return next() | ||
49 | } | ||
50 | ] | ||
51 | |||
52 | const addVideoCommentReplyValidator = [ | ||
53 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | ||
54 | param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'), | ||
55 | body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'), | ||
56 | |||
57 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
58 | logger.debug('Checking blacklistRemove parameters.', { parameters: req.params }) | ||
59 | |||
60 | if (areValidationErrors(req, res)) return | ||
61 | if (!await isVideoExist(req.params.videoId, res)) return | ||
62 | if (!await isVideoCommentExist(req.params.commentId, req.params.videoId, res)) return | ||
63 | |||
64 | return next() | ||
65 | } | ||
66 | ] | ||
67 | |||
68 | // --------------------------------------------------------------------------- | ||
69 | |||
70 | export { | ||
71 | listVideoCommentThreadsValidator, | ||
72 | listVideoThreadCommentsValidator, | ||
73 | addVideoCommentThreadValidator, | ||
74 | addVideoCommentReplyValidator | ||
75 | } | ||
76 | |||
77 | // --------------------------------------------------------------------------- | ||
78 | |||
79 | async function isVideoCommentThreadExist (id: number, videoId: number, res: express.Response) { | ||
80 | const videoComment = await VideoCommentModel.loadById(id) | ||
81 | |||
82 | if (!videoComment) { | ||
83 | res.status(404) | ||
84 | .json({ error: 'Video comment thread not found' }) | ||
85 | .end() | ||
86 | |||
87 | return false | ||
88 | } | ||
89 | |||
90 | if (videoComment.videoId !== videoId) { | ||
91 | res.status(400) | ||
92 | .json({ error: 'Video comment is associated to this video.' }) | ||
93 | .end() | ||
94 | |||
95 | return false | ||
96 | } | ||
97 | |||
98 | if (videoComment.inReplyToCommentId !== null) { | ||
99 | res.status(400) | ||
100 | .json({ error: 'Video comment is not a thread.' }) | ||
101 | .end() | ||
102 | |||
103 | return false | ||
104 | } | ||
105 | |||
106 | res.locals.videoCommentThread = videoComment | ||
107 | return true | ||
108 | } | ||
109 | |||
110 | async function isVideoCommentExist (id: number, videoId: number, res: express.Response) { | ||
111 | const videoComment = await VideoCommentModel.loadById(id) | ||
112 | |||
113 | if (!videoComment) { | ||
114 | res.status(404) | ||
115 | .json({ error: 'Video comment thread not found' }) | ||
116 | .end() | ||
117 | |||
118 | return false | ||
119 | } | ||
120 | |||
121 | if (videoComment.videoId !== videoId) { | ||
122 | res.status(400) | ||
123 | .json({ error: 'Video comment is associated to this video.' }) | ||
124 | .end() | ||
125 | |||
126 | return false | ||
127 | } | ||
128 | |||
129 | res.locals.videoComment = videoComment | ||
130 | return true | ||
131 | } | ||