]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
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 { areValidationErrors, checkUserCanManageVideo, doesVideoExist, isValidVideoIdParam } from '../shared'
8
9const videoOverallStatsValidator = [
10 isValidVideoIdParam('videoId'),
11
49f0468d
C
12 query('startDate')
13 .optional()
396f6f01 14 .custom(isDateValid),
49f0468d
C
15
16 query('endDate')
17 .optional()
396f6f01 18 .custom(isDateValid),
49f0468d 19
b2111066 20 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
b2111066
C
21 if (areValidationErrors(req, res)) return
22 if (!await commonStatsCheck(req, res)) return
23
24 return next()
25 }
26]
27
28const videoRetentionStatsValidator = [
29 isValidVideoIdParam('videoId'),
30
31 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
b2111066
C
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
46const videoTimeserieStatsValidator = [
47 isValidVideoIdParam('videoId'),
48
49 param('metric')
396f6f01 50 .custom(isValidStatTimeserieMetric),
b2111066 51
901bcf5c
C
52 query('startDate')
53 .optional()
396f6f01 54 .custom(isDateValid),
901bcf5c
C
55
56 query('endDate')
57 .optional()
396f6f01 58 .custom(isDateValid),
901bcf5c 59
b2111066 60 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
b2111066
C
61 if (areValidationErrors(req, res)) return
62 if (!await commonStatsCheck(req, res)) return
63
901bcf5c
C
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
b2111066
C
82 return next()
83 }
84]
85
86// ---------------------------------------------------------------------------
87
88export {
89 videoOverallStatsValidator,
90 videoTimeserieStatsValidator,
91 videoRetentionStatsValidator
92}
93
94// ---------------------------------------------------------------------------
95
96async 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}
901bcf5c
C
102
103function 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}