diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-22 10:50:07 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-22 11:29:12 +0100 |
commit | bf1f650817dadfd5eeee9e5e0b6b6938c136e25d (patch) | |
tree | 0f1dc95d87089be177ebe60740a55dd0c96b2414 /server/middlewares | |
parent | 6d8524702874120a4667269a81a61e3c7c5e300d (diff) | |
download | PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.tar.gz PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.tar.zst PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.zip |
Add comments controller
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/sort.ts | 9 | ||||
-rw-r--r-- | server/middlewares/validators/sort.ts | 5 | ||||
-rw-r--r-- | server/middlewares/validators/video-comments.ts | 131 |
3 files changed, 143 insertions, 2 deletions
diff --git a/server/middlewares/sort.ts b/server/middlewares/sort.ts index 5d2a43acc..0eb50db89 100644 --- a/server/middlewares/sort.ts +++ b/server/middlewares/sort.ts | |||
@@ -32,6 +32,12 @@ function setVideosSort (req: express.Request, res: express.Response, next: expre | |||
32 | return next() | 32 | return next() |
33 | } | 33 | } |
34 | 34 | ||
35 | function setVideoCommentThreadsSort (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
36 | if (!req.query.sort) req.query.sort = '-createdAt' | ||
37 | |||
38 | return next() | ||
39 | } | ||
40 | |||
35 | function setFollowersSort (req: express.Request, res: express.Response, next: express.NextFunction) { | 41 | function setFollowersSort (req: express.Request, res: express.Response, next: express.NextFunction) { |
36 | if (!req.query.sort) req.query.sort = '-createdAt' | 42 | if (!req.query.sort) req.query.sort = '-createdAt' |
37 | 43 | ||
@@ -75,5 +81,6 @@ export { | |||
75 | setBlacklistSort, | 81 | setBlacklistSort, |
76 | setFollowersSort, | 82 | setFollowersSort, |
77 | setFollowingSort, | 83 | setFollowingSort, |
78 | setJobsSort | 84 | setJobsSort, |
85 | setVideoCommentThreadsSort | ||
79 | } | 86 | } |
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) | |||
9 | const SORTABLE_JOBS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.JOBS) | 9 | const SORTABLE_JOBS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.JOBS) |
10 | const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) | 10 | const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) |
11 | const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) | 11 | const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) |
12 | const SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS) | ||
12 | const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS) | 13 | const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS) |
13 | const SORTABLE_VIDEO_CHANNELS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS) | 14 | const SORTABLE_VIDEO_CHANNELS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS) |
14 | const SORTABLE_FOLLOWERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWERS) | 15 | const SORTABLE_FOLLOWERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWERS) |
@@ -18,6 +19,7 @@ const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS) | |||
18 | const jobsSortValidator = checkSort(SORTABLE_JOBS_COLUMNS) | 19 | const jobsSortValidator = checkSort(SORTABLE_JOBS_COLUMNS) |
19 | const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS) | 20 | const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS) |
20 | const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS) | 21 | const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS) |
22 | const videoCommentThreadsSortValidator = checkSort(SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS) | ||
21 | const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS) | 23 | const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS) |
22 | const videoChannelsSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_COLUMNS) | 24 | const videoChannelsSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_COLUMNS) |
23 | const followersSortValidator = checkSort(SORTABLE_FOLLOWERS_COLUMNS) | 25 | const 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 @@ | |||
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 | } | ||