From 8a02bd0433b7101c5ea36e87a4edb63204d2adec Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 19 Oct 2017 09:43:01 +0200 Subject: Add pod list endpoint with pagination, sort... --- server/middlewares/sort.ts | 7 +++++ server/middlewares/validators/pods.ts | 29 +------------------- server/middlewares/validators/remote/index.ts | 1 + server/middlewares/validators/remote/pods.ts | 38 +++++++++++++++++++++++++++ server/middlewares/validators/sort.ts | 3 +++ server/middlewares/validators/utils.ts | 4 +-- 6 files changed, 52 insertions(+), 30 deletions(-) create mode 100644 server/middlewares/validators/remote/pods.ts (limited to 'server/middlewares') 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' import { SortType } from '../helpers' import { database } from '../initializers' +function setPodsSort (req: express.Request, res: express.Response, next: express.NextFunction) { + if (!req.query.sort) req.query.sort = '-createdAt' + + return next() +} + function setUsersSort (req: express.Request, res: express.Response, next: express.NextFunction) { if (!req.query.sort) req.query.sort = '-createdAt' @@ -46,6 +52,7 @@ function setBlacklistSort (req: express.Request, res: express.Response, next: ex // --------------------------------------------------------------------------- export { + setPodsSort, setUsersSort, setVideoAbusesSort, 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' import { database as db } from '../../initializers/database' import { checkErrors } from './utils' -import { logger, isEachUniqueHostValid, isHostValid } from '../../helpers' +import { logger, isEachUniqueHostValid } from '../../helpers' import { CONFIG } from '../../initializers' import { hasFriends } from '../../lib' import { isTestInstance } from '../../helpers' @@ -41,32 +41,6 @@ const makeFriendsValidator = [ } ] -const podsAddValidator = [ - body('host').custom(isHostValid).withMessage('Should have a host'), - body('email').isEmail().withMessage('Should have an email'), - body('publicKey').not().isEmpty().withMessage('Should have a public key'), - - (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking podsAdd parameters', { parameters: req.body }) - - checkErrors(req, res, () => { - db.Pod.loadByHost(req.body.host) - .then(pod => { - // Pod with this host already exists - if (pod) { - return res.sendStatus(409) - } - - return next() - }) - .catch(err => { - logger.error('Cannot load pod by host.', err) - res.sendStatus(500) - }) - }) - } -] - const podRemoveValidator = [ param('id').isNumeric().not().isEmpty().withMessage('Should have a valid id'), @@ -96,6 +70,5 @@ const podRemoveValidator = [ export { makeFriendsValidator, - podsAddValidator, podRemoveValidator } 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 @@ +export * from './pods' export * from './signature' 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 @@ +import { body } from 'express-validator/check' +import * as express from 'express' + +import { database as db } from '../../../initializers' +import { isHostValid, logger } from '../../../helpers' +import { checkErrors } from '../utils' + +const remotePodsAddValidator = [ + body('host').custom(isHostValid).withMessage('Should have a host'), + body('email').isEmail().withMessage('Should have an email'), + body('publicKey').not().isEmpty().withMessage('Should have a public key'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking podsAdd parameters', { parameters: req.body }) + + checkErrors(req, res, () => { + db.Pod.loadByHost(req.body.host) + .then(pod => { + // Pod with this host already exists + if (pod) { + return res.sendStatus(409) + } + + return next() + }) + .catch(err => { + logger.error('Cannot load pod by host.', err) + res.sendStatus(500) + }) + }) + } +] + +// --------------------------------------------------------------------------- + +export { + remotePodsAddValidator +} 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' import { SORTABLE_COLUMNS } from '../../initializers' // Initialize constants here for better performances +const SORTABLE_PODS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.PODS) const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS) +const podsSortValidator = checkSort(SORTABLE_PODS_COLUMNS) const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS) const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS) const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS) @@ -19,6 +21,7 @@ const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS) // --------------------------------------------------------------------------- export { + podsSortValidator, usersSortValidator, videoAbusesSortValidator, 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' import { logger } from '../../helpers' -function checkErrors (req: express.Request, res: express.Response, next: express.NextFunction, statusCode = 400) { +function checkErrors (req: express.Request, res: express.Response, next: express.NextFunction) { const errors = validationResult(req) if (!errors.isEmpty()) { logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors.mapped() }) - return res.status(statusCode).json({ errors: errors.mapped() }) + return res.status(400).json({ errors: errors.mapped() }) } return next() -- cgit v1.2.3