]>
Commit | Line | Data |
---|---|---|
bf1f6508 C |
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' | |
d3ea8975 | 7 | import { VideoModel } from '../../models/video/video' |
bf1f6508 C |
8 | import { VideoCommentModel } from '../../models/video/video-comment' |
9 | import { areValidationErrors } from './utils' | |
10 | ||
11 | const listVideoCommentThreadsValidator = [ | |
12 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | |
13 | ||
14 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
d3ea8975 | 15 | logger.debug('Checking listVideoCommentThreads parameters.', { parameters: req.params }) |
bf1f6508 C |
16 | |
17 | if (areValidationErrors(req, res)) return | |
18 | if (!await isVideoExist(req.params.videoId, res)) return | |
19 | ||
20 | return next() | |
21 | } | |
22 | ] | |
23 | ||
24 | const listVideoThreadCommentsValidator = [ | |
25 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | |
26 | param('threadId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid threadId'), | |
27 | ||
28 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
d3ea8975 | 29 | logger.debug('Checking listVideoThreadComments parameters.', { parameters: req.params }) |
bf1f6508 C |
30 | |
31 | if (areValidationErrors(req, res)) return | |
32 | if (!await isVideoExist(req.params.videoId, res)) return | |
d3ea8975 | 33 | if (!await isVideoCommentThreadExist(req.params.threadId, res.locals.video, res)) return |
bf1f6508 C |
34 | |
35 | return next() | |
36 | } | |
37 | ] | |
38 | ||
39 | const addVideoCommentThreadValidator = [ | |
40 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | |
41 | body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'), | |
42 | ||
43 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
d3ea8975 | 44 | logger.debug('Checking addVideoCommentThread parameters.', { parameters: req.params }) |
bf1f6508 C |
45 | |
46 | if (areValidationErrors(req, res)) return | |
47 | if (!await isVideoExist(req.params.videoId, res)) return | |
48 | ||
49 | return next() | |
50 | } | |
51 | ] | |
52 | ||
53 | const addVideoCommentReplyValidator = [ | |
54 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | |
55 | param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'), | |
56 | body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'), | |
57 | ||
58 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
d3ea8975 | 59 | logger.debug('Checking addVideoCommentReply parameters.', { parameters: req.params }) |
bf1f6508 C |
60 | |
61 | if (areValidationErrors(req, res)) return | |
62 | if (!await isVideoExist(req.params.videoId, res)) return | |
d3ea8975 | 63 | if (!await isVideoCommentExist(req.params.commentId, res.locals.video, res)) return |
bf1f6508 C |
64 | |
65 | return next() | |
66 | } | |
67 | ] | |
68 | ||
69 | // --------------------------------------------------------------------------- | |
70 | ||
71 | export { | |
72 | listVideoCommentThreadsValidator, | |
73 | listVideoThreadCommentsValidator, | |
74 | addVideoCommentThreadValidator, | |
75 | addVideoCommentReplyValidator | |
76 | } | |
77 | ||
78 | // --------------------------------------------------------------------------- | |
79 | ||
d3ea8975 | 80 | async function isVideoCommentThreadExist (id: number, video: VideoModel, res: express.Response) { |
bf1f6508 C |
81 | const videoComment = await VideoCommentModel.loadById(id) |
82 | ||
83 | if (!videoComment) { | |
84 | res.status(404) | |
85 | .json({ error: 'Video comment thread not found' }) | |
86 | .end() | |
87 | ||
88 | return false | |
89 | } | |
90 | ||
d3ea8975 | 91 | if (videoComment.videoId !== video.id) { |
bf1f6508 C |
92 | res.status(400) |
93 | .json({ error: 'Video comment is associated to this video.' }) | |
94 | .end() | |
95 | ||
96 | return false | |
97 | } | |
98 | ||
99 | if (videoComment.inReplyToCommentId !== null) { | |
100 | res.status(400) | |
101 | .json({ error: 'Video comment is not a thread.' }) | |
102 | .end() | |
103 | ||
104 | return false | |
105 | } | |
106 | ||
107 | res.locals.videoCommentThread = videoComment | |
108 | return true | |
109 | } | |
110 | ||
d3ea8975 | 111 | async function isVideoCommentExist (id: number, video: VideoModel, res: express.Response) { |
bf1f6508 C |
112 | const videoComment = await VideoCommentModel.loadById(id) |
113 | ||
114 | if (!videoComment) { | |
115 | res.status(404) | |
116 | .json({ error: 'Video comment thread not found' }) | |
117 | .end() | |
118 | ||
119 | return false | |
120 | } | |
121 | ||
d3ea8975 | 122 | if (videoComment.videoId !== video.id) { |
bf1f6508 C |
123 | res.status(400) |
124 | .json({ error: 'Video comment is associated to this video.' }) | |
125 | .end() | |
126 | ||
127 | return false | |
128 | } | |
129 | ||
130 | res.locals.videoComment = videoComment | |
131 | return true | |
132 | } |