]>
Commit | Line | Data |
---|---|---|
5c6d985f C |
1 | import * as express from 'express' |
2 | import 'express-validator' | |
c100a614 | 3 | import { body, param, query } from 'express-validator/check' |
22834691 | 4 | import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc' |
c100a614 | 5 | import { isRatingValid } from '../../../helpers/custom-validators/video-rates' |
3e753302 | 6 | import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos' |
5c6d985f C |
7 | import { logger } from '../../../helpers/logger' |
8 | import { areValidationErrors } from '../utils' | |
9 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
10 | import { VideoRateType } from '../../../../shared/models/videos' | |
11 | import { isAccountNameValid } from '../../../helpers/custom-validators/accounts' | |
3e753302 | 12 | import { doesVideoExist } from '../../../helpers/middlewares' |
5c6d985f C |
13 | |
14 | const videoUpdateRateValidator = [ | |
15 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | |
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 C |
23 | |
24 | return next() | |
25 | } | |
26 | ] | |
27 | ||
28 | const getAccountVideoRateValidator = function (rateType: VideoRateType) { | |
29 | return [ | |
30 | param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'), | |
31 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | |
32 | ||
33 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
34 | logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params }) | |
35 | ||
36 | if (areValidationErrors(req, res)) return | |
37 | ||
38 | const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, req.params.videoId) | |
39 | if (!rate) { | |
40 | return res.status(404) | |
41 | .json({ error: 'Video rate not found' }) | |
42 | .end() | |
43 | } | |
44 | ||
45 | res.locals.accountVideoRate = rate | |
46 | ||
47 | return next() | |
48 | } | |
49 | ] | |
50 | } | |
51 | ||
c100a614 YB |
52 | const videoRatingValidator = [ |
53 | query('rating').optional().custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'), | |
54 | ||
55 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
56 | logger.debug('Checking rating parameter', { parameters: req.params }) | |
57 | ||
58 | if (areValidationErrors(req, res)) return | |
59 | ||
60 | return next() | |
61 | } | |
62 | ] | |
63 | ||
5c6d985f C |
64 | // --------------------------------------------------------------------------- |
65 | ||
66 | export { | |
67 | videoUpdateRateValidator, | |
c100a614 YB |
68 | getAccountVideoRateValidator, |
69 | videoRatingValidator | |
5c6d985f | 70 | } |