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'
10 doesAccountNameWithHostExist,
11 doesUserFeedTokenCorrespond,
12 doesVideoChannelIdExist,
13 doesVideoChannelNameWithHostExist,
17 const feedsFormatValidator = [
20 .custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
23 .custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
25 (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 if (areValidationErrors(req, res)) return
32 function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
33 const format = req.query.format || req.params.format || 'rss'
35 let acceptableContentTypes: string[]
36 if (format === 'atom' || format === 'atom1') {
37 acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ]
38 } else if (format === 'json' || format === 'json1') {
39 acceptableContentTypes = [ 'application/json' ]
40 } else if (format === 'rss' || format === 'rss2') {
41 acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
43 acceptableContentTypes = [ 'application/xml', 'text/xml' ]
46 if (req.accepts(acceptableContentTypes)) {
47 res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
50 status: HttpStatusCode.NOT_ACCEPTABLE_406,
51 message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
58 const videoFeedsValidator = [
66 query('videoChannelId')
70 query('videoChannelName')
73 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
74 if (areValidationErrors(req, res)) return
76 if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return
77 if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return
78 if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return
79 if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return
85 const videoSubscriptionFeedsValidator = [
92 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
93 if (areValidationErrors(req, res)) return
95 if (!await doesAccountIdExist(req.query.accountId, res)) return
96 if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
102 const videoCommentsFeedsValidator = [
105 .customSanitizer(toCompleteUUID)
106 .custom(isIdOrUUIDValid),
108 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
109 if (areValidationErrors(req, res)) return
111 if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
112 return res.fail({ message: 'videoId cannot be mixed with a channel filter' })
115 if (req.query.videoId) {
116 if (!await doesVideoExist(req.query.videoId, res)) return
117 if (!await checkCanSeeVideo({ req, res, paramId: req.query.videoId, video: res.locals.videoAll })) return
124 // ---------------------------------------------------------------------------
127 feedsFormatValidator,
128 setFeedFormatContentType,
130 videoSubscriptionFeedsValidator,
131 videoCommentsFeedsValidator