]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/feeds.ts
replace numbers with typed http status codes (#3409)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / feeds.ts
CommitLineData
244e76a5 1import * as express from 'express'
c8861d5d 2import { param, query } from 'express-validator'
244e76a5 3import { isValidRSSFeed } from '../../helpers/custom-validators/feeds'
18490b07
C
4import { exists, isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
5import { logger } from '../../helpers/logger'
3e753302
C
6import {
7 doesAccountIdExist,
8 doesAccountNameWithHostExist,
18490b07 9 doesUserFeedTokenCorrespond,
3e753302 10 doesVideoChannelIdExist,
18490b07 11 doesVideoChannelNameWithHostExist
3e753302 12} from '../../helpers/middlewares'
18490b07
C
13import { doesVideoExist } from '../../helpers/middlewares/videos'
14import { areValidationErrors } from './utils'
2d53be02 15import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
244e76a5 16
f2f0eda5 17const feedsFormatValidator = [
244e76a5 18 param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
f2f0eda5
RK
19 query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)')
20]
21
22function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
23 const format = req.query.format || req.params.format || 'rss'
24
25 let acceptableContentTypes: string[]
26 if (format === 'atom' || format === 'atom1') {
a1587156 27 acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ]
f2f0eda5 28 } else if (format === 'json' || format === 'json1') {
a1587156 29 acceptableContentTypes = [ 'application/json' ]
f2f0eda5 30 } else if (format === 'rss' || format === 'rss2') {
a1587156 31 acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
f2f0eda5 32 } else {
a1587156 33 acceptableContentTypes = [ 'application/xml', 'text/xml' ]
f2f0eda5
RK
34 }
35
36 if (req.accepts(acceptableContentTypes)) {
37 res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
38 } else {
2d53be02 39 return res.status(HttpStatusCode.NOT_ACCEPTABLE_406)
18490b07
C
40 .json({
41 message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
42 })
f2f0eda5
RK
43 }
44
45 return next()
46}
47
48const videoFeedsValidator = [
18490b07
C
49 query('accountId')
50 .optional()
51 .custom(isIdValid)
52 .withMessage('Should have a valid account id'),
53
54 query('accountName')
55 .optional(),
56
57 query('videoChannelId')
58 .optional()
59 .custom(isIdValid)
60 .withMessage('Should have a valid channel id'),
61
62 query('videoChannelName')
63 .optional(),
244e76a5
RK
64
65 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
66 logger.debug('Checking feeds parameters', { parameters: req.query })
67
68 if (areValidationErrors(req, res)) return
69
0f6acda1
C
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
244e76a5
RK
74
75 return next()
76 }
77]
78
18490b07
C
79const videoSubscriptionFeedsValidator = [
80 query('accountId')
81 .custom(isIdValid)
82 .withMessage('Should have a valid account id'),
83
84 query('token')
85 .custom(exists)
86 .withMessage('Should have a token'),
afff310e
RK
87
88 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
18490b07 89 logger.debug('Checking subscription feeds parameters', { parameters: req.query })
afff310e
RK
90
91 if (areValidationErrors(req, res)) return
92
18490b07
C
93 if (!await doesAccountIdExist(req.query.accountId, res)) return
94 if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
afff310e
RK
95
96 return next()
97 }
98]
99
fe3a55b0 100const videoCommentsFeedsValidator = [
fe3a55b0
C
101 query('videoId').optional().custom(isIdOrUUIDValid),
102
103 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
104 logger.debug('Checking feeds parameters', { parameters: req.query })
105
106 if (areValidationErrors(req, res)) return
107
00494d6e 108 if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
2d53be02 109 return res.status(HttpStatusCode.BAD_REQUEST_400)
18490b07
C
110 .json({
111 message: 'videoId cannot be mixed with a channel filter'
112 })
00494d6e
RK
113 }
114
0f6acda1 115 if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return
fe3a55b0
C
116
117 return next()
118 }
119]
120
244e76a5
RK
121// ---------------------------------------------------------------------------
122
123export {
f2f0eda5
RK
124 feedsFormatValidator,
125 setFeedFormatContentType,
fe3a55b0 126 videoFeedsValidator,
18490b07 127 videoSubscriptionFeedsValidator,
fe3a55b0 128 videoCommentsFeedsValidator
244e76a5 129}