]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-rates.ts
7933545207a2184b4bfcb12270997940d4976bfb
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-rates.ts
1 import * as express from 'express'
2 import 'express-validator'
3 import { body, param } from 'express-validator/check'
4 import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc'
5 import { isVideoExist, isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos'
6 import { logger } from '../../../helpers/logger'
7 import { areValidationErrors } from '../utils'
8 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
9 import { VideoRateType } from '../../../../shared/models/videos'
10 import { isAccountNameValid } from '../../../helpers/custom-validators/accounts'
11
12 const videoUpdateRateValidator = [
13 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
14 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
15
16 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
17 logger.debug('Checking videoRate parameters', { parameters: req.body })
18
19 if (areValidationErrors(req, res)) return
20 if (!await isVideoExist(req.params.id, res)) return
21
22 return next()
23 }
24 ]
25
26 const getAccountVideoRateValidator = function (rateType: VideoRateType) {
27 return [
28 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
29 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
30
31 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
32 logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
33
34 if (areValidationErrors(req, res)) return
35
36 const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, req.params.videoId)
37 if (!rate) {
38 return res.status(404)
39 .json({ error: 'Video rate not found' })
40 .end()
41 }
42
43 res.locals.accountVideoRate = rate
44
45 return next()
46 }
47 ]
48 }
49
50 // ---------------------------------------------------------------------------
51
52 export {
53 videoUpdateRateValidator,
54 getAccountVideoRateValidator
55 }