]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-comments.ts
Create another test suite for transcoding jobs
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-comments.ts
CommitLineData
41fb13c3 1import express from 'express'
0f8d00e3 2import { body, param, query } from 'express-validator'
26d6bf65 3import { MUserAccountUrl } from '@server/types/models'
d17c7b4e 4import { HttpStatusCode, UserRight } from '@shared/models'
d4a8e7a6 5import { exists, isBooleanValid, isIdValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
10363c74 6import { isValidVideoCommentText } from '../../../helpers/custom-validators/video-comments'
6e46de09 7import { logger } from '../../../helpers/logger'
ceba0e65
C
8import { AcceptResult, isLocalVideoCommentReplyAccepted, isLocalVideoThreadAccepted } from '../../../lib/moderation'
9import { Hooks } from '../../../lib/plugins/hooks'
57f6896f 10import { MCommentOwnerVideoReply, MVideo, MVideoFullLight } from '../../../types/models/video'
84c8d986
C
11import {
12 areValidationErrors,
13 checkCanSeeVideoIfPrivate,
14 doesVideoCommentExist,
15 doesVideoCommentThreadExist,
16 doesVideoExist,
17 isValidVideoIdParam
18} from '../shared'
bf1f6508 19
0f8d00e3
C
20const listVideoCommentsValidator = [
21 query('isLocal')
22 .optional()
f1273314 23 .customSanitizer(toBooleanOrNull)
0f8d00e3
C
24 .custom(isBooleanValid)
25 .withMessage('Should have a valid is local boolean'),
26
27 query('search')
28 .optional()
29 .custom(exists).withMessage('Should have a valid search'),
30
31 query('searchAccount')
32 .optional()
33 .custom(exists).withMessage('Should have a valid account search'),
34
35 query('searchVideo')
36 .optional()
37 .custom(exists).withMessage('Should have a valid video search'),
38
39 (req: express.Request, res: express.Response, next: express.NextFunction) => {
40 logger.debug('Checking listVideoCommentsValidator parameters.', { parameters: req.query })
41
42 if (areValidationErrors(req, res)) return
43
44 return next()
45 }
46]
47
bf1f6508 48const listVideoCommentThreadsValidator = [
d4a8e7a6 49 isValidVideoIdParam('videoId'),
bf1f6508
C
50
51 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
d3ea8975 52 logger.debug('Checking listVideoCommentThreads parameters.', { parameters: req.params })
bf1f6508
C
53
54 if (areValidationErrors(req, res)) return
0f6acda1 55 if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return
bf1f6508 56
c729caf6 57 if (!await checkCanSeeVideoIfPrivate(req, res, res.locals.onlyVideo)) return
84c8d986 58
bf1f6508
C
59 return next()
60 }
61]
62
63const listVideoThreadCommentsValidator = [
d4a8e7a6
C
64 isValidVideoIdParam('videoId'),
65
66 param('threadId')
67 .custom(isIdValid).not().isEmpty().withMessage('Should have a valid threadId'),
bf1f6508
C
68
69 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
d3ea8975 70 logger.debug('Checking listVideoThreadComments parameters.', { parameters: req.params })
bf1f6508
C
71
72 if (areValidationErrors(req, res)) return
0f6acda1 73 if (!await doesVideoExist(req.params.videoId, res, 'only-video')) return
453e83ea 74 if (!await doesVideoCommentThreadExist(req.params.threadId, res.locals.onlyVideo, res)) return
bf1f6508 75
c729caf6 76 if (!await checkCanSeeVideoIfPrivate(req, res, res.locals.onlyVideo)) return
84c8d986 77
bf1f6508
C
78 return next()
79 }
80]
81
82const addVideoCommentThreadValidator = [
d4a8e7a6
C
83 isValidVideoIdParam('videoId'),
84
85 body('text')
86 .custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'),
bf1f6508
C
87
88 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
5de8a55a 89 logger.debug('Checking addVideoCommentThread parameters.', { parameters: req.params, body: req.body })
bf1f6508
C
90
91 if (areValidationErrors(req, res)) return
0f6acda1 92 if (!await doesVideoExist(req.params.videoId, res)) return
6ea9295b
C
93
94 if (!await checkCanSeeVideoIfPrivate(req, res, res.locals.videoAll)) {
95 return res.fail({
96 status: HttpStatusCode.FORBIDDEN_403,
97 message: 'Cannot access to this ressource'
98 })
99 }
100
453e83ea 101 if (!isVideoCommentsEnabled(res.locals.videoAll, res)) return
a1587156 102 if (!await isVideoCommentAccepted(req, res, res.locals.videoAll, false)) return
bf1f6508
C
103
104 return next()
105 }
106]
107
108const addVideoCommentReplyValidator = [
d4a8e7a6
C
109 isValidVideoIdParam('videoId'),
110
bf1f6508 111 param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'),
d4a8e7a6 112
bf1f6508
C
113 body('text').custom(isValidVideoCommentText).not().isEmpty().withMessage('Should have a valid comment text'),
114
115 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
5de8a55a 116 logger.debug('Checking addVideoCommentReply parameters.', { parameters: req.params, body: req.body })
bf1f6508
C
117
118 if (areValidationErrors(req, res)) return
0f6acda1 119 if (!await doesVideoExist(req.params.videoId, res)) return
6ea9295b
C
120
121 if (!await checkCanSeeVideoIfPrivate(req, res, res.locals.videoAll)) {
122 return res.fail({
123 status: HttpStatusCode.FORBIDDEN_403,
124 message: 'Cannot access to this ressource'
125 })
126 }
127
453e83ea
C
128 if (!isVideoCommentsEnabled(res.locals.videoAll, res)) return
129 if (!await doesVideoCommentExist(req.params.commentId, res.locals.videoAll, res)) return
130 if (!await isVideoCommentAccepted(req, res, res.locals.videoAll, true)) return
bf1f6508
C
131
132 return next()
133 }
134]
135
da854ddd 136const videoCommentGetValidator = [
d4a8e7a6
C
137 isValidVideoIdParam('videoId'),
138
139 param('commentId')
140 .custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'),
da854ddd
C
141
142 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
143 logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
144
145 if (areValidationErrors(req, res)) return
0f6acda1 146 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
453e83ea 147 if (!await doesVideoCommentExist(req.params.commentId, res.locals.videoId, res)) return
da854ddd
C
148
149 return next()
150 }
151]
152
4cb6d457 153const removeVideoCommentValidator = [
d4a8e7a6
C
154 isValidVideoIdParam('videoId'),
155
4cb6d457
C
156 param('commentId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid commentId'),
157
158 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
159 logger.debug('Checking removeVideoCommentValidator parameters.', { parameters: req.params })
160
161 if (areValidationErrors(req, res)) return
0f6acda1 162 if (!await doesVideoExist(req.params.videoId, res)) return
453e83ea 163 if (!await doesVideoCommentExist(req.params.commentId, res.locals.videoAll, res)) return
4cb6d457
C
164
165 // Check if the user who did the request is able to delete the video
453e83ea 166 if (!checkUserCanDeleteVideoComment(res.locals.oauth.token.User, res.locals.videoCommentFull, res)) return
4cb6d457
C
167
168 return next()
169 }
170]
171
bf1f6508
C
172// ---------------------------------------------------------------------------
173
174export {
175 listVideoCommentThreadsValidator,
176 listVideoThreadCommentsValidator,
177 addVideoCommentThreadValidator,
0f8d00e3 178 listVideoCommentsValidator,
da854ddd 179 addVideoCommentReplyValidator,
4cb6d457
C
180 videoCommentGetValidator,
181 removeVideoCommentValidator
bf1f6508
C
182}
183
184// ---------------------------------------------------------------------------
185
453e83ea 186function isVideoCommentsEnabled (video: MVideo, res: express.Response) {
47564bbe 187 if (video.commentsEnabled !== true) {
76148b27
RK
188 res.fail({
189 status: HttpStatusCode.CONFLICT_409,
190 message: 'Video comments are disabled for this video.'
191 })
47564bbe
C
192 return false
193 }
194
195 return true
196}
4cb6d457 197
fde37dc9 198function checkUserCanDeleteVideoComment (user: MUserAccountUrl, videoComment: MCommentOwnerVideoReply, res: express.Response) {
c883db6d 199 if (videoComment.isDeleted()) {
76148b27
RK
200 res.fail({
201 status: HttpStatusCode.CONFLICT_409,
202 message: 'This comment is already deleted'
203 })
c883db6d
C
204 return false
205 }
206
fde37dc9
C
207 const userAccount = user.Account
208
209 if (
210 user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT) === false && // Not a moderator
211 videoComment.accountId !== userAccount.id && // Not the comment owner
212 videoComment.Video.VideoChannel.accountId !== userAccount.id // Not the video owner
213 ) {
76148b27
RK
214 res.fail({
215 status: HttpStatusCode.FORBIDDEN_403,
216 message: 'Cannot remove video comment of another user'
217 })
4cb6d457
C
218 return false
219 }
220
221 return true
222}
b4055e1c 223
453e83ea 224async function isVideoCommentAccepted (req: express.Request, res: express.Response, video: MVideoFullLight, isReply: boolean) {
b4055e1c 225 const acceptParameters = {
453e83ea 226 video,
b4055e1c
C
227 commentBody: req.body,
228 user: res.locals.oauth.token.User
229 }
230
231 let acceptedResult: AcceptResult
232
233 if (isReply) {
453e83ea 234 const acceptReplyParameters = Object.assign(acceptParameters, { parentComment: res.locals.videoCommentFull })
b4055e1c 235
6691c522
C
236 acceptedResult = await Hooks.wrapFun(
237 isLocalVideoCommentReplyAccepted,
238 acceptReplyParameters,
b4055e1c
C
239 'filter:api.video-comment-reply.create.accept.result'
240 )
241 } else {
6691c522
C
242 acceptedResult = await Hooks.wrapFun(
243 isLocalVideoThreadAccepted,
244 acceptParameters,
b4055e1c
C
245 'filter:api.video-thread.create.accept.result'
246 )
247 }
248
249 if (!acceptedResult || acceptedResult.accepted !== true) {
250 logger.info('Refused local comment.', { acceptedResult, acceptParameters })
b4055e1c 251
76148b27
RK
252 res.fail({
253 status: HttpStatusCode.FORBIDDEN_403,
254 message: acceptedResult?.errorMessage || 'Refused local comment'
255 })
b4055e1c
C
256 return false
257 }
258
259 return true
260}