From 1194e8b46f53204967271f1c6bfb7428a717a483 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 24 Jul 2018 16:27:45 +0200 Subject: Add max count in pagination --- server/initializers/constants.ts | 10 +++++++--- server/middlewares/pagination.ts | 6 ++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index e8e0da683..85c5335ad 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -21,8 +21,12 @@ const LAST_MIGRATION_VERSION = 235 // API version const API_VERSION = 'v1' -// Number of results by default for the pagination -const PAGINATION_COUNT_DEFAULT = 15 +const PAGINATION = { + COUNT: { + DEFAULT: 15, + MAX: 100 + } +} // Sortable columns per schema const SORTABLE_COLUMNS = { @@ -539,7 +543,7 @@ export { OAUTH_LIFETIME, CUSTOM_HTML_TAG_COMMENTS, BROADCAST_CONCURRENCY, - PAGINATION_COUNT_DEFAULT, + PAGINATION, ACTOR_FOLLOW_SCORE, PREVIEWS_SIZE, REMOTE_SCHEME, diff --git a/server/middlewares/pagination.ts b/server/middlewares/pagination.ts index 2ea2a6b82..9b497b19e 100644 --- a/server/middlewares/pagination.ts +++ b/server/middlewares/pagination.ts @@ -1,15 +1,17 @@ import 'express-validator' import * as express from 'express' -import { PAGINATION_COUNT_DEFAULT } from '../initializers' +import { PAGINATION } from '../initializers' function setDefaultPagination (req: express.Request, res: express.Response, next: express.NextFunction) { if (!req.query.start) req.query.start = 0 else req.query.start = parseInt(req.query.start, 10) - if (!req.query.count) req.query.count = PAGINATION_COUNT_DEFAULT + if (!req.query.count) req.query.count = PAGINATION.COUNT.DEFAULT else req.query.count = parseInt(req.query.count, 10) + if (req.query.count > PAGINATION.COUNT.MAX) req.query.count = PAGINATION.COUNT.MAX + return next() } -- cgit v1.2.3