From 4b11cd0da5494b6d565a8fdf1e934dcaedb488cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 22 Jun 2023 23:38:57 -0400 Subject: [PATCH] feat: closes #11747, add pagination to tokens page --- src/api/utils.js | 6 ++++-- src/controllers/admin/settings.js | 13 +++++++++++-- src/views/admin/settings/api.tpl | 5 +++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/api/utils.js b/src/api/utils.js index 73f8bb67b4..4597bed42b 100644 --- a/src/api/utils.js +++ b/src/api/utils.js @@ -10,12 +10,14 @@ const utils = module.exports; // internal token management utilities only utils.tokens = {}; -utils.tokens.list = async () => { +utils.tokens.list = async (start = 0, stop = -1) => { // Validation handled at higher level - const tokens = await db.getSortedSetRange(`tokens:createtime`, 0, -1); + const tokens = await db.getSortedSetRange(`tokens:createtime`, start, stop); return await utils.tokens.get(tokens); }; +utils.tokens.count = async () => await db.sortedSetCard('tokens:createtime'); + utils.tokens.get = async (tokens) => { // Validation handled at higher level if (!tokens) { diff --git a/src/controllers/admin/settings.js b/src/controllers/admin/settings.js index 210c6ce614..9ecf08548d 100644 --- a/src/controllers/admin/settings.js +++ b/src/controllers/admin/settings.js @@ -10,7 +10,7 @@ const languages = require('../../languages'); const navigationAdmin = require('../../navigation/admin'); const social = require('../../social'); const api = require('../../api'); - +const pagination = require('../../pagination'); const helpers = require('../helpers'); const translator = require('../../translator'); @@ -104,9 +104,18 @@ settingsController.navigation = async function (req, res) { }; settingsController.api = async (req, res) => { - const tokens = await api.utils.tokens.list(); + const page = parseInt(req.query.page, 10) || 1; + const resultsPerPage = 50; + const start = Math.max(0, page - 1) * resultsPerPage; + const stop = start + resultsPerPage - 1; + const [tokens, count] = await Promise.all([ + api.utils.tokens.list(start, stop), + api.utils.tokens.count(), + ]); + const pageCount = Math.ceil(count / resultsPerPage); res.render('admin/settings/api', { title: '[[admin/menu:settings/api]]', tokens, + pagination: pagination.create(page, pageCount, req.query), }); }; diff --git a/src/views/admin/settings/api.tpl b/src/views/admin/settings/api.tpl index d38b546a37..20f0528b3e 100644 --- a/src/views/admin/settings/api.tpl +++ b/src/views/admin/settings/api.tpl @@ -32,8 +32,8 @@ -
- +
+
@@ -92,4 +92,5 @@
[[admin/settings/api:token]]
+