]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-abuses.ts
Stronger model typings
[github/Chocobozzz/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 doesVideoExist(req.params.videoId, res)) return
36 if (!await doesVideoAbuseExist(req.params.id, res.locals.videoAll.id, res)) return
37
38 return next()
39 }
40 ]
41
42 const videoAbuseUpdateValidator = [
43 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
44 param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
45 body('state')
46 .optional()
47 .custom(isVideoAbuseStateValid).withMessage('Should have a valid video abuse state'),
48 body('moderationComment')
49 .optional()
50 .custom(isVideoAbuseModerationCommentValid).withMessage('Should have a valid video moderation comment'),
51
52 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
53 logger.debug('Checking videoAbuseUpdateValidator parameters', { parameters: req.body })
54
55 if (areValidationErrors(req, res)) return
56 if (!await doesVideoExist(req.params.videoId, res)) return
57 if (!await doesVideoAbuseExist(req.params.id, res.locals.videoAll.id, res)) return
58
59 return next()
60 }
61 ]
62
63 // ---------------------------------------------------------------------------
64
65 export {
66 videoAbuseReportValidator,
67 videoAbuseGetValidator,
68 videoAbuseUpdateValidator
69 }