source: Klonkt/test/reply-moderation.test.js@ 4d81de5

main
Last change on this file since 4d81de5 was 67c1f24, checked in by Robin <roboburr@…>, 8 weeks ago

Feature: owner moderation of incoming replies (remove + tombstone + report)

The site owner could not remove an incoming reply, and thread-filling would
re-fetch a locally deleted one (crawlThread seeds its dedup set from
ap_interactions). New ap_rejected_objects table: rejectInteraction() (tenancy-
scoped to the owner's site) deletes the row and tombstones the object URI;
handleInbox and crawlThread skip tombstoned URIs, so a removed reply never
returns via re-delivery or thread-filling. A report action feeds sendReport
from the locally stored object/actor URIs, so flagging also works for private
notes that authorize_interaction cannot fetch (401/404). Thread nodes get
owner-only report + remove buttons (confirm via the global data-confirm
handler); i18n NL/EN/DE. Covered by test/reply-moderation.test.js (59 tests
green); full flow verified in a browser: buttons render for the logged-in
owner, remove deletes the reply and writes the tombstone. Beads: klonkt-demo-qul.

Co-Authored-By: Claude Opus 4.8 <noreply@…>

  • Property mode set to 100644
File size: 3.5 KB
Line 
1// Moderatie van inkomende replies: rejectInteraction verwijdert + tombstonet
2// (zodat ingest/thread-crawl 'm nooit terugbrengen), tenancy-gescoped zodat een
3// andere site andermans replies niet kan verwijderen. Flag-target komt uit de
4// lokale kopie (werkt dus ook voor private notes die niet te fetchen zijn).
5//
6// Run: npm test (= node --test)
7
8import { test } from 'node:test';
9import assert from 'node:assert/strict';
10
11process.env.DATABASE_PATH = ':memory:';
12process.env.PUBLIC_BASE_URL = 'https://klonkt.test';
13
14const dbMod = await import('../src/config/database.js');
15const db = dbMod.default;
16const AP = await import('../src/services/ActivityPubService.js');
17
18dbMod.initializeDatabase();
19
20// ── Seed: twee sites, elk met een post; een reply op post van site A ─────
21db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)')
22 .run('u1', 'u1', 'u1@test', 'x', 'god');
23db.prepare('INSERT INTO sites (id, slug, title, owner_id) VALUES (?,?,?,?)').run('sA', 'sitea', 'A', 'u1');
24db.prepare('INSERT INTO sites (id, slug, title, owner_id) VALUES (?,?,?,?)').run('sB', 'siteb', 'B', 'u1');
25db.prepare(`INSERT INTO posts (id, site_id, slug, author_id, title, content, status, type, created_at, updated_at, published_at)
26 VALUES ('pA','sA','post-a','u1','A','<p>x</p>','published','post',datetime('now'),datetime('now'),datetime('now'))`).run();
27
28function addReply(objectUri) {
29 db.prepare(`INSERT INTO ap_interactions (kind, post_id, object_uri, actor_uri, actor_name, content, visibility)
30 VALUES ('reply','pA',?,?,?,?,'public')`).run(objectUri, 'https://remote.test/users/troll', 'Troll', '<p>weg ermee</p>');
31 return db.prepare('SELECT id FROM ap_interactions WHERE object_uri = ?').get(objectUri).id;
32}
33
34test('rejectInteraction: verwijdert de rij en tombstonet de object_uri', () => {
35 const id = addReply('https://remote.test/n/bad1');
36 const r = AP.rejectInteraction({ slug: 'sitea' }, id, 'test');
37 assert.equal(r.ok, true);
38 assert.equal(db.prepare('SELECT 1 FROM ap_interactions WHERE id = ?').get(id), undefined);
39 assert.equal(AP.isRejectedObject('https://remote.test/n/bad1'), true);
40});
41
42test('tombstone blijft de thread uit houden (getInteractions na herinsert-poging)', () => {
43 // simuleer re-delivery: zelfde object_uri opnieuw inserten zou door de
44 // ingest-guard (isRejectedObject) geweigerd worden; de guard is de check hier
45 assert.equal(AP.isRejectedObject('https://remote.test/n/bad1'), true);
46 // en een nooit-getombstonede URI is vrij
47 assert.equal(AP.isRejectedObject('https://remote.test/n/fresh'), false);
48});
49
50test('tenancy: site B kan een reply op site A NIET verwijderen', () => {
51 const id = addReply('https://remote.test/n/bad2');
52 const r = AP.rejectInteraction({ slug: 'siteb' }, id, 'poging');
53 assert.equal(r.error, 'forbidden');
54 assert.ok(db.prepare('SELECT 1 FROM ap_interactions WHERE id = ?').get(id), 'reply mag niet verdwenen zijn');
55 assert.equal(AP.isRejectedObject('https://remote.test/n/bad2'), false);
56});
57
58test('interactionReportTarget: geeft lokale URIs (site A), null voor site B', () => {
59 const id = addReply('https://remote.test/n/bad3');
60 const tgt = AP.interactionReportTarget({ slug: 'sitea' }, id);
61 assert.equal(tgt.objectUri, 'https://remote.test/n/bad3');
62 assert.equal(tgt.actorUri, 'https://remote.test/users/troll');
63 assert.equal(AP.interactionReportTarget({ slug: 'siteb' }, id), null);
64});
65
66test('onbestaande interactie = not_found', () => {
67 assert.equal(AP.rejectInteraction({ slug: 'sitea' }, 999999, 'x').error, 'not_found');
68});
Note: See TracBrowser for help on using the repository browser.