]>
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, | |
afff310e RK |
12 | doesVideoChannelNameWithHostExist, |
13 | doesUserFeedTokenCorrespond | |
3e753302 | 14 | } from '../../helpers/middlewares' |
244e76a5 | 15 | |
f2f0eda5 | 16 | const feedsFormatValidator = [ |
244e76a5 | 17 | param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), |
f2f0eda5 RK |
18 | query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)') |
19 | ] | |
20 | ||
21 | function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) { | |
22 | const format = req.query.format || req.params.format || 'rss' | |
23 | ||
24 | let acceptableContentTypes: string[] | |
25 | if (format === 'atom' || format === 'atom1') { | |
a1587156 | 26 | acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ] |
f2f0eda5 | 27 | } else if (format === 'json' || format === 'json1') { |
a1587156 | 28 | acceptableContentTypes = [ 'application/json' ] |
f2f0eda5 | 29 | } else if (format === 'rss' || format === 'rss2') { |
a1587156 | 30 | acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ] |
f2f0eda5 | 31 | } else { |
a1587156 | 32 | acceptableContentTypes = [ 'application/xml', 'text/xml' ] |
f2f0eda5 RK |
33 | } |
34 | ||
35 | if (req.accepts(acceptableContentTypes)) { | |
36 | res.set('Content-Type', req.accepts(acceptableContentTypes) as string) | |
37 | } else { | |
38 | return res.status(406).send({ | |
39 | message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}` | |
40 | }).end() | |
41 | } | |
42 | ||
43 | return next() | |
44 | } | |
45 | ||
46 | const videoFeedsValidator = [ | |
57cfff78 C |
47 | query('accountId').optional().custom(isIdValid), |
48 | query('accountName').optional(), | |
49 | query('videoChannelId').optional().custom(isIdValid), | |
50 | query('videoChannelName').optional(), | |
244e76a5 RK |
51 | |
52 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
53 | logger.debug('Checking feeds parameters', { parameters: req.query }) | |
54 | ||
55 | if (areValidationErrors(req, res)) return | |
56 | ||
0f6acda1 C |
57 | if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return |
58 | if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return | |
59 | if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return | |
60 | if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return | |
244e76a5 RK |
61 | |
62 | return next() | |
63 | } | |
64 | ] | |
65 | ||
afff310e RK |
66 | const videoSubscriptonFeedsValidator = [ |
67 | query('accountId').optional().custom(isIdValid), | |
68 | query('token').optional(), | |
69 | ||
70 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
71 | logger.debug('Checking feeds parameters', { parameters: req.query }) | |
72 | ||
73 | if (areValidationErrors(req, res)) return | |
74 | ||
75 | // a token alone is erroneous | |
76 | if (req.query.token && !req.query.accountId) return | |
77 | if (req.query.token && !await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return | |
78 | ||
79 | return next() | |
80 | } | |
81 | ] | |
82 | ||
fe3a55b0 | 83 | const videoCommentsFeedsValidator = [ |
fe3a55b0 C |
84 | query('videoId').optional().custom(isIdOrUUIDValid), |
85 | ||
86 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
87 | logger.debug('Checking feeds parameters', { parameters: req.query }) | |
88 | ||
89 | if (areValidationErrors(req, res)) return | |
90 | ||
00494d6e RK |
91 | if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) { |
92 | return res.status(400).send({ | |
93 | message: 'videoId cannot be mixed with a channel filter' | |
94 | }).end() | |
95 | } | |
96 | ||
0f6acda1 | 97 | if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return |
fe3a55b0 C |
98 | |
99 | return next() | |
100 | } | |
101 | ] | |
102 | ||
244e76a5 RK |
103 | // --------------------------------------------------------------------------- |
104 | ||
105 | export { | |
f2f0eda5 RK |
106 | feedsFormatValidator, |
107 | setFeedFormatContentType, | |
fe3a55b0 | 108 | videoFeedsValidator, |
afff310e | 109 | videoSubscriptonFeedsValidator, |
fe3a55b0 | 110 | videoCommentsFeedsValidator |
244e76a5 | 111 | } |