Skip to content

Node.js based forum software built for the modern web

fix(activitypub): handle before cursor for upward infinite scroll navigation

Julian Lam0c6988c78e71
diff --git a/public/src/client/world.js b/public/src/client/world.js
index ec2c4ef497..fb0bcd2ce0 100644
--- a/public/src/client/world.js
+++ b/public/src/client/world.js
@@ -129,11 +129,11 @@ define('forum/world', [
return Math.floor(after / config.topicsPerPage) + (direction > 0 ? 1 : 0);
}
- function loadTopicsAfter(index, after, direction, callback) {
+ function loadTopicsAfter(index, referenceTid, direction, callback) {
callback = callback || function () {};
const query = utils.params();
query.page = calculateNextPage(index, direction);
- query.after = after;
+ query[direction > 0 ? 'after' : 'before'] = referenceTid;
infinitescroll.loadMoreXhr(query, callback);
}
diff --git a/src/controllers/activitypub/topics.js b/src/controllers/activitypub/topics.js
index 473dca7a7d..28a015bfe1 100644
--- a/src/controllers/activitypub/topics.js
+++ b/src/controllers/activitypub/topics.js
@@ -78,16 +78,25 @@ controller.list = async function (req, res) {
({ tids, topicCount, topics: topicData } = await topics.getSortedTopics(cidQuery));
/**
- * Use `after` if passed in (only on IS) to update `start`/`stop`, this is useful
+ * Use `after` or `before` cursors (only on IS) to update `start`/`stop`, this is useful
* to prevent loading duplicate posts if the sorted topics have received new topics
* since the set was last loaded.
*/
- if (after) {
- const index = tids.indexOf(utils.isNumber(after) ? parseInt(after, 10) : after);
- if (index && start - index < 1) {
- const count = stop - start;
+ let cursor = after;
+ let direction = 1;
+ if (req.query.before) {
+ cursor = req.query.before;
+ direction = -1;
+ }
+ if (cursor) {
+ const index = tids.indexOf(utils.isNumber(cursor) ? parseInt(cursor, 10) : cursor);
+ const count = stop - start + 1;
+ if (direction === 1 && index >= start) {
start = index + 1;
- stop = start + count;
+ stop = start + count - 1;
+ } else if (direction === -1 && index > 0) {
+ stop = index - 1;
+ start = stop - count + 1;
}
}
tids = tids.slice(start, stop !== -1 ? stop + 1 : undefined);