]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { param, query } from 'express-validator' | |
3 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' | |
4 | import { isValidRSSFeed } from '../../helpers/custom-validators/feeds' | |
5 | import { exists, isIdOrUUIDValid, isIdValid, toCompleteUUID } from '../../helpers/custom-validators/misc' | |
6 | import { logger } from '../../helpers/logger' | |
7 | import { | |
8 | areValidationErrors, | |
9 | checkCanSeeVideo, | |
10 | doesAccountIdExist, | |
11 | doesAccountNameWithHostExist, | |
12 | doesUserFeedTokenCorrespond, | |
13 | doesVideoChannelIdExist, | |
14 | doesVideoChannelNameWithHostExist, | |
15 | doesVideoExist | |
16 | } from './shared' | |
17 | ||
18 | const feedsFormatValidator = [ | |
19 | param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), | |
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') { | |
28 | acceptableContentTypes = [ 'application/atom+xml', 'application/xml', 'text/xml' ] | |
29 | } else if (format === 'json' || format === 'json1') { | |
30 | acceptableContentTypes = [ 'application/json' ] | |
31 | } else if (format === 'rss' || format === 'rss2') { | |
32 | acceptableContentTypes = [ 'application/rss+xml', 'application/xml', 'text/xml' ] | |
33 | } else { | |
34 | acceptableContentTypes = [ 'application/xml', 'text/xml' ] | |
35 | } | |
36 | ||
37 | if (req.accepts(acceptableContentTypes)) { | |
38 | res.set('Content-Type', req.accepts(acceptableContentTypes) as string) | |
39 | } else { | |
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 | }) | |
44 | } | |
45 | ||
46 | return next() | |
47 | } | |
48 | ||
49 | const videoFeedsValidator = [ | |
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(), | |
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 | ||
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 | |
75 | ||
76 | return next() | |
77 | } | |
78 | ] | |
79 | ||
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'), | |
88 | ||
89 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
90 | logger.debug('Checking subscription feeds parameters', { parameters: req.query }) | |
91 | ||
92 | if (areValidationErrors(req, res)) return | |
93 | ||
94 | if (!await doesAccountIdExist(req.query.accountId, res)) return | |
95 | if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return | |
96 | ||
97 | return next() | |
98 | } | |
99 | ] | |
100 | ||
101 | const videoCommentsFeedsValidator = [ | |
102 | query('videoId') | |
103 | .customSanitizer(toCompleteUUID) | |
104 | .optional() | |
105 | .custom(isIdOrUUIDValid), | |
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 | ||
112 | if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) { | |
113 | return res.fail({ message: 'videoId cannot be mixed with a channel filter' }) | |
114 | } | |
115 | ||
116 | if (req.query.videoId) { | |
117 | if (!await doesVideoExist(req.query.videoId, res)) return | |
118 | if (!await checkCanSeeVideo({ req, res, paramId: req.query.videoId, video: res.locals.videoAll })) return | |
119 | } | |
120 | ||
121 | return next() | |
122 | } | |
123 | ] | |
124 | ||
125 | // --------------------------------------------------------------------------- | |
126 | ||
127 | export { | |
128 | feedsFormatValidator, | |
129 | setFeedFormatContentType, | |
130 | videoFeedsValidator, | |
131 | videoSubscriptionFeedsValidator, | |
132 | videoCommentsFeedsValidator | |
133 | } |