]>
Commit | Line | Data |
---|---|---|
5c6d985f | 1 | import * as express from 'express' |
c8861d5d | 2 | import { body, param, query } from 'express-validator' |
10363c74 C |
3 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' |
4 | import { VideoRateType } from '../../../../shared/models/videos' | |
5 | import { isAccountNameValid } from '../../../helpers/custom-validators/accounts' | |
74d249bc | 6 | import { isIdOrUUIDValid, 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' |
10363c74 | 11 | import { areValidationErrors, doesVideoExist } from '../shared' |
5c6d985f C |
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 | ||
75ba887d | 27 | const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) { |
5c6d985f C |
28 | return [ |
29 | param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'), | |
74d249bc | 30 | param('videoId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid videoId'), |
5c6d985f C |
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 | ||
74d249bc | 37 | const rate = await AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId) |
5c6d985f | 38 | if (!rate) { |
76148b27 RK |
39 | return res.fail({ |
40 | status: HttpStatusCode.NOT_FOUND_404, | |
41 | message: 'Video rate not found' | |
42 | }) | |
5c6d985f C |
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 | ||
a1587156 | 55 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
c100a614 YB |
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, | |
75ba887d | 68 | getAccountVideoRateValidatorFactory, |
c100a614 | 69 | videoRatingValidator |
5c6d985f | 70 | } |