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