]>
Commit | Line | Data |
---|---|---|
244e76a5 | 1 | import * as express from 'express' |
c8861d5d | 2 | import { param, query } from 'express-validator' |
d4a8e7a6 | 3 | |
10363c74 | 4 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
244e76a5 | 5 | import { isValidRSSFeed } from '../../helpers/custom-validators/feeds' |
d4a8e7a6 | 6 | import { exists, isIdOrUUIDValid, isIdValid, toCompleteUUID } from '../../helpers/custom-validators/misc' |
18490b07 | 7 | import { logger } from '../../helpers/logger' |
3e753302 | 8 | import { |
10363c74 | 9 | areValidationErrors, |
3e753302 C |
10 | doesAccountIdExist, |
11 | doesAccountNameWithHostExist, | |
18490b07 | 12 | doesUserFeedTokenCorrespond, |
3e753302 | 13 | doesVideoChannelIdExist, |
10363c74 C |
14 | doesVideoChannelNameWithHostExist, |
15 | doesVideoExist | |
16 | } from './shared' | |
244e76a5 | 17 | |
f2f0eda5 | 18 | const feedsFormatValidator = [ |
244e76a5 | 19 | param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), |
f2f0eda5 RK |
20 | query('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)') |
21 | ] | |
22 | ||
23 | function setFeedFormatContentType (req: express.Request, res: express.Response, next: express.NextFunction) { | |
24 | const format = req.query.format || req.params.format || 'rss' | |
25 | ||
26 | let acceptableContentTypes: string[] | |
27 | if (format === 'atom' || format === 'atom1') { | |
a1587156 | 28 | acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ] |
f2f0eda5 | 29 | } else if (format === 'json' || format === 'json1') { |
a1587156 | 30 | acceptableContentTypes = [ 'application/json' ] |
f2f0eda5 | 31 | } else if (format === 'rss' || format === 'rss2') { |
a1587156 | 32 | acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ] |
f2f0eda5 | 33 | } else { |
a1587156 | 34 | acceptableContentTypes = [ 'application/xml', 'text/xml' ] |
f2f0eda5 RK |
35 | } |
36 | ||
37 | if (req.accepts(acceptableContentTypes)) { | |
38 | res.set('Content-Type', req.accepts(acceptableContentTypes) as string) | |
39 | } else { | |
76148b27 RK |
40 | return res.fail({ |
41 | status: HttpStatusCode.NOT_ACCEPTABLE_406, | |
42 | message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}` | |
43 | }) | |
f2f0eda5 RK |
44 | } |
45 | ||
46 | return next() | |
47 | } | |
48 | ||
49 | const videoFeedsValidator = [ | |
18490b07 C |
50 | query('accountId') |
51 | .optional() | |
52 | .custom(isIdValid) | |
53 | .withMessage('Should have a valid account id'), | |
54 | ||
55 | query('accountName') | |
56 | .optional(), | |
57 | ||
58 | query('videoChannelId') | |
59 | .optional() | |
60 | .custom(isIdValid) | |
61 | .withMessage('Should have a valid channel id'), | |
62 | ||
63 | query('videoChannelName') | |
64 | .optional(), | |
244e76a5 RK |
65 | |
66 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
67 | logger.debug('Checking feeds parameters', { parameters: req.query }) | |
68 | ||
69 | if (areValidationErrors(req, res)) return | |
70 | ||
0f6acda1 C |
71 | if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return |
72 | if (req.query.videoChannelId && !await doesVideoChannelIdExist(req.query.videoChannelId, res)) return | |
73 | if (req.query.accountName && !await doesAccountNameWithHostExist(req.query.accountName, res)) return | |
74 | if (req.query.videoChannelName && !await doesVideoChannelNameWithHostExist(req.query.videoChannelName, res)) return | |
244e76a5 RK |
75 | |
76 | return next() | |
77 | } | |
78 | ] | |
79 | ||
18490b07 C |
80 | const videoSubscriptionFeedsValidator = [ |
81 | query('accountId') | |
82 | .custom(isIdValid) | |
83 | .withMessage('Should have a valid account id'), | |
84 | ||
85 | query('token') | |
86 | .custom(exists) | |
87 | .withMessage('Should have a token'), | |
afff310e RK |
88 | |
89 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
18490b07 | 90 | logger.debug('Checking subscription feeds parameters', { parameters: req.query }) |
afff310e RK |
91 | |
92 | if (areValidationErrors(req, res)) return | |
93 | ||
18490b07 C |
94 | if (!await doesAccountIdExist(req.query.accountId, res)) return |
95 | if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return | |
afff310e RK |
96 | |
97 | return next() | |
98 | } | |
99 | ] | |
100 | ||
fe3a55b0 | 101 | const videoCommentsFeedsValidator = [ |
d4a8e7a6 C |
102 | query('videoId') |
103 | .customSanitizer(toCompleteUUID) | |
104 | .optional() | |
105 | .custom(isIdOrUUIDValid), | |
fe3a55b0 C |
106 | |
107 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
108 | logger.debug('Checking feeds parameters', { parameters: req.query }) | |
109 | ||
110 | if (areValidationErrors(req, res)) return | |
111 | ||
00494d6e | 112 | if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) { |
76148b27 | 113 | return res.fail({ message: 'videoId cannot be mixed with a channel filter' }) |
00494d6e RK |
114 | } |
115 | ||
0f6acda1 | 116 | if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return |
fe3a55b0 C |
117 | |
118 | return next() | |
119 | } | |
120 | ] | |
121 | ||
244e76a5 RK |
122 | // --------------------------------------------------------------------------- |
123 | ||
124 | export { | |
f2f0eda5 RK |
125 | feedsFormatValidator, |
126 | setFeedFormatContentType, | |
fe3a55b0 | 127 | videoFeedsValidator, |
18490b07 | 128 | videoSubscriptionFeedsValidator, |
fe3a55b0 | 129 | videoCommentsFeedsValidator |
244e76a5 | 130 | } |