1 import * as express from 'express'
2 import { param, query } from 'express-validator'
3 import { isValidRSSFeed } from '../../helpers/custom-validators/feeds'
4 import { exists, isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
5 import { logger } from '../../helpers/logger'
8 doesAccountNameWithHostExist,
9 doesUserFeedTokenCorrespond,
10 doesVideoChannelIdExist,
11 doesVideoChannelNameWithHostExist
12 } from '../../helpers/middlewares'
13 import { doesVideoExist } from '../../helpers/middlewares/videos'
14 import { areValidationErrors } from './utils'
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)')
21 function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
22 const format = req.query.format || req.params.format || 'rss'
24 let acceptableContentTypes: string[]
25 if (format === 'atom' || format === 'atom1') {
26 acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ]
27 } else if (format === 'json' || format === 'json1') {
28 acceptableContentTypes = [ 'application/json' ]
29 } else if (format === 'rss' || format === 'rss2') {
30 acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
32 acceptableContentTypes = [ 'application/xml', 'text/xml' ]
35 if (req.accepts(acceptableContentTypes)) {
36 res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
38 return res.status(406)
40 message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
47 const videoFeedsValidator = [
51 .withMessage('Should have a valid account id'),
56 query('videoChannelId')
59 .withMessage('Should have a valid channel id'),
61 query('videoChannelName')
64 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
65 logger.debug('Checking feeds parameters', { parameters: req.query })
67 if (areValidationErrors(req, res)) return
69 if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return
70 if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return
71 if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return
72 if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return
78 const videoSubscriptionFeedsValidator = [
81 .withMessage('Should have a valid account id'),
85 .withMessage('Should have a token'),
87 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
88 logger.debug('Checking subscription feeds parameters', { parameters: req.query })
90 if (areValidationErrors(req, res)) return
92 if (!await doesAccountIdExist(req.query.accountId, res)) return
93 if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
99 const videoCommentsFeedsValidator = [
100 query('videoId').optional().custom(isIdOrUUIDValid),
102 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
103 logger.debug('Checking feeds parameters', { parameters: req.query })
105 if (areValidationErrors(req, res)) return
107 if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
108 return res.status(400)
110 message: 'videoId cannot be mixed with a channel filter'
114 if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return
120 // ---------------------------------------------------------------------------
123 feedsFormatValidator,
124 setFeedFormatContentType,
126 videoSubscriptionFeedsValidator,
127 videoCommentsFeedsValidator