]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/feeds.ts
Support studio transcoding in peertube runner
[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/http/http-error-codes'
4 import { isValidRSSFeed } from '../../helpers/custom-validators/feeds'
5 import { exists, isIdOrUUIDValid, isIdValid, toCompleteUUID } from '../../helpers/custom-validators/misc'
6 import {
7 areValidationErrors,
8 checkCanSeeVideo,
9 doesAccountIdExist,
10 doesAccountNameWithHostExist,
11 doesUserFeedTokenCorrespond,
12 doesVideoChannelIdExist,
13 doesVideoChannelNameWithHostExist,
14 doesVideoExist
15 } from './shared'
16
17 const feedsFormatValidator = [
18 param('format')
19 .optional()
20 .custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
21 query('format')
22 .optional()
23 .custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
24
25 (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 if (areValidationErrors(req, res)) return
27
28 return next()
29 }
30 ]
31
32 function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) {
33 const format = req.query.format || req.params.format || 'rss'
34
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' ]
42 } else {
43 acceptableContentTypes = [ 'application/xml', 'text/xml' ]
44 }
45
46 if (req.accepts(acceptableContentTypes)) {
47 res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
48 } else {
49 return res.fail({
50 status: HttpStatusCode.NOT_ACCEPTABLE_406,
51 message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
52 })
53 }
54
55 return next()
56 }
57
58 const videoFeedsValidator = [
59 query('accountId')
60 .optional()
61 .custom(isIdValid),
62
63 query('accountName')
64 .optional(),
65
66 query('videoChannelId')
67 .optional()
68 .custom(isIdValid),
69
70 query('videoChannelName')
71 .optional(),
72
73 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
74 if (areValidationErrors(req, res)) return
75
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
80
81 return next()
82 }
83 ]
84
85 const videoSubscriptionFeedsValidator = [
86 query('accountId')
87 .custom(isIdValid),
88
89 query('token')
90 .custom(exists),
91
92 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
93 if (areValidationErrors(req, res)) return
94
95 if (!await doesAccountIdExist(req.query.accountId, res)) return
96 if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
97
98 return next()
99 }
100 ]
101
102 const videoCommentsFeedsValidator = [
103 query('videoId')
104 .optional()
105 .customSanitizer(toCompleteUUID)
106 .custom(isIdOrUUIDValid),
107
108 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
109 if (areValidationErrors(req, res)) return
110
111 if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
112 return res.fail({ message: 'videoId cannot be mixed with a channel filter' })
113 }
114
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
118 }
119
120 return next()
121 }
122 ]
123
124 // ---------------------------------------------------------------------------
125
126 export {
127 feedsFormatValidator,
128 setFeedFormatContentType,
129 videoFeedsValidator,
130 videoSubscriptionFeedsValidator,
131 videoCommentsFeedsValidator
132 }