diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 14:44:51 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:53 +0100 |
commit | 4e50b6a1c9a3eb261e04ede73241648e6edf21d6 (patch) | |
tree | e1c6c121d561ffc1cf2996daec03a1e7f27f0a25 /server/middlewares/validators | |
parent | 74bb2cb8348d6794ed3a0e2ec94c8c9abdde82cf (diff) | |
download | PeerTube-4e50b6a1c9a3eb261e04ede73241648e6edf21d6.tar.gz PeerTube-4e50b6a1c9a3eb261e04ede73241648e6edf21d6.tar.zst PeerTube-4e50b6a1c9a3eb261e04ede73241648e6edf21d6.zip |
Add shares forward and collection on videos/video channels
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r-- | server/middlewares/validators/account.ts | 26 | ||||
-rw-r--r-- | server/middlewares/validators/utils.ts | 16 | ||||
-rw-r--r-- | server/middlewares/validators/video-channels.ts | 41 | ||||
-rw-r--r-- | server/middlewares/validators/videos.ts | 26 |
4 files changed, 77 insertions, 32 deletions
diff --git a/server/middlewares/validators/account.ts b/server/middlewares/validators/account.ts index 07ae76b63..47ed6a7bb 100644 --- a/server/middlewares/validators/account.ts +++ b/server/middlewares/validators/account.ts | |||
@@ -1,9 +1,7 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { param } from 'express-validator/check' | 2 | import { param } from 'express-validator/check' |
3 | import { logger } from '../../helpers' | 3 | import { logger } from '../../helpers' |
4 | import { isAccountNameValid } from '../../helpers/custom-validators/accounts' | 4 | import { checkLocalAccountNameExists, isAccountNameValid } from '../../helpers/custom-validators/accounts' |
5 | import { database as db } from '../../initializers/database' | ||
6 | import { AccountInstance } from '../../models' | ||
7 | import { checkErrors } from './utils' | 5 | import { checkErrors } from './utils' |
8 | 6 | ||
9 | const localAccountValidator = [ | 7 | const localAccountValidator = [ |
@@ -13,7 +11,7 @@ const localAccountValidator = [ | |||
13 | logger.debug('Checking localAccountValidator parameters', { parameters: req.params }) | 11 | logger.debug('Checking localAccountValidator parameters', { parameters: req.params }) |
14 | 12 | ||
15 | checkErrors(req, res, () => { | 13 | checkErrors(req, res, () => { |
16 | checkLocalAccountExists(req.params.name, res, next) | 14 | checkLocalAccountNameExists(req.params.name, res, next) |
17 | }) | 15 | }) |
18 | } | 16 | } |
19 | ] | 17 | ] |
@@ -23,23 +21,3 @@ const localAccountValidator = [ | |||
23 | export { | 21 | export { |
24 | localAccountValidator | 22 | localAccountValidator |
25 | } | 23 | } |
26 | |||
27 | // --------------------------------------------------------------------------- | ||
28 | |||
29 | function checkLocalAccountExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => void) { | ||
30 | db.Account.loadLocalByName(name) | ||
31 | .then(account => { | ||
32 | if (!account) { | ||
33 | return res.status(404) | ||
34 | .send({ error: 'Account not found' }) | ||
35 | .end() | ||
36 | } | ||
37 | |||
38 | res.locals.account = account | ||
39 | return callback(null, account) | ||
40 | }) | ||
41 | .catch(err => { | ||
42 | logger.error('Error in account request validator.', err) | ||
43 | return res.sendStatus(500) | ||
44 | }) | ||
45 | } | ||
diff --git a/server/middlewares/validators/utils.ts b/server/middlewares/validators/utils.ts index ea107bbe8..77a1a0d4b 100644 --- a/server/middlewares/validators/utils.ts +++ b/server/middlewares/validators/utils.ts | |||
@@ -14,8 +14,22 @@ function checkErrors (req: express.Request, res: express.Response, next: express | |||
14 | return next() | 14 | return next() |
15 | } | 15 | } |
16 | 16 | ||
17 | function areValidationErrors (req: express.Request, res: express.Response) { | ||
18 | const errors = validationResult(req) | ||
19 | |||
20 | if (!errors.isEmpty()) { | ||
21 | logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors.mapped() }) | ||
22 | res.status(400).json({ errors: errors.mapped() }) | ||
23 | |||
24 | return true | ||
25 | } | ||
26 | |||
27 | return false | ||
28 | } | ||
29 | |||
17 | // --------------------------------------------------------------------------- | 30 | // --------------------------------------------------------------------------- |
18 | 31 | ||
19 | export { | 32 | export { |
20 | checkErrors | 33 | checkErrors, |
34 | areValidationErrors | ||
21 | } | 35 | } |
diff --git a/server/middlewares/validators/video-channels.ts b/server/middlewares/validators/video-channels.ts index c6fd3b59d..f30fbf0dc 100644 --- a/server/middlewares/validators/video-channels.ts +++ b/server/middlewares/validators/video-channels.ts | |||
@@ -1,13 +1,19 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { body, param } from 'express-validator/check' | 2 | import { body, param } from 'express-validator/check' |
3 | import { UserRight } from '../../../shared' | 3 | import { UserRight } from '../../../shared' |
4 | import { checkVideoAccountExists } from '../../helpers/custom-validators/accounts' | 4 | import { checkAccountIdExists } from '../../helpers/custom-validators/accounts' |
5 | import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels' | 5 | import { isIdValid } from '../../helpers/custom-validators/misc' |
6 | import { checkVideoChannelExists, isIdOrUUIDValid } from '../../helpers/index' | 6 | import { |
7 | checkVideoChannelExists, | ||
8 | isVideoChannelDescriptionValid, | ||
9 | isVideoChannelExistsPromise, | ||
10 | isVideoChannelNameValid | ||
11 | } from '../../helpers/custom-validators/video-channels' | ||
12 | import { isIdOrUUIDValid } from '../../helpers/index' | ||
7 | import { logger } from '../../helpers/logger' | 13 | import { logger } from '../../helpers/logger' |
8 | import { database as db } from '../../initializers' | 14 | import { database as db } from '../../initializers' |
9 | import { UserInstance } from '../../models' | 15 | import { UserInstance } from '../../models' |
10 | import { checkErrors } from './utils' | 16 | import { areValidationErrors, checkErrors } from './utils' |
11 | 17 | ||
12 | const listVideoAccountChannelsValidator = [ | 18 | const listVideoAccountChannelsValidator = [ |
13 | param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'), | 19 | param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'), |
@@ -16,7 +22,7 @@ const listVideoAccountChannelsValidator = [ | |||
16 | logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body }) | 22 | logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body }) |
17 | 23 | ||
18 | checkErrors(req, res, () => { | 24 | checkErrors(req, res, () => { |
19 | checkVideoAccountExists(req.params.accountId, res, next) | 25 | checkAccountIdExists(req.params.accountId, res, next) |
20 | }) | 26 | }) |
21 | } | 27 | } |
22 | ] | 28 | ] |
@@ -90,6 +96,28 @@ const videoChannelsGetValidator = [ | |||
90 | } | 96 | } |
91 | ] | 97 | ] |
92 | 98 | ||
99 | const videoChannelsShareValidator = [ | ||
100 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | ||
101 | param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'), | ||
102 | |||
103 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
104 | logger.debug('Checking videoChannelShare parameters', { parameters: req.params }) | ||
105 | |||
106 | if (areValidationErrors(req, res)) return | ||
107 | if (!await isVideoChannelExistsPromise(req.params.id, res)) return | ||
108 | |||
109 | const share = await db.VideoChannelShare.load(res.locals.video.id, req.params.accountId) | ||
110 | if (!share) { | ||
111 | return res.status(404) | ||
112 | .end() | ||
113 | } | ||
114 | |||
115 | res.locals.videoChannelShare = share | ||
116 | |||
117 | return next() | ||
118 | } | ||
119 | ] | ||
120 | |||
93 | // --------------------------------------------------------------------------- | 121 | // --------------------------------------------------------------------------- |
94 | 122 | ||
95 | export { | 123 | export { |
@@ -97,7 +125,8 @@ export { | |||
97 | videoChannelsAddValidator, | 125 | videoChannelsAddValidator, |
98 | videoChannelsUpdateValidator, | 126 | videoChannelsUpdateValidator, |
99 | videoChannelsRemoveValidator, | 127 | videoChannelsRemoveValidator, |
100 | videoChannelsGetValidator | 128 | videoChannelsGetValidator, |
129 | videoChannelsShareValidator | ||
101 | } | 130 | } |
102 | 131 | ||
103 | // --------------------------------------------------------------------------- | 132 | // --------------------------------------------------------------------------- |
diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index df0eb7b96..5ffc85210 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts | |||
@@ -24,7 +24,8 @@ import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers' | |||
24 | import { database as db } from '../../initializers/database' | 24 | import { database as db } from '../../initializers/database' |
25 | import { UserInstance } from '../../models/account/user-interface' | 25 | import { UserInstance } from '../../models/account/user-interface' |
26 | import { authenticate } from '../oauth' | 26 | import { authenticate } from '../oauth' |
27 | import { checkErrors } from './utils' | 27 | import { areValidationErrors, checkErrors } from './utils' |
28 | import { isVideoExistsPromise } from '../../helpers/index' | ||
28 | 29 | ||
29 | const videosAddValidator = [ | 30 | const videosAddValidator = [ |
30 | body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage( | 31 | body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage( |
@@ -230,6 +231,28 @@ const videoRateValidator = [ | |||
230 | } | 231 | } |
231 | ] | 232 | ] |
232 | 233 | ||
234 | const videosShareValidator = [ | ||
235 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | ||
236 | param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'), | ||
237 | |||
238 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
239 | logger.debug('Checking videoShare parameters', { parameters: req.params }) | ||
240 | |||
241 | if (areValidationErrors(req, res)) return | ||
242 | if (!await isVideoExistsPromise(req.params.id, res)) return | ||
243 | |||
244 | const share = await db.VideoShare.load(req.params.accountId, res.locals.video.id) | ||
245 | if (!share) { | ||
246 | return res.status(404) | ||
247 | .end() | ||
248 | } | ||
249 | |||
250 | res.locals.videoShare = share | ||
251 | |||
252 | return next() | ||
253 | } | ||
254 | ] | ||
255 | |||
233 | // --------------------------------------------------------------------------- | 256 | // --------------------------------------------------------------------------- |
234 | 257 | ||
235 | export { | 258 | export { |
@@ -238,6 +261,7 @@ export { | |||
238 | videosGetValidator, | 261 | videosGetValidator, |
239 | videosRemoveValidator, | 262 | videosRemoveValidator, |
240 | videosSearchValidator, | 263 | videosSearchValidator, |
264 | videosShareValidator, | ||
241 | 265 | ||
242 | videoAbuseReportValidator, | 266 | videoAbuseReportValidator, |
243 | 267 | ||