]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-blacklist.ts
Fix express validator
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-blacklist.ts
1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { isBooleanValid, isIdOrUUIDValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
4 import { logger } from '../../../helpers/logger'
5 import { areValidationErrors } from '../utils'
6 import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../../helpers/custom-validators/video-blacklist'
7 import { doesVideoBlacklistExist, doesVideoExist } from '../../../helpers/middlewares'
8
9 const videosBlacklistRemoveValidator = [
10 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
11
12 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
13 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
14
15 if (areValidationErrors(req, res)) return
16 if (!await doesVideoExist(req.params.videoId, res)) return
17 if (!await doesVideoBlacklistExist(res.locals.video.id, res)) return
18
19 return next()
20 }
21 ]
22
23 const videosBlacklistAddValidator = [
24 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
25 body('unfederate')
26 .optional()
27 .customSanitizer(toBooleanOrNull)
28 .custom(isBooleanValid).withMessage('Should have a valid unfederate boolean'),
29 body('reason')
30 .optional()
31 .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
32
33 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
34 logger.debug('Checking videosBlacklistAdd parameters', { parameters: req.params })
35
36 if (areValidationErrors(req, res)) return
37 if (!await doesVideoExist(req.params.videoId, res)) return
38
39 const video = res.locals.video
40 if (req.body.unfederate === true && video.remote === true) {
41 return res
42 .status(409)
43 .send({ error: 'You cannot unfederate a remote video.' })
44 .end()
45 }
46
47 return next()
48 }
49 ]
50
51 const videosBlacklistUpdateValidator = [
52 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
53 body('reason')
54 .optional()
55 .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
56
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })
59
60 if (areValidationErrors(req, res)) return
61 if (!await doesVideoExist(req.params.videoId, res)) return
62 if (!await doesVideoBlacklistExist(res.locals.video.id, res)) return
63
64 return next()
65 }
66 ]
67
68 const videosBlacklistFiltersValidator = [
69 query('type')
70 .optional()
71 .custom(isVideoBlacklistTypeValid).withMessage('Should have a valid video blacklist type attribute'),
72
73 (req: express.Request, res: express.Response, next: express.NextFunction) => {
74 logger.debug('Checking videos blacklist filters query', { parameters: req.query })
75
76 if (areValidationErrors(req, res)) return
77
78 return next()
79 }
80 ]
81
82 // ---------------------------------------------------------------------------
83
84 export {
85 videosBlacklistAddValidator,
86 videosBlacklistRemoveValidator,
87 videosBlacklistUpdateValidator,
88 videosBlacklistFiltersValidator
89 }