video-blacklist.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import express from 'express'
  2. import { body, query } from 'express-validator'
  3. import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
  4. import { isBooleanValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
  5. import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../../helpers/custom-validators/video-blacklist'
  6. import { logger } from '../../../helpers/logger'
  7. import { areValidationErrors, doesVideoBlacklistExist, doesVideoExist, isValidVideoIdParam } from '../shared'
  8. const videosBlacklistRemoveValidator = [
  9. isValidVideoIdParam('videoId'),
  10. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  11. logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })
  12. if (areValidationErrors(req, res)) return
  13. if (!await doesVideoExist(req.params.videoId, res)) return
  14. if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
  15. return next()
  16. }
  17. ]
  18. const videosBlacklistAddValidator = [
  19. isValidVideoIdParam('videoId'),
  20. body('unfederate')
  21. .optional()
  22. .customSanitizer(toBooleanOrNull)
  23. .custom(isBooleanValid).withMessage('Should have a valid unfederate boolean'),
  24. body('reason')
  25. .optional()
  26. .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
  27. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  28. logger.debug('Checking videosBlacklistAdd parameters', { parameters: req.params })
  29. if (areValidationErrors(req, res)) return
  30. if (!await doesVideoExist(req.params.videoId, res)) return
  31. const video = res.locals.videoAll
  32. if (req.body.unfederate === true && video.remote === true) {
  33. return res.fail({
  34. status: HttpStatusCode.CONFLICT_409,
  35. message: 'You cannot unfederate a remote video.'
  36. })
  37. }
  38. return next()
  39. }
  40. ]
  41. const videosBlacklistUpdateValidator = [
  42. isValidVideoIdParam('videoId'),
  43. body('reason')
  44. .optional()
  45. .custom(isVideoBlacklistReasonValid).withMessage('Should have a valid reason'),
  46. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  47. logger.debug('Checking videosBlacklistUpdate parameters', { parameters: req.params })
  48. if (areValidationErrors(req, res)) return
  49. if (!await doesVideoExist(req.params.videoId, res)) return
  50. if (!await doesVideoBlacklistExist(res.locals.videoAll.id, res)) return
  51. return next()
  52. }
  53. ]
  54. const videosBlacklistFiltersValidator = [
  55. query('type')
  56. .optional()
  57. .customSanitizer(toIntOrNull)
  58. .custom(isVideoBlacklistTypeValid).withMessage('Should have a valid video blacklist type attribute'),
  59. query('search')
  60. .optional()
  61. .not()
  62. .isEmpty().withMessage('Should have a valid search'),
  63. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  64. logger.debug('Checking videos blacklist filters query', { parameters: req.query })
  65. if (areValidationErrors(req, res)) return
  66. return next()
  67. }
  68. ]
  69. // ---------------------------------------------------------------------------
  70. export {
  71. videosBlacklistAddValidator,
  72. videosBlacklistRemoveValidator,
  73. videosBlacklistUpdateValidator,
  74. videosBlacklistFiltersValidator
  75. }