]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/middlewares/validators/feeds.ts
Add plugins check params tests
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / feeds.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { param, query } from 'express-validator/check'
3import { doesAccountIdExist, doesAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
4import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
5import { logger } from '../../helpers/logger'
6import { areValidationErrors } from './utils'
7import { isValidRSSFeed } from '../../helpers/custom-validators/feeds'
8import { doesVideoChannelIdExist, doesVideoChannelNameWithHostExist } from '../../helpers/custom-validators/video-channels'
9import { doesVideoExist } from '../../helpers/custom-validators/videos'
10
11const videoFeedsValidator = [
12 param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
13 query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
14 query('accountId').optional().custom(isIdValid),
15 query('accountName').optional(),
16 query('videoChannelId').optional().custom(isIdValid),
17 query('videoChannelName').optional(),
18
19 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
20 logger.debug('Checking feeds parameters', { parameters: req.query })
21
22 if (areValidationErrors(req, res)) return
23
24 if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return
25 if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return
26 if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return
27 if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return
28
29 return next()
30 }
31]
32
33const videoCommentsFeedsValidator = [
34 param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
35 query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
36 query('videoId').optional().custom(isIdOrUUIDValid),
37
38 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
39 logger.debug('Checking feeds parameters', { parameters: req.query })
40
41 if (areValidationErrors(req, res)) return
42
43 if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return
44
45 return next()
46 }
47]
48
49// ---------------------------------------------------------------------------
50
51export {
52 videoFeedsValidator,
53 videoCommentsFeedsValidator
54}