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'
11 doesAccountNameWithHostExist,
12 doesUserFeedTokenCorrespond,
13 doesVideoChannelIdExist,
14 doesVideoChannelNameWithHostExist,
18 const feedsFormatValidator = [
21 .custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
24 .custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
26 (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking feeds format parameters', { parameters: req.query })
29 if (areValidationErrors(req, res)) return
35 function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
36 const format = req.query.format || req.params.format || 'rss'
38 let acceptableContentTypes: string[]
39 if (format === 'atom' || format === 'atom1') {
40 acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ]
41 } else if (format === 'json' || format === 'json1') {
42 acceptableContentTypes = [ 'application/json' ]
43 } else if (format === 'rss' || format === 'rss2') {
44 acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
46 acceptableContentTypes = [ 'application/xml', 'text/xml' ]
49 if (req.accepts(acceptableContentTypes)) {
50 res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
53 status: HttpStatusCode.NOT_ACCEPTABLE_406,
54 message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
61 const videoFeedsValidator = [
69 query('videoChannelId')
73 query('videoChannelName')
76 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
77 logger.debug('Checking feeds parameters', { parameters: req.query })
79 if (areValidationErrors(req, res)) return
81 if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return
82 if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return
83 if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return
84 if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return
90 const videoSubscriptionFeedsValidator = [
97 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
98 logger.debug('Checking subscription feeds parameters', { parameters: req.query })
100 if (areValidationErrors(req, res)) return
102 if (!await doesAccountIdExist(req.query.accountId, res)) return
103 if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
109 const videoCommentsFeedsValidator = [
112 .customSanitizer(toCompleteUUID)
113 .custom(isIdOrUUIDValid),
115 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
116 logger.debug('Checking feeds parameters', { parameters: req.query })
118 if (areValidationErrors(req, res)) return
120 if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
121 return res.fail({ message: 'videoId cannot be mixed with a channel filter' })
124 if (req.query.videoId) {
125 if (!await doesVideoExist(req.query.videoId, res)) return
126 if (!await checkCanSeeVideo({ req, res, paramId: req.query.videoId, video: res.locals.videoAll })) return
133 // ---------------------------------------------------------------------------
136 feedsFormatValidator,
137 setFeedFormatContentType,
139 videoSubscriptionFeedsValidator,
140 videoCommentsFeedsValidator