Make sure a report doesn't get deleted upon the deletion of its video
[oweals/peertube.git] / server / middlewares / validators / videos / video-abuses.ts
1 import * as express from 'express'
2 import { body, param } from 'express-validator'
3 import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc'
4 import { logger } from '../../../helpers/logger'
5 import { areValidationErrors } from '../utils'
6 import {
7   isVideoAbuseModerationCommentValid,
8   isVideoAbuseReasonValid,
9   isVideoAbuseStateValid
10 } from '../../../helpers/custom-validators/video-abuses'
11 import { doesVideoAbuseExist, doesVideoExist } from '../../../helpers/middlewares'
12
13 const videoAbuseReportValidator = [
14   param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
15   body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
16
17   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
18     logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
19
20     if (areValidationErrors(req, res)) return
21     if (!await doesVideoExist(req.params.videoId, res)) return
22
23     return next()
24   }
25 ]
26
27 const videoAbuseGetValidator = [
28   param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
29   param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
30
31   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
32     logger.debug('Checking videoAbuseGetValidator parameters', { parameters: req.body })
33
34     if (areValidationErrors(req, res)) return
35     if (!await doesVideoAbuseExist(req.params.id, req.params.videoId, res)) return
36
37     return next()
38   }
39 ]
40
41 const videoAbuseUpdateValidator = [
42   param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
43   param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
44   body('state')
45     .optional()
46     .custom(isVideoAbuseStateValid).withMessage('Should have a valid video abuse state'),
47   body('moderationComment')
48     .optional()
49     .custom(isVideoAbuseModerationCommentValid).withMessage('Should have a valid video moderation comment'),
50
51   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
52     logger.debug('Checking videoAbuseUpdateValidator parameters', { parameters: req.body })
53
54     if (areValidationErrors(req, res)) return
55     if (!await doesVideoAbuseExist(req.params.id, req.params.videoId, res)) return
56
57     return next()
58   }
59 ]
60
61 // ---------------------------------------------------------------------------
62
63 export {
64   videoAbuseReportValidator,
65   videoAbuseGetValidator,
66   videoAbuseUpdateValidator
67 }