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'
10 const videosBlacklistRemoveValidator = [
11 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
13 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
14 logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
16 if (areValidationErrors(req, res)) return
17 if (!await isVideoExist(req.params.videoId, res)) return
18 if (!await checkVideoIsBlacklisted(res.locals.video, res)) return
24 const videosBlacklistAddValidator = [
25 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
27 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
28 logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
30 if (areValidationErrors(req, res)) return
31 if (!await isVideoExist(req.params.videoId, res)) return
32 if (!checkVideoIsBlacklistable(res.locals.video, res)) return
38 // ---------------------------------------------------------------------------
41 videosBlacklistAddValidator,
42 videosBlacklistRemoveValidator
44 // ---------------------------------------------------------------------------
46 function checkVideoIsBlacklistable (video: VideoModel, res: express.Response) {
47 if (video.isOwned() === true) {
49 .json({ error: 'Cannot blacklist a local video' })
58 async function checkVideoIsBlacklisted (video: VideoModel, res: express.Response) {
59 const blacklistedVideo = await VideoBlacklistModel.loadByVideoId(video.id)
60 if (!blacklistedVideo) {
62 .send('Blacklisted video not found')
67 res.locals.blacklistedVideo = blacklistedVideo