From 804052f272f709c718c2f1e6b402e666b46af6bd Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 17 Mar 2025 12:02:43 -0400 Subject: [PATCH] test: add tests for topics slotting into remote categories if addressed --- src/categories/topics.js | 3 ++- src/topics/create.js | 2 +- test/activitypub/notes.js | 47 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/categories/topics.js b/src/categories/topics.js index 64e9046614..ebb3136360 100644 --- a/src/categories/topics.js +++ b/src/categories/topics.js @@ -9,6 +9,7 @@ const user = require('../user'); const notifications = require('../notifications'); const translator = require('../translator'); const batch = require('../batch'); +const utils = require('../utils'); module.exports = function (Categories) { Categories.getCategoryTopics = async function (data) { @@ -186,7 +187,7 @@ module.exports = function (Categories) { } const promises = [ db.sortedSetAdd(`cid:${cid}:pids`, postData.timestamp, postData.pid), - db.incrObjectField(`category:${cid}`, 'post_count'), + db.incrObjectField(`${utils.isNumber(cid) ? 'category' : 'categoryRemote'}:${cid}`, 'post_count'), ]; if (!pinned) { promises.push(db.sortedSetIncrBy(`cid:${cid}:tids:posts`, 1, postData.tid)); diff --git a/src/topics/create.js b/src/topics/create.js index e6d97af77f..352823a202 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -67,7 +67,7 @@ module.exports = function (Topics) { db.sortedSetsAdd(timestampedSortedSetKeys, timestamp, topicData.tid), db.sortedSetsAdd(countedSortedSetKeys, 0, topicData.tid), user.addTopicIdToUser(topicData.uid, topicData.tid, timestamp), - db.incrObjectField(`category:${topicData.cid}`, 'topic_count'), + db.incrObjectField(`${utils.isNumber(topicData.cid) ? 'category' : 'categoryRemote'}:${topicData.cid}`, 'topic_count'), utils.isNumber(tid) ? db.incrObjectField('global', 'topicCount') : null, Topics.createTags(data.tags, topicData.tid, timestamp), scheduled ? Promise.resolve() : categories.updateRecentTid(topicData.cid, topicData.tid), diff --git a/test/activitypub/notes.js b/test/activitypub/notes.js index c9f83372f8..a2ba213940 100644 --- a/test/activitypub/notes.js +++ b/test/activitypub/notes.js @@ -22,7 +22,7 @@ describe('Notes', () => { await install.giveWorldPrivileges(); }); - describe('Public objects', () => { + describe.only('Public objects', () => { it('should pull a remote root-level object by its id and create a new topic', async () => { const { id } = helpers.mocks.note(); const assertion = await activitypub.notes.assert(0, id, { skipChecks: true }); @@ -63,6 +63,51 @@ describe('Notes', () => { const exists = await topics.exists(tid); assert(exists); }); + + it('should slot newly created topic in local category if addressed', async () => { + const { cid } = await categories.create({ name: utils.generateUUID() }); + const { id } = helpers.mocks.note({ + cc: ['https://example.org/user/foobar/followers', `${nconf.get('url')}/category/${cid}`], + }); + + const assertion = await activitypub.notes.assert(0, id); + assert(assertion); + + const { tid, count } = assertion; + assert(tid); + assert.strictEqual(count, 1); + + const topic = await topics.getTopicData(tid); + assert.strictEqual(topic.cid, cid); + }); + + it('should slot newly created topic in remote category if addressed', async () => { + const { id: cid, actor } = helpers.mocks.group(); + activitypub._cache.set(`0;${cid}`, actor); + await activitypub.actors.assertGroup([cid]); + + const { id } = helpers.mocks.note({ + cc: ['https://example.org/user/foobar/followers', cid], + }); + + const assertion = await activitypub.notes.assert(0, id); + assert(assertion); + + const { tid, count } = assertion; + assert(tid); + assert.strictEqual(count, 1); + + const topic = await topics.getTopicData(tid); + assert.strictEqual(topic.cid, cid); + + const tids = await db.getSortedSetMembers(`cid:${cid}:tids`); + assert(tids.includes(tid)); + + const category = await categories.getCategoryData(cid); + ['topic_count', 'post_count', 'totalPostCount', 'totalTopicCount'].forEach((prop) => { + assert.strictEqual(category[prop], 1); + }); + }); }); describe('Private objects', () => {