]>
Commit | Line | Data |
---|---|---|
244e76a5 | 1 | import * as express from 'express' |
c8861d5d | 2 | import { param, query } from 'express-validator' |
244e76a5 | 3 | import { isValidRSSFeed } from '../../helpers/custom-validators/feeds' |
18490b07 C |
4 | import { exists, isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' |
5 | import { logger } from '../../helpers/logger' | |
3e753302 C |
6 | import { |
7 | doesAccountIdExist, | |
8 | doesAccountNameWithHostExist, | |
18490b07 | 9 | doesUserFeedTokenCorrespond, |
3e753302 | 10 | doesVideoChannelIdExist, |
18490b07 | 11 | doesVideoChannelNameWithHostExist |
3e753302 | 12 | } from '../../helpers/middlewares' |
18490b07 C |
13 | import { doesVideoExist } from '../../helpers/middlewares/videos' |
14 | import { areValidationErrors } from './utils' | |
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 { | |
18490b07 C |
38 | return res.status(406) |
39 | .json({ | |
40 | message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}` | |
41 | }) | |
f2f0eda5 RK |
42 | } |
43 | ||
44 | return next() | |
45 | } | |
46 | ||
47 | const videoFeedsValidator = [ | |
18490b07 C |
48 | query('accountId') |
49 | .optional() | |
50 | .custom(isIdValid) | |
51 | .withMessage('Should have a valid account id'), | |
52 | ||
53 | query('accountName') | |
54 | .optional(), | |
55 | ||
56 | query('videoChannelId') | |
57 | .optional() | |
58 | .custom(isIdValid) | |
59 | .withMessage('Should have a valid channel id'), | |
60 | ||
61 | query('videoChannelName') | |
62 | .optional(), | |
244e76a5 RK |
63 | |
64 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
65 | logger.debug('Checking feeds parameters', { parameters: req.query }) | |
66 | ||
67 | if (areValidationErrors(req, res)) return | |
68 | ||
0f6acda1 C |
69 | if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return |
70 | if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return | |
71 | if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return | |
72 | if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return | |
244e76a5 RK |
73 | |
74 | return next() | |
75 | } | |
76 | ] | |
77 | ||
18490b07 C |
78 | const videoSubscriptionFeedsValidator = [ |
79 | query('accountId') | |
80 | .custom(isIdValid) | |
81 | .withMessage('Should have a valid account id'), | |
82 | ||
83 | query('token') | |
84 | .custom(exists) | |
85 | .withMessage('Should have a token'), | |
afff310e RK |
86 | |
87 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
18490b07 | 88 | logger.debug('Checking subscription feeds parameters', { parameters: req.query }) |
afff310e RK |
89 | |
90 | if (areValidationErrors(req, res)) return | |
91 | ||
18490b07 C |
92 | if (!await doesAccountIdExist(req.query.accountId, res)) return |
93 | if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return | |
afff310e RK |
94 | |
95 | return next() | |
96 | } | |
97 | ] | |
98 | ||
fe3a55b0 | 99 | const videoCommentsFeedsValidator = [ |
fe3a55b0 C |
100 | query('videoId').optional().custom(isIdOrUUIDValid), |
101 | ||
102 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
103 | logger.debug('Checking feeds parameters', { parameters: req.query }) | |
104 | ||
105 | if (areValidationErrors(req, res)) return | |
106 | ||
00494d6e | 107 | if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) { |
18490b07 C |
108 | return res.status(400) |
109 | .json({ | |
110 | message: 'videoId cannot be mixed with a channel filter' | |
111 | }) | |
00494d6e RK |
112 | } |
113 | ||
0f6acda1 | 114 | if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return |
fe3a55b0 C |
115 | |
116 | return next() | |
117 | } | |
118 | ] | |
119 | ||
244e76a5 RK |
120 | // --------------------------------------------------------------------------- |
121 | ||
122 | export { | |
f2f0eda5 RK |
123 | feedsFormatValidator, |
124 | setFeedFormatContentType, | |
fe3a55b0 | 125 | videoFeedsValidator, |
18490b07 | 126 | videoSubscriptionFeedsValidator, |
fe3a55b0 | 127 | videoCommentsFeedsValidator |
244e76a5 | 128 | } |