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