1 import * as express from 'express'
2 import { param, query } from 'express-validator'
3 import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
4 import { logger } from '../../helpers/logger'
5 import { areValidationErrors } from './utils'
6 import { isValidRSSFeed } from '../../helpers/custom-validators/feeds'
7 import { doesVideoExist } from '../../helpers/middlewares/videos'
10 doesAccountNameWithHostExist,
11 doesVideoChannelIdExist,
12 doesVideoChannelNameWithHostExist
13 } from '../../helpers/middlewares'
15 const feedsFormatValidator = [
16 param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
17 query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)')
20 function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
21 const format = req.query.format || req.params.format || 'rss'
23 let acceptableContentTypes: string[]
24 if (format === 'atom' || format === 'atom1') {
25 acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ]
26 } else if (format === 'json' || format === 'json1') {
27 acceptableContentTypes = [ 'application/json' ]
28 } else if (format === 'rss' || format === 'rss2') {
29 acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
31 acceptableContentTypes = [ 'application/xml', 'text/xml' ]
34 if (req.accepts(acceptableContentTypes)) {
35 res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
37 return res.status(406).send({
38 message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
45 const videoFeedsValidator = [
46 query('accountId').optional().custom(isIdValid),
47 query('accountName').optional(),
48 query('videoChannelId').optional().custom(isIdValid),
49 query('videoChannelName').optional(),
51 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
52 logger.debug('Checking feeds parameters', { parameters: req.query })
54 if (areValidationErrors(req, res)) return
56 if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return
57 if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return
58 if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return
59 if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return
65 const videoCommentsFeedsValidator = [
66 query('videoId').optional().custom(isIdOrUUIDValid),
68 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
69 logger.debug('Checking feeds parameters', { parameters: req.query })
71 if (areValidationErrors(req, res)) return
73 if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return
79 // ---------------------------------------------------------------------------
83 setFeedFormatContentType,
85 videoCommentsFeedsValidator