aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/video-comments.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-22 10:50:07 +0100
committerChocobozzz <me@florianbigard.com>2017-12-22 11:29:12 +0100
commitbf1f650817dadfd5eeee9e5e0b6b6938c136e25d (patch)
tree0f1dc95d87089be177ebe60740a55dd0c96b2414 /server/middlewares/validators/video-comments.ts
parent6d8524702874120a4667269a81a61e3c7c5e300d (diff)
downloadPeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.tar.gz
PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.tar.zst
PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.zip
Add comments controller
Diffstat (limited to 'server/middlewares/validators/video-comments.ts')
-rw-r--r--server/middlewares/validators/video-comments.ts131
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 @@
1import * as express from 'express'
2import { body, param } from 'express-validator/check'
3import { logger } from '../../helpers'
4import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
5import { isValidVideoCommentText } from '../../helpers/custom-validators/video-comments'
6import { isVideoExist } from '../../helpers/custom-validators/videos'
7import { VideoCommentModel } from '../../models/video/video-comment'
8import { areValidationErrors } from './utils'
9
10const 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
23const 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
38const 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
52const 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
70export {
71 listVideoCommentThreadsValidator,
72 listVideoThreadCommentsValidator,
73 addVideoCommentThreadValidator,
74 addVideoCommentReplyValidator
75}
76
77// ---------------------------------------------------------------------------
78
79async 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
110async 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}