]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-rates.ts
Merge branch 'release/5.0.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 { AccountVideoRateModel } from '../../../models/account/account-video-rate'
ff9d43f6 10import { areValidationErrors, checkCanSeeVideo, doesVideoExist, isValidVideoIdParam } from '../shared'
5c6d985f
C
11
12const videoUpdateRateValidator = [
d4a8e7a6
C
13 isValidVideoIdParam('id'),
14
396f6f01
C
15 body('rating')
16 .custom(isVideoRatingTypeValid),
5c6d985f
C
17
18 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
5c6d985f 19 if (areValidationErrors(req, res)) return
0f6acda1 20 if (!await doesVideoExist(req.params.id, res)) return
5c6d985f 21
ff9d43f6 22 if (!await checkCanSeeVideo({ req, res, paramId: req.params.id, video: res.locals.videoAll })) return
6ea9295b 23
5c6d985f
C
24 return next()
25 }
26]
27
75ba887d 28const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) {
5c6d985f 29 return [
396f6f01
C
30 param('name')
31 .custom(isAccountNameValid),
32 param('videoId')
33 .custom(isIdValid),
5c6d985f
C
34
35 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
5c6d985f
C
36 if (areValidationErrors(req, res)) return
37
74d249bc 38 const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId)
5c6d985f 39 if (!rate) {
76148b27
RK
40 return res.fail({
41 status: HttpStatusCode.NOT_FOUND_404,
42 message: 'Video rate not found'
43 })
5c6d985f
C
44 }
45
46 res.locals.accountVideoRate = rate
47
48 return next()
49 }
50 ]
51}
52
c100a614 53const videoRatingValidator = [
396f6f01
C
54 query('rating')
55 .optional()
56 .custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
c100a614 57
a1587156 58 (req: express.Request, res: express.Response, next: express.NextFunction) => {
c100a614
YB
59 if (areValidationErrors(req, res)) return
60
61 return next()
62 }
63]
64
5c6d985f
C
65// ---------------------------------------------------------------------------
66
67export {
68 videoUpdateRateValidator,
75ba887d 69 getAccountVideoRateValidatorFactory,
c100a614 70 videoRatingValidator
5c6d985f 71}