Skip to content

Node.js based forum software built for the modern web

fix(activitypub): correct getOutbox pagination condition and partOf URL

Julian Lam3a0797a80670
diff --git a/src/controllers/activitypub/index.js b/src/controllers/activitypub/index.js
index b101692b50..cc387b1bef 100644
--- a/src/controllers/activitypub/index.js
+++ b/src/controllers/activitypub/index.js
@@ -138,78 +138,76 @@ Controller.getOutbox = async (req, res) => {
let prev;
let next;
- const partOf = paginate && (after || before) && `${nconf.get('url')}/uid/${uid}/outbox`;
+ const partOf = paginate && `${nconf.get('url')}/uid/${uid}/outbox`;
const first = paginate && !after && !before && `${nconf.get('url')}/uid/${uid}/outbox?after=${Date.now()}`;
const last = paginate && !after && !before && `${nconf.get('url')}/uid/${uid}/outbox?before=0`;
let activities;
+ let post, upvote, downvote, share;
+
+ if (after || before) {
+ const limit = after ? parseInt(after, 10) - 1 : parseInt(before, 10) + 1;
+ const method = after ? 'getSortedSetRevRangeByScoreWithScores' : 'getSortedSetRangeByScoreWithScores';
+
+ [post, upvote, downvote, share] = await Promise.all([
+ db[method](`uid:${uid}:posts`, 0, 20, limit, `${after ? '-' : '+'}inf`),
+ db[method](`uid:${uid}:upvote`, 0, 20, limit, `${after ? '-' : '+'}inf`),
+ db[method](`uid:${uid}:downvote`, 0, 20, limit, `${after ? '-' : '+'}inf`),
+ db[method](`uid:${uid}:shares`, 0, 20, limit, `${after ? '-' : '+'}inf`),
+ ]);
+ } else {
+ // Simple range query for first page (no pagination params)
+ [post, upvote, downvote, share] = await Promise.all([
+ db.getSortedSetRangeWithScores(`uid:${uid}:posts`, 0, 19),
+ db.getSortedSetRangeWithScores(`uid:${uid}:upvote`, 0, 19),
+ db.getSortedSetRangeWithScores(`uid:${uid}:downvote`, 0, 19),
+ db.getSortedSetRangeWithScores(`uid:${uid}:shares`, 0, 19),
+ ]);
+ }
- if (!paginate || after || before || !activities) {
- let post, upvote, downvote, share;
-
- if (!paginate) {
- // Simple range query for first page (no pagination params)
- [post, upvote, downvote, share] = await Promise.all([
- db.getSortedSetRangeWithScores(`uid:${uid}:posts`, 0, 19),
- db.getSortedSetRangeWithScores(`uid:${uid}:upvote`, 0, 19),
- db.getSortedSetRangeWithScores(`uid:${uid}:downvote`, 0, 19),
- db.getSortedSetRangeWithScores(`uid:${uid}:shares`, 0, 19),
- ]);
- } else {
- const limit = after ? parseInt(after, 10) - 1 : parseInt(before, 10) + 1;
- const method = after ? 'getSortedSetRevRangeByScoreWithScores' : 'getSortedSetRangeByScoreWithScores';
-
- [post, upvote, downvote, share] = await Promise.all([
- db[method](`uid:${uid}:posts`, 0, 20, limit, `${after ? '-' : '+'}inf`),
- db[method](`uid:${uid}:upvote`, 0, 20, limit, `${after ? '-' : '+'}inf`),
- db[method](`uid:${uid}:downvote`, 0, 20, limit, `${after ? '-' : '+'}inf`),
- db[method](`uid:${uid}:shares`, 0, 20, limit, `${after ? '-' : '+'}inf`),
- ]);
- }
- activities = [
- post.map(post => ({ ...post, type: 'post' })),
- upvote.map(upvote => ({ ...upvote, type: 'upvote' })),
- downvote.map(downvote => ({ ...downvote, type: 'downvote' })),
- share.map(share => ({ ...share, type: 'share' })),
- ].flat().sort((a, b) => b.score - a.score);
- if (!paginate || before) {
- activities = activities.slice(-20);
- } else {
- activities = activities.slice(0, 20);
- }
+ activities = [
+ post.map(post => ({ ...post, type: 'post' })),
+ upvote.map(upvote => ({ ...upvote, type: 'upvote' })),
+ downvote.map(downvote => ({ ...downvote, type: 'downvote' })),
+ share.map(share => ({ ...share, type: 'share' })),
+ ].flat().sort((a, b) => b.score - a.score);
+ if (!paginate || before) {
+ activities = activities.slice(-20);
+ } else {
+ activities = activities.slice(0, 20);
+ }
- if (activities.length) {
- prev = `${nconf.get('url')}/uid/${uid}/outbox?before=${activities[0].score}`;
- next = `${nconf.get('url')}/uid/${uid}/outbox?after=${activities[activities.length - 1].score}`;
-
- let postsData = activities.filter((({ type }) => type === 'post'));
- postsData = await posts.getPostSummaryByPids(postsData.map(({ value }) => value), 0, { stripTags: false });
- postsData = postsData.reduce((map, postData) => {
- map.set(postData.pid, postData);
- return map;
- }, new Map());
-
- activities = await Promise.all(activities.map(async ({ type, value: id }) => {
- switch (type) {
- case 'post': {
- const { activity } = await activitypub.mocks.activities.create(id, 0, postsData.get(id));
- return activity;
- }
-
- case 'upvote': {
- return activitypub.mocks.activities.like(id, uid);
- }
-
- case 'downvote': {
- return activitypub.mocks.activities.dislike(id, uid);
- }
-
- case 'share': {
- const { activity } = await activitypub.mocks.activities.announce(id, uid);
- return activity;
- }
+ if (activities.length) {
+ prev = `${nconf.get('url')}/uid/${uid}/outbox?before=${activities[0].score}`;
+ next = `${nconf.get('url')}/uid/${uid}/outbox?after=${activities[activities.length - 1].score}`;
+
+ let postsData = activities.filter((({ type }) => type === 'post'));
+ postsData = await posts.getPostSummaryByPids(postsData.map(({ value }) => value), 0, { stripTags: false });
+ postsData = postsData.reduce((map, postData) => {
+ map.set(postData.pid, postData);
+ return map;
+ }, new Map());
+
+ activities = await Promise.all(activities.map(async ({ type, value: id }) => {
+ switch (type) {
+ case 'post': {
+ const { activity } = await activitypub.mocks.activities.create(id, 0, postsData.get(id));
+ return activity;
}
- }));
- }
+
+ case 'upvote': {
+ return activitypub.mocks.activities.like(id, uid);
+ }
+
+ case 'downvote': {
+ return activitypub.mocks.activities.dislike(id, uid);
+ }
+
+ case 'share': {
+ const { activity } = await activitypub.mocks.activities.announce(id, uid);
+ return activity;
+ }
+ }
+ }));
}
res.status(200).json({
diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js
index f28fa4a40c..4c50d1bed5 100644
--- a/src/controllers/helpers.js
+++ b/src/controllers/helpers.js
@@ -485,7 +485,6 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {
return res.sendStatus(statusCode);
}
- console.log(statusCode, payload);
if (String(statusCode).startsWith('2')) {
if (res.req.loggedIn) {
res.set('cache-control', 'private');
diff --git a/test/activitypub/notes.js b/test/activitypub/notes.js
index 6e89bd6ad9..1c169f17de 100644
--- a/test/activitypub/notes.js
+++ b/test/activitypub/notes.js
@@ -630,7 +630,6 @@ describe('Notes', () => {
afterEach(function () {
helpers.mocks._baseUrl = this._baseUrl;
- console.log('setting back to ', this._baseUrl);
});
it('should NOT queue posts with severity 1 (suspend)', async () => {
diff --git a/test/activitypub/out.js b/test/activitypub/out.js
index f18f351eba..48da334216 100644
--- a/test/activitypub/out.js
+++ b/test/activitypub/out.js
@@ -56,7 +56,6 @@ describe('Outbound activities module', () => {
describe('.topic() (remote topic; by cid)', () => {
before(async function () {
const { id: pid, note } = helpers.mocks.note();
- console.log(pid, note);
const { cid } = await categories.create({ name: utils.generateUUID() });
await activitypub.notes.assert(0, pid, { skipChecks: 1, cid });