fix: off-by-one error in helpers.generateCollection, fixed incorrect totalItems count between pages in Actors.topic

This commit is contained in:
Julian Lam
2026-04-02 14:48:00 -04:00
parent 531d20af3e
commit 5a04916879
2 changed files with 6 additions and 5 deletions

View File

@@ -483,14 +483,15 @@ Helpers.generateCollection = async ({ set, method, count, page, perPage, url })
paginate = false;
}
page = parseInt(page, 10) || undefined;
page = parseInt(page, 10) || 1;
page = Math.max(1, Math.min(page, pageCount));
if (page) {
const invalidPagination = page < 1 || page > pageCount;
if (invalidPagination) {
throw new Error('[[error:invalid-data]]');
}
const start = Math.max(0, ((page - 1) * perPage) - 1);
const start = Math.max(0, (page - 1) * perPage);
const stop = Math.max(0, start + perPage - 1);
items = await method.call(null, start, stop);
}

View File

@@ -150,10 +150,10 @@ Actors.topic = async function (req, res, next) {
} catch (e) {
return next(); // invalid page; 404
}
pids.push(mainPid);
pids = pids.map(pid => (utils.isNumber(pid) ? `${nconf.get('url')}/post/${pid}` : pid));
// Generate digest for ETag
pids.push(mainPid);
pids = pids.map(pid => (utils.isNumber(pid) ? `${nconf.get('url')}/post/${pid}` : pid));
const digest = activitypub.helpers.generateDigest(new Set(pids));
const ifNoneMatch = (req.get('If-None-Match') || '').split(',').map((tag) => {
tag = tag.trim();
@@ -169,11 +169,11 @@ Actors.topic = async function (req, res, next) {
res.set('ETag', digest);
// Add OP to collection on first (or only) page
collection.totalItems += 1; // account for mainPid
if (page || collection.totalItems < perPage) {
collection.orderedItems = collection.orderedItems || [];
if (!page || page === 1) {
collection.orderedItems.unshift(mainPid);
collection.totalItems += 1;
}
}