]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-stats.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-stats.ts
CommitLineData
b2111066 1import express from 'express'
901bcf5c
C
2import { param, query } from 'express-validator'
3import { isDateValid } from '@server/helpers/custom-validators/misc'
b2111066 4import { isValidStatTimeserieMetric } from '@server/helpers/custom-validators/video-stats'
901bcf5c
C
5import { STATS_TIMESERIE } from '@server/initializers/constants'
6import { HttpStatusCode, UserRight, VideoStatsTimeserieQuery } from '@shared/models'
b2111066
C
7import { logger } from '../../../helpers/logger'
8import { areValidationErrors, checkUserCanManageVideo, doesVideoExist, isValidVideoIdParam } from '../shared'
9
10const videoOverallStatsValidator = [
11 isValidVideoIdParam('videoId'),
12
49f0468d
C
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
b2111066
C
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
33const 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
53const videoTimeserieStatsValidator = [
54 isValidVideoIdParam('videoId'),
55
56 param('metric')
57 .custom(isValidStatTimeserieMetric)
58 .withMessage('Should have a valid timeserie metric'),
59
901bcf5c
C
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
b2111066
C
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
901bcf5c
C
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
b2111066
C
94 return next()
95 }
96]
97
98// ---------------------------------------------------------------------------
99
100export {
101 videoOverallStatsValidator,
102 videoTimeserieStatsValidator,
103 videoRetentionStatsValidator
104}
105
106// ---------------------------------------------------------------------------
107
108async 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}
901bcf5c
C
114
115function 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}