aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares')
-rw-r--r--server/middlewares/sort.ts7
-rw-r--r--server/middlewares/validators/pods.ts29
-rw-r--r--server/middlewares/validators/remote/index.ts1
-rw-r--r--server/middlewares/validators/remote/pods.ts38
-rw-r--r--server/middlewares/validators/sort.ts3
-rw-r--r--server/middlewares/validators/utils.ts4
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'
4import { SortType } from '../helpers' 4import { SortType } from '../helpers'
5import { database } from '../initializers' 5import { database } from '../initializers'
6 6
7function 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
7function setUsersSort (req: express.Request, res: express.Response, next: express.NextFunction) { 13function 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
48export { 54export {
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
4import { database as db } from '../../initializers/database' 4import { database as db } from '../../initializers/database'
5import { checkErrors } from './utils' 5import { checkErrors } from './utils'
6import { logger, isEachUniqueHostValid, isHostValid } from '../../helpers' 6import { logger, isEachUniqueHostValid } from '../../helpers'
7import { CONFIG } from '../../initializers' 7import { CONFIG } from '../../initializers'
8import { hasFriends } from '../../lib' 8import { hasFriends } from '../../lib'
9import { isTestInstance } from '../../helpers' 9import { isTestInstance } from '../../helpers'
@@ -41,32 +41,6 @@ const makeFriendsValidator = [
41 } 41 }
42] 42]
43 43
44const 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
70const podRemoveValidator = [ 44const 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
97export { 71export {
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 @@
1export * from './pods'
1export * from './signature' 2export * from './signature'
2export * from './videos' 3export * 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 @@
1import { body } from 'express-validator/check'
2import * as express from 'express'
3
4import { database as db } from '../../../initializers'
5import { isHostValid, logger } from '../../../helpers'
6import { checkErrors } from '../utils'
7
8const 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
36export {
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'
6import { SORTABLE_COLUMNS } from '../../initializers' 6import { SORTABLE_COLUMNS } from '../../initializers'
7 7
8// Initialize constants here for better performances 8// Initialize constants here for better performances
9const SORTABLE_PODS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.PODS)
9const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) 10const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS)
10const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) 11const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES)
11const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) 12const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS)
12const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS) 13const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS)
13 14
15const podsSortValidator = checkSort(SORTABLE_PODS_COLUMNS)
14const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS) 16const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS)
15const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS) 17const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS)
16const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS) 18const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS)
@@ -19,6 +21,7 @@ const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS)
19// --------------------------------------------------------------------------- 21// ---------------------------------------------------------------------------
20 22
21export { 23export {
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
4import { logger } from '../../helpers' 4import { logger } from '../../helpers'
5 5
6function checkErrors (req: express.Request, res: express.Response, next: express.NextFunction, statusCode = 400) { 6function 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()