]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-blacklist.ts
Fix express validator
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-blacklist.ts
CommitLineData
35bf0c83 1import * as express from 'express'
c8861d5d
C
2import { body, param, query } from 'express-validator'
3import { isBooleanValid, isIdOrUUIDValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
6e46de09
C
4import { logger } from '../../../helpers/logger'
5import { areValidationErrors } from '../utils'
3e753302
C
6import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../../helpers/custom-validators/video-blacklist'
7import { doesVideoBlacklistExist, doesVideoExist } from '../../../helpers/middlewares'
35bf0c83
C
8
9const videosBlacklistRemoveValidator = [
72c7248b 10 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
35bf0c83 11
a2431b7d 12 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
35bf0c83
C
13 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
14
a2431b7d 15 if (areValidationErrors(req, res)) return
0f6acda1
C
16 if (!await doesVideoExist(req.params.videoId, res)) return
17 if (!await doesVideoBlacklistExist(res.locals.video.id, res)) return
a2431b7d
C
18
19 return next()
35bf0c83
C
20 }
21]
22
23const videosBlacklistAddValidator = [
72c7248b 24 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
5abb9fbb
C
25 body('unfederate')
26 .optional()
c8861d5d 27 .customSanitizer(toBooleanOrNull)
5abb9fbb 28 .custom(isBooleanValid).withMessage('Should have a valid unfederate boolean'),
26b7305a
C
29 body('reason')
30 .optional()
31 .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
35bf0c83 32
a2431b7d 33 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26b7305a 34 logger.debug('Checking videosBlacklistAdd parameters', { parameters: req.params })
35bf0c83 35
a2431b7d 36 if (areValidationErrors(req, res)) return
0f6acda1 37 if (!await doesVideoExist(req.params.videoId, res)) return
a2431b7d 38
dae86118 39 const video = res.locals.video
5abb9fbb
C
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
a2431b7d 47 return next()
35bf0c83
C
48 }
49]
50
26b7305a
C
51const 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'),
35bf0c83 56
26b7305a
C
57 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
58 logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })
35bf0c83 59
26b7305a 60 if (areValidationErrors(req, res)) return
0f6acda1
C
61 if (!await doesVideoExist(req.params.videoId, res)) return
62 if (!await doesVideoBlacklistExist(res.locals.video.id, res)) return
a2431b7d 63
26b7305a 64 return next()
35bf0c83 65 }
26b7305a 66]
35bf0c83 67
7ccddd7b
JM
68const 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
26b7305a 82// ---------------------------------------------------------------------------
35bf0c83 83
26b7305a
C
84export {
85 videosBlacklistAddValidator,
86 videosBlacklistRemoveValidator,
7ccddd7b
JM
87 videosBlacklistUpdateValidator,
88 videosBlacklistFiltersValidator
35bf0c83 89}