]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { param, query } from 'express-validator' | |
3 | import { isDateValid } from '@server/helpers/custom-validators/misc' | |
4 | import { isValidStatTimeserieMetric } from '@server/helpers/custom-validators/video-stats' | |
5 | import { STATS_TIMESERIE } from '@server/initializers/constants' | |
6 | import { HttpStatusCode, UserRight, VideoStatsTimeserieQuery } from '@shared/models' | |
7 | import { logger } from '../../../helpers/logger' | |
8 | import { areValidationErrors, checkUserCanManageVideo, doesVideoExist, isValidVideoIdParam } from '../shared' | |
9 | ||
10 | const videoOverallStatsValidator = [ | |
11 | isValidVideoIdParam('videoId'), | |
12 | ||
13 | query('startDate') | |
14 | .optional() | |
15 | .custom(isDateValid) | |
16 | .withMessage('Should have a valid start date'), | |
17 | ||
18 | query('endDate') | |
19 | .optional() | |
20 | .custom(isDateValid) | |
21 | .withMessage('Should have a valid end date'), | |
22 | ||
23 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
24 | logger.debug('Checking videoOverallStatsValidator parameters', { parameters: req.body }) | |
25 | ||
26 | if (areValidationErrors(req, res)) return | |
27 | if (!await commonStatsCheck(req, res)) return | |
28 | ||
29 | return next() | |
30 | } | |
31 | ] | |
32 | ||
33 | const videoRetentionStatsValidator = [ | |
34 | isValidVideoIdParam('videoId'), | |
35 | ||
36 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
37 | logger.debug('Checking videoRetentionStatsValidator parameters', { parameters: req.body }) | |
38 | ||
39 | if (areValidationErrors(req, res)) return | |
40 | if (!await commonStatsCheck(req, res)) return | |
41 | ||
42 | if (res.locals.videoAll.isLive) { | |
43 | return res.fail({ | |
44 | status: HttpStatusCode.BAD_REQUEST_400, | |
45 | message: 'Cannot get retention stats of live video' | |
46 | }) | |
47 | } | |
48 | ||
49 | return next() | |
50 | } | |
51 | ] | |
52 | ||
53 | const videoTimeserieStatsValidator = [ | |
54 | isValidVideoIdParam('videoId'), | |
55 | ||
56 | param('metric') | |
57 | .custom(isValidStatTimeserieMetric) | |
58 | .withMessage('Should have a valid timeserie metric'), | |
59 | ||
60 | query('startDate') | |
61 | .optional() | |
62 | .custom(isDateValid) | |
63 | .withMessage('Should have a valid start date'), | |
64 | ||
65 | query('endDate') | |
66 | .optional() | |
67 | .custom(isDateValid) | |
68 | .withMessage('Should have a valid end date'), | |
69 | ||
70 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
71 | logger.debug('Checking videoTimeserieStatsValidator parameters', { parameters: req.body }) | |
72 | ||
73 | if (areValidationErrors(req, res)) return | |
74 | if (!await commonStatsCheck(req, res)) return | |
75 | ||
76 | const query: VideoStatsTimeserieQuery = req.query | |
77 | if ( | |
78 | (query.startDate && !query.endDate) || | |
79 | (!query.startDate && query.endDate) | |
80 | ) { | |
81 | return res.fail({ | |
82 | status: HttpStatusCode.BAD_REQUEST_400, | |
83 | message: 'Both start date and end date should be defined if one of them is specified' | |
84 | }) | |
85 | } | |
86 | ||
87 | if (query.startDate && getIntervalByDays(query.startDate, query.endDate) > STATS_TIMESERIE.MAX_DAYS) { | |
88 | return res.fail({ | |
89 | status: HttpStatusCode.BAD_REQUEST_400, | |
90 | message: 'Star date and end date interval is too big' | |
91 | }) | |
92 | } | |
93 | ||
94 | return next() | |
95 | } | |
96 | ] | |
97 | ||
98 | // --------------------------------------------------------------------------- | |
99 | ||
100 | export { | |
101 | videoOverallStatsValidator, | |
102 | videoTimeserieStatsValidator, | |
103 | videoRetentionStatsValidator | |
104 | } | |
105 | ||
106 | // --------------------------------------------------------------------------- | |
107 | ||
108 | async function commonStatsCheck (req: express.Request, res: express.Response) { | |
109 | if (!await doesVideoExist(req.params.videoId, res, 'all')) return false | |
110 | if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.SEE_ALL_VIDEOS, res)) return false | |
111 | ||
112 | return true | |
113 | } | |
114 | ||
115 | function getIntervalByDays (startDateString: string, endDateString: string) { | |
116 | const startDate = new Date(startDateString) | |
117 | const endDate = new Date(endDateString) | |
118 | ||
119 | return (endDate.getTime() - startDate.getTime()) / 1000 / 86400 | |
120 | } |