]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/feeds.ts
Use largest avatar in RSS feeds, unique guid for liveItems (#5817)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / feeds.ts
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'
7 import {
8 areValidationErrors,
9 checkCanSeeVideo,
10 doesAccountIdExist,
11 doesAccountNameWithHostExist,
12 doesUserFeedTokenCorrespond,
13 doesVideoChannelIdExist,
14 doesVideoChannelNameWithHostExist,
15 doesVideoExist
16 } from './shared'
17
18 const feedsFormatValidator = [
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 if (areValidationErrors(req, res)) return
28
29 return next()
30 }
31 ]
32
33 function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
34 const format = req.query.format || req.params.format || 'rss'
35
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' ]
43 } else {
44 acceptableContentTypes = [ 'application/xml', 'text/xml' ]
45 }
46
47 return feedContentTypeResponse(req, res, next, acceptableContentTypes)
48 }
49
50 function setFeedPodcastContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
51 const acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
52
53 return feedContentTypeResponse(req, res, next, acceptableContentTypes)
54 }
55
56 function feedContentTypeResponse (
57 req: express.Request,
58 res: express.Response,
59 next: express.NextFunction,
60 acceptableContentTypes: string[]
61 ) {
62 if (req.accepts(acceptableContentTypes)) {
63 res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
64 } else {
65 return res.fail({
66 status: HttpStatusCode.NOT_ACCEPTABLE_406,
67 message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
68 })
69 }
70
71 return next()
72 }
73
74 // ---------------------------------------------------------------------------
75
76 const videoFeedsValidator = [
77 query('accountId')
78 .optional()
79 .custom(isIdValid),
80
81 query('accountName')
82 .optional(),
83
84 query('videoChannelId')
85 .optional()
86 .custom(isIdValid),
87
88 query('videoChannelName')
89 .optional(),
90
91 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
92 if (areValidationErrors(req, res)) return
93
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
98
99 return next()
100 }
101 ]
102
103 // ---------------------------------------------------------------------------
104
105 const videoFeedsPodcastValidator = [
106 query('videoChannelId')
107 .custom(isIdValid),
108
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
112
113 return next()
114 }
115 ]
116
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 }) ]
121 }
122
123 return next()
124 }
125 ]
126 // ---------------------------------------------------------------------------
127
128 const videoSubscriptionFeedsValidator = [
129 query('accountId')
130 .custom(isIdValid),
131
132 query('token')
133 .custom(exists),
134
135 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
136 if (areValidationErrors(req, res)) return
137
138 if (!await doesAccountIdExist(req.query.accountId, res)) return
139 if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
140
141 return next()
142 }
143 ]
144
145 const videoCommentsFeedsValidator = [
146 query('videoId')
147 .optional()
148 .customSanitizer(toCompleteUUID)
149 .custom(isIdOrUUIDValid),
150
151 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
152 if (areValidationErrors(req, res)) return
153
154 if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
155 return res.fail({ message: 'videoId cannot be mixed with a channel filter' })
156 }
157
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
161 }
162
163 return next()
164 }
165 ]
166
167 // ---------------------------------------------------------------------------
168
169 export {
170 feedsFormatValidator,
171 setFeedFormatContentType,
172 setFeedPodcastContentType,
173 videoFeedsValidator,
174 videoFeedsPodcastValidator,
175 videoSubscriptionFeedsValidator,
176 videoFeedsPodcastSetCacheKey,
177 videoCommentsFeedsValidator
178 }