refactor: collection logic out to an activitypub helper

This commit is contained in:
Julian Lam
2024-07-05 15:14:35 -04:00
parent 8e1fccf014
commit 352857cfb6
2 changed files with 66 additions and 40 deletions

View File

@@ -355,3 +355,52 @@ Helpers.remoteAnchorToLocalProfile = async (content) => {
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
Helpers.makeSet = (object, properties) => new Set(properties.reduce((memo, property) => memo.concat(Array.isArray(object[property]) ? object[property] : [object[property]]), [])); Helpers.makeSet = (object, properties) => new Set(properties.reduce((memo, property) => memo.concat(Array.isArray(object[property]) ? object[property] : [object[property]]), []));
Helpers.generateCollection = async ({ set, method, page, perPage, url }) => {
if (!method) {
method = db.getSortedSetRange;
}
const count = await db.sortedSetCard(set);
// const { cid, titleRaw: name, mainPid, slug, postcount: count } = await topics.getTopicFields(req.params.tid, ['cid', 'title', 'mainPid', 'slug', 'postcount']);
const pageCount = Math.max(1, Math.ceil(count / perPage));
let items;
let paginate = true;
if (!page && pageCount === 1) {
page = 1;
paginate = false;
}
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 stop = Math.max(0, start + perPage - 1);
items = await method(set, start, stop);
}
const object = {
type: paginate && items ? 'OrderedCollectionPage' : 'OrderedCollection',
totalItems: count,
};
if (items) {
object.orderedItems = items;
if (paginate) {
object.partOf = url;
object.next = page < pageCount ? `${url}?page=${page + 1}` : null;
object.prev = page > 1 ? `${url}?page=${page - 1}` : null;
}
}
if (paginate) {
object.first = `${url}?page=1`;
object.last = `${url}?page=${pageCount}`;
}
return object;
};

View File

@@ -68,59 +68,36 @@ Actors.topic = async function (req, res, next) {
return res.sendStatus(404); return res.sendStatus(404);
} }
let page = parseInt(req.query.page, 10); const page = parseInt(req.query.page, 10);
const { cid, titleRaw: name, mainPid, slug, postcount } = await topics.getTopicFields(req.params.tid, ['cid', 'title', 'mainPid', 'slug', 'postcount']); const perPage = meta.config.postsPerPage;
const pageCount = Math.max(1, Math.ceil(postcount / meta.config.postsPerPage)); const { cid, titleRaw: name, mainPid, slug } = await topics.getTopicFields(req.params.tid, ['cid', 'title', 'mainPid', 'slug']);
let items; const collection = await activitypub.helpers.generateCollection({
let paginate = true; set: `tid:${req.params.tid}:posts`,
method: posts.getPidsFromSet,
page,
perPage,
url: `${nconf.get('url')}/topic/${req.params.tid}`,
});
if (!page && pageCount === 1) { // Convert pids to urls
page = 1; if (collection.orderedItems) {
paginate = false;
}
if (page) {
const invalidPagination = page < 1 || page > pageCount;
if (invalidPagination) {
return next();
}
const start = Math.max(0, ((page - 1) * meta.config.postsPerPage) - 1);
const stop = Math.max(0, start + meta.config.postsPerPage - 1);
const pids = await posts.getPidsFromSet(`tid:${req.params.tid}:posts`, start, stop);
if (page === 1) { if (page === 1) {
pids.unshift(mainPid); collection.orderedItems.unshift(mainPid);
pids.length = Math.min(pids.length, meta.config.postsPerPage); collection.orderedItems.length = Math.min(collection.orderedItems.length, meta.config.postsPerPage);
} }
items = pids.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));
} }
const object = { const object = {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
id: `${nconf.get('url')}/topic/${req.params.tid}${paginate && page ? `?page=${page}` : ''}`, id: `${nconf.get('url')}/topic/${req.params.tid}${collection.orderedItems && page ? `?page=${page}` : ''}`,
url: `${nconf.get('url')}/topic/${slug}`, url: `${nconf.get('url')}/topic/${slug}`,
name, name,
type: paginate && items ? 'OrderedCollectionPage' : 'OrderedCollection',
attributedTo: `${nconf.get('url')}/category/${cid}`, attributedTo: `${nconf.get('url')}/category/${cid}`,
audience: cid !== -1 ? `${nconf.get('url')}/category/${cid}/followers` : undefined, audience: cid !== -1 ? `${nconf.get('url')}/category/${cid}/followers` : undefined,
totalItems: postcount, ...collection,
}; };
if (items) {
object.orderedItems = items;
if (paginate) {
object.partOf = `${nconf.get('url')}/topic/${req.params.tid}`;
object.next = page < pageCount ? `${nconf.get('url')}/topic/${req.params.tid}?page=${page + 1}` : null;
object.prev = page > 1 ? `${nconf.get('url')}/topic/${req.params.tid}?page=${page - 1}` : null;
}
}
if (paginate) {
object.first = `${nconf.get('url')}/topic/${req.params.tid}?page=1`;
object.last = `${nconf.get('url')}/topic/${req.params.tid}?page=${pageCount}`;
}
res.status(200).json(object); res.status(200).json(object);
}; };