]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/middlewares/validators/videos/video-rates.ts
Merge branch 'develop' into shorter-URLs-channels-accounts
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-rates.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { body, param, query } from 'express-validator'
3import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc'
4import { isRatingValid } from '../../../helpers/custom-validators/video-rates'
5import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos'
6import { logger } from '../../../helpers/logger'
7import { areValidationErrors } from '../utils'
8import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
9import { VideoRateType } from '../../../../shared/models/videos'
10import { isAccountNameValid } from '../../../helpers/custom-validators/accounts'
11import { doesVideoExist } from '../../../helpers/middlewares'
12import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
13
14const videoUpdateRateValidator = [
15 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
16 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
17
18 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
19 logger.debug('Checking videoRate parameters', { parameters: req.body })
20
21 if (areValidationErrors(req, res)) return
22 if (!await doesVideoExist(req.params.id, res)) return
23
24 return next()
25 }
26]
27
28const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
29 return [
30 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
31 param('videoId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid videoId'),
32
33 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
34 logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
35
36 if (areValidationErrors(req, res)) return
37
38 const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId)
39 if (!rate) {
40 return res.status(HttpStatusCode.NOT_FOUND_404)
41 .json({ error: 'Video rate not found' })
42 }
43
44 res.locals.accountVideoRate = rate
45
46 return next()
47 }
48 ]
49}
50
51const videoRatingValidator = [
52 query('rating').optional().custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
53
54 (req: express.Request, res: express.Response, next: express.NextFunction) => {
55 logger.debug('Checking rating parameter', { parameters: req.params })
56
57 if (areValidationErrors(req, res)) return
58
59 return next()
60 }
61]
62
63// ---------------------------------------------------------------------------
64
65export {
66 videoUpdateRateValidator,
67 getAccountVideoRateValidatorFactory,
68 videoRatingValidator
69}