1 import * as express from 'express'
2 import { param } from 'express-validator/check'
3 import { isIdOrUUIDValid, logger } from '../../helpers'
4 import { isVideoExist } from '../../helpers/custom-validators/videos'
5 import { database as db } from '../../initializers/database'
6 import { VideoInstance } from '../../models/video/video-interface'
7 import { areValidationErrors } from './utils'
9 const videosBlacklistRemoveValidator = [
10 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
12 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
13 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
15 if (areValidationErrors(req, res)) return
16 if (!await isVideoExist(req.params.videoId, res)) return
17 if (!await checkVideoIsBlacklisted(res.locals.video, res)) return
23 const videosBlacklistAddValidator = [
24 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
29 if (areValidationErrors(req, res)) return
30 if (!await isVideoExist(req.params.videoId, res)) return
31 if (!checkVideoIsBlacklistable(res.locals.video, res)) return
37 // ---------------------------------------------------------------------------
40 videosBlacklistAddValidator,
41 videosBlacklistRemoveValidator
43 // ---------------------------------------------------------------------------
45 function checkVideoIsBlacklistable (video: VideoInstance, res: express.Response) {
46 if (video.isOwned() === true) {
48 .json({ error: 'Cannot blacklist a local video' })
57 async function checkVideoIsBlacklisted (video: VideoInstance, res: express.Response) {
58 const blacklistedVideo = await db.BlacklistedVideo.loadByVideoId(video.id)
59 if (!blacklistedVideo) {
61 .send('Blacklisted video not found')
66 res.locals.blacklistedVideo = blacklistedVideo