1
0
Fork 0
peertube/server/middlewares/validators/video-blacklist.ts

70 lines
2.3 KiB
TypeScript
Raw Normal View History

2017-10-10 04:02:18 -04:00
import * as express from 'express'
2017-11-27 11:30:46 -05:00
import { param } from 'express-validator/check'
2017-12-12 11:53:50 -05:00
import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
2017-11-27 11:30:46 -05:00
import { isVideoExist } from '../../helpers/custom-validators/videos'
2017-12-28 05:16:08 -05:00
import { logger } from '../../helpers/logger'
2017-12-12 11:53:50 -05:00
import { VideoModel } from '../../models/video/video'
import { VideoBlacklistModel } from '../../models/video/video-blacklist'
2017-11-27 11:30:46 -05:00
import { areValidationErrors } from './utils'
2017-10-10 04:02:18 -04:00
const videosBlacklistRemoveValidator = [
2017-10-24 13:41:09 -04:00
param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
2017-10-10 04:02:18 -04:00
2017-11-27 11:30:46 -05:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
2017-10-10 04:02:18 -04:00
logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
2017-11-27 11:30:46 -05:00
if (areValidationErrors(req, res)) return
if (!await isVideoExist(req.params.videoId, res)) return
if (!await checkVideoIsBlacklisted(res.locals.video, res)) return
return next()
2017-10-10 04:02:18 -04:00
}
]
const videosBlacklistAddValidator = [
2017-10-24 13:41:09 -04:00
param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
2017-10-10 04:02:18 -04:00
2017-11-27 11:30:46 -05:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
2017-10-10 04:02:18 -04:00
logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
2017-11-27 11:30:46 -05:00
if (areValidationErrors(req, res)) return
if (!await isVideoExist(req.params.videoId, res)) return
if (!checkVideoIsBlacklistable(res.locals.video, res)) return
return next()
2017-10-10 04:02:18 -04:00
}
]
// ---------------------------------------------------------------------------
export {
videosBlacklistAddValidator,
videosBlacklistRemoveValidator
}
// ---------------------------------------------------------------------------
2017-12-12 11:53:50 -05:00
function checkVideoIsBlacklistable (video: VideoModel, res: express.Response) {
2017-11-27 11:30:46 -05:00
if (video.isOwned() === true) {
res.status(403)
2017-10-10 04:02:18 -04:00
.json({ error: 'Cannot blacklist a local video' })
.end()
2017-11-27 11:30:46 -05:00
return false
2017-10-10 04:02:18 -04:00
}
2017-11-27 11:30:46 -05:00
return true
2017-10-10 04:02:18 -04:00
}
2017-12-12 11:53:50 -05:00
async function checkVideoIsBlacklisted (video: VideoModel, res: express.Response) {
const blacklistedVideo = await VideoBlacklistModel.loadByVideoId(video.id)
2017-11-27 11:30:46 -05:00
if (!blacklistedVideo) {
res.status(404)
.send('Blacklisted video not found')
2017-10-10 04:02:18 -04:00
2017-11-27 11:30:46 -05:00
return false
}
2017-10-10 04:02:18 -04:00
2017-11-27 11:30:46 -05:00
res.locals.blacklistedVideo = blacklistedVideo
return true
2017-10-10 04:02:18 -04:00
}