feeds.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import express from 'express'
  2. import { param, query } from 'express-validator'
  3. import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
  4. import { isValidRSSFeed } from '../../helpers/custom-validators/feeds'
  5. import { exists, isIdOrUUIDValid, isIdValid, toCompleteUUID } from '../../helpers/custom-validators/misc'
  6. import { logger } from '../../helpers/logger'
  7. import {
  8. areValidationErrors,
  9. doesAccountIdExist,
  10. doesAccountNameWithHostExist,
  11. doesUserFeedTokenCorrespond,
  12. doesVideoChannelIdExist,
  13. doesVideoChannelNameWithHostExist,
  14. doesVideoExist
  15. } from './shared'
  16. const feedsFormatValidator = [
  17. param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
  18. query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)')
  19. ]
  20. function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
  21. const format = req.query.format || req.params.format || 'rss'
  22. let acceptableContentTypes: string[]
  23. if (format === 'atom' || format === 'atom1') {
  24. acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ]
  25. } else if (format === 'json' || format === 'json1') {
  26. acceptableContentTypes = [ 'application/json' ]
  27. } else if (format === 'rss' || format === 'rss2') {
  28. acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
  29. } else {
  30. acceptableContentTypes = [ 'application/xml', 'text/xml' ]
  31. }
  32. if (req.accepts(acceptableContentTypes)) {
  33. res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
  34. } else {
  35. return res.fail({
  36. status: HttpStatusCode.NOT_ACCEPTABLE_406,
  37. message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
  38. })
  39. }
  40. return next()
  41. }
  42. const videoFeedsValidator = [
  43. query('accountId')
  44. .optional()
  45. .custom(isIdValid)
  46. .withMessage('Should have a valid account id'),
  47. query('accountName')
  48. .optional(),
  49. query('videoChannelId')
  50. .optional()
  51. .custom(isIdValid)
  52. .withMessage('Should have a valid channel id'),
  53. query('videoChannelName')
  54. .optional(),
  55. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  56. logger.debug('Checking feeds parameters', { parameters: req.query })
  57. if (areValidationErrors(req, res)) return
  58. if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return
  59. if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return
  60. if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return
  61. if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return
  62. return next()
  63. }
  64. ]
  65. const videoSubscriptionFeedsValidator = [
  66. query('accountId')
  67. .custom(isIdValid)
  68. .withMessage('Should have a valid account id'),
  69. query('token')
  70. .custom(exists)
  71. .withMessage('Should have a token'),
  72. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  73. logger.debug('Checking subscription feeds parameters', { parameters: req.query })
  74. if (areValidationErrors(req, res)) return
  75. if (!await doesAccountIdExist(req.query.accountId, res)) return
  76. if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
  77. return next()
  78. }
  79. ]
  80. const videoCommentsFeedsValidator = [
  81. query('videoId')
  82. .customSanitizer(toCompleteUUID)
  83. .optional()
  84. .custom(isIdOrUUIDValid),
  85. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  86. logger.debug('Checking feeds parameters', { parameters: req.query })
  87. if (areValidationErrors(req, res)) return
  88. if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
  89. return res.fail({ message: 'videoId cannot be mixed with a channel filter' })
  90. }
  91. if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return
  92. return next()
  93. }
  94. ]
  95. // ---------------------------------------------------------------------------
  96. export {
  97. feedsFormatValidator,
  98. setFeedFormatContentType,
  99. videoFeedsValidator,
  100. videoSubscriptionFeedsValidator,
  101. videoCommentsFeedsValidator
  102. }