feeds.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import * as express from 'express'
  2. import { param, query } from 'express-validator/check'
  3. import { doesAccountIdExist, doesAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
  4. import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
  5. import { logger } from '../../helpers/logger'
  6. import { areValidationErrors } from './utils'
  7. import { isValidRSSFeed } from '../../helpers/custom-validators/feeds'
  8. import { doesVideoChannelIdExist, doesVideoChannelNameWithHostExist } from '../../helpers/custom-validators/video-channels'
  9. import { doesVideoExist } from '../../helpers/custom-validators/videos'
  10. const videoFeedsValidator = [
  11. param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
  12. query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
  13. query('accountId').optional().custom(isIdValid),
  14. query('accountName').optional(),
  15. query('videoChannelId').optional().custom(isIdValid),
  16. query('videoChannelName').optional(),
  17. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  18. logger.debug('Checking feeds parameters', { parameters: req.query })
  19. if (areValidationErrors(req, res)) return
  20. if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return
  21. if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return
  22. if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return
  23. if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return
  24. return next()
  25. }
  26. ]
  27. const videoCommentsFeedsValidator = [
  28. param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
  29. query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
  30. query('videoId').optional().custom(isIdOrUUIDValid),
  31. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  32. logger.debug('Checking feeds parameters', { parameters: req.query })
  33. if (areValidationErrors(req, res)) return
  34. if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return
  35. return next()
  36. }
  37. ]
  38. // ---------------------------------------------------------------------------
  39. export {
  40. videoFeedsValidator,
  41. videoCommentsFeedsValidator
  42. }