]>
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' |
0f6acda1 | 6 | import { doesVideoExist, 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' | |
12 | ||
13 | const videoUpdateRateValidator = [ | |
14 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | |
15 | body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'), | |
16 | ||
17 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
18 | logger.debug('Checking videoRate parameters', { parameters: req.body }) | |
19 | ||
20 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 21 | if (!await doesVideoExist(req.params.id, res)) return |
5c6d985f C |
22 | |
23 | return next() | |
24 | } | |
25 | ] | |
26 | ||
27 | const getAccountVideoRateValidator = function (rateType: VideoRateType) { | |
28 | return [ | |
29 | param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'), | |
30 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | |
31 | ||
32 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
33 | logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params }) | |
34 | ||
35 | if (areValidationErrors(req, res)) return | |
36 | ||
37 | const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, req.params.videoId) | |
38 | if (!rate) { | |
39 | return res.status(404) | |
40 | .json({ error: 'Video rate not found' }) | |
41 | .end() | |
42 | } | |
43 | ||
44 | res.locals.accountVideoRate = rate | |
45 | ||
46 | return next() | |
47 | } | |
48 | ] | |
49 | } | |
50 | ||
c100a614 YB |
51 | const videoRatingValidator = [ |
52 | query('rating').optional().custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'), | |
53 | ||
54 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
55 | logger.debug('Checking rating parameter', { parameters: req.params }) | |
56 | ||
57 | if (areValidationErrors(req, res)) return | |
58 | ||
59 | return next() | |
60 | } | |
61 | ] | |
62 | ||
5c6d985f C |
63 | // --------------------------------------------------------------------------- |
64 | ||
65 | export { | |
66 | videoUpdateRateValidator, | |
c100a614 YB |
67 | getAccountVideoRateValidator, |
68 | videoRatingValidator | |
5c6d985f | 69 | } |