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