]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-stats.ts
Merge branch 'release/5.0.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-stats.ts
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 { areValidationErrors, checkUserCanManageVideo, doesVideoExist, isValidVideoIdParam } from '../shared'
8
9 const videoOverallStatsValidator = [
10 isValidVideoIdParam('videoId'),
11
12 query('startDate')
13 .optional()
14 .custom(isDateValid),
15
16 query('endDate')
17 .optional()
18 .custom(isDateValid),
19
20 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
21 if (areValidationErrors(req, res)) return
22 if (!await commonStatsCheck(req, res)) return
23
24 return next()
25 }
26 ]
27
28 const videoRetentionStatsValidator = [
29 isValidVideoIdParam('videoId'),
30
31 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
32 if (areValidationErrors(req, res)) return
33 if (!await commonStatsCheck(req, res)) return
34
35 if (res.locals.videoAll.isLive) {
36 return res.fail({
37 status: HttpStatusCode.BAD_REQUEST_400,
38 message: 'Cannot get retention stats of live video'
39 })
40 }
41
42 return next()
43 }
44 ]
45
46 const videoTimeserieStatsValidator = [
47 isValidVideoIdParam('videoId'),
48
49 param('metric')
50 .custom(isValidStatTimeserieMetric),
51
52 query('startDate')
53 .optional()
54 .custom(isDateValid),
55
56 query('endDate')
57 .optional()
58 .custom(isDateValid),
59
60 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
61 if (areValidationErrors(req, res)) return
62 if (!await commonStatsCheck(req, res)) return
63
64 const query: VideoStatsTimeserieQuery = req.query
65 if (
66 (query.startDate && !query.endDate) ||
67 (!query.startDate && query.endDate)
68 ) {
69 return res.fail({
70 status: HttpStatusCode.BAD_REQUEST_400,
71 message: 'Both start date and end date should be defined if one of them is specified'
72 })
73 }
74
75 if (query.startDate && getIntervalByDays(query.startDate, query.endDate) > STATS_TIMESERIE.MAX_DAYS) {
76 return res.fail({
77 status: HttpStatusCode.BAD_REQUEST_400,
78 message: 'Star date and end date interval is too big'
79 })
80 }
81
82 return next()
83 }
84 ]
85
86 // ---------------------------------------------------------------------------
87
88 export {
89 videoOverallStatsValidator,
90 videoTimeserieStatsValidator,
91 videoRetentionStatsValidator
92 }
93
94 // ---------------------------------------------------------------------------
95
96 async function commonStatsCheck (req: express.Request, res: express.Response) {
97 if (!await doesVideoExist(req.params.videoId, res, 'all')) return false
98 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.SEE_ALL_VIDEOS, res)) return false
99
100 return true
101 }
102
103 function getIntervalByDays (startDateString: string, endDateString: string) {
104 const startDate = new Date(startDateString)
105 const endDate = new Date(endDateString)
106
107 return (endDate.getTime() - startDate.getTime()) / 1000 / 86400
108 }