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