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