]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { param, query } from 'express-validator' | |
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 feedsFormatValidator = [ | |
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 | ] | |
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') { | |
25 | acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ] | |
26 | } else if (format === 'json' || format === 'json1') { | |
27 | acceptableContentTypes = [ 'application/json' ] | |
28 | } else if (format === 'rss' || format === 'rss2') { | |
29 | acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ] | |
30 | } else { | |
31 | acceptableContentTypes = [ 'application/xml', 'text/xml' ] | |
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 = [ | |
46 | query('accountId').optional().custom(isIdValid), | |
47 | query('accountName').optional(), | |
48 | query('videoChannelId').optional().custom(isIdValid), | |
49 | query('videoChannelName').optional(), | |
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 | ||
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 | |
60 | ||
61 | return next() | |
62 | } | |
63 | ] | |
64 | ||
65 | const videoCommentsFeedsValidator = [ | |
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 | ||
73 | if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) { | |
74 | return res.status(400).send({ | |
75 | message: 'videoId cannot be mixed with a channel filter' | |
76 | }).end() | |
77 | } | |
78 | ||
79 | if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return | |
80 | ||
81 | return next() | |
82 | } | |
83 | ] | |
84 | ||
85 | // --------------------------------------------------------------------------- | |
86 | ||
87 | export { | |
88 | feedsFormatValidator, | |
89 | setFeedFormatContentType, | |
90 | videoFeedsValidator, | |
91 | videoCommentsFeedsValidator | |
92 | } |