db318dcdbdd00dd3fcf8a8e4f94abb6ddb17b7c0
[oweals/peertube.git] / server / middlewares / validators / videos / video-blacklist.ts
1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { isBooleanValid, isIdOrUUIDValid } from '../../../helpers/custom-validators/misc'
4 import { doesVideoExist } from '../../../helpers/custom-validators/videos'
5 import { logger } from '../../../helpers/logger'
6 import { areValidationErrors } from '../utils'
7 import { doesVideoBlacklistExist, isVideoBlacklistReasonValid } from '../../../helpers/custom-validators/video-blacklist'
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     .toBoolean()
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 // ---------------------------------------------------------------------------
69
70 export {
71   videosBlacklistAddValidator,
72   videosBlacklistRemoveValidator,
73   videosBlacklistUpdateValidator
74 }