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