diff options
Diffstat (limited to 'server/server/middlewares/validators/videos/video-rates.ts')
-rw-r--r-- | server/server/middlewares/validators/videos/video-rates.ts | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/server/server/middlewares/validators/videos/video-rates.ts b/server/server/middlewares/validators/videos/video-rates.ts new file mode 100644 index 000000000..f7b784fe3 --- /dev/null +++ b/server/server/middlewares/validators/videos/video-rates.ts | |||
@@ -0,0 +1,71 @@ | |||
1 | import express from 'express' | ||
2 | import { body, param, query } from 'express-validator' | ||
3 | import { HttpStatusCode, VideoRateType } from '@peertube/peertube-models' | ||
4 | import { isAccountNameValid } from '../../../helpers/custom-validators/accounts.js' | ||
5 | import { isIdValid } from '../../../helpers/custom-validators/misc.js' | ||
6 | import { isRatingValid } from '../../../helpers/custom-validators/video-rates.js' | ||
7 | import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos.js' | ||
8 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate.js' | ||
9 | import { areValidationErrors, checkCanSeeVideo, doesVideoExist, isValidVideoIdParam, isValidVideoPasswordHeader } from '../shared/index.js' | ||
10 | |||
11 | const videoUpdateRateValidator = [ | ||
12 | isValidVideoIdParam('id'), | ||
13 | |||
14 | body('rating') | ||
15 | .custom(isVideoRatingTypeValid), | ||
16 | isValidVideoPasswordHeader(), | ||
17 | |||
18 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
19 | if (areValidationErrors(req, res)) return | ||
20 | if (!await doesVideoExist(req.params.id, res)) return | ||
21 | |||
22 | if (!await checkCanSeeVideo({ req, res, paramId: req.params.id, video: res.locals.videoAll })) return | ||
23 | |||
24 | return next() | ||
25 | } | ||
26 | ] | ||
27 | |||
28 | const getAccountVideoRateValidatorFactory = function (rateType: VideoRateType) { | ||
29 | return [ | ||
30 | param('name') | ||
31 | .custom(isAccountNameValid), | ||
32 | param('videoId') | ||
33 | .custom(isIdValid), | ||
34 | |||
35 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
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.fail({ | ||
41 | status: HttpStatusCode.NOT_FOUND_404, | ||
42 | message: 'Video rate not found' | ||
43 | }) | ||
44 | } | ||
45 | |||
46 | res.locals.accountVideoRate = rate | ||
47 | |||
48 | return next() | ||
49 | } | ||
50 | ] | ||
51 | } | ||
52 | |||
53 | const videoRatingValidator = [ | ||
54 | query('rating') | ||
55 | .optional() | ||
56 | .custom(isRatingValid).withMessage('Value must be one of "like" or "dislike"'), | ||
57 | |||
58 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
59 | if (areValidationErrors(req, res)) return | ||
60 | |||
61 | return next() | ||
62 | } | ||
63 | ] | ||
64 | |||
65 | // --------------------------------------------------------------------------- | ||
66 | |||
67 | export { | ||
68 | videoUpdateRateValidator, | ||
69 | getAccountVideoRateValidatorFactory, | ||
70 | videoRatingValidator | ||
71 | } | ||