Skip to content

Node.js based forum software built for the modern web

feat(activitypub): add global age cutoff for incoming post categorization

Julian Lamd8c7486d2fda
diff --git a/install/data/defaults.json b/install/data/defaults.json
index 708801c2be..3ceae5b58c 100644
--- a/install/data/defaults.json
+++ b/install/data/defaults.json
@@ -215,5 +215,6 @@
"activitypubFilter": 0,
"activitypubSummaryLimit": 500,
"activitypubBreakString": "[...]",
- "activitypubWorldDefaultCid": -1
+ "activitypubWorldDefaultCid": -1,
+ "activitypubRulesCutoffDays": 30
}
diff --git a/public/language/en-GB/admin/settings/activitypub.json b/public/language/en-GB/admin/settings/activitypub.json
index b708f5642b..d03cd84f2c 100644
--- a/public/language/en-GB/admin/settings/activitypub.json
+++ b/public/language/en-GB/admin/settings/activitypub.json
@@ -35,6 +35,9 @@
"rules.filter.help": "If checked, matching content will be passed to the post queue for review before being categorized.",
"rules.filter-warning": "You have categorization rules set to queue posts, but the post queue is currently disabled. Posts matching these rules will be published immediately.",
"rules.cid": "Category",
+ "rules.cutoff.title": "Age Cutoff",
+ "rules.cutoff.label": "Categorization age cutoff (days)",
+ "rules.cutoff.help": "Content older than this number of days will not be categorized via rules. Set to 0 to disable the cutoff. (Default: 0)",
"relays": "Relays",
"relays.intro": "A relay improves discovery of content to and from your NodeBB. Subscribing to a relay means content received by the relay is forwarded here, and content posted here is syndicated outward by the relay.",
diff --git a/src/activitypub/notes.js b/src/activitypub/notes.js
index 6999da2b2a..6b608d0834 100644
--- a/src/activitypub/notes.js
+++ b/src/activitypub/notes.js
@@ -574,6 +574,15 @@ async function assertRelation(post) {
}
async function assignCategory(post) {
+ const ageLimitDays = meta.config.activitypubRulesCutoffDays || 0;
+ if (ageLimitDays > 0 && post.timestamp) {
+ const ageDays = (Date.now() - post.timestamp) / (1000 * 60 * 60 * 24);
+ if (ageDays > ageLimitDays) {
+ activitypub.helpers.log(`[activitypub] Post ${post.pid} is ${ageDays.toFixed(1)} days old, skipping categorization (limit: ${ageLimitDays} days)`);
+ return { cid: undefined, filter: false };
+ }
+ }
+
activitypub.helpers.log('[activitypub] Checking auto-categorization rules.');
const rules = await activitypub.rules.list();
let tags = await Notes._normalizeTags(post._activitypub.tag || []);
diff --git a/src/controllers/admin/federation.js b/src/controllers/admin/federation.js
index 1ccabe6b7c..d455bded8e 100644
--- a/src/controllers/admin/federation.js
+++ b/src/controllers/admin/federation.js
@@ -29,7 +29,6 @@ federationController.rules = async function (req, res) {
rules,
postQueueEnabled,
hasFilterRules,
- hideSave: true,
});
};
diff --git a/src/views/admin/federation/rules.tpl b/src/views/admin/federation/rules.tpl
index 9138fb560d..7d47ad39c3 100644
--- a/src/views/admin/federation/rules.tpl
+++ b/src/views/admin/federation/rules.tpl
@@ -10,6 +10,19 @@
<div class="alert alert-warning">[[admin/settings/activitypub:rules.filter-warning]]</div>
{{{ end }}}
+ <div id="cutoff" class="mb-4">
+ <h5 class="fw-bold tracking-tight settings-header">[[admin/settings/activitypub:rules.cutoff.title]]</h5>
+ <form>
+ <div class="mb-3">
+ <label class="form-label" for="activitypubRulesCutoffDays">[[admin/settings/activitypub:rules.cutoff.label]]</label>
+ <input type="number" id="activitypubRulesCutoffDays" name="activitypubRulesCutoffDays" data-field="activitypubRulesCutoffDays" title="[[admin/settings/activitypub:rules.cutoff.label]]" class="form-control" value="{activitypubRulesCutoffDays}" min="0" />
+ <div class="form-text">
+ [[admin/settings/activitypub:rules.cutoff.help]]
+ </div>
+ </div>
+ </form>
+ </div>
+
<div class="mb-3 table-responsive-md">
<table class="table table-striped" id="rules">
<thead>
diff --git a/test/activitypub/notes.js b/test/activitypub/notes.js
index 7484317681..5e5a8e33bf 100644
--- a/test/activitypub/notes.js
+++ b/test/activitypub/notes.js
@@ -821,4 +821,204 @@ describe('Notes', () => {
assert.strictEqual(parsedData.tid, assertion.tid);
});
});
+
+ describe('auto-categorization age cutoff', () => {
+ let remoteCid;
+ let targetCid;
+ let rid;
+ const tagName = utils.generateUUID().slice(0, 8);
+
+ before(async () => {
+ // Create a remote group actor
+ ({ id: remoteCid } = helpers.mocks.group());
+ // Create a local target category
+ ({ cid: targetCid } = await categories.create({ name: utils.generateUUID().slice(0, 8) }));
+ // Add a hashtag-type auto-categorization rule with filter (queue=true)
+ rid = await activitypub.rules.upsert('hashtag', tagName, targetCid, true);
+ meta.config.postQueue = 1;
+ });
+
+ after(async () => {
+ delete meta.config.postQueue;
+ delete meta.config.activitypubRulesCutoffDays;
+ if (rid) {
+ await activitypub.rules.delete(rid);
+ }
+ });
+
+ beforeEach(async () => {
+ // Clear the queue
+ const queuedIds = await db.getSortedSetMembers('post:queue');
+ await Promise.all(queuedIds.map(async (id) => {
+ await db.delete(`post:queue:${id}`);
+ }));
+ await db.delete('post:queue');
+ });
+
+ describe('cutoff disabled (0)', () => {
+ beforeEach(() => {
+ meta.config.activitypubRulesCutoffDays = 0;
+ });
+
+ afterEach(() => {
+ delete meta.config.activitypubRulesCutoffDays;
+ });
+
+ it('should queue crosspost for old posts when cutoff is 0', async () => {
+ // Create a very old post (365 days ago)
+ const publishedDate = new Date(Date.now() - (365 * 24 * 60 * 60 * 1000)).toISOString();
+ const { id: noteId } = helpers.mocks.note({
+ audience: [remoteCid],
+ published: publishedDate,
+ tag: [
+ { type: 'Hashtag', name: `#${tagName}` },
+ ],
+ });
+ const assertion = await activitypub.notes.assert(0, noteId, {
+ skipChecks: true,
+ });
+
+ assert(assertion);
+ assert(assertion.tid, 'Topic should be created');
+ assert.strictEqual(assertion.queued, 0, 'Topic should not be queued');
+
+ // Verify crosspost was queued despite post age
+ const queueIds = await db.getSortedSetMembers('post:queue');
+ assert.strictEqual(queueIds.length, 1, 'Crosspost should be queued');
+
+ const queueData = await db.getObject(`post:queue:${queueIds[0]}`);
+ assert.strictEqual(queueData.type, 'crosspost');
+ const parsedData = typeof queueData.data === 'string' ? JSON.parse(queueData.data) : queueData.data;
+ assert.strictEqual(parseInt(parsedData.crosspostCid, 10), targetCid);
+ });
+ });
+
+ describe('cutoff enabled', () => {
+ const cutoffDays = 30;
+
+ beforeEach(() => {
+ meta.config.activitypubRulesCutoffDays = cutoffDays;
+ });
+
+ afterEach(() => {
+ delete meta.config.activitypubRulesCutoffDays;
+ });
+
+ it('should queue crosspost for recent posts when within cutoff', async () => {
+ // Create a recent post (1 day old)
+ const publishedDate = new Date(Date.now() - (1 * 24 * 60 * 60 * 1000)).toISOString();
+ const { id: noteId } = helpers.mocks.note({
+ audience: [remoteCid],
+ published: publishedDate,
+ tag: [
+ { type: 'Hashtag', name: `#${tagName}` },
+ ],
+ });
+ const assertion = await activitypub.notes.assert(0, noteId, {
+ skipChecks: true,
+ });
+
+ assert(assertion);
+ assert(assertion.tid, 'Topic should be created');
+ assert.strictEqual(assertion.queued, 0, 'Topic should not be queued');
+
+ // Verify crosspost was queued
+ const queueIds = await db.getSortedSetMembers('post:queue');
+ assert.strictEqual(queueIds.length, 1, 'Crosspost should be queued');
+
+ const queueData = await db.getObject(`post:queue:${queueIds[0]}`);
+ assert.strictEqual(queueData.type, 'crosspost');
+ const parsedData = typeof queueData.data === 'string' ? JSON.parse(queueData.data) : queueData.data;
+ assert.strictEqual(parseInt(parsedData.crosspostCid, 10), targetCid);
+ });
+
+ it('should queue crosspost for posts at the cutoff boundary', async () => {
+ // Create a post just under the cutoff (29 days old, leaving room for test timing)
+ const publishedDate = new Date(Date.now() - ((cutoffDays - 1) * 24 * 60 * 60 * 1000)).toISOString();
+ const { id: noteId } = helpers.mocks.note({
+ audience: [remoteCid],
+ published: publishedDate,
+ tag: [
+ { type: 'Hashtag', name: `#${tagName}` },
+ ],
+ });
+ const assertion = await activitypub.notes.assert(0, noteId, {
+ skipChecks: true,
+ });
+
+ assert(assertion);
+ assert(assertion.tid, 'Topic should be created');
+
+ // Verify crosspost was queued
+ const queueIds = await db.getSortedSetMembers('post:queue');
+ assert.strictEqual(queueIds.length, 1, 'Crosspost should be queued');
+ });
+
+ it('should NOT queue crosspost for posts older than cutoff', async () => {
+ // Create a post older than cutoff (31 days old)
+ const publishedDate = new Date(Date.now() - ((cutoffDays + 1) * 24 * 60 * 60 * 1000)).toISOString();
+ const { id: noteId } = helpers.mocks.note({
+ audience: [remoteCid],
+ published: publishedDate,
+ tag: [
+ { type: 'Hashtag', name: `#${tagName}` },
+ ],
+ });
+ const assertion = await activitypub.notes.assert(0, noteId, {
+ skipChecks: true,
+ });
+
+ assert(assertion);
+ assert(assertion.tid, 'Topic should still be created');
+ assert.strictEqual(assertion.queued, 0, 'Topic should not be queued');
+
+ // Verify no crosspost was queued
+ const queueIds = await db.getSortedSetMembers('post:queue');
+ assert.strictEqual(queueIds.length, 0, 'No crosspost should be queued');
+ });
+
+ it('should NOT queue crosspost for very old posts (60 days)', async () => {
+ // Create a post older than cutoff (60 days old)
+ const publishedDate = new Date(Date.now() - (60 * 24 * 60 * 60 * 1000)).toISOString();
+ const { id: noteId } = helpers.mocks.note({
+ audience: [remoteCid],
+ published: publishedDate,
+ tag: [
+ { type: 'Hashtag', name: `#${tagName}` },
+ ],
+ });
+ const assertion = await activitypub.notes.assert(0, noteId, {
+ skipChecks: true,
+ });
+
+ assert(assertion);
+
+ // Verify no crosspost was queued
+ const queueIds = await db.getSortedSetMembers('post:queue');
+ assert.strictEqual(queueIds.length, 0, 'No crosspost should be queued');
+ });
+
+ it('should NOT queue crosspost for very old posts (1 year)', async () => {
+ // Create a very old post (365 days old)
+ const publishedDate = new Date(Date.now() - (365 * 24 * 60 * 60 * 1000)).toISOString();
+ const { id: noteId } = helpers.mocks.note({
+ audience: [remoteCid],
+ published: publishedDate,
+ tag: [
+ { type: 'Hashtag', name: `#${tagName}` },
+ ],
+ });
+ const assertion = await activitypub.notes.assert(0, noteId, {
+ skipChecks: true,
+ });
+
+ assert(assertion);
+ assert(assertion.tid, 'Topic should still be created');
+
+ // Verify no crosspost was queued
+ const queueIds = await db.getSortedSetMembers('post:queue');
+ assert.strictEqual(queueIds.length, 0, 'No crosspost should be queued');
+ });
+ });
+ });
});