]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/feeds.ts
Cleanup useless express validator messages
[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 { logger } from '../../helpers/logger'
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 logger.debug('Checking feeds format parameters', { parameters: req.query })
28
29 if (areValidationErrors(req, res)) return
30
31 return next()
32 }
33 ]
34
35 function 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') {
40 acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ]
41 } else if (format === 'json' || format === 'json1') {
42 acceptableContentTypes = [ 'application/json' ]
43 } else if (format === 'rss' || format === 'rss2') {
44 acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ]
45 } else {
46 acceptableContentTypes = [ 'application/xml', 'text/xml' ]
47 }
48
49 if (req.accepts(acceptableContentTypes)) {
50 res.set('Content-Type', req.accepts(acceptableContentTypes) as string)
51 } else {
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 })
56 }
57
58 return next()
59 }
60
61 const videoFeedsValidator = [
62 query('accountId')
63 .optional()
64 .custom(isIdValid),
65
66 query('accountName')
67 .optional(),
68
69 query('videoChannelId')
70 .optional()
71 .custom(isIdValid),
72
73 query('videoChannelName')
74 .optional(),
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
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
85
86 return next()
87 }
88 ]
89
90 const videoSubscriptionFeedsValidator = [
91 query('accountId')
92 .custom(isIdValid),
93
94 query('token')
95 .custom(exists),
96
97 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
98 logger.debug('Checking subscription feeds parameters', { parameters: req.query })
99
100 if (areValidationErrors(req, res)) return
101
102 if (!await doesAccountIdExist(req.query.accountId, res)) return
103 if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
104
105 return next()
106 }
107 ]
108
109 const videoCommentsFeedsValidator = [
110 query('videoId')
111 .optional()
112 .customSanitizer(toCompleteUUID)
113 .custom(isIdOrUUIDValid),
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
120 if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
121 return res.fail({ message: 'videoId cannot be mixed with a channel filter' })
122 }
123
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 }
128
129 return next()
130 }
131 ]
132
133 // ---------------------------------------------------------------------------
134
135 export {
136 feedsFormatValidator,
137 setFeedFormatContentType,
138 videoFeedsValidator,
139 videoSubscriptionFeedsValidator,
140 videoCommentsFeedsValidator
141 }