video-abuses.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import * as express from 'express'
  2. import 'express-validator'
  3. import { body, param } from 'express-validator/check'
  4. import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc'
  5. import { doesVideoExist } from '../../../helpers/custom-validators/videos'
  6. import { logger } from '../../../helpers/logger'
  7. import { areValidationErrors } from '../utils'
  8. import {
  9. doesVideoAbuseExist,
  10. isVideoAbuseModerationCommentValid,
  11. isVideoAbuseReasonValid,
  12. isVideoAbuseStateValid
  13. } from '../../../helpers/custom-validators/video-abuses'
  14. const videoAbuseReportValidator = [
  15. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
  16. body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
  17. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  18. logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
  19. if (areValidationErrors(req, res)) return
  20. if (!await doesVideoExist(req.params.videoId, res)) return
  21. return next()
  22. }
  23. ]
  24. const videoAbuseGetValidator = [
  25. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
  26. param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
  27. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  28. logger.debug('Checking videoAbuseGetValidator parameters', { parameters: req.body })
  29. if (areValidationErrors(req, res)) return
  30. if (!await doesVideoExist(req.params.videoId, res)) return
  31. if (!await doesVideoAbuseExist(req.params.id, res.locals.video.id, res)) return
  32. return next()
  33. }
  34. ]
  35. const videoAbuseUpdateValidator = [
  36. param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
  37. param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
  38. body('state')
  39. .optional()
  40. .custom(isVideoAbuseStateValid).withMessage('Should have a valid video abuse state'),
  41. body('moderationComment')
  42. .optional()
  43. .custom(isVideoAbuseModerationCommentValid).withMessage('Should have a valid video moderation comment'),
  44. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  45. logger.debug('Checking videoAbuseUpdateValidator parameters', { parameters: req.body })
  46. if (areValidationErrors(req, res)) return
  47. if (!await doesVideoExist(req.params.videoId, res)) return
  48. if (!await doesVideoAbuseExist(req.params.id, res.locals.video.id, res)) return
  49. return next()
  50. }
  51. ]
  52. // ---------------------------------------------------------------------------
  53. export {
  54. videoAbuseReportValidator,
  55. videoAbuseGetValidator,
  56. videoAbuseUpdateValidator
  57. }