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'
15 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
17 const feedsFormatValidator = [
18 param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
19 query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)')
22 function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
23 const format = req.query.format || req.params.format || 'rss'
25 let acceptableContentTypes: string[]
26 if (format === 'atom' || format === 'atom1') {
27 acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ]
28 } else if (format === 'json' || format === 'json1') {
29 acceptableContentTypes = [ 'application/json' ]
30 } else if (format === 'rss' || format === 'rss2') {
31 acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
33 acceptableContentTypes = [ 'application/xml', 'text/xml' ]
36 if (req.accepts(acceptableContentTypes)) {
37 res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
39 return res.status(HttpStatusCode.NOT_ACCEPTABLE_406)
41 message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
48 const videoFeedsValidator = [
52 .withMessage('Should have a valid account id'),
57 query('videoChannelId')
60 .withMessage('Should have a valid channel id'),
62 query('videoChannelName')
65 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
66 logger.debug('Checking feeds parameters', { parameters: req.query })
68 if (areValidationErrors(req, res)) return
70 if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return
71 if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return
72 if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return
73 if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return
79 const videoSubscriptionFeedsValidator = [
82 .withMessage('Should have a valid account id'),
86 .withMessage('Should have a token'),
88 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
89 logger.debug('Checking subscription feeds parameters', { parameters: req.query })
91 if (areValidationErrors(req, res)) return
93 if (!await doesAccountIdExist(req.query.accountId, res)) return
94 if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
100 const videoCommentsFeedsValidator = [
101 query('videoId').optional().custom(isIdOrUUIDValid),
103 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
104 logger.debug('Checking feeds parameters', { parameters: req.query })
106 if (areValidationErrors(req, res)) return
108 if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
109 return res.status(HttpStatusCode.BAD_REQUEST_400)
111 message: 'videoId cannot be mixed with a channel filter'
115 if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return
121 // ---------------------------------------------------------------------------
124 feedsFormatValidator,
125 setFeedFormatContentType,
127 videoSubscriptionFeedsValidator,
128 videoCommentsFeedsValidator