]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-rates.ts
Fix incorrect IDs in AP federation
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-rates.ts
CommitLineData
5c6d985f 1import * as express from 'express'
c8861d5d 2import { body, param, query } from 'express-validator'
22834691 3import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc'
c100a614 4import { isRatingValid } from '../../../helpers/custom-validators/video-rates'
3e753302 5import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos'
5c6d985f
C
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'
3e753302 11import { doesVideoExist } from '../../../helpers/middlewares'
5c6d985f
C
12
13const videoUpdateRateValidator = [
14 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
15 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
16
17 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
18 logger.debug('Checking videoRate parameters', { parameters: req.body })
19
20 if (areValidationErrors(req, res)) return
0f6acda1 21 if (!await doesVideoExist(req.params.id, res)) return
5c6d985f
C
22
23 return next()
24 }
25]
26
75ba887d 27const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
5c6d985f
C
28 return [
29 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
30 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
31
32 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
33 logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
34
35 if (areValidationErrors(req, res)) return
36
37 const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, req.params.videoId)
38 if (!rate) {
39 return res.status(404)
40 .json({ error: 'Video rate not found' })
5c6d985f
C
41 }
42
43 res.locals.accountVideoRate = rate
44
45 return next()
46 }
47 ]
48}
49
c100a614
YB
50const videoRatingValidator = [
51 query('rating').optional().custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
52
a1587156 53 (req: express.Request, res: express.Response, next: express.NextFunction) => {
c100a614
YB
54 logger.debug('Checking rating parameter', { parameters: req.params })
55
56 if (areValidationErrors(req, res)) return
57
58 return next()
59 }
60]
61
5c6d985f
C
62// ---------------------------------------------------------------------------
63
64export {
65 videoUpdateRateValidator,
75ba887d 66 getAccountVideoRateValidatorFactory,
c100a614 67 videoRatingValidator
5c6d985f 68}