aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators
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
parent6d8524702874120a4667269a81a61e3c7c5e300d (diff)
downloadPeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.tar.gz
PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.tar.zst
PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.zip
Add comments controller
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r--server/middlewares/validators/sort.ts5
-rw-r--r--server/middlewares/validators/video-comments.ts131
2 files changed, 135 insertions, 1 deletions
diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts
index 38184fefa..56855bda0 100644
--- a/server/middlewares/validators/sort.ts
+++ b/server/middlewares/validators/sort.ts
@@ -9,6 +9,7 @@ const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS)
9const SORTABLE_JOBS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.JOBS) 9const SORTABLE_JOBS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.JOBS)
10const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) 10const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES)
11const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) 11const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS)
12const SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS)
12const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS) 13const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS)
13const SORTABLE_VIDEO_CHANNELS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS) 14const SORTABLE_VIDEO_CHANNELS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS)
14const SORTABLE_FOLLOWERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWERS) 15const SORTABLE_FOLLOWERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWERS)
@@ -18,6 +19,7 @@ const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS)
18const jobsSortValidator = checkSort(SORTABLE_JOBS_COLUMNS) 19const jobsSortValidator = checkSort(SORTABLE_JOBS_COLUMNS)
19const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS) 20const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS)
20const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS) 21const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS)
22const videoCommentThreadsSortValidator = checkSort(SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS)
21const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS) 23const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS)
22const videoChannelsSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_COLUMNS) 24const videoChannelsSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_COLUMNS)
23const followersSortValidator = checkSort(SORTABLE_FOLLOWERS_COLUMNS) 25const followersSortValidator = checkSort(SORTABLE_FOLLOWERS_COLUMNS)
@@ -33,7 +35,8 @@ export {
33 blacklistSortValidator, 35 blacklistSortValidator,
34 followersSortValidator, 36 followersSortValidator,
35 followingSortValidator, 37 followingSortValidator,
36 jobsSortValidator 38 jobsSortValidator,
39 videoCommentThreadsSortValidator
37} 40}
38 41
39// --------------------------------------------------------------------------- 42// ---------------------------------------------------------------------------
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}