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