| 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 |
|
|---|
| 8 | import { test } from 'node:test';
|
|---|
| 9 | import assert from 'node:assert/strict';
|
|---|
| 10 |
|
|---|
| 11 | process.env.DATABASE_PATH = ':memory:';
|
|---|
| 12 | process.env.PUBLIC_BASE_URL = 'https://klonkt.test';
|
|---|
| 13 |
|
|---|
| 14 | const dbMod = await import('../src/config/database.js');
|
|---|
| 15 | const db = dbMod.default;
|
|---|
| 16 | const AP = await import('../src/services/ActivityPubService.js');
|
|---|
| 17 |
|
|---|
| 18 | dbMod.initializeDatabase();
|
|---|
| 19 |
|
|---|
| 20 | // ── Seed: twee sites, elk met een post; een reply op post van site A ─────
|
|---|
| 21 | db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)')
|
|---|
| 22 | .run('u1', 'u1', 'u1@test', 'x', 'god');
|
|---|
| 23 | db.prepare('INSERT INTO sites (id, slug, title, owner_id) VALUES (?,?,?,?)').run('sA', 'sitea', 'A', 'u1');
|
|---|
| 24 | db.prepare('INSERT INTO sites (id, slug, title, owner_id) VALUES (?,?,?,?)').run('sB', 'siteb', 'B', 'u1');
|
|---|
| 25 | db.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 |
|
|---|
| 28 | function 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 |
|
|---|
| 34 | test('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 |
|
|---|
| 42 | test('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 |
|
|---|
| 50 | test('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 |
|
|---|
| 58 | test('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 |
|
|---|
| 66 | test('onbestaande interactie = not_found', () => {
|
|---|
| 67 | assert.equal(AP.rejectInteraction({ slug: 'sitea' }, 999999, 'x').error, 'not_found');
|
|---|
| 68 | });
|
|---|