Files
wekan/models/lib/domainTablePage.js
T
Lauri Ojansivu c1add5bc5e Paginate the admin lists honestly: one page of ten, index-backed.
Admin Panel / People showed the admin on every one of its 578 pages, and
Admin Panel / Problems / Broken cards was one endless page under a pager that
said "1 / 1". Same bug in two shapes: the pane subscribes to a publication
that sends exactly one page, then reads the page back out of minimongo - which
holds far more than the page. The logged-in user's own record is always there
(accounts publishes it), and so is every card of every board the admin has
opened. A plain find(query) cannot tell those apart from the page.

So the server names the page. getPeoplePageIds returns the ids of the People
page with the publication's own selector, sort and window; the Broken cards,
Cards and Boards report publications send the ids of the page they just sent
alongside it. Both panes render that list, in that order, and nothing else.

Every paginated page in WeKan now loads ten rows at a time, from one constant -
TABLE_PAGE_ROWS_PER_PAGE - including the pages that draw a pager of their own:
the reports and event streams, the four People panes, Translation, the search
pages and the archive. docs/Design/Page/Table.md records the rule and its two
deliberate exceptions.

And the counting behind those pagers is index-backed. Every count already asks
the database for a count rather than fetching the rows, but several counted and
paged on unindexed fields: People sorts users newest-first, so on an instance
with 14000 users each page of ten sorted all 14000 first. Broken cards asks an
$or over boardId/swimlaneId/listId/type, which uses no index at all unless
every branch has one. The missing indexes are created at startup by the
idempotent ensureIndex, so an upgrade adds them by itself.

Thanks to xet7 !
2026-07-27 23:39:37 +03:00

76 lines
2.8 KiB
JavaScript

'use strict';
// Pure filter + sort + paginate for the Admin Panel > People > Domains table.
// Extracted so the page logic is unit-testable without Meteor and shared by the
// server method that feeds the table.
//
// The Domains table used to load EVERY domain (aggregated from all users) into
// the browser at once, with a fixed order and no search. To behave like the
// Board Table view — send only one small page to the browser, let the user
// order by a column and search — the server aggregates all users into
// { domain, count } rows and then hands them to this helper, which applies the
// search filter, the requested column sort and the requested page, returning
// only that page plus the total so the client can render pagination controls.
// The app's one rows-per-page (TABLE_PAGE_ROWS_PER_PAGE in models/lib/tablePage.js,
// docs/Design/Page/Table.md). Written out rather than imported because this module
// is loaded by a plain-node test with require(), which cannot parse ESM imports;
// tests/tablePage.test.cjs pins the two to the same number.
const PER_PAGE_DEFAULT = 10;
const PER_PAGE_MAX = 200;
function paginateDomains(rows, opts = {}) {
const all = Array.isArray(rows) ? rows : [];
const search =
typeof opts.search === 'string' ? opts.search.trim().toLowerCase() : '';
// Only two real columns; anything else falls back to the domain name.
const sortField = opts.sortField === 'count' ? 'count' : 'domain';
const sortDirection = opts.sortDirection === -1 ? -1 : 1;
const rawPerPage = Number(opts.perPage);
const perPage = Math.min(
PER_PAGE_MAX,
Number.isFinite(rawPerPage) && rawPerPage >= 1 ? rawPerPage : PER_PAGE_DEFAULT,
);
let filtered = all;
if (search) {
filtered = all.filter(
row => (row.domain || '').toLowerCase().indexOf(search) !== -1,
);
}
const byDomain = (a, b) =>
(a.domain || '').localeCompare(b.domain || '', undefined, {
sensitivity: 'base',
numeric: true,
});
filtered = filtered.slice().sort((a, b) => {
if (sortField === 'count') {
const cmp = ((a.count || 0) - (b.count || 0)) * sortDirection;
if (cmp !== 0) return cmp;
// Ties always break alphabetically (ascending), independent of direction.
return byDomain(a, b);
}
return byDomain(a, b) * sortDirection;
});
const total = filtered.length;
const totalPages = Math.max(1, Math.ceil(total / perPage));
// Clamp the requested page into range so a stale/too-large page never yields
// an empty table.
const page = Math.min(Math.max(1, Number(opts.page) || 1), totalPages);
const start = (page - 1) * perPage;
return {
rows: filtered.slice(start, start + perPage),
total,
page,
totalPages,
perPage,
};
}
module.exports = { paginateDomains, PER_PAGE_DEFAULT, PER_PAGE_MAX };