PeerTube/server/middlewares/validators/videos/video-rates.ts

74 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-08-27 12:32:44 +00:00
import express from 'express'
2019-07-25 14:23:44 +00:00
import { body, param, query } from 'express-validator'
2021-07-16 08:42:24 +00:00
import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
import { VideoRateType } from '../../../../shared/models/videos'
import { isAccountNameValid } from '../../../helpers/custom-validators/accounts'
import { isIdValid } from '../../../helpers/custom-validators/misc'
import { isRatingValid } from '../../../helpers/custom-validators/video-rates'
2019-07-23 08:40:39 +00:00
import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos'
2018-11-14 14:01:28 +00:00
import { logger } from '../../../helpers/logger'
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
2022-06-22 07:44:08 +00:00
import { areValidationErrors, checkCanSeeVideo, doesVideoExist, isValidVideoIdParam } from '../shared'
2018-11-14 14:01:28 +00:00
const videoUpdateRateValidator = [
isValidVideoIdParam('id'),
2018-11-14 14:01:28 +00:00
body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoRate parameters', { parameters: req.body })
if (areValidationErrors(req, res)) return
2019-03-19 08:26:50 +00:00
if (!await doesVideoExist(req.params.id, res)) return
2018-11-14 14:01:28 +00:00
2022-06-22 07:44:08 +00:00
if (!await checkCanSeeVideo({ req, res, paramId: req.params.id, video: res.locals.videoAll })) return
2018-11-14 14:01:28 +00:00
return next()
}
]
const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
2018-11-14 14:01:28 +00:00
return [
param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
param('videoId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid videoId'),
2018-11-14 14:01:28 +00:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
if (areValidationErrors(req, res)) return
const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId)
2018-11-14 14:01:28 +00:00
if (!rate) {
return res.fail({
status: HttpStatusCode.NOT_FOUND_404,
message: 'Video rate not found'
})
2018-11-14 14:01:28 +00:00
}
res.locals.accountVideoRate = rate
return next()
}
]
}
const videoRatingValidator = [
query('rating').optional().custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
2020-01-31 15:56:52 +00:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking rating parameter', { parameters: req.params })
if (areValidationErrors(req, res)) return
return next()
}
]
2018-11-14 14:01:28 +00:00
// ---------------------------------------------------------------------------
export {
videoUpdateRateValidator,
getAccountVideoRateValidatorFactory,
videoRatingValidator
2018-11-14 14:01:28 +00:00
}