]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-rates.ts
Refactor middleware helpers
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-rates.ts
CommitLineData
5c6d985f
C
1import * as express from 'express'
2import 'express-validator'
c100a614 3import { body, param, query } from 'express-validator/check'
22834691 4import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc'
c100a614 5import { isRatingValid } from '../../../helpers/custom-validators/video-rates'
3e753302 6import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos'
5c6d985f
C
7import { logger } from '../../../helpers/logger'
8import { areValidationErrors } from '../utils'
9import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
10import { VideoRateType } from '../../../../shared/models/videos'
11import { isAccountNameValid } from '../../../helpers/custom-validators/accounts'
3e753302 12import { doesVideoExist } from '../../../helpers/middlewares'
5c6d985f
C
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
0f6acda1 22 if (!await doesVideoExist(req.params.id, res)) return
5c6d985f
C
23
24 return next()
25 }
26]
27
28const getAccountVideoRateValidator = function (rateType: VideoRateType) {
29 return [
30 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
31 param('videoId').custom(isIdOrUUIDValid).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(404)
41 .json({ error: 'Video rate not found' })
42 .end()
43 }
44
45 res.locals.accountVideoRate = rate
46
47 return next()
48 }
49 ]
50}
51
c100a614
YB
52const videoRatingValidator = [
53 query('rating').optional().custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
54
55 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
56 logger.debug('Checking rating parameter', { parameters: req.params })
57
58 if (areValidationErrors(req, res)) return
59
60 return next()
61 }
62]
63
5c6d985f
C
64// ---------------------------------------------------------------------------
65
66export {
67 videoUpdateRateValidator,
c100a614
YB
68 getAccountVideoRateValidator,
69 videoRatingValidator
5c6d985f 70}