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