mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-06 03:50:26 +02:00
Video blacklist refractoring
This commit is contained in:
parent
769d332177
commit
35bf0c83c8
33 changed files with 239 additions and 269 deletions
|
@ -1,35 +0,0 @@
|
|||
import { param } from 'express-validator/check'
|
||||
import * as express from 'express'
|
||||
|
||||
import { database as db } from '../../initializers/database'
|
||||
import { checkErrors } from './utils'
|
||||
import { logger } from '../../helpers'
|
||||
|
||||
const blacklistRemoveValidator = [
|
||||
param('id').isNumeric().not().isEmpty().withMessage('Should have a valid id'),
|
||||
|
||||
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
|
||||
|
||||
checkErrors(req, res, () => {
|
||||
db.BlacklistedVideo.loadById(req.params.id)
|
||||
.then(entry => {
|
||||
if (!entry) return res.status(404).send('Blacklisted video not found')
|
||||
|
||||
res.locals.blacklistEntryToRemove = entry
|
||||
|
||||
next()
|
||||
})
|
||||
.catch(err => {
|
||||
logger.error('Error in blacklistRemove request validator', { error: err })
|
||||
return res.sendStatus(500)
|
||||
})
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
blacklistRemoveValidator
|
||||
}
|
|
@ -4,4 +4,4 @@ export * from './pods'
|
|||
export * from './sort'
|
||||
export * from './users'
|
||||
export * from './videos'
|
||||
export * from './blacklist'
|
||||
export * from './video-blacklist'
|
||||
|
|
67
server/middlewares/validators/video-blacklist.ts
Normal file
67
server/middlewares/validators/video-blacklist.ts
Normal file
|
@ -0,0 +1,67 @@
|
|||
import { param } from 'express-validator/check'
|
||||
import * as express from 'express'
|
||||
|
||||
import { database as db } from '../../initializers/database'
|
||||
import { checkErrors } from './utils'
|
||||
import { logger, isVideoIdOrUUIDValid, checkVideoExists } from '../../helpers'
|
||||
|
||||
const videosBlacklistRemoveValidator = [
|
||||
param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
|
||||
|
||||
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
|
||||
|
||||
checkErrors(req, res, () => {
|
||||
checkVideoExists(req.params.videoId, res, () => {
|
||||
checkVideoIsBlacklisted(req, res, next)
|
||||
})
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
const videosBlacklistAddValidator = [
|
||||
param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
|
||||
|
||||
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
|
||||
|
||||
checkErrors(req, res, () => {
|
||||
checkVideoExists(req.params.videoId, res, () => {
|
||||
checkVideoIsBlacklistable(req, res, next)
|
||||
})
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
videosBlacklistAddValidator,
|
||||
videosBlacklistRemoveValidator
|
||||
}
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
|
||||
if (res.locals.video.isOwned() === true) {
|
||||
return res.status(403)
|
||||
.json({ error: 'Cannot blacklist a local video' })
|
||||
.end()
|
||||
}
|
||||
|
||||
callback()
|
||||
}
|
||||
|
||||
function checkVideoIsBlacklisted (req: express.Request, res: express.Response, callback: () => void) {
|
||||
db.BlacklistedVideo.loadByVideoId(res.locals.video.id)
|
||||
.then(blacklistedVideo => {
|
||||
if (!blacklistedVideo) return res.status(404).send('Blacklisted video not found')
|
||||
|
||||
res.locals.blacklistedVideo = blacklistedVideo
|
||||
|
||||
callback()
|
||||
})
|
||||
.catch(err => {
|
||||
logger.error('Error in blacklistRemove request validator', { error: err })
|
||||
return res.sendStatus(500)
|
||||
})
|
||||
}
|
|
@ -1,7 +1,5 @@
|
|||
import { body, param, query } from 'express-validator/check'
|
||||
import * as express from 'express'
|
||||
import * as Promise from 'bluebird'
|
||||
import * as validator from 'validator'
|
||||
|
||||
import { database as db } from '../../initializers/database'
|
||||
import { checkErrors } from './utils'
|
||||
|
@ -20,9 +18,9 @@ import {
|
|||
isVideoIdOrUUIDValid,
|
||||
isVideoAbuseReasonValid,
|
||||
isVideoRatingTypeValid,
|
||||
getDurationFromVideoFile
|
||||
getDurationFromVideoFile,
|
||||
checkVideoExists
|
||||
} from '../../helpers'
|
||||
import { VideoInstance } from '../../models'
|
||||
|
||||
const videosAddValidator = [
|
||||
body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage('Should have a valid file'),
|
||||
|
@ -186,20 +184,6 @@ const videoRateValidator = [
|
|||
}
|
||||
]
|
||||
|
||||
const videosBlacklistValidator = [
|
||||
param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
|
||||
|
||||
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
logger.debug('Checking videosBlacklist parameters', { parameters: req.params })
|
||||
|
||||
checkErrors(req, res, () => {
|
||||
checkVideoExists(req.params.id, res, () => {
|
||||
checkVideoIsBlacklistable(req, res, next)
|
||||
})
|
||||
})
|
||||
}
|
||||
]
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
|
@ -211,37 +195,11 @@ export {
|
|||
|
||||
videoAbuseReportValidator,
|
||||
|
||||
videoRateValidator,
|
||||
|
||||
videosBlacklistValidator
|
||||
videoRateValidator
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function checkVideoExists (id: string, res: express.Response, callback: () => void) {
|
||||
let promise: Promise<VideoInstance>
|
||||
if (validator.isInt(id)) {
|
||||
promise = db.Video.loadAndPopulateAuthorAndPodAndTags(+id)
|
||||
} else { // UUID
|
||||
promise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(id)
|
||||
}
|
||||
|
||||
promise.then(video => {
|
||||
if (!video) {
|
||||
return res.status(404)
|
||||
.json({ error: 'Video not found' })
|
||||
.end()
|
||||
}
|
||||
|
||||
res.locals.video = video
|
||||
callback()
|
||||
})
|
||||
.catch(err => {
|
||||
logger.error('Error in video request validator.', err)
|
||||
return res.sendStatus(500)
|
||||
})
|
||||
}
|
||||
|
||||
function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
|
||||
// Retrieve the user who did the request
|
||||
db.User.loadById(userId)
|
||||
|
@ -269,13 +227,3 @@ function checkUserCanDeleteVideo (userId: number, res: express.Response, callbac
|
|||
return res.sendStatus(500)
|
||||
})
|
||||
}
|
||||
|
||||
function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
|
||||
if (res.locals.video.isOwned() === true) {
|
||||
return res.status(403)
|
||||
.json({ error: 'Cannot blacklist a local video' })
|
||||
.end()
|
||||
}
|
||||
|
||||
callback()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue