]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/video-blacklist.ts
3c1ef1b4ed391ff391ab9321cf6454d3350256b2
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / video-blacklist.ts
1 import * as express from 'express'
2 import { param } from 'express-validator/check'
3 import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
4 import { isVideoExist } from '../../helpers/custom-validators/videos'
5 import { logger } from '../../helpers/logger'
6 import { VideoModel } from '../../models/video/video'
7 import { VideoBlacklistModel } from '../../models/video/video-blacklist'
8 import { areValidationErrors } from './utils'
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 checkVideoIsBlacklisted(res.locals.video, 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
27 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
28 logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
29
30 if (areValidationErrors(req, res)) return
31 if (!await isVideoExist(req.params.videoId, res)) return
32 if (!checkVideoIsBlacklistable(res.locals.video, res)) return
33
34 return next()
35 }
36 ]
37
38 // ---------------------------------------------------------------------------
39
40 export {
41 videosBlacklistAddValidator,
42 videosBlacklistRemoveValidator
43 }
44 // ---------------------------------------------------------------------------
45
46 function checkVideoIsBlacklistable (video: VideoModel, res: express.Response) {
47 if (video.isOwned() === true) {
48 res.status(403)
49 .json({ error: 'Cannot blacklist a local video' })
50 .end()
51
52 return false
53 }
54
55 return true
56 }
57
58 async function checkVideoIsBlacklisted (video: VideoModel, res: express.Response) {
59 const blacklistedVideo = await VideoBlacklistModel.loadByVideoId(video.id)
60 if (!blacklistedVideo) {
61 res.status(404)
62 .send('Blacklisted video not found')
63
64 return false
65 }
66
67 res.locals.blacklistedVideo = blacklistedVideo
68 return true
69 }