video-blacklist.ts 3.5 KB

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