]>
Commit | Line | Data |
---|---|---|
5c6d985f | 1 | import * as express from 'express' |
c8861d5d | 2 | import { body, param, query } from 'express-validator' |
c0e8b12e | 3 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' |
10363c74 C |
4 | import { VideoRateType } from '../../../../shared/models/videos' |
5 | import { isAccountNameValid } from '../../../helpers/custom-validators/accounts' | |
d4a8e7a6 | 6 | import { isIdValid } from '../../../helpers/custom-validators/misc' |
c100a614 | 7 | import { isRatingValid } from '../../../helpers/custom-validators/video-rates' |
3e753302 | 8 | import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos' |
5c6d985f | 9 | import { logger } from '../../../helpers/logger' |
5c6d985f | 10 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
d4a8e7a6 | 11 | import { areValidationErrors, doesVideoExist, isValidVideoIdParam } from '../shared' |
5c6d985f C |
12 | |
13 | const 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 C |
23 | |
24 | return next() | |
25 | } | |
26 | ] | |
27 | ||
75ba887d | 28 | const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) { |
5c6d985f C |
29 | return [ |
30 | param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'), | |
74d249bc | 31 | param('videoId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid videoId'), |
5c6d985f C |
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 | ||
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 YB |
53 | const videoRatingValidator = [ |
54 | query('rating').optional().custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'), | |
55 | ||
a1587156 | 56 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
c100a614 YB |
57 | logger.debug('Checking rating parameter', { parameters: req.params }) |
58 | ||
59 | if (areValidationErrors(req, res)) return | |
60 | ||
61 | return next() | |
62 | } | |
63 | ] | |
64 | ||
5c6d985f C |
65 | // --------------------------------------------------------------------------- |
66 | ||
67 | export { | |
68 | videoUpdateRateValidator, | |
75ba887d | 69 | getAccountVideoRateValidatorFactory, |
c100a614 | 70 | videoRatingValidator |
5c6d985f | 71 | } |