video-stats.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import express from 'express'
  2. import { param } from 'express-validator'
  3. import { isValidStatTimeserieMetric } from '@server/helpers/custom-validators/video-stats'
  4. import { HttpStatusCode, UserRight } from '@shared/models'
  5. import { logger } from '../../../helpers/logger'
  6. import { areValidationErrors, checkUserCanManageVideo, doesVideoExist, isValidVideoIdParam } from '../shared'
  7. const videoOverallStatsValidator = [
  8. isValidVideoIdParam('videoId'),
  9. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  10. logger.debug('Checking videoOverallStatsValidator parameters', { parameters: req.body })
  11. if (areValidationErrors(req, res)) return
  12. if (!await commonStatsCheck(req, res)) return
  13. return next()
  14. }
  15. ]
  16. const videoRetentionStatsValidator = [
  17. isValidVideoIdParam('videoId'),
  18. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  19. logger.debug('Checking videoRetentionStatsValidator parameters', { parameters: req.body })
  20. if (areValidationErrors(req, res)) return
  21. if (!await commonStatsCheck(req, res)) return
  22. if (res.locals.videoAll.isLive) {
  23. return res.fail({
  24. status: HttpStatusCode.BAD_REQUEST_400,
  25. message: 'Cannot get retention stats of live video'
  26. })
  27. }
  28. return next()
  29. }
  30. ]
  31. const videoTimeserieStatsValidator = [
  32. isValidVideoIdParam('videoId'),
  33. param('metric')
  34. .custom(isValidStatTimeserieMetric)
  35. .withMessage('Should have a valid timeserie metric'),
  36. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  37. logger.debug('Checking videoTimeserieStatsValidator parameters', { parameters: req.body })
  38. if (areValidationErrors(req, res)) return
  39. if (!await commonStatsCheck(req, res)) return
  40. return next()
  41. }
  42. ]
  43. // ---------------------------------------------------------------------------
  44. export {
  45. videoOverallStatsValidator,
  46. videoTimeserieStatsValidator,
  47. videoRetentionStatsValidator
  48. }
  49. // ---------------------------------------------------------------------------
  50. async function commonStatsCheck (req: express.Request, res: express.Response) {
  51. if (!await doesVideoExist(req.params.videoId, res, 'all')) return false
  52. if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.SEE_ALL_VIDEOS, res)) return false
  53. return true
  54. }