]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-rates.ts
Check video privacy when creating comments/rates
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-rates.ts
1 import express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
4 import { VideoRateType } from '../../../../shared/models/videos'
5 import { isAccountNameValid } from '../../../helpers/custom-validators/accounts'
6 import { isIdValid } from '../../../helpers/custom-validators/misc'
7 import { isRatingValid } from '../../../helpers/custom-validators/video-rates'
8 import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos'
9 import { logger } from '../../../helpers/logger'
10 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
11 import { areValidationErrors, checkCanSeeVideoIfPrivate, doesVideoExist, isValidVideoIdParam } from '../shared'
12
13 const videoUpdateRateValidator = [
14 isValidVideoIdParam('id'),
15
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 if (!await checkCanSeeVideoIfPrivate(req, res, res.locals.videoAll)) {
25 return res.fail({
26 status: HttpStatusCode.FORBIDDEN_403,
27 message: 'Cannot access to this ressource'
28 })
29 }
30
31 return next()
32 }
33 ]
34
35 const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
36 return [
37 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
38 param('videoId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid videoId'),
39
40 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
41 logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
42
43 if (areValidationErrors(req, res)) return
44
45 const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId)
46 if (!rate) {
47 return res.fail({
48 status: HttpStatusCode.NOT_FOUND_404,
49 message: 'Video rate not found'
50 })
51 }
52
53 res.locals.accountVideoRate = rate
54
55 return next()
56 }
57 ]
58 }
59
60 const videoRatingValidator = [
61 query('rating').optional().custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
62
63 (req: express.Request, res: express.Response, next: express.NextFunction) => {
64 logger.debug('Checking rating parameter', { parameters: req.params })
65
66 if (areValidationErrors(req, res)) return
67
68 return next()
69 }
70 ]
71
72 // ---------------------------------------------------------------------------
73
74 export {
75 videoUpdateRateValidator,
76 getAccountVideoRateValidatorFactory,
77 videoRatingValidator
78 }