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