1 import * as express from 'express'
2 import { param, query } from 'express-validator'
4 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
5 import { isValidRSSFeed } from '../../helpers/custom-validators/feeds'
6 import { exists, isIdOrUUIDValid, isIdValid, toCompleteUUID } from '../../helpers/custom-validators/misc'
7 import { logger } from '../../helpers/logger'
11 doesAccountNameWithHostExist,
12 doesUserFeedTokenCorrespond,
13 doesVideoChannelIdExist,
14 doesVideoChannelNameWithHostExist,
18 const feedsFormatValidator = [
19 param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
20 query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)')
23 function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
24 const format = req.query.format || req.params.format || 'rss'
26 let acceptableContentTypes: string[]
27 if (format === 'atom' || format === 'atom1') {
28 acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ]
29 } else if (format === 'json' || format === 'json1') {
30 acceptableContentTypes = [ 'application/json' ]
31 } else if (format === 'rss' || format === 'rss2') {
32 acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
34 acceptableContentTypes = [ 'application/xml', 'text/xml' ]
37 if (req.accepts(acceptableContentTypes)) {
38 res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
41 status: HttpStatusCode.NOT_ACCEPTABLE_406,
42 message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
49 const videoFeedsValidator = [
53 .withMessage('Should have a valid account id'),
58 query('videoChannelId')
61 .withMessage('Should have a valid channel id'),
63 query('videoChannelName')
66 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
67 logger.debug('Checking feeds parameters', { parameters: req.query })
69 if (areValidationErrors(req, res)) return
71 if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return
72 if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return
73 if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return
74 if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return
80 const videoSubscriptionFeedsValidator = [
83 .withMessage('Should have a valid account id'),
87 .withMessage('Should have a token'),
89 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
90 logger.debug('Checking subscription feeds parameters', { parameters: req.query })
92 if (areValidationErrors(req, res)) return
94 if (!await doesAccountIdExist(req.query.accountId, res)) return
95 if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
101 const videoCommentsFeedsValidator = [
103 .customSanitizer(toCompleteUUID)
105 .custom(isIdOrUUIDValid),
107 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
108 logger.debug('Checking feeds parameters', { parameters: req.query })
110 if (areValidationErrors(req, res)) return
112 if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
113 return res.fail({ message: 'videoId cannot be mixed with a channel filter' })
116 if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return
122 // ---------------------------------------------------------------------------
125 feedsFormatValidator,
126 setFeedFormatContentType,
128 videoSubscriptionFeedsValidator,
129 videoCommentsFeedsValidator