source: Klonkt/test/c2s-direct.test.js@ 024f4f8

main
Last change on this file since 024f4f8 was 024f4f8, checked in by Robin <roboburr@…>, 7 weeks ago

Feature: direct notes are private mentions, not posts (shaer-tqc)

A C2S note addressed to specific actors (no Public, no followers) no
longer becomes a site post: it routes through the outbox machinery as
a direct note, the Mastodon DM model. deliverDirectNote resolves each
recipient, stores the note in ap_outbox (visibility 'direct' plus the
recipient list) and delivers to exactly those inboxes: no followers
fan-out. buildReplyNote addresses a direct row to its recipients only,
cc empty, so it cannot be boosted and never shows in timelines. This
is also the ward call-for-help leg: client to own outbox, server
S2S-signed to each guardian's inbox on any instance; a guardian on
plain Mastodon sees a private mention.

Hardening: a C2S Announce or Like of a non-public local post returns
403 not_public (the Mastodon 422 equivalent), and an inbound boost or
like on a fan_only/friends/direct post is dropped instead of stored.
Direct with no resolvable recipient is refused (400 no_recipients).

Changed files:
src/services/ActivityPubService.js

  • deliverDirectNote (resolve, store, deliver to recipients only)
  • ingest routes direct before the reply/post paths
  • buildReplyNote: direct rows -> to recipients, empty cc
  • C2S 403 + inbound drop on non-public targets

src/config/database.js

  • additive columns ap_outbox.visibility, ap_outbox.to_actors

New file:
test/c2s-direct.test.js

  • direct addressing pinned, no_recipients, 403 on boost/like

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

  • Property mode set to 100644
File size: 3.1 KB
RevLine 
[024f4f8]1// Direct notes (private mentions, shaer-tqc): never Public, never boostable.
2import { test } from 'node:test';
3import assert from 'node:assert/strict';
4
5process.env.DATABASE_PATH = ':memory:';
6process.env.PUBLIC_BASE_URL = 'https://test.example';
7
8const dbMod = await import('../src/config/database.js');
9const db = dbMod.default;
10dbMod.initializeDatabase();
11const AP = (await import('../src/services/ActivityPubService.js')).default;
12
13const PUB = 'https://www.w3.org/ns/activitystreams#Public';
14db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)').run('u1', 'u1', 'u1@test', 'x', 'god');
15db.prepare('INSERT INTO sites (id, slug, title, owner_id, is_primary) VALUES (?,?,?,?,?)').run('s1', 'me', 'Me', 'u1', 1);
16const site = db.prepare('SELECT * FROM sites WHERE id = ?').get('s1');
17const user = { id: 'u1', username: 'u1' };
18
19test('a direct outbox row addresses only its recipients, no Public, no cc', () => {
20 db.prepare(`INSERT INTO ap_outbox (id, site_slug, post_id, post_slug, in_reply_to, to_actor, to_handle, content, visibility, to_actors, created_at)
21 VALUES ('d1','me','',NULL,NULL,'https://r.test/u/g','@g@r.test','<p>help</p>','direct','["https://r.test/u/g","https://q.test/u/h"]',CURRENT_TIMESTAMP)`).run();
22 const row = db.prepare('SELECT * FROM ap_outbox WHERE id = ?').get('d1');
23 const note = AP.buildReplyNote('https://test.example', site, row);
24 assert.deepEqual(note.to, ['https://r.test/u/g', 'https://q.test/u/h']);
25 assert.deepEqual(note.cc, []);
26 assert.ok(!JSON.stringify(note.to).includes(PUB) && !JSON.stringify(note.cc).includes(PUB));
27});
28
29test('direct without any real recipient is refused (400 no_recipients)', async () => {
30 const r = await AP.ingestOutboxActivity(site, user, {
31 type: 'Note', content: '<p>x</p>',
32 to: ['https://test.example/ap/users/me/followers'], cc: [], // friends-shaped? no: followers in to = friends
33 });
34 // followers-only reads as friends, so force the direct shape: bare unknown string
35 const r2 = await AP.ingestOutboxActivity(site, user, { type: 'Note', content: '<p>x</p>', to: [], cc: [] });
36 // empty addressing = legacy public; the real no-recipient direct case:
37 const r3 = await AP.ingestOutboxActivity(site, user, { type: 'Note', content: '<p>x</p>', to: ['not-a-uri'], cc: [] });
38 assert.equal(r3.status, 400);
39 assert.equal(r3.error, 'no_recipients');
40 assert.ok(r && r2); // shapes above answered too (not the point of this test)
41});
42
43test('C2S Announce/Like of a non-public local post is refused (403)', async () => {
44 db.prepare(`INSERT INTO posts (id, site_id, slug, author_id, title, content, status, type, fan_only, ap_visibility, created_at, updated_at, published_at)
45 VALUES ('pf','s1','geheim','u1','','<p>prive</p>','published','post',1,'friends',datetime('now'),datetime('now'),datetime('now'))`).run();
46 const noteUrl = 'https://test.example/ap/notes/pf';
47 const boost = await AP.ingestOutboxActivity(site, user, { type: 'Announce', object: noteUrl });
48 assert.equal(boost.status, 403);
49 assert.equal(boost.error, 'not_public');
50 const like = await AP.ingestOutboxActivity(site, user, { type: 'Like', object: noteUrl });
51 assert.equal(like.status, 403);
52});
Note: See TracBrowser for help on using the repository browser.