diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-10-19 09:43:01 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-10-19 09:43:01 +0200 |
commit | 8a02bd0433b7101c5ea36e87a4edb63204d2adec (patch) | |
tree | d7ab4b6164aef752c216bd2f22f8b3b270a724b8 /server/middlewares | |
parent | 9fd540562c356cb54b98861d2d9e7d4fbfcd00e0 (diff) | |
download | PeerTube-8a02bd0433b7101c5ea36e87a4edb63204d2adec.tar.gz PeerTube-8a02bd0433b7101c5ea36e87a4edb63204d2adec.tar.zst PeerTube-8a02bd0433b7101c5ea36e87a4edb63204d2adec.zip |
Add pod list endpoint with pagination, sort...
Diffstat (limited to 'server/middlewares')
-rw-r--r-- | server/middlewares/sort.ts | 7 | ||||
-rw-r--r-- | server/middlewares/validators/pods.ts | 29 | ||||
-rw-r--r-- | server/middlewares/validators/remote/index.ts | 1 | ||||
-rw-r--r-- | server/middlewares/validators/remote/pods.ts | 38 | ||||
-rw-r--r-- | server/middlewares/validators/sort.ts | 3 | ||||
-rw-r--r-- | server/middlewares/validators/utils.ts | 4 |
6 files changed, 52 insertions, 30 deletions
diff --git a/server/middlewares/sort.ts b/server/middlewares/sort.ts index 687ce097b..2c70ff5f0 100644 --- a/server/middlewares/sort.ts +++ b/server/middlewares/sort.ts | |||
@@ -4,6 +4,12 @@ import * as express from 'express' | |||
4 | import { SortType } from '../helpers' | 4 | import { SortType } from '../helpers' |
5 | import { database } from '../initializers' | 5 | import { database } from '../initializers' |
6 | 6 | ||
7 | function setPodsSort (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
8 | if (!req.query.sort) req.query.sort = '-createdAt' | ||
9 | |||
10 | return next() | ||
11 | } | ||
12 | |||
7 | function setUsersSort (req: express.Request, res: express.Response, next: express.NextFunction) { | 13 | function setUsersSort (req: express.Request, res: express.Response, next: express.NextFunction) { |
8 | if (!req.query.sort) req.query.sort = '-createdAt' | 14 | if (!req.query.sort) req.query.sort = '-createdAt' |
9 | 15 | ||
@@ -46,6 +52,7 @@ function setBlacklistSort (req: express.Request, res: express.Response, next: ex | |||
46 | // --------------------------------------------------------------------------- | 52 | // --------------------------------------------------------------------------- |
47 | 53 | ||
48 | export { | 54 | export { |
55 | setPodsSort, | ||
49 | setUsersSort, | 56 | setUsersSort, |
50 | setVideoAbusesSort, | 57 | setVideoAbusesSort, |
51 | setVideosSort, | 58 | setVideosSort, |
diff --git a/server/middlewares/validators/pods.ts b/server/middlewares/validators/pods.ts index ab7702e78..575c36526 100644 --- a/server/middlewares/validators/pods.ts +++ b/server/middlewares/validators/pods.ts | |||
@@ -3,7 +3,7 @@ import * as express from 'express' | |||
3 | 3 | ||
4 | import { database as db } from '../../initializers/database' | 4 | import { database as db } from '../../initializers/database' |
5 | import { checkErrors } from './utils' | 5 | import { checkErrors } from './utils' |
6 | import { logger, isEachUniqueHostValid, isHostValid } from '../../helpers' | 6 | import { logger, isEachUniqueHostValid } from '../../helpers' |
7 | import { CONFIG } from '../../initializers' | 7 | import { CONFIG } from '../../initializers' |
8 | import { hasFriends } from '../../lib' | 8 | import { hasFriends } from '../../lib' |
9 | import { isTestInstance } from '../../helpers' | 9 | import { isTestInstance } from '../../helpers' |
@@ -41,32 +41,6 @@ const makeFriendsValidator = [ | |||
41 | } | 41 | } |
42 | ] | 42 | ] |
43 | 43 | ||
44 | const podsAddValidator = [ | ||
45 | body('host').custom(isHostValid).withMessage('Should have a host'), | ||
46 | body('email').isEmail().withMessage('Should have an email'), | ||
47 | body('publicKey').not().isEmpty().withMessage('Should have a public key'), | ||
48 | |||
49 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
50 | logger.debug('Checking podsAdd parameters', { parameters: req.body }) | ||
51 | |||
52 | checkErrors(req, res, () => { | ||
53 | db.Pod.loadByHost(req.body.host) | ||
54 | .then(pod => { | ||
55 | // Pod with this host already exists | ||
56 | if (pod) { | ||
57 | return res.sendStatus(409) | ||
58 | } | ||
59 | |||
60 | return next() | ||
61 | }) | ||
62 | .catch(err => { | ||
63 | logger.error('Cannot load pod by host.', err) | ||
64 | res.sendStatus(500) | ||
65 | }) | ||
66 | }) | ||
67 | } | ||
68 | ] | ||
69 | |||
70 | const podRemoveValidator = [ | 44 | const podRemoveValidator = [ |
71 | param('id').isNumeric().not().isEmpty().withMessage('Should have a valid id'), | 45 | param('id').isNumeric().not().isEmpty().withMessage('Should have a valid id'), |
72 | 46 | ||
@@ -96,6 +70,5 @@ const podRemoveValidator = [ | |||
96 | 70 | ||
97 | export { | 71 | export { |
98 | makeFriendsValidator, | 72 | makeFriendsValidator, |
99 | podsAddValidator, | ||
100 | podRemoveValidator | 73 | podRemoveValidator |
101 | } | 74 | } |
diff --git a/server/middlewares/validators/remote/index.ts b/server/middlewares/validators/remote/index.ts index d0d7740b1..f1f26043e 100644 --- a/server/middlewares/validators/remote/index.ts +++ b/server/middlewares/validators/remote/index.ts | |||
@@ -1,2 +1,3 @@ | |||
1 | export * from './pods' | ||
1 | export * from './signature' | 2 | export * from './signature' |
2 | export * from './videos' | 3 | export * from './videos' |
diff --git a/server/middlewares/validators/remote/pods.ts b/server/middlewares/validators/remote/pods.ts new file mode 100644 index 000000000..f917b61ee --- /dev/null +++ b/server/middlewares/validators/remote/pods.ts | |||
@@ -0,0 +1,38 @@ | |||
1 | import { body } from 'express-validator/check' | ||
2 | import * as express from 'express' | ||
3 | |||
4 | import { database as db } from '../../../initializers' | ||
5 | import { isHostValid, logger } from '../../../helpers' | ||
6 | import { checkErrors } from '../utils' | ||
7 | |||
8 | const remotePodsAddValidator = [ | ||
9 | body('host').custom(isHostValid).withMessage('Should have a host'), | ||
10 | body('email').isEmail().withMessage('Should have an email'), | ||
11 | body('publicKey').not().isEmpty().withMessage('Should have a public key'), | ||
12 | |||
13 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
14 | logger.debug('Checking podsAdd parameters', { parameters: req.body }) | ||
15 | |||
16 | checkErrors(req, res, () => { | ||
17 | db.Pod.loadByHost(req.body.host) | ||
18 | .then(pod => { | ||
19 | // Pod with this host already exists | ||
20 | if (pod) { | ||
21 | return res.sendStatus(409) | ||
22 | } | ||
23 | |||
24 | return next() | ||
25 | }) | ||
26 | .catch(err => { | ||
27 | logger.error('Cannot load pod by host.', err) | ||
28 | res.sendStatus(500) | ||
29 | }) | ||
30 | }) | ||
31 | } | ||
32 | ] | ||
33 | |||
34 | // --------------------------------------------------------------------------- | ||
35 | |||
36 | export { | ||
37 | remotePodsAddValidator | ||
38 | } | ||
diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts index a6f5ccb6b..227f309ad 100644 --- a/server/middlewares/validators/sort.ts +++ b/server/middlewares/validators/sort.ts | |||
@@ -6,11 +6,13 @@ import { logger } from '../../helpers' | |||
6 | import { SORTABLE_COLUMNS } from '../../initializers' | 6 | import { SORTABLE_COLUMNS } from '../../initializers' |
7 | 7 | ||
8 | // Initialize constants here for better performances | 8 | // Initialize constants here for better performances |
9 | const SORTABLE_PODS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.PODS) | ||
9 | const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) | 10 | const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) |
10 | const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) | 11 | const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) |
11 | const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) | 12 | const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) |
12 | const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS) | 13 | const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS) |
13 | 14 | ||
15 | const podsSortValidator = checkSort(SORTABLE_PODS_COLUMNS) | ||
14 | const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS) | 16 | const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS) |
15 | const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS) | 17 | const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS) |
16 | const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS) | 18 | const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS) |
@@ -19,6 +21,7 @@ const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS) | |||
19 | // --------------------------------------------------------------------------- | 21 | // --------------------------------------------------------------------------- |
20 | 22 | ||
21 | export { | 23 | export { |
24 | podsSortValidator, | ||
22 | usersSortValidator, | 25 | usersSortValidator, |
23 | videoAbusesSortValidator, | 26 | videoAbusesSortValidator, |
24 | videosSortValidator, | 27 | videosSortValidator, |
diff --git a/server/middlewares/validators/utils.ts b/server/middlewares/validators/utils.ts index 8845f8399..ea107bbe8 100644 --- a/server/middlewares/validators/utils.ts +++ b/server/middlewares/validators/utils.ts | |||
@@ -3,12 +3,12 @@ import * as express from 'express' | |||
3 | 3 | ||
4 | import { logger } from '../../helpers' | 4 | import { logger } from '../../helpers' |
5 | 5 | ||
6 | function checkErrors (req: express.Request, res: express.Response, next: express.NextFunction, statusCode = 400) { | 6 | function checkErrors (req: express.Request, res: express.Response, next: express.NextFunction) { |
7 | const errors = validationResult(req) | 7 | const errors = validationResult(req) |
8 | 8 | ||
9 | if (!errors.isEmpty()) { | 9 | if (!errors.isEmpty()) { |
10 | logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors.mapped() }) | 10 | logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors.mapped() }) |
11 | return res.status(statusCode).json({ errors: errors.mapped() }) | 11 | return res.status(400).json({ errors: errors.mapped() }) |
12 | } | 12 | } |
13 | 13 | ||
14 | return next() | 14 | return next() |