]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/feeds.ts
Reorganize imports
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / feeds.ts
1 import * as 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 { logger } from '../../helpers/logger'
7 import {
8 areValidationErrors,
9 doesAccountIdExist,
10 doesAccountNameWithHostExist,
11 doesUserFeedTokenCorrespond,
12 doesVideoChannelIdExist,
13 doesVideoChannelNameWithHostExist,
14 doesVideoExist
15 } from './shared'
16
17 const feedsFormatValidator = [
18 param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
19 query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)')
20 ]
21
22 function 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') {
27 acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ]
28 } else if (format === 'json' || format === 'json1') {
29 acceptableContentTypes = [ 'application/json' ]
30 } else if (format === 'rss' || format === 'rss2') {
31 acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
32 } else {
33 acceptableContentTypes = [ 'application/xml', 'text/xml' ]
34 }
35
36 if (req.accepts(acceptableContentTypes)) {
37 res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
38 } else {
39 return res.fail({
40 status: HttpStatusCode.NOT_ACCEPTABLE_406,
41 message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
42 })
43 }
44
45 return next()
46 }
47
48 const videoFeedsValidator = [
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(),
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
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
74
75 return next()
76 }
77 ]
78
79 const 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'),
87
88 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
89 logger.debug('Checking subscription feeds parameters', { parameters: req.query })
90
91 if (areValidationErrors(req, res)) return
92
93 if (!await doesAccountIdExist(req.query.accountId, res)) return
94 if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
95
96 return next()
97 }
98 ]
99
100 const videoCommentsFeedsValidator = [
101 query('videoId')
102 .customSanitizer(toCompleteUUID)
103 .optional()
104 .custom(isIdOrUUIDValid),
105
106 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
107 logger.debug('Checking feeds parameters', { parameters: req.query })
108
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 && !await doesVideoExist(req.query.videoId, res)) return
116
117 return next()
118 }
119 ]
120
121 // ---------------------------------------------------------------------------
122
123 export {
124 feedsFormatValidator,
125 setFeedFormatContentType,
126 videoFeedsValidator,
127 videoSubscriptionFeedsValidator,
128 videoCommentsFeedsValidator
129 }