]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-blacklist.ts
Add ability to unfederate a local video (on blacklist)
[github/Chocobozzz/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 { isVideoExist } from '../../../helpers/custom-validators/videos'
5 import { logger } from '../../../helpers/logger'
6 import { areValidationErrors } from '../utils'
7 import { isVideoBlacklistExist, isVideoBlacklistReasonValid } from '../../../helpers/custom-validators/video-blacklist'
8 import { VideoModel } from '../../../models/video/video'
9
10 const videosBlacklistRemoveValidator = [
11 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
12
13 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
14 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
15
16 if (areValidationErrors(req, res)) return
17 if (!await isVideoExist(req.params.videoId, res)) return
18 if (!await isVideoBlacklistExist(res.locals.video.id, res)) return
19
20 return next()
21 }
22 ]
23
24 const videosBlacklistAddValidator = [
25 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
26 body('unfederate')
27 .optional()
28 .toBoolean()
29 .custom(isBooleanValid).withMessage('Should have a valid unfederate boolean'),
30 body('reason')
31 .optional()
32 .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
33
34 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
35 logger.debug('Checking videosBlacklistAdd parameters', { parameters: req.params })
36
37 if (areValidationErrors(req, res)) return
38 if (!await isVideoExist(req.params.videoId, res)) return
39
40 const video: VideoModel = res.locals.video
41 if (req.body.unfederate === true && video.remote === true) {
42 return res
43 .status(409)
44 .send({ error: 'You cannot unfederate a remote video.' })
45 .end()
46 }
47
48 return next()
49 }
50 ]
51
52 const videosBlacklistUpdateValidator = [
53 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
54 body('reason')
55 .optional()
56 .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
57
58 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
59 logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })
60
61 if (areValidationErrors(req, res)) return
62 if (!await isVideoExist(req.params.videoId, res)) return
63 if (!await isVideoBlacklistExist(res.locals.video.id, res)) return
64
65 return next()
66 }
67 ]
68
69 // ---------------------------------------------------------------------------
70
71 export {
72 videosBlacklistAddValidator,
73 videosBlacklistRemoveValidator,
74 videosBlacklistUpdateValidator
75 }