Update server dependencies
[oweals/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.videoAll.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.videoAll
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.videoAll.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   query('search')
73     .optional()
74     .not()
75     .isEmpty().withMessage('Should have a valid search'),
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
86 // ---------------------------------------------------------------------------
87
88 export {
89   videosBlacklistAddValidator,
90   videosBlacklistRemoveValidator,
91   videosBlacklistUpdateValidator,
92   videosBlacklistFiltersValidator
93 }