1 import express from 'express'
2 import { param, query } from 'express-validator'
3 import { HttpStatusCode } from '@shared/models'
4 import { isValidRSSFeed } from '../../helpers/custom-validators/feeds'
5 import { exists, isIdOrUUIDValid, isIdValid, toCompleteUUID } from '../../helpers/custom-validators/misc'
6 import { buildPodcastGroupsCache } from '../cache'
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 if (areValidationErrors(req, res)) return
33 function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
34 const format = req.query.format || req.params.format || 'rss'
36 let acceptableContentTypes: string[]
37 if (format === 'atom' || format === 'atom1') {
38 acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ]
39 } else if (format === 'json' || format === 'json1') {
40 acceptableContentTypes = [ 'application/json' ]
41 } else if (format === 'rss' || format === 'rss2') {
42 acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
44 acceptableContentTypes = [ 'application/xml', 'text/xml' ]
47 return feedContentTypeResponse(req, res, next, acceptableContentTypes)
50 function setFeedPodcastContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
51 const acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
53 return feedContentTypeResponse(req, res, next, acceptableContentTypes)
56 function feedContentTypeResponse (
58 res: express.Response,
59 next: express.NextFunction,
60 acceptableContentTypes: string[]
62 if (req.accepts(acceptableContentTypes)) {
63 res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
66 status: HttpStatusCode.NOT_ACCEPTABLE_406,
67 message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
74 // ---------------------------------------------------------------------------
76 const videoFeedsValidator = [
84 query('videoChannelId')
88 query('videoChannelName')
91 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
92 if (areValidationErrors(req, res)) return
94 if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return
95 if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return
96 if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return
97 if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return
103 // ---------------------------------------------------------------------------
105 const videoFeedsPodcastValidator = [
106 query('videoChannelId')
109 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
110 if (areValidationErrors(req, res)) return
111 if (!await doesVideoChannelIdExist(req.query.videoChannelId, res)) return
117 const videoFeedsPodcastSetCacheKey = [
118 (req: express.Request, res: express.Response, next: express.NextFunction) => {
119 if (req.query.videoChannelId) {
120 res.locals.apicacheGroups = [ buildPodcastGroupsCache({ channelId: req.query.videoChannelId }) ]
126 // ---------------------------------------------------------------------------
128 const videoSubscriptionFeedsValidator = [
135 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
136 if (areValidationErrors(req, res)) return
138 if (!await doesAccountIdExist(req.query.accountId, res)) return
139 if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
145 const videoCommentsFeedsValidator = [
148 .customSanitizer(toCompleteUUID)
149 .custom(isIdOrUUIDValid),
151 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
152 if (areValidationErrors(req, res)) return
154 if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
155 return res.fail({ message: 'videoId cannot be mixed with a channel filter' })
158 if (req.query.videoId) {
159 if (!await doesVideoExist(req.query.videoId, res)) return
160 if (!await checkCanSeeVideo({ req, res, paramId: req.query.videoId, video: res.locals.videoAll })) return
167 // ---------------------------------------------------------------------------
170 feedsFormatValidator,
171 setFeedFormatContentType,
172 setFeedPodcastContentType,
174 videoFeedsPodcastValidator,
175 videoSubscriptionFeedsValidator,
176 videoFeedsPodcastSetCacheKey,
177 videoCommentsFeedsValidator