]>
Commit | Line | Data |
---|---|---|
244e76a5 | 1 | import * as express from 'express' |
c8861d5d | 2 | import { param, query } from 'express-validator' |
57cfff78 | 3 | import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' |
244e76a5 RK |
4 | import { logger } from '../../helpers/logger' |
5 | import { areValidationErrors } from './utils' | |
6 | import { isValidRSSFeed } from '../../helpers/custom-validators/feeds' | |
3e753302 C |
7 | import { doesVideoExist } from '../../helpers/middlewares/videos' |
8 | import { | |
9 | doesAccountIdExist, | |
10 | doesAccountNameWithHostExist, | |
11 | doesVideoChannelIdExist, | |
12 | doesVideoChannelNameWithHostExist | |
13 | } from '../../helpers/middlewares' | |
244e76a5 | 14 | |
f2f0eda5 | 15 | const feedsFormatValidator = [ |
244e76a5 | 16 | param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), |
f2f0eda5 RK |
17 | query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)') |
18 | ] | |
19 | ||
20 | function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) { | |
21 | const format = req.query.format || req.params.format || 'rss' | |
22 | ||
23 | let acceptableContentTypes: string[] | |
24 | if (format === 'atom' || format === 'atom1') { | |
a1587156 | 25 | acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ] |
f2f0eda5 | 26 | } else if (format === 'json' || format === 'json1') { |
a1587156 | 27 | acceptableContentTypes = [ 'application/json' ] |
f2f0eda5 | 28 | } else if (format === 'rss' || format === 'rss2') { |
a1587156 | 29 | acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ] |
f2f0eda5 | 30 | } else { |
a1587156 | 31 | acceptableContentTypes = [ 'application/xml', 'text/xml' ] |
f2f0eda5 RK |
32 | } |
33 | ||
34 | if (req.accepts(acceptableContentTypes)) { | |
35 | res.set('Content-Type', req.accepts(acceptableContentTypes) as string) | |
36 | } else { | |
37 | return res.status(406).send({ | |
38 | message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}` | |
39 | }).end() | |
40 | } | |
41 | ||
42 | return next() | |
43 | } | |
44 | ||
45 | const videoFeedsValidator = [ | |
57cfff78 C |
46 | query('accountId').optional().custom(isIdValid), |
47 | query('accountName').optional(), | |
48 | query('videoChannelId').optional().custom(isIdValid), | |
49 | query('videoChannelName').optional(), | |
244e76a5 RK |
50 | |
51 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
52 | logger.debug('Checking feeds parameters', { parameters: req.query }) | |
53 | ||
54 | if (areValidationErrors(req, res)) return | |
55 | ||
0f6acda1 C |
56 | if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return |
57 | if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return | |
58 | if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return | |
59 | if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return | |
244e76a5 RK |
60 | |
61 | return next() | |
62 | } | |
63 | ] | |
64 | ||
fe3a55b0 | 65 | const videoCommentsFeedsValidator = [ |
fe3a55b0 C |
66 | query('videoId').optional().custom(isIdOrUUIDValid), |
67 | ||
68 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
69 | logger.debug('Checking feeds parameters', { parameters: req.query }) | |
70 | ||
71 | if (areValidationErrors(req, res)) return | |
72 | ||
0f6acda1 | 73 | if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return |
fe3a55b0 C |
74 | |
75 | return next() | |
76 | } | |
77 | ] | |
78 | ||
244e76a5 RK |
79 | // --------------------------------------------------------------------------- |
80 | ||
81 | export { | |
f2f0eda5 RK |
82 | feedsFormatValidator, |
83 | setFeedFormatContentType, | |
fe3a55b0 C |
84 | videoFeedsValidator, |
85 | videoCommentsFeedsValidator | |
244e76a5 | 86 | } |