]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/video-blacklist.ts
Video blacklist refractoring
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / video-blacklist.ts
1 import { param } from 'express-validator/check'
2 import * as express from 'express'
3
4 import { database as db } from '../../initializers/database'
5 import { checkErrors } from './utils'
6 import { logger, isVideoIdOrUUIDValid, checkVideoExists } from '../../helpers'
7
8 const videosBlacklistRemoveValidator = [
9 param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
10
11 (req: express.Request, res: express.Response, next: express.NextFunction) => {
12 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
13
14 checkErrors(req, res, () => {
15 checkVideoExists(req.params.videoId, res, () => {
16 checkVideoIsBlacklisted(req, res, next)
17 })
18 })
19 }
20 ]
21
22 const videosBlacklistAddValidator = [
23 param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
24
25 (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
27
28 checkErrors(req, res, () => {
29 checkVideoExists(req.params.videoId, res, () => {
30 checkVideoIsBlacklistable(req, res, next)
31 })
32 })
33 }
34 ]
35
36 // ---------------------------------------------------------------------------
37
38 export {
39 videosBlacklistAddValidator,
40 videosBlacklistRemoveValidator
41 }
42 // ---------------------------------------------------------------------------
43
44 function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
45 if (res.locals.video.isOwned() === true) {
46 return res.status(403)
47 .json({ error: 'Cannot blacklist a local video' })
48 .end()
49 }
50
51 callback()
52 }
53
54 function checkVideoIsBlacklisted (req: express.Request, res: express.Response, callback: () => void) {
55 db.BlacklistedVideo.loadByVideoId(res.locals.video.id)
56 .then(blacklistedVideo => {
57 if (!blacklistedVideo) return res.status(404).send('Blacklisted video not found')
58
59 res.locals.blacklistedVideo = blacklistedVideo
60
61 callback()
62 })
63 .catch(err => {
64 logger.error('Error in blacklistRemove request validator', { error: err })
65 return res.sendStatus(500)
66 })
67 }