| 1 | // Inline @mentions in posts — buildNote turns already-linked @user@host mentions into AS2
|
|---|
| 2 | // Mention tags and addresses the mentioned actor in cc (so Mastodon notifies them). The
|
|---|
| 3 | // WebFinger resolution/linking happens at delivery time; here we cover the sync build step.
|
|---|
| 4 | import { test } from 'node:test';
|
|---|
| 5 | import assert from 'node:assert/strict';
|
|---|
| 6 |
|
|---|
| 7 | process.env.DATABASE_PATH = ':memory:';
|
|---|
| 8 | process.env.PUBLIC_BASE_URL = 'https://test.example';
|
|---|
| 9 |
|
|---|
| 10 | const dbMod = await import('../src/config/database.js');
|
|---|
| 11 | const db = dbMod.default;
|
|---|
| 12 | dbMod.initializeDatabase();
|
|---|
| 13 | const AP = (await import('../src/services/ActivityPubService.js')).default;
|
|---|
| 14 |
|
|---|
| 15 | const BASE = 'https://test.example';
|
|---|
| 16 | db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)').run('u1', 'u1', 'u1@test', 'x', 'god');
|
|---|
| 17 | db.prepare('INSERT INTO sites (id, slug, title, owner_id, is_primary) VALUES (?,?,?,?,?)').run('s1', 'demo', 'Demo', 'u1', 1);
|
|---|
| 18 | const site = db.prepare('SELECT * FROM sites WHERE id = ?').get('s1');
|
|---|
| 19 | site.primary_slug = 'demo';
|
|---|
| 20 |
|
|---|
| 21 | // A post whose content already carries a resolved mention link (as deliverCreate produces).
|
|---|
| 22 | const linked = '<p>hi <a href="https://mastodon.social/@bob" class="u-url mention" data-actor="https://mastodon.social/users/bob">@bob@mastodon.social</a></p>';
|
|---|
| 23 | const post = { id: 'm1', slug: 'x', title: '', content: linked, tags: '[]', created_at: '2026-01-01T00:00:00Z' };
|
|---|
| 24 |
|
|---|
| 25 | test('a mention becomes an AS2 Mention tag with the actor URI as href', () => {
|
|---|
| 26 | const note = AP.buildNote(BASE, site, post);
|
|---|
| 27 | const m = note.tag.find((t) => t.type === 'Mention');
|
|---|
| 28 | assert.ok(m, 'note.tag has a Mention');
|
|---|
| 29 | assert.equal(m.href, 'https://mastodon.social/users/bob');
|
|---|
| 30 | assert.equal(m.name, '@bob@mastodon.social');
|
|---|
| 31 | });
|
|---|
| 32 |
|
|---|
| 33 | test('the mentioned actor is addressed in cc', () => {
|
|---|
| 34 | const note = AP.buildNote(BASE, site, post);
|
|---|
| 35 | assert.ok(note.cc.includes('https://mastodon.social/users/bob'), 'mentioned actor in cc');
|
|---|
| 36 | assert.ok(note.cc.includes(`${BASE}/ap/users/demo/followers`), 'followers still in cc');
|
|---|
| 37 | });
|
|---|
| 38 |
|
|---|
| 39 | test('a post without mentions has no Mention tag and an unchanged cc', () => {
|
|---|
| 40 | const plain = { id: 'm2', slug: 'y', title: '', content: '<p>just text</p>', tags: '[]', created_at: '2026-01-01T00:00:00Z' };
|
|---|
| 41 | const note = AP.buildNote(BASE, site, plain);
|
|---|
| 42 | assert.equal(note.tag.filter((t) => t.type === 'Mention').length, 0);
|
|---|
| 43 | assert.deepEqual(note.cc, [`${BASE}/ap/users/demo/followers`]);
|
|---|
| 44 | });
|
|---|