]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/video-comments.ts
Propagate old comment on new follow
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / video-comments.ts
1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
4 import { isValidVideoCommentText } from '../../helpers/custom-validators/video-comments'
5 import { isVideoExist } from '../../helpers/custom-validators/videos'
6 import { logger } from '../../helpers/logger'
7 import { VideoModel } from '../../models/video/video'
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) => {
15 logger.debug('Checking listVideoCommentThreads parameters.', { parameters: req.params })
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) => {
29 logger.debug('Checking listVideoThreadComments parameters.', { parameters: req.params })
30
31 if (areValidationErrors(req, res)) return
32 if (!await isVideoExist(req.params.videoId, res)) return
33 if (!await isVideoCommentThreadExist(req.params.threadId, res.locals.video, res)) return
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) => {
44 logger.debug('Checking addVideoCommentThread parameters.', { parameters: req.params })
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) => {
59 logger.debug('Checking addVideoCommentReply parameters.', { parameters: req.params })
60
61 if (areValidationErrors(req, res)) return
62 if (!await isVideoExist(req.params.videoId, res)) return
63 if (!await isVideoCommentExist(req.params.commentId, res.locals.video, res)) return
64
65 return next()
66 }
67 ]
68
69 const videoCommentGetValidator = [
70 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
71 param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'),
72
73 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
74 logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
75
76 if (areValidationErrors(req, res)) return
77 if (!await isVideoExist(req.params.videoId, res)) return
78 if (!await isVideoCommentExist(req.params.commentId, res.locals.video, res)) return
79
80 return next()
81 }
82 ]
83
84 // ---------------------------------------------------------------------------
85
86 export {
87 listVideoCommentThreadsValidator,
88 listVideoThreadCommentsValidator,
89 addVideoCommentThreadValidator,
90 addVideoCommentReplyValidator,
91 videoCommentGetValidator
92 }
93
94 // ---------------------------------------------------------------------------
95
96 async function isVideoCommentThreadExist (id: number, video: VideoModel, res: express.Response) {
97 const videoComment = await VideoCommentModel.loadById(id)
98
99 if (!videoComment) {
100 res.status(404)
101 .json({ error: 'Video comment thread not found' })
102 .end()
103
104 return false
105 }
106
107 if (videoComment.videoId !== video.id) {
108 res.status(400)
109 .json({ error: 'Video comment is associated to this video.' })
110 .end()
111
112 return false
113 }
114
115 if (videoComment.inReplyToCommentId !== null) {
116 res.status(400)
117 .json({ error: 'Video comment is not a thread.' })
118 .end()
119
120 return false
121 }
122
123 res.locals.videoCommentThread = videoComment
124 return true
125 }
126
127 async function isVideoCommentExist (id: number, video: VideoModel, res: express.Response) {
128 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
129
130 if (!videoComment) {
131 res.status(404)
132 .json({ error: 'Video comment thread not found' })
133 .end()
134
135 return false
136 }
137
138 if (videoComment.videoId !== video.id) {
139 res.status(400)
140 .json({ error: 'Video comment is associated to this video.' })
141 .end()
142
143 return false
144 }
145
146 res.locals.videoComment = videoComment
147 return true
148 }