]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-rates.ts
Cleanup useless express validator messages
[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, checkCanSeeVideo, doesVideoExist, isValidVideoIdParam } from '../shared'
12
13 const videoUpdateRateValidator = [
14 isValidVideoIdParam('id'),
15
16 body('rating')
17 .custom(isVideoRatingTypeValid),
18
19 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
20 logger.debug('Checking videoRate parameters', { parameters: req.body })
21
22 if (areValidationErrors(req, res)) return
23 if (!await doesVideoExist(req.params.id, res)) return
24
25 if (!await checkCanSeeVideo({ req, res, paramId: req.params.id, video: res.locals.videoAll })) return
26
27 return next()
28 }
29 ]
30
31 const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
32 return [
33 param('name')
34 .custom(isAccountNameValid),
35 param('videoId')
36 .custom(isIdValid),
37
38 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
39 logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params })
40
41 if (areValidationErrors(req, res)) return
42
43 const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId)
44 if (!rate) {
45 return res.fail({
46 status: HttpStatusCode.NOT_FOUND_404,
47 message: 'Video rate not found'
48 })
49 }
50
51 res.locals.accountVideoRate = rate
52
53 return next()
54 }
55 ]
56 }
57
58 const videoRatingValidator = [
59 query('rating')
60 .optional()
61 .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 }