]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { param } from 'express-validator' | |
3 | import { isValidStatTimeserieMetric } from '@server/helpers/custom-validators/video-stats' | |
4 | import { HttpStatusCode, UserRight } from '@shared/models' | |
5 | import { logger } from '../../../helpers/logger' | |
6 | import { areValidationErrors, checkUserCanManageVideo, doesVideoExist, isValidVideoIdParam } from '../shared' | |
7 | ||
8 | const videoOverallStatsValidator = [ | |
9 | isValidVideoIdParam('videoId'), | |
10 | ||
11 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
12 | logger.debug('Checking videoOverallStatsValidator parameters', { parameters: req.body }) | |
13 | ||
14 | if (areValidationErrors(req, res)) return | |
15 | if (!await commonStatsCheck(req, res)) return | |
16 | ||
17 | return next() | |
18 | } | |
19 | ] | |
20 | ||
21 | const videoRetentionStatsValidator = [ | |
22 | isValidVideoIdParam('videoId'), | |
23 | ||
24 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
25 | logger.debug('Checking videoRetentionStatsValidator parameters', { parameters: req.body }) | |
26 | ||
27 | if (areValidationErrors(req, res)) return | |
28 | if (!await commonStatsCheck(req, res)) return | |
29 | ||
30 | if (res.locals.videoAll.isLive) { | |
31 | return res.fail({ | |
32 | status: HttpStatusCode.BAD_REQUEST_400, | |
33 | message: 'Cannot get retention stats of live video' | |
34 | }) | |
35 | } | |
36 | ||
37 | return next() | |
38 | } | |
39 | ] | |
40 | ||
41 | const videoTimeserieStatsValidator = [ | |
42 | isValidVideoIdParam('videoId'), | |
43 | ||
44 | param('metric') | |
45 | .custom(isValidStatTimeserieMetric) | |
46 | .withMessage('Should have a valid timeserie metric'), | |
47 | ||
48 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
49 | logger.debug('Checking videoTimeserieStatsValidator parameters', { parameters: req.body }) | |
50 | ||
51 | if (areValidationErrors(req, res)) return | |
52 | if (!await commonStatsCheck(req, res)) return | |
53 | ||
54 | return next() | |
55 | } | |
56 | ] | |
57 | ||
58 | // --------------------------------------------------------------------------- | |
59 | ||
60 | export { | |
61 | videoOverallStatsValidator, | |
62 | videoTimeserieStatsValidator, | |
63 | videoRetentionStatsValidator | |
64 | } | |
65 | ||
66 | // --------------------------------------------------------------------------- | |
67 | ||
68 | async function commonStatsCheck (req: express.Request, res: express.Response) { | |
69 | if (!await doesVideoExist(req.params.videoId, res, 'all')) return false | |
70 | if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.SEE_ALL_VIDEOS, res)) return false | |
71 | ||
72 | return true | |
73 | } |