fix: flawed logic in generating topic context collection

This commit is contained in:
Julian Lam
2024-07-11 14:33:07 -04:00
parent 3c0d7616db
commit 31c7226e34
2 changed files with 32 additions and 27 deletions

View File

@@ -386,7 +386,7 @@ Helpers.generateCollection = async ({ set, method, page, perPage, url }) => {
totalItems: count, totalItems: count,
}; };
if (items) { if (items.length) {
object.orderedItems = items; object.orderedItems = items;
if (paginate) { if (paginate) {

View File

@@ -90,7 +90,7 @@ Actors.replies = async function (req, res) {
res.status(200).json(object); res.status(200).json(object);
}; };
Actors.topic = async function (req, res) { Actors.topic = async function (req, res, next) {
const allowed = await privileges.topics.can('topics:read', req.params.tid, activitypub._constants.uid); const allowed = await privileges.topics.can('topics:read', req.params.tid, activitypub._constants.uid);
if (!allowed) { if (!allowed) {
return res.sendStatus(404); return res.sendStatus(404);
@@ -99,6 +99,7 @@ Actors.topic = async function (req, res) {
const page = parseInt(req.query.page, 10); const page = parseInt(req.query.page, 10);
const perPage = meta.config.postsPerPage; const perPage = meta.config.postsPerPage;
const { cid, titleRaw: name, mainPid, slug } = await topics.getTopicFields(req.params.tid, ['cid', 'title', 'mainPid', 'slug']); const { cid, titleRaw: name, mainPid, slug } = await topics.getTopicFields(req.params.tid, ['cid', 'title', 'mainPid', 'slug']);
try {
const collection = await activitypub.helpers.generateCollection({ const collection = await activitypub.helpers.generateCollection({
set: `tid:${req.params.tid}:posts`, set: `tid:${req.params.tid}:posts`,
method: posts.getPidsFromSet, method: posts.getPidsFromSet,
@@ -108,10 +109,11 @@ Actors.topic = async function (req, res) {
}); });
// Convert pids to urls // Convert pids to urls
if (collection.orderedItems) { collection.totalItems += 1;
if (page === 1) { if (page || collection.totalItems < meta.config.postsPerPage) {
collection.orderedItems = collection.orderedItems || [];
if (!page || page === 1) { // add OP to collection
collection.orderedItems.unshift(mainPid); collection.orderedItems.unshift(mainPid);
collection.orderedItems.length = Math.min(collection.orderedItems.length, meta.config.postsPerPage);
} }
collection.orderedItems = collection.orderedItems.map(pid => (utils.isNumber(pid) ? `${nconf.get('url')}/post/${pid}` : pid)); collection.orderedItems = collection.orderedItems.map(pid => (utils.isNumber(pid) ? `${nconf.get('url')}/post/${pid}` : pid));
} }
@@ -127,6 +129,9 @@ Actors.topic = async function (req, res) {
}; };
res.status(200).json(object); res.status(200).json(object);
} catch (e) {
return next();
}
}; };
Actors.category = async function (req, res, next) { Actors.category = async function (req, res, next) {