source: Klonkt/src/config/database.js@ acbc9fc

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

Eigenaarspoort: volgers eerst goedkeuren, op z'n fediverse (Robins wens, 18-8)

Een site met approve_followers aan accepteert een Follow niet meer
automatisch: de actor adverteert manuallyApprovesFollowers, het verzoek
wacht in de bestaande wachtrij (ap_pending_follows, quorum 'owner') en
de eigenaar beslist op /connect — accepteren stuurt de Accept en de
backfill, weigeren een Reject. Zo kan niemand een klonkt ongevraagd aan
een hub of ander verzamelplatform hangen. Wards blijven bij hun
guardianpoort: die gaat vóór, en de eigenaarsroute weigert daar hard.
Instelbaar per site in het sitebeheer, teksten in NL/EN/DE.

Co-Authored-By: Claude Fable 5 <noreply@…>

  • Property mode set to 100644
File size: 59.2 KB
Line 
1import Database from 'better-sqlite3';
2import path from 'path';
3import { fileURLToPath } from 'url';
4import fs from 'fs';
5
6const __dirname = path.dirname(fileURLToPath(import.meta.url));
7const dbPath = process.env.DATABASE_PATH || path.join(__dirname, '../../storage/database.sqlite');
8
9// Ensure storage directory exists
10const storageDir = path.dirname(dbPath);
11if (!fs.existsSync(storageDir)) {
12 fs.mkdirSync(storageDir, { recursive: true });
13}
14
15// Initialize database
16const db = new Database(dbPath);
17db.pragma('journal_mode = WAL');
18db.pragma('foreign_keys = ON');
19// With WAL + several concurrent writers (request handlers, the delivery worker, the
20// background thread-crawler) a short write-lock should retry rather than throw SQLITE_BUSY.
21db.pragma('busy_timeout = 5000'); // wait up to 5s for a lock instead of failing immediately
22db.pragma('synchronous = NORMAL'); // safe with WAL (no torn writes); fewer fsyncs = faster writes
23
24export function initializeDatabase() {
25 const tableExists = db.prepare(`
26 SELECT name FROM sqlite_master WHERE type='table' AND name='users'
27 `).get();
28
29 if (!tableExists) {
30 console.log('🔧 Initializing database schema...');
31 const schemaPath = path.join(__dirname, '..', 'db', 'migrations', '001-init.sql');
32 const schema = fs.readFileSync(schemaPath, 'utf-8');
33 db.exec(schema);
34 console.log('✅ Database initialized with v9-soul schema');
35 }
36
37 // Additive column migrations — safe to run every boot.
38 // SQLite throws if the column already exists; we swallow that.
39 ensureColumn('sites', 'enable_audio_player', 'INTEGER DEFAULT 1');
40 // Eigenaarspoort (Robins wens, 18-8-2026): volgers niet automatisch
41 // accepteren maar door de eigenaar laten beslissen, op z'n fediverse.
42 ensureColumn('sites', 'approve_followers', 'INTEGER DEFAULT 0');
43 // (Verwijderd 31-7-2026: sites.guardian_only en ap_guardian_invites hoorden
44 // bij de guardian-lite accounts. Bestaande installaties houden kolom en tabel
45 // ongebruikt; nieuwe krijgen ze niet meer.)
46 // FEP-633c §5.3: follows targeting a ward are held pending until its
47 // guardians approve (Guardian 2). Gating applies only to ward-actors.
48 db.exec(`CREATE TABLE IF NOT EXISTS ap_pending_follows (
49 id TEXT PRIMARY KEY,
50 ward_slug TEXT NOT NULL,
51 follower_uri TEXT NOT NULL,
52 follower_inbox TEXT,
53 follower_shared_inbox TEXT,
54 follower_name TEXT,
55 follower_handle TEXT,
56 follower_icon TEXT,
57 activity_json TEXT,
58 quorum TEXT DEFAULT 'any',
59 status TEXT DEFAULT 'pending',
60 created_at TEXT DEFAULT CURRENT_TIMESTAMP
61 )`);
62 db.exec(`CREATE TABLE IF NOT EXISTS ap_pending_follow_approvals (
63 follow_id TEXT NOT NULL,
64 guardian_uri TEXT NOT NULL,
65 decision TEXT NOT NULL,
66 created_at TEXT DEFAULT CURRENT_TIMESTAMP,
67 PRIMARY KEY (follow_id, guardian_uri)
68 )`);
69 // FEP-633c §5.3, the OTHER direction (shaer-p729): a ward's own follow is
70 // held until its guardians approve. Deliberately not ap_pending_follows —
71 // that table is keyed with the ward as the TARGET ("who wants to follow me"),
72 // and adding a direction column would make every existing query ambiguous.
73 db.exec(`CREATE TABLE IF NOT EXISTS ap_pending_outgoing_follows (
74 id TEXT PRIMARY KEY,
75 ward_slug TEXT NOT NULL,
76 target_uri TEXT NOT NULL,
77 target_inbox TEXT,
78 target_name TEXT,
79 target_handle TEXT,
80 target_icon TEXT,
81 quorum TEXT DEFAULT 'any',
82 status TEXT DEFAULT 'pending',
83 created_at TEXT DEFAULT CURRENT_TIMESTAMP
84 )`);
85 db.exec(`CREATE UNIQUE INDEX IF NOT EXISTS idx_ap_outgoing_follows_target
86 ON ap_pending_outgoing_follows(ward_slug, target_uri)`);
87 // Wat er gebeurd is, en waarom (shaer-p729, §4.2). Guardianship-events waren
88 // vluchtig: onGuardianshipEvent wekte de long-poll en stuurde eventueel een
89 // push, en de rest van de gebeurtenis loste op. Een weigering droeg
90 // `reason: 'not_a_teapot'` tot in die functie en verder niet -- de ward en
91 // zijn guardians hoorden het alleen doordat het aanbod uit de wachtrij
92 // verdween. §4.2 eist dat ze het TE HOREN krijgen, met de reden erbij.
93 //
94 // Een logboek, geen wachtrij: hier staat niets dat om een antwoord vraagt.
95 // Daarom hoort het ook ingeklapt te staan -- naast wat nog wel wacht, maakt
96 // afgelopen nieuws de open vraag onleesbaar.
97 db.exec(`CREATE TABLE IF NOT EXISTS ap_guardian_events (
98 id INTEGER PRIMARY KEY AUTOINCREMENT,
99 slug TEXT NOT NULL,
100 kind TEXT NOT NULL,
101 payload TEXT,
102 created_at TEXT DEFAULT CURRENT_TIMESTAMP
103 )`);
104 db.exec('CREATE INDEX IF NOT EXISTS idx_ap_guardian_events_slug ON ap_guardian_events(slug, id DESC)');
105 db.exec(`CREATE TABLE IF NOT EXISTS ap_outgoing_follow_approvals (
106 follow_id TEXT NOT NULL,
107 guardian_uri TEXT NOT NULL,
108 decision TEXT NOT NULL,
109 created_at TEXT DEFAULT CURRENT_TIMESTAMP,
110 PRIMARY KEY (follow_id, guardian_uri)
111 )`);
112 // Cross-instance follow-approval (modelled on the guardian offer): the
113 // guardian-side COPY of a gated follow on a REMOTE ward, forwarded here by
114 // the ward's server as an Offer(Follow). The decision is sent back to the
115 // ward's inbox. (Local wards use ap_pending_follows directly.)
116 db.exec(`CREATE TABLE IF NOT EXISTS ap_follow_reviews (
117 id TEXT NOT NULL,
118 guardian_slug TEXT NOT NULL,
119 ward_uri TEXT NOT NULL,
120 ward_inbox TEXT,
121 follower_uri TEXT NOT NULL,
122 follower_handle TEXT,
123 follower_icon TEXT,
124 follow_json TEXT,
125 status TEXT DEFAULT 'pending',
126 created_at TEXT DEFAULT CURRENT_TIMESTAMP,
127 PRIMARY KEY (guardian_slug, id)
128 )`);
129 // Guardianship Fase 2 (shaer-jdb): een doorgestuurde follow-goedkeuring draagt
130 // een RICHTING. Bij een inkomende is de follower iemand anders en de ward het
131 // doel; bij een uitgaande is de ward zelf de follower en staat het doel in het
132 // Follow-object. Zonder deze twee kolommen werd een uitgaande opgeslagen als
133 // "deze ward wil deze ward volgen" en viel het doel weg -- dan valt er niets
134 // zinnigs te tonen, hoe je de wachtrij ook vult.
135 ensureColumn('ap_follow_reviews', 'direction', "TEXT DEFAULT 'incoming'");
136 ensureColumn('ap_follow_reviews', 'target_uri', 'TEXT');
137 ensureColumn('ap_follow_reviews', 'target_handle', 'TEXT');
138 ensureColumn('sites', 'profile_photo', 'TEXT');
139 // De MusicBrainz-koppeling van de artiest (shaer-mbz). Een MBID is een
140 // verwijzing naar hun register, geen kopie ervan -- de naam staat erbij zodat
141 // het beheerscherm kan tonen WAT er gekoppeld is zonder ervoor te moeten
142 // netwerken, en zodat een verkeerde koppeling opvalt.
143 ensureColumn('sites', 'mb_artist_id', 'TEXT');
144 ensureColumn('sites', 'mb_artist_name', 'TEXT');
145 // Een UITGAVE heeft twee dingen die een afspeellijst niet heeft (shaer-756s).
146 //
147 // release_date en niet `year`: die kolom bestaat al en blijft, maar hun
148 // AlbumSerializer leest `released` als een DateField. Een jaartal als
149 // 2024-01-01 versturen is een dag verzinnen, en dat is precies wat we bij
150 // artiesten en albums niet doen. Volledige datum of niets.
151 //
152 // mb_release_id is de tegenhanger van sites.mb_artist_id: dezelfde soort
153 // verwijzing naar MusicBrainz, een niveau lager.
154 //
155 // Ze horen ALLEEN bij kind='album'. PlaylistService dwingt dat af bij het
156 // opslaan -- zie daar waarom dat niet alleen in het scherm mag zitten.
157 ensureColumn('playlists', 'release_date', 'TEXT');
158 ensureColumn('playlists', 'mb_release_id', 'TEXT');
159 ensureColumn('audio_tracks', 'cover_url', 'TEXT');
160 ensureColumn('audio_tracks', 'album', 'TEXT');
161 ensureColumn('users', 'reset_token', 'TEXT');
162 ensureColumn('users', 'reset_token_expires', 'DATETIME');
163 // Google OAuth: link a Google account to a user (login via Google).
164 ensureColumn('users', 'google_sub', 'TEXT');
165 // Read-only/viewer account: can view everything but make no changes.
166 ensureColumn('users', 'readonly', 'INTEGER DEFAULT 0');
167 // Personal interface language (nl|en|de). Null = follow the default (site/env/browser).
168 ensureColumn('users', 'lang', 'TEXT');
169 // Site-level moderation toggle. 'trust' = auto-approve, 'moderate' = pending until reviewed.
170 // Circles: whether this site may appear in other sites' circles (surfacing opt-out).
171 ensureColumn('sites', 'allow_circle', 'INTEGER DEFAULT 1');
172
173 // One EXPLICIT primary/main site (= the company/label site in hub mode,
174 // the only site in solo) instead of the fragile "oldest = main" convention
175 // that was duplicated in 4 places. Backfill: mark the oldest if no primary
176 // site exists yet, so existing behaviour is preserved exactly.
177 ensureColumn('sites', 'is_primary', 'INTEGER DEFAULT 0');
178 try {
179 const hasPrimary = db.prepare('SELECT 1 FROM sites WHERE is_primary = 1 LIMIT 1').get();
180 if (!hasPrimary) {
181 const oldest = db.prepare('SELECT id FROM sites ORDER BY created_at ASC LIMIT 1').get();
182 if (oldest) db.prepare('UPDATE sites SET is_primary = 1 WHERE id = ?').run(oldest.id);
183 }
184 } catch (e) { /* sites table still empty/absent on fresh init — ensurePrimarySite handles it */ }
185
186 // v9 audit additions —————————————————————————————————————————
187 // SEO/social columns the v9 template uses (most live in 001-init.sql already
188 // for fresh DBs but ensureColumn is idempotent for existing DBs).
189 ensureColumn('sites', 'twitter', 'TEXT'); // @handle (with @)
190 ensureColumn('sites', 'schema_type', "TEXT DEFAULT 'Person'"); // Person|Organization
191 ensureColumn('sites', 'publisher_name', 'TEXT');
192 ensureColumn('sites', 'publisher_url', 'TEXT');
193 ensureColumn('sites', 'publisher_logo', 'TEXT');
194 ensureColumn('sites', 'profile_enabled', 'INTEGER DEFAULT 1');
195 ensureColumn('sites', 'profile_name', 'TEXT'); // display name (falls back to title)
196 ensureColumn('sites', 'profile_bio', 'TEXT'); // short bio for header
197 ensureColumn('sites', 'profile_links', 'TEXT'); // JSON array [{platform, url}]
198 ensureColumn('sites', 'feed_view_default', "TEXT DEFAULT 'grid'"); // timeline | grid
199 ensureColumn('sites', 'feed_view_switch', 'INTEGER DEFAULT 1'); // show switcher
200 ensureColumn('sites', 'show_search', 'INTEGER DEFAULT 1');
201 ensureColumn('sites', 'show_archive_link', 'INTEGER DEFAULT 1');
202 // Gated feature (FEP-633c): may external (non-fediverse) embeds be shown to
203 // this account? NULL = auto, which means OFF for a ward and ON for anyone
204 // else. The guardians flip it; the gate itself lives server-side, so a ward
205 // never even receives the thumbnail it is not allowed to see.
206 ensureColumn('sites', 'external_embeds', 'INTEGER');
207 // De rest van de gate-familie (shaer-ahy.1, "maak ze allemaal functioneel",
208 // Barts opdracht 8-8). Zelfde drietal als external_embeds: NULL is de
209 // automatiek (dicht voor een ward, open voor de rest), 0/1 is een besluit
210 // van de guardians en wint van de automatiek.
211 ensureColumn('sites', 'external_threads', 'INTEGER'); // replies van vreemden onder een post (shaer-9y2)
212 ensureColumn('sites', 'gate_replies', 'INTEGER'); // zelf antwoorden in een gesprek (shaer-r4c)
213 ensureColumn('sites', 'gate_images', 'INTEGER'); // afbeeldingsbijlagen (shaer-6p5)
214 ensureColumn('sites', 'gate_messages', 'INTEGER'); // heel Messages (shaer-3ow)
215 ensureColumn('sites', 'gate_compose', 'INTEGER'); // zelf posten, de (+) kaart (shaer-qgev)
216 ensureColumn('sites', 'gate_music', 'INTEGER'); // audiobijlagen (shaer-rmz)
217 ensureColumn('sites', 'gate_quote_cards', 'INTEGER'); // ingebedde quote-kaarten (shaer-mls)
218 ensureColumn('sites', 'gate_custom_emoji', 'INTEGER'); // FEP-9098 emoji-plaatjes (shaer-ytw)
219 ensureColumn('sites', 'gate_account_move', 'INTEGER'); // FEP-7628 Move (shaer-tge)
220 // Wie de ward ZELF mag volgen (shaer-p729): de tegenhanger van shaer:follows,
221 // dat over de andere richting gaat. §5.3 schrijft alleen het doorsturen voor
222 // van een Follow NAAR een ward; wat je verder gated is een keuze van de
223 // implementatie, en dit is die keuze. Verstelbaar, anders dan de inkomende
224 // kant: een kind dat ouder wordt hoort niet eeuwig te blijven vragen.
225 ensureColumn('sites', 'gate_following', 'INTEGER'); // zelf iemand volgen (shaer-p729)
226 // The heavier sibling (FEP-633c 5.6): may a player from outside this app run
227 // INSIDE it? A preview is a picture; playback hands the screen to a third
228 // party's engine, recommendations and all. Two settings, so the guardians can
229 // allow the one without the other. NULL = auto, which means off for a ward.
230 ensureColumn('sites', 'external_playback', 'INTEGER');
231 ensureColumn('sites', 'og_theme', 'TEXT'); // OG share-card variant: NULL=auto (follow site theme) | 'light' | 'dark'
232 // FEP-7628: former identities this actor claims (JSON array of actor URIs).
233 // Publishing them as alsoKnownAs is what lets the OLD server approve a Move
234 // of its followers to this account — the claim must be visible on OUR side.
235 ensureColumn('sites', 'ap_aliases', 'TEXT');
236 ensureColumn('sites', 'moved_to', 'TEXT'); // FEP-7628 slice 2: waarheen dit account vertrok
237 // FEP-1580: staat de ingest-routine nog open? 1 = klaar (en dat is ook de
238 // stand van een site die nooit iets gemigreerd heeft, want er hangt niets).
239 // Derden pollen op deze vlag, dus hij moet ook "er valt niets te wachten"
240 // kunnen zeggen.
241 ensureColumn('sites', 'migration_complete', 'INTEGER DEFAULT 1');
242
243 // Per-post noindex + type
244 ensureColumn('posts', 'noindex', 'INTEGER DEFAULT 0');
245 ensureColumn('posts', 'publish_at', 'DATETIME'); // release planning (premium #3): scheduled go-live
246 ensureColumn('posts', 'fan_only', 'INTEGER DEFAULT 0'); // fan-only preview (premium #3)
247 ensureColumn('posts', 'nsfw', 'INTEGER DEFAULT 0'); // sensitive content → blur + click-to-reveal; fediverse sensitive
248 ensureColumn('posts', 'cover_video_url', 'TEXT'); // muted loop MP4 for an animated cover (Safari-smooth)
249 ensureColumn('posts', 'cover_alt', 'TEXT'); // alt text / description for the cover (a11y → AS2 attachment `name`)
250 ensureColumn('posts', 'language', 'TEXT'); // BCP-47 content language → federates as AS2 contentMap (Mastodon language filter/translate)
251 ensureColumn('posts', 'content_warning', 'TEXT'); // custom CW label (empty = default "Gevoelige inhoud")
252 ensureColumn('posts', 'type', "TEXT DEFAULT 'post'"); // post | foto | video | audio
253 ensureColumn('posts', 'poll_json', 'TEXT'); // a poll WE host → federates as AS2 Question: {multiple,options[{name}],endTime,closed}
254
255 // Statistics (premium module) — bare counters, cookie-free.
256 ensureColumn('posts', 'view_count', 'INTEGER DEFAULT 0'); // views per post
257 ensureColumn('audio_tracks', 'play_count', 'INTEGER DEFAULT 0'); // plays per track
258 ensureColumn('audio_tracks', 'downloadable', 'INTEGER DEFAULT 0'); // download-for-email (premium #2)
259 ensureColumn('audio_tracks', 'credit', 'TEXT'); // owner/credit (copyright holder)
260 ensureColumn('audio_tracks', 'license', 'TEXT'); // license (e.g. "CC BY 4.0", "All rights reserved")
261 ensureColumn('audio_tracks', 'link_spotify', 'TEXT'); // "open in" links per track
262 ensureColumn('audio_tracks', 'link_youtube', 'TEXT');
263 ensureColumn('audio_tracks', 'link_soundcloud', 'TEXT');
264 // Per-track: federate the actual audio file as an AS2 Audio attachment so it plays inline
265 // in EVERY fediverse client (incl. the Mastodon apps). Default 0 = gated (web player only,
266 // file not exposed). Opt-in 1 = the file is served ungated + shared on the fediverse.
267 ensureColumn('audio_tracks', 'fedi_open', 'INTEGER DEFAULT 0');
268
269 // Playlists (v9 feature) — first-class entity. CREATE IF NOT EXISTS is
270 // idempotent so it's safe to run on every boot regardless of DB age.
271 db.exec(`
272 CREATE TABLE IF NOT EXISTS playlists (
273 id TEXT PRIMARY KEY,
274 site_id TEXT NOT NULL,
275 title TEXT NOT NULL,
276 artist TEXT,
277 year INTEGER,
278 cover_url TEXT,
279 kind TEXT DEFAULT 'album',
280 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
281 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
282 FOREIGN KEY (site_id) REFERENCES sites(id)
283 );
284 CREATE TABLE IF NOT EXISTS playlist_tracks (
285 playlist_id TEXT NOT NULL,
286 track_id TEXT NOT NULL,
287 position INTEGER NOT NULL DEFAULT 0,
288 PRIMARY KEY (playlist_id, track_id),
289 FOREIGN KEY (playlist_id) REFERENCES playlists(id) ON DELETE CASCADE,
290 FOREIGN KEY (track_id) REFERENCES audio_tracks(id) ON DELETE CASCADE
291 );
292 CREATE INDEX IF NOT EXISTS idx_playlist_tracks_pos
293 ON playlist_tracks(playlist_id, position);
294 `);
295
296 // Global app settings (key/value singleton). One instance is one owner, so
297 // there is no tenancy mode here anymore — see SettingsService.
298 // Oudere installaties dragen nog een dode rij key='tenancy' ('solo' of
299 // 'circle'). Niets leest hem; bewust laten staan (shaer-x7c0) in plaats van
300 // er opruimcode voor te schrijven die na één ronde zelf dood is.
301 db.exec(`
302 CREATE TABLE IF NOT EXISTS app_settings (
303 key TEXT PRIMARY KEY,
304 value TEXT,
305 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
306 );
307 `);
308
309 // ── Statistics (premium) — cookie-free ──────────────────────
310 // stat_daily: pageview count per day per site (bare counter).
311 // stat_visitor_day: one row per UNIQUE visitor hash per day per site
312 // (sha256 of IP+UA+day-salt; the salt rotates daily and is never stored
313 // → no persistent identifier, no cookie, no consent required).
314 db.exec(`
315 CREATE TABLE IF NOT EXISTS stat_daily (
316 site_id TEXT NOT NULL,
317 day TEXT NOT NULL,
318 pageviews INTEGER NOT NULL DEFAULT 0,
319 PRIMARY KEY (site_id, day)
320 );
321 CREATE TABLE IF NOT EXISTS stat_visitor_day (
322 site_id TEXT NOT NULL,
323 day TEXT NOT NULL,
324 visitor_hash TEXT NOT NULL,
325 PRIMARY KEY (site_id, day, visitor_hash)
326 );
327 CREATE INDEX IF NOT EXISTS idx_stat_visitor_day ON stat_visitor_day(site_id, day);
328 CREATE TABLE IF NOT EXISTS stat_referrer (
329 site_id TEXT NOT NULL,
330 host TEXT NOT NULL,
331 count INTEGER NOT NULL DEFAULT 0,
332 PRIMARY KEY (site_id, host)
333 );
334 `);
335
336 // Newsletter / mailing list (premium). Subscribers per site; double opt-in when SMTP
337 // is configured (status 'pending' until confirmed), otherwise single opt-in ('confirmed').
338 // 'unsub' = unsubscribed. token = confirm/unsubscribe key (used in email links).
339 db.exec(`
340 CREATE TABLE IF NOT EXISTS subscribers (
341 id TEXT PRIMARY KEY,
342 site_id TEXT NOT NULL,
343 email TEXT NOT NULL,
344 status TEXT NOT NULL DEFAULT 'pending',
345 source TEXT DEFAULT 'widget',
346 token TEXT NOT NULL,
347 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
348 confirmed_at DATETIME,
349 UNIQUE(site_id, email)
350 );
351 CREATE INDEX IF NOT EXISTS idx_subscribers_site_status ON subscribers(site_id, status);
352 `);
353
354 // Sent newsletters (history + counts).
355 db.exec(`
356 CREATE TABLE IF NOT EXISTS newsletters (
357 id TEXT PRIMARY KEY,
358 site_id TEXT NOT NULL,
359 subject TEXT NOT NULL,
360 body TEXT NOT NULL,
361 sent_at DATETIME DEFAULT CURRENT_TIMESTAMP,
362 recipient_count INTEGER DEFAULT 0
363 );
364 `);
365
366 // Show agenda (premium #8): tour dates / gigs per site.
367 db.exec(`
368 CREATE TABLE IF NOT EXISTS shows (
369 id TEXT PRIMARY KEY,
370 site_id TEXT NOT NULL,
371 date TEXT NOT NULL,
372 time TEXT,
373 city TEXT NOT NULL,
374 venue TEXT,
375 country TEXT,
376 ticket_url TEXT,
377 notes TEXT,
378 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
379 );
380 CREATE INDEX IF NOT EXISTS idx_shows_site_date ON shows(site_id, date);
381 `);
382
383 // Link-in-bio click statistics (premium #6). One counter per (site, url); the
384 // link-in-bio page links via /links/go/:i which counts the click and redirects.
385 db.exec(`
386 CREATE TABLE IF NOT EXISTS link_clicks (
387 site_id TEXT NOT NULL,
388 url TEXT NOT NULL,
389 clicks INTEGER DEFAULT 0,
390 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
391 PRIMARY KEY (site_id, url)
392 );
393 `);
394
395
396 // ── ActivityPub (fediverse bridge) ──────────────────────────
397 // RSA keypair per actor (Mastodon-compatible HTTP Signatures; separate from
398 // the Cirkels Ed25519 keys). ap_followers = remote AP actors following us.
399 db.exec(`
400 CREATE TABLE IF NOT EXISTS ap_keys (
401 slug TEXT PRIMARY KEY,
402 public_pem TEXT NOT NULL,
403 private_pem TEXT NOT NULL,
404 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
405 ); -- LUISTERAARS (shaer-0nh). Wie de BIBLIOTHEEK volgt, niet de actor.
406 --
407 -- Een eigen tabel en niet een vlag op ap_followers, en dat is met opzet:
408 -- deze accounts horen onze gewone posts NIET te krijgen. Zolang ze in een
409 -- andere tabel staan kan een bezorging ze niet per ongeluk meenemen -- een
410 -- vlag die iemand vergeet te filteren zou dat wel doen, en dan komt de
411 -- Krant van een site bij mensen die alleen muziek wilden.
412 CREATE TABLE IF NOT EXISTS ap_library_followers (
413 id INTEGER PRIMARY KEY AUTOINCREMENT,
414 slug TEXT NOT NULL,
415 actor_uri TEXT NOT NULL,
416 inbox TEXT,
417 shared_inbox TEXT,
418 name TEXT,
419 handle TEXT,
420 icon TEXT,
421 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
422 last_delivery_at DATETIME,
423 last_error_at DATETIME,
424 UNIQUE (slug, actor_uri)
425 );
426
427 CREATE TABLE IF NOT EXISTS ap_followers (
428 id INTEGER PRIMARY KEY AUTOINCREMENT,
429 slug TEXT NOT NULL,
430 actor_uri TEXT NOT NULL,
431 inbox TEXT,
432 shared_inbox TEXT,
433 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
434 UNIQUE(slug, actor_uri)
435 );
436 CREATE INDEX IF NOT EXISTS idx_ap_followers_slug ON ap_followers(slug);
437 CREATE TABLE IF NOT EXISTS ap_interactions (
438 id INTEGER PRIMARY KEY AUTOINCREMENT,
439 kind TEXT NOT NULL, -- 'reply' | 'like' | 'announce'
440 post_id TEXT NOT NULL,
441 object_uri TEXT NOT NULL DEFAULT '', -- remote note id (reply) or '' (like/announce)
442 actor_uri TEXT NOT NULL,
443 actor_name TEXT,
444 actor_handle TEXT,
445 actor_url TEXT,
446 actor_icon TEXT,
447 content TEXT, -- sanitized HTML (reply)
448 published TEXT,
449 parent_uri TEXT, -- the note this reply replies to (for nesting)
450 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
451 UNIQUE(kind, post_id, actor_uri, object_uri)
452 );
453 CREATE INDEX IF NOT EXISTS idx_ap_inter_post ON ap_interactions(post_id, kind);
454 -- Moderation tombstones: object URIs the site owner removed. Checked at ingest
455 -- (handleInbox) AND by the thread-crawler, so a removed reply never comes back
456 -- via thread-filling. Private notes can't be flagged via authorize_interaction
457 -- (their fetch 401s), so owner moderation acts on the locally stored copy.
458 CREATE TABLE IF NOT EXISTS ap_rejected_objects (
459 object_uri TEXT PRIMARY KEY,
460 post_id TEXT,
461 reason TEXT,
462 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
463 );
464 -- ActivityPub C2S (client-to-server): OAuth 2.0 for native/web clients (Shaer).
465 -- Public clients + PKCE (RFC 8252); tokens stored hashed; token is per user+site.
466 CREATE TABLE IF NOT EXISTS oauth_clients (
467 client_id TEXT PRIMARY KEY,
468 client_name TEXT,
469 redirect_uris TEXT NOT NULL, -- JSON array
470 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
471 );
472 CREATE TABLE IF NOT EXISTS oauth_codes (
473 code TEXT PRIMARY KEY,
474 client_id TEXT NOT NULL,
475 user_id TEXT NOT NULL,
476 site_slug TEXT NOT NULL,
477 redirect_uri TEXT NOT NULL,
478 code_challenge TEXT, -- PKCE S256 (verplicht voor public clients)
479 scope TEXT,
480 expires_at DATETIME NOT NULL,
481 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
482 );
483 CREATE TABLE IF NOT EXISTS oauth_tokens (
484 token_hash TEXT PRIMARY KEY, -- sha256(bearer); het token zelf slaan we nooit op
485 client_id TEXT NOT NULL,
486 user_id TEXT NOT NULL,
487 site_slug TEXT NOT NULL,
488 scope TEXT,
489 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
490 last_used_at DATETIME
491 );
492 -- Paid posts (klonkt-demo-aki): the site owner's own Patreon campaign.
493 -- Secrets are encrypted at rest (CryptoBox). Never reuses the instance-level
494 -- patreon_* settings, which are Klonkt Premium's separate license flow.
495 CREATE TABLE IF NOT EXISTS paid_patreon (
496 site_id TEXT PRIMARY KEY,
497 client_id TEXT,
498 client_secret_enc TEXT,
499 campaign_id TEXT,
500 access_token_enc TEXT,
501 refresh_token_enc TEXT,
502 token_exp INTEGER, -- unix seconds
503 default_min_cents INTEGER DEFAULT 0,
504 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
505 );
506 -- One row per passkey. NO patron identity is stored (design decision):
507 -- {passkey, site, proven cents, expiry}. Not traceable to a person.
508 CREATE TABLE IF NOT EXISTS paid_entitlements (
509 credential_id TEXT PRIMARY KEY, -- WebAuthn credential id (opaque, base64url)
510 site_id TEXT NOT NULL,
511 public_key TEXT NOT NULL, -- COSE public key, base64url
512 counter INTEGER DEFAULT 0,
513 transports TEXT,
514 min_cents INTEGER DEFAULT 0, -- the amount proven at link time
515 expires_at INTEGER NOT NULL, -- unix seconds; re-link after
516 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
517 );
518 -- Web Push (docs/webpush-design.md): one row per browser/device the owner
519 -- enabled notifications on. Payloads are encrypted to p256dh/auth (RFC 8291).
520 CREATE TABLE IF NOT EXISTS push_subscriptions (
521 endpoint TEXT PRIMARY KEY, -- push-service URL for this device
522 user_id TEXT NOT NULL,
523 p256dh TEXT NOT NULL, -- client public key
524 auth TEXT NOT NULL, -- client auth secret
525 alert_types TEXT, -- JSON {follow,reply,like,boost,dm}
526 ua_label TEXT,
527 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
528 last_ok_at DATETIME
529 );
530 CREATE TABLE IF NOT EXISTS ap_outbox (
531 id TEXT PRIMARY KEY, -- note path segment (uuid) → /ap/notes/<id>
532 site_slug TEXT NOT NULL,
533 post_id TEXT NOT NULL,
534 post_slug TEXT,
535 in_reply_to TEXT, -- remote status uri we reply to
536 to_actor TEXT, -- remote actor uri (mentioned)
537 to_handle TEXT,
538 content TEXT NOT NULL, -- sanitized HTML of our reply
539 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
540 );
541 CREATE INDEX IF NOT EXISTS idx_ap_outbox_post ON ap_outbox(post_id);
542 -- Your like/boost state on a REMOTE post (the interact page), so those become toggles.
543 CREATE TABLE IF NOT EXISTS ap_my_reactions (
544 site_slug TEXT NOT NULL,
545 target_uri TEXT NOT NULL,
546 kind TEXT NOT NULL, -- 'like' | 'boost'
547 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
548 UNIQUE(site_slug, target_uri, kind)
549 );
550 `);
551 ensureColumn('ap_interactions', 'parent_uri', 'TEXT'); // nesting (existing DBs)
552 ensureColumn('ap_interactions', 'acted_boost', 'INTEGER DEFAULT 0'); // owner boosted this comment (🔁) → can undo
553 ensureColumn('ap_interactions', 'acted_like', 'INTEGER DEFAULT 0'); // owner liked this comment (⭐) → can undo
554
555 // Fediverse CLIENT: accounts WE follow (outbound) + the home timeline of their posts.
556 db.exec(`
557 CREATE TABLE IF NOT EXISTS ap_following (
558 id INTEGER PRIMARY KEY AUTOINCREMENT,
559 slug TEXT NOT NULL, -- our site that follows
560 actor_uri TEXT NOT NULL, -- the followed account's actor id
561 handle TEXT, name TEXT, icon TEXT, url TEXT,
562 inbox TEXT, -- their inbox (for Create delivery / Undo)
563 follow_id TEXT, -- the Follow activity id we sent (Accept matching)
564 status TEXT DEFAULT 'pending', -- pending | accepted
565 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
566 UNIQUE(slug, actor_uri)
567 );
568 -- Antwoorden van accounts die we volgen komen gewoon binnen, ondertekend
569 -- door de schrijver, maar horen niet in de Krant (belongsInTimeline) en
570 -- werden daarna nergens bewaard. Kwam er later een doorgestuurd antwoord OP
571 -- zo'n bericht, dan kenden we de ouder niet en wezen we het af (shaer-e9g).
572 -- Alleen de URI, geen inhoud: dit voedt uitsluitend de vraag "kennen wij dit
573 -- bericht?". Wordt na 30 dagen gesnoeid; doorsturen gebeurt kort na het
574 -- antwoord, dus langer bewaren levert niets op.
575 CREATE TABLE IF NOT EXISTS ap_seen_notes (
576 uri TEXT PRIMARY KEY,
577 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
578 );
579 CREATE INDEX IF NOT EXISTS idx_ap_seen_notes_age ON ap_seen_notes(created_at);
580 CREATE TABLE IF NOT EXISTS ap_timeline (
581 id TEXT NOT NULL, -- the remote note's AP id
582 slug TEXT NOT NULL, -- whose home timeline (our site)
583 author_uri TEXT, author_name TEXT, author_handle TEXT, author_icon TEXT, author_url TEXT,
584 content TEXT, url TEXT, published TEXT, media_json TEXT,
585 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
586 UNIQUE(slug, id)
587 );
588 CREATE INDEX IF NOT EXISTS idx_ap_timeline_slug ON ap_timeline(slug, published);
589 -- canonicalReactionUri herleidt een permalink naar het object-id door op (slug, url)
590 -- te zoeken. Zonder deze index viel dat terug op idx_ap_timeline_slug, dus een scan
591 -- van elke rij van die slug. Dat gebeurt PER REACTIE in getInteractions, en de
592 -- reactie-migratie erft het in haar re-key-join, die synchroon vóór listen draait:
593 -- de opstartkosten waren reacties maal tijdlijnrijen.
594 CREATE INDEX IF NOT EXISTS idx_ap_timeline_url ON ap_timeline(slug, url);
595 -- FEP-1580: de vertaaltabel van een verhuizing. Per gemigreerd object waar
596 -- het VANDAAN kwam en welke URI het HIER kreeg. Derden lezen deze mapping en
597 -- werken er hun eigen inReplyTo/Like-verwijzingen mee bij; zonder deze tabel
598 -- is "de berichten krijgen nieuwe adressen" een permanent kapotte draad.
599 --
600 -- De spec eist omgekeerd-chronologisch op het moment dat de kopie HIER is
601 -- aangemaakt (niet de oorspronkelijke publicatiedatum). Daarom sorteren we
602 -- op de autoincrement-id en niet op created_at: die heeft secondeprecisie,
603 -- en een ingest zet er tientallen per seconde in.
604 CREATE TABLE IF NOT EXISTS ap_migration (
605 id INTEGER PRIMARY KEY AUTOINCREMENT,
606 slug TEXT NOT NULL, -- onze site, de DOELkant van de verhuizing
607 origin TEXT NOT NULL, -- object-URI op de broninstantie
608 target TEXT NOT NULL, -- de URI die het object hier kreeg
609 source_actor TEXT NOT NULL DEFAULT '',
610 is_public INTEGER NOT NULL DEFAULT 1, -- niet-publieke items horen niet in een publieke pagina
611 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
612 UNIQUE(slug, origin)
613 );
614 CREATE INDEX IF NOT EXISTS idx_ap_migration_slug ON ap_migration(slug, id DESC);
615 -- De Move-activities zelf, gededupliceerd. FEP-1580 wil dat deze collectie
616 -- een op zichzelf staand bewijs vormt voor de items in ap_migration, met een
617 -- FEP-8b32 integrity proof van de bron-actor plus een kopie van diens
618 -- actor-document. Wij bewaren allebei die stukken al (activity_json en
619 -- actor_json), maar Klonkt kent 8b32 nog niet: zie shaer-j1v0. Zolang dat
620 -- open staat is deze collectie structureel goed en niet verifieerbaar.
621 CREATE TABLE IF NOT EXISTS ap_moves (
622 id INTEGER PRIMARY KEY AUTOINCREMENT,
623 slug TEXT NOT NULL,
624 move_id TEXT NOT NULL, -- id van de Move-activity, tevens dedup-sleutel
625 source_actor TEXT NOT NULL,
626 target_actor TEXT NOT NULL,
627 activity_json TEXT NOT NULL,
628 actor_json TEXT, -- inline kopie van het bron-actordocument
629 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
630 UNIQUE(slug, move_id)
631 );
632 CREATE TABLE IF NOT EXISTS ap_blocks (
633 id INTEGER PRIMARY KEY AUTOINCREMENT,
634 slug TEXT NOT NULL, -- our site that set the block
635 target TEXT NOT NULL, -- actor URI (actor block) or domain (domain block)
636 kind TEXT NOT NULL, -- 'actor' | 'domain'
637 label TEXT, -- display (@handle or domain)
638 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
639 UNIQUE(slug, target)
640 );
641 CREATE INDEX IF NOT EXISTS idx_ap_blocks_target ON ap_blocks(target);
642 -- Committed guardian ↔ ward relations, one row per local side. role
643 -- 'ward' = the local slug is a ward of other_uri; 'guardian' = the local
644 -- slug guards other_uri. status is always 'accepted' here now: PENDING
645 -- offers live in ap_guardian_offers below (FEP-633c multi-party handshake).
646 CREATE TABLE IF NOT EXISTS ap_guardianships (
647 id INTEGER PRIMARY KEY AUTOINCREMENT,
648 slug TEXT NOT NULL, -- our local site in this relation (guardianship module)
649 role TEXT NOT NULL, -- 'guardian' (slug guards other) | 'ward' (other guards slug)
650 other_uri TEXT NOT NULL, -- the counterpart actor URI (local or remote)
651 other_handle TEXT, -- cached @user@host for display
652 status TEXT NOT NULL, -- 'offered' (legacy) | 'accepted'
653 offer_id TEXT, -- the Offer activity id (FEP-633c section 3)
654 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
655 UNIQUE(slug, role, other_uri)
656 );
657 CREATE INDEX IF NOT EXISTS idx_ap_guardianships_slug ON ap_guardianships(slug, role, status);
658 -- The multi-party handshake (FEP-633c section 3), one row per offer this
659 -- instance is a party to. Mirrors the Shaer test daemon's Handshake:
660 -- accepts accumulate in ap_guardian_offer_accepts, and the offer commits
661 -- only when the candidate returns the handle after ward + candidate + at
662 -- least one existing guardian have accepted.
663 CREATE TABLE IF NOT EXISTS ap_guardian_offers (
664 offer_id TEXT NOT NULL, -- the Offer activity id (minted by the candidate)
665 slug TEXT NOT NULL, -- the local site tracking this handshake (each party keeps its own copy)
666 ward_uri TEXT NOT NULL, -- the ward-to-be
667 candidate_uri TEXT NOT NULL, -- the guardian-candidate (fixed initiator)
668 existing_guardians TEXT NOT NULL DEFAULT '[]', -- JSON array of the ward's current guardian URIs
669 status TEXT NOT NULL DEFAULT 'pending', -- 'pending' | 'committed' | 'void'
670 handle TEXT, -- the escalation handle returned at commit (section 6)
671 ward_handle TEXT, -- cached @ward@host for display
672 candidate_handle TEXT, -- cached @candidate@host for display
673 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
674 PRIMARY KEY (slug, offer_id)
675 );
676 CREATE INDEX IF NOT EXISTS idx_ap_guardian_offers_slug ON ap_guardian_offers(slug, status);
677 CREATE TABLE IF NOT EXISTS ap_guardian_offer_accepts (
678 offer_id TEXT NOT NULL, -- FK to ap_guardian_offers
679 slug TEXT NOT NULL, -- the local site's copy of the tally
680 party_uri TEXT NOT NULL, -- the party who accepted (ward | candidate | an existing guardian)
681 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
682 PRIMARY KEY (slug, offer_id, party_uri)
683 );
684 -- FEP-633c §5.6: a gated setting a ward's guardians decide together, which
685 -- has to work when they live on other servers (the ordinary case). One row
686 -- per guardian answer; the ward's server tallies (§3.5) and enforces.
687 -- The proposals themselves, so an Accept that only references the offer
688 -- id can still be resolved to "which feature, which value".
689 CREATE TABLE IF NOT EXISTS ap_gated_offers (
690 offer_id TEXT PRIMARY KEY,
691 slug TEXT NOT NULL, -- the ward, on this server
692 feature TEXT NOT NULL,
693 value INTEGER NOT NULL,
694 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
695 );
696 -- The guardian-side COPY of a gated-setting proposal on a ward, forwarded
697 -- here by the WARD's server (the same shape ap_follow_reviews has for a
698 -- gated follow). Without it a guardian on another server never learns a
699 -- proposal exists and can never answer it, so a threshold of two can never
700 -- be reached and every proposal expires. The answer goes back to the
701 -- ward's inbox, which tallies (5.6).
702 CREATE TABLE IF NOT EXISTS ap_gated_reviews (
703 id TEXT NOT NULL, -- the offer id, as minted by the proposer
704 guardian_slug TEXT NOT NULL, -- us, one of the ward's guardians
705 ward_uri TEXT NOT NULL,
706 ward_inbox TEXT,
707 proposer TEXT, -- who opened it (for display)
708 feature TEXT NOT NULL,
709 value INTEGER NOT NULL,
710 created_at TEXT DEFAULT CURRENT_TIMESTAMP,
711 PRIMARY KEY (guardian_slug, id)
712 );
713 -- The PROPOSER's own record of a gated proposal it sent (5.6). Without it
714 -- a guardian clicks "propose", the ward's server tallies somewhere else,
715 -- and the proposer has nowhere to even see that something is running: the
716 -- status was a button caption that did not survive a page refresh. The
717 -- ward's server answers the Offer once the decision settles (Accept when
718 -- it settled on the proposed value, Reject otherwise); that answer lands
719 -- in status. An open row past the decision window renders as expired.
720 CREATE TABLE IF NOT EXISTS ap_gated_sent (
721 offer_id TEXT PRIMARY KEY, -- as minted by us, the proposer
722 guardian_slug TEXT NOT NULL, -- us
723 ward_uri TEXT NOT NULL,
724 feature TEXT NOT NULL,
725 value INTEGER NOT NULL, -- what we proposed
726 status TEXT NOT NULL DEFAULT 'open', -- 'open' | 'accepted' | 'rejected'
727 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
728 );
729 CREATE TABLE IF NOT EXISTS ap_gated_votes (
730 slug TEXT NOT NULL, -- the WARD, on this server
731 feature TEXT NOT NULL, -- e.g. 'shaer:externalEmbeds'
732 guardian_uri TEXT NOT NULL, -- who answered (must be a committed guardian)
733 value INTEGER NOT NULL, -- the value they voted for (0/1)
734 opened_at DATETIME NOT NULL, -- when this decision opened (the window start)
735 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
736 PRIMARY KEY (slug, feature, guardian_uri)
737 );
738 -- Guardian availability (FEP-633c 3.6): one guardian's attention as seen
739 -- from one ward on this server. Never public; the ward reads it via the
740 -- owner-only guardians queue. One rule above all: one answer restores
741 -- everything, so every row here is one answer away from disappearing.
742 CREATE TABLE IF NOT EXISTS ap_guardian_attention (
743 ward_slug TEXT NOT NULL,
744 guardian_uri TEXT NOT NULL,
745 state TEXT NOT NULL DEFAULT 'active', -- 'active' | 'away' | 'dormant'
746 away_until INTEGER, -- epoch ms while declared away
747 PRIMARY KEY (ward_slug, guardian_uri)
748 );
749 -- The ONLY admissible dormancy evidence (3.6.2): directly addressed
750 -- requests that went unanswered. Calendar time alone never counts.
751 CREATE TABLE IF NOT EXISTS ap_attention_requests (
752 ward_slug TEXT NOT NULL,
753 guardian_uri TEXT NOT NULL,
754 request_id TEXT NOT NULL,
755 asked_at INTEGER NOT NULL, -- epoch ms
756 PRIMARY KEY (ward_slug, guardian_uri, request_id)
757 );
758 -- A lapse (3.6.3): the available co-guardians deciding to release a
759 -- dormant one. Irreversible, so the window always runs in full; any sign
760 -- of life from the target cancels it outright.
761 CREATE TABLE IF NOT EXISTS ap_lapses (
762 id TEXT PRIMARY KEY,
763 ward_slug TEXT NOT NULL,
764 ward_uri TEXT NOT NULL,
765 target_uri TEXT NOT NULL,
766 opened_by TEXT NOT NULL,
767 set_json TEXT NOT NULL, -- the available set at open, target excluded
768 accepts_json TEXT NOT NULL DEFAULT '[]',
769 rejects_json TEXT NOT NULL DEFAULT '[]',
770 opened_at INTEGER NOT NULL, -- epoch ms
771 window_ms INTEGER NOT NULL,
772 cancelled INTEGER NOT NULL DEFAULT 0,
773 applied INTEGER NOT NULL DEFAULT 0,
774 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
775 );
776 CREATE TABLE IF NOT EXISTS ap_delivery (
777 id INTEGER PRIMARY KEY AUTOINCREMENT,
778 slug TEXT NOT NULL, -- our site/actor that signs the delivery
779 inbox TEXT NOT NULL, -- recipient inbox URL
780 body TEXT NOT NULL, -- the activity JSON to POST
781 attempts INTEGER NOT NULL DEFAULT 0,
782 next_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
783 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
784 );
785 CREATE INDEX IF NOT EXISTS idx_ap_delivery_due ON ap_delivery(next_at);
786 CREATE TABLE IF NOT EXISTS poll_votes (
787 id INTEGER PRIMARY KEY AUTOINCREMENT,
788 post_id INTEGER NOT NULL, -- our local poll post (posts.id)
789 actor_uri TEXT NOT NULL, -- the remote voter's AP actor URI
790 choice TEXT NOT NULL, -- the chosen option's name (matches poll_json options[].name)
791 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
792 UNIQUE(post_id, actor_uri, choice)
793 );
794 CREATE INDEX IF NOT EXISTS idx_poll_votes_post ON poll_votes(post_id);
795 CREATE TABLE IF NOT EXISTS ap_mentions (
796 id INTEGER PRIMARY KEY AUTOINCREMENT,
797 slug TEXT NOT NULL, -- our mentioned site/actor
798 object_uri TEXT NOT NULL, -- the remote note that mentions us
799 note_url TEXT, -- its human URL (open/interact)
800 actor_uri TEXT, actor_name TEXT, actor_handle TEXT, actor_icon TEXT, actor_url TEXT,
801 content TEXT, -- sanitized HTML snippet of the mentioning note
802 published TEXT,
803 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
804 UNIQUE(slug, object_uri)
805 );
806 CREATE INDEX IF NOT EXISTS idx_ap_mentions_slug ON ap_mentions(slug, created_at);
807 CREATE TABLE IF NOT EXISTS ap_reports (
808 id INTEGER PRIMARY KEY AUTOINCREMENT,
809 slug TEXT NOT NULL, -- our site the report is about (its owner moderates)
810 actor_uri TEXT, -- the reporter's actor URI
811 actor_name TEXT, actor_handle TEXT, actor_icon TEXT,
812 content TEXT, -- the reason (plain text)
813 objects TEXT, -- JSON array of reported object URIs (our actor + statuses)
814 seen INTEGER DEFAULT 0,
815 created_at DATETIME DEFAULT CURRENT_TIMESTAMP
816 );
817 CREATE INDEX IF NOT EXISTS idx_ap_reports_slug ON ap_reports(slug, created_at);
818 `);
819 // "Feature" a followed account: its posts show in the local Cirkel. Heet in de
820 // UI "Uitgelicht" / "Featured" (tl.autoboost), en zo heet de kolom ook in
821 // following.csv. Niet te verwarren met `featured` op de ACTOR: dat is de
822 // collectie vastgezette POSTS (toot:featured), iets heel anders.
823 ensureColumn('ap_following', 'auto_boost', 'INTEGER DEFAULT 0');
824 // A timeline post you boosted (🔁) — also shown in the Cirkel (mixed by date).
825 ensureColumn('ap_timeline', 'boosted', 'INTEGER DEFAULT 0');
826 ensureColumn('ap_timeline', 'liked', 'INTEGER DEFAULT 0'); // a feed post you liked (⭐) → toggle
827 ensureColumn('ap_timeline', 'nsfw', 'INTEGER DEFAULT 0'); // remote sensitive post → blur in the Cirkel
828 ensureColumn('ap_timeline', 'cw', 'TEXT'); // remote content-warning text
829 ensureColumn('ap_timeline', 'emoji_json', 'TEXT'); // FEP-9098 custom emoji Emoji tags from the inbound note, served back as `tag`
830 ensureColumn('ap_timeline', 'link_json', 'TEXT'); // FEP-e232 object-link (quote/ref) tags from the inbound note, served back as `tag`
831 ensureColumn('ap_timeline', 'quote_json', 'TEXT'); // FEP-044f resolved quoted-post snapshot (author + content), for the embedded quote card
832 // FEP-044f: the fediverse object THIS post quotes, resolved once at publish
833 // time so buildNote (sync, also used by the outbox) needs no network.
834 ensureColumn('posts', 'quote_uri', 'TEXT'); // the quoted object's id
835 // De kaart op je EIGEN post (shaer-k3f): dezelfde snapshots die ap_timeline
836 // voor binnenkomende posts draagt, maar dan voor wat je zelf publiceert --
837 // zonder deze twee kan de app een eigen post nooit als kaart tonen.
838 ensureColumn('posts', 'quote_json', 'TEXT'); // FEP-044f resolved quote snapshot
839 ensureColumn('posts', 'embed_json', 'TEXT'); // externe linkkaart (oEmbed/OG), thumbnail-only
840 ensureColumn('posts', 'quote_actor', 'TEXT'); // its author, so we can address them
841 ensureColumn('ap_timeline', 'embed_json', 'TEXT'); // resolved EXTERNAL embed (oEmbed/provider), thumbnail-only; gated per site (sites.external_embeds)
842 ensureColumn('ap_timeline', 'author_emoji_json', 'TEXT'); // FEP-9098 custom emojis in the author's display name (shaer:author.emojis)
843 ensureColumn('ap_timeline', 'reblog_emoji_json', 'TEXT'); // FEP-9098 custom emojis in the booster's display name (shaer:booster.emojis)
844 ensureColumn('ap_timeline', 'reblog_name', 'TEXT'); // a followed account boosted this → "X boosted"
845 ensureColumn('ap_timeline', 'reblog_handle', 'TEXT'); // the booster's @handle
846 ensureColumn('ap_timeline', 'reblog_icon', 'TEXT'); // the booster's avatar
847 ensureColumn('ap_timeline', 'poll_json', 'TEXT'); // a Question (poll): {multiple,options[{name,count}],endTime,closed,voters,voted}
848
849 // Delivery health per follower → surface dead accounts for manual cleanup.
850 ensureColumn('ap_followers', 'last_delivery_at', 'DATETIME'); // last SUCCESSFUL delivery to this follower's inbox
851 ensureColumn('ap_followers', 'last_error_at', 'DATETIME'); // last time a delivery to it gave up (max retries)
852
853 // ActivityPub `source` model: content_rendered = baked display HTML (#hashtags / URLs /
854 // @mentions linkified once at save). `content` stays the raw source used for editing and
855 // re-rendering. NULL on old posts → the render route bakes on the fly as a fallback.
856 ensureColumn('posts', 'content_rendered', 'TEXT');
857
858 // AP addressing of an incoming interaction: 'public' | 'unlisted' | 'followers' | 'direct',
859 // derived from the note's to/cc at ingest. The public post page only renders public/unlisted
860 // replies; followers/direct replies surface in notifications (and later Messages) with post
861 // context instead. Existing rows default to 'public' (historically almost all were).
862 ensureColumn('ap_interactions', 'visibility', "TEXT DEFAULT 'public'");
863 ensureColumn('ap_interactions', 'emoji_json', 'TEXT'); // FEP-9098 custom emojis in a reply's content (messages + thread)
864 ensureColumn('ap_interactions', 'actor_emoji_json', 'TEXT'); // FEP-9098 custom emojis in the reply author's display name
865 // Rich replies: the reply's language (BCP47 code) → contentMap on the outgoing Note.
866 ensureColumn('ap_outbox', 'language', 'TEXT');
867 // Rich replies: JSON array [{url, mediaType, name}] → `attachment` on the Note.
868 ensureColumn('ap_outbox', 'attachments', 'TEXT');
869 ensureColumn('posts', 'ap_visibility', 'TEXT'); // public|quiet|friends|direct (C2S addressing, shaer-60b)
870 ensureColumn('posts', 'paid', 'INTEGER DEFAULT 0'); // paid post (klonkt-demo-aki)
871 ensureColumn('posts', 'paid_min_cents', 'INTEGER'); // required support; null = owner default
872 ensureColumn('paid_patreon', 'patreon_url', 'TEXT'); // owner's public Patreon page → "Word supporter" link (klonkt-demo-aki)
873 ensureColumn('ap_outbox', 'visibility', 'TEXT'); // 'direct' = private mention, never Public (shaer-tqc)
874 ensureColumn('ap_outbox', 'to_actors', 'TEXT'); // JSON array of recipient actor URIs for direct notes
875 ensureColumn('ap_outbox', 'help_request', 'INTEGER'); // FEP-633c shaer:helpRequest (ward's call for help)
876 // Wie er op een hulpvraag af is, en wanneer hij is afgesloten (shaer-lgo).
877 // Los van ap_mentions, want dit is GEDEELDE staat: elke guardian van dit kind
878 // heeft er een kopie van, en die komt binnen als bericht van een ander. Een
879 // kolom op de mention zou alleen over onszelf gaan.
880 //
881 // OPGEPIKT mag stapelen: twee mensen die tegelijk reageren op een kind dat om
882 // hulp vraagt is geen probleem. Twee mensen die allebei niets doen omdat de
883 // ander het "geclaimd" had, wel.
884 //
885 // AFGEHANDELD kent geen terugdraai. Sluiten gebeurt met een stevige
886 // bevestiging, en leeft de vraag daarna nog, dan wordt hij opnieuw gesteld --
887 // een nieuwe hulpvraag. Zo blijft het verslag eerlijk: er wordt niets
888 // herschreven, er wordt toegevoegd.
889 db.exec(`CREATE TABLE IF NOT EXISTS ap_help_state (
890 note_uri TEXT NOT NULL,
891 guardian_uri TEXT NOT NULL,
892 kind TEXT NOT NULL, -- pickup | handled
893 guardian_handle TEXT,
894 created_at TEXT DEFAULT CURRENT_TIMESTAMP,
895 PRIMARY KEY (note_uri, guardian_uri, kind)
896 )`);
897 db.exec('CREATE INDEX IF NOT EXISTS idx_ap_help_state_note ON ap_help_state(note_uri)');
898 // Een kind dat zelf om een poort vraagt (shaer-8ru). Een VRAAG, geen stem:
899 // pas als een guardian hem oppakt wordt het een voorstel dat langs de tally
900 // gaat. handled_at in plaats van verwijderen -- wat een kind gevraagd heeft
901 // hoort terug te vinden te zijn, ook als het antwoord nee was.
902 db.exec(`CREATE TABLE IF NOT EXISTS ap_gate_requests (
903 id INTEGER PRIMARY KEY AUTOINCREMENT,
904 slug TEXT NOT NULL, -- de guardian die hem ontving
905 ward_uri TEXT NOT NULL,
906 feature TEXT NOT NULL,
907 note_uri TEXT,
908 created_at TEXT DEFAULT CURRENT_TIMESTAMP,
909 handled_at TEXT,
910 UNIQUE (slug, ward_uri, feature, handled_at)
911 )`);
912 db.exec('CREATE INDEX IF NOT EXISTS idx_ap_gate_requests_slug ON ap_gate_requests(slug, handled_at)');
913 ensureColumn('ap_mentions', 'help_request', 'INTEGER'); // inbound ward call-for-help (Guardian PWA message centre)
914 ensureColumn('ap_outbox', 'wave', 'INTEGER'); // FEP-633c shaer:wave (guardian -> ward nudge)
915 ensureColumn('ap_outbox', 'away_until', 'INTEGER'); // FEP-633c 3.6.1 shaer:away + endTime (epoch ms)
916 ensureColumn('ap_gated_offers', 'proposer', 'TEXT'); // who proposed (5.6): the settle-answer goes back to them
917 // Zou JOUW antwoord het besluit afmaken (shaer-8vt)? De telling loopt op de
918 // server van het kind; zonder dit veld kan een guardian elders niet weten dat
919 // hij de doorslag geeft. Ontbreekt hij, dan waarschuwen we -- bij twijfel.
920 ensureColumn('ap_gated_reviews', 'decisive', 'INTEGER');
921 // Did a guardian actually say yes to this follower? That is what makes the
922 // mutual shortcut sound: a ward may follow back anyone its guardians already
923 // admitted, without asking the same question twice. Only follows that came
924 // through the §5.3 gate carry the mark; a free actor's followers never faced
925 // one. Everyone already following when this column arrives is grandfathered
926 // in (Barts besluit, 3-8): the rule is exact from that moment forward rather
927 // than retroactively suspicious of relationships that already exist.
928 {
929 const had = db.prepare("SELECT COUNT(*) AS n FROM pragma_table_info('ap_followers') WHERE name = 'gate_approved'").get();
930 ensureColumn('ap_followers', 'gate_approved', 'INTEGER DEFAULT 0');
931 if (!had || !had.n) {
932 try { db.prepare('UPDATE ap_followers SET gate_approved = 1').run(); } catch { /* table still empty on a fresh init */ }
933 }
934 }
935 ensureColumn('posts', 'c2s_attachments', 'TEXT'); // media a C2S Note carried (JSON [{url,mediaType,name}]); buildNote federates them
936 // 30-7: C2S posts briefly got their content media copied onto the cover,
937 // which showed the same video twice on the post page. Clear the covers that
938 // duplicate their own content; idempotent, only ever touches those.
939 try {
940 db.prepare("UPDATE posts SET cover_video_url = NULL WHERE cover_video_url LIKE '/media/reply-media/%' AND instr(content, cover_video_url) > 0").run();
941 db.prepare("UPDATE posts SET cover_image_url = NULL WHERE cover_image_url LIKE '/media/reply-media/%' AND instr(content, cover_image_url) > 0").run();
942 } catch { /* posts table absent on fresh init */ }
943 ensureColumn('ap_mentions', 'wave', 'INTEGER'); // inbound guardian wave
944 // FEP-633c §2.2: object hint that the author is a ward. Register-only for now;
945 // used later at reddings-boei / escalation routing.
946 ensureColumn('ap_timeline', 'has_guardians', 'INTEGER');
947 ensureColumn('ap_mentions', 'has_guardians', 'INTEGER');
948 // Berichten and de Krant render a post the same way, so a mention or a reply
949 // needs the same trimmings a timeline row already has: custom emojis, the
950 // media the note carried, and the quote / link-preview card.
951 ensureColumn('ap_mentions', 'emoji_json', 'TEXT'); // FEP-9098, in the content
952 ensureColumn('ap_mentions', 'actor_emoji_json', 'TEXT'); // FEP-9098, in the display name
953 ensureColumn('ap_mentions', 'media_json', 'TEXT');
954 ensureColumn('ap_mentions', 'quote_json', 'TEXT'); // FEP-044f quoted post
955 ensureColumn('ap_mentions', 'embed_json', 'TEXT'); // external link preview
956 ensureColumn('ap_interactions', 'media_json', 'TEXT');
957 ensureColumn('ap_interactions', 'quote_json', 'TEXT');
958 ensureColumn('ap_interactions', 'embed_json', 'TEXT');
959 ensureColumn('ap_followers', 'name', 'TEXT'); // cached display name (shaer-aa3)
960 ensureColumn('ap_followers', 'handle', 'TEXT'); // @user@host
961 ensureColumn('ap_followers', 'icon', 'TEXT'); // avatar URL
962 feedStateTriggers();
963}
964
965/**
966 * Wat er met een tijdlijn gebeurd is, op één plek (shaer-n05).
967 *
968 * De inbox-lezing voegt vier bronnen samen. De vraag "is er iets veranderd" werd
969 * eerst beantwoord met MAX(rowid) over die vier -- een TOEVALLIGE eigenschap van
970 * de tabellen, geen feit dat ergens is opgeschreven. Dat gaf precies de gebreken
971 * die je van zo'n afleiding verwacht: bewerkingen en verwijderingen bewogen hem
972 * niet, en hij kon achteruit lopen. Dezelfde fout als reacties uitlezen uit
973 * ap_timeline.liked (shaer-9e9).
974 *
975 * Nu één rij per bericht per tijdlijn, met een oplopende `rev` en `kind`. Dat
976 * beantwoordt drie vragen die anders drie eigen oplossingen zouden krijgen:
977 * is er iets veranderd sinds N, wát is er veranderd, en is dit bericht bewerkt.
978 *
979 * Bijgehouden door TRIGGERS en niet door de aanroepende code, om dezelfde reden
980 * dat er geen gebeurtenis-emitter is: een trigger zit in de database, dus geen
981 * enkel codepad kan hem vergeten. De prijs is onzichtbare logica -- wie alleen de
982 * JavaScript leest ziet niet waarom deze tabel vult. Vandaar dat ze hier staan,
983 * bij de tabel, en niet verspreid.
984 *
985 * Let op de `UPDATE OF`-kolomlijsten: die zijn niet decoratief. Een like schrijft
986 * ap_timeline.liked en een 🔁 schrijft .boosted; zonder die afbakening zou je
987 * eigen like het bericht als BEWERKT merken en elke wachtende client wekken.
988 */
989function feedStateTriggers() {
990 try {
991 db.exec(`
992 -- Tot waar jij een gesprek gelezen hebt (shaer-frontend-3tx).
993 --
994 -- Een MARKERING, geen teller: het aantal ongelezen berichten is een
995 -- COUNT over de berichten die na deze cursor komen. Een opgeslagen
996 -- getal zou opgehoogd, verlaagd en gerepareerd moeten worden, en zou
997 -- blijven staan als er iets verwijderd wordt -- badge zegt 3, er is
998 -- niets.
999 --
1000 -- De cursor is samengesteld ('<stempel>|<ref>'), dezelfde vorm als de
1001 -- gesprekspaginering en om dezelfde reden: twee berichten in dezelfde
1002 -- seconde is bij DM's een gesprek, geen randgeval.
1003 CREATE TABLE IF NOT EXISTS ap_read_markers (
1004 slug TEXT NOT NULL,
1005 other TEXT NOT NULL, -- de tegenpartij (actor uri)
1006 cursor TEXT NOT NULL,
1007 at DATETIME DEFAULT CURRENT_TIMESTAMP,
1008 PRIMARY KEY (slug, other)
1009 );
1010 CREATE TABLE IF NOT EXISTS ap_feed_state (
1011 slug TEXT NOT NULL,
1012 object_uri TEXT NOT NULL,
1013 rev INTEGER NOT NULL,
1014 kind TEXT NOT NULL, -- new | updated | deleted
1015 at DATETIME DEFAULT CURRENT_TIMESTAMP,
1016 PRIMARY KEY (slug, object_uri)
1017 );
1018 CREATE INDEX IF NOT EXISTS idx_ap_feed_state_rev ON ap_feed_state(slug, rev);
1019 -- Eén doorlopende teller voor de hele instance. Bewust niet MAX(rev) uit de
1020 -- tabel zelf: verdwijnt de hoogste rij, dan zou die teruglopen en denkt een
1021 -- client dat er niets gebeurd is.
1022 CREATE TABLE IF NOT EXISTS ap_feed_rev (n INTEGER NOT NULL);
1023 `);
1024 if (!db.prepare('SELECT COUNT(*) AS n FROM ap_feed_rev').get().n) {
1025 db.prepare('INSERT INTO ap_feed_rev (n) VALUES (0)').run();
1026 }
1027 // slug + object_uri verschillen per bron; de rest is voor alle vier gelijk.
1028 const zet = (naam, gebeurtenis, tabel, slug, uri, kind, extra = '', wanneer = '') => `
1029 DROP TRIGGER IF EXISTS ${naam};
1030 CREATE TRIGGER ${naam} AFTER ${gebeurtenis} ON ${tabel}${wanneer ? ` WHEN ${wanneer}` : ''} BEGIN
1031 UPDATE ap_feed_rev SET n = n + 1;
1032 INSERT INTO ap_feed_state (slug, object_uri, rev, kind)
1033 ${extra || `VALUES (${slug}, ${uri}, (SELECT n FROM ap_feed_rev), '${kind}')`}
1034 ON CONFLICT(slug, object_uri) DO UPDATE
1035 SET rev = excluded.rev, kind = excluded.kind, at = CURRENT_TIMESTAMP;
1036 END;`;
1037 const joinPosts = (uri, kind) => `
1038 SELECT s.slug, ${uri}, (SELECT n FROM ap_feed_rev), '${kind}'
1039 FROM posts p JOIN sites s ON s.id = p.site_id`;
1040 db.exec([
1041 zet('trg_feed_tl_ins', 'INSERT', 'ap_timeline', 'NEW.slug', 'NEW.id', 'new'),
1042 zet('trg_feed_tl_upd', 'UPDATE OF content, media_json, nsfw, cw, url, poll_json, quote_json, embed_json', 'ap_timeline', 'NEW.slug', 'NEW.id', 'updated'),
1043 zet('trg_feed_tl_del', 'DELETE', 'ap_timeline', 'OLD.slug', 'OLD.id', 'deleted'),
1044 zet('trg_feed_mn_ins', 'INSERT', 'ap_mentions', 'NEW.slug', 'NEW.object_uri', 'new'),
1045 zet('trg_feed_mn_upd', 'UPDATE OF content, media_json, quote_json, embed_json', 'ap_mentions', 'NEW.slug', 'NEW.object_uri', 'updated'),
1046 zet('trg_feed_mn_del', 'DELETE', 'ap_mentions', 'OLD.slug', 'OLD.object_uri', 'deleted'),
1047 zet('trg_feed_ob_ins', 'INSERT', 'ap_outbox', 'NEW.site_slug', 'NEW.id', 'new'),
1048 zet('trg_feed_ob_upd', 'UPDATE OF content, attachments', 'ap_outbox', 'NEW.site_slug', 'NEW.id', 'updated'),
1049 zet('trg_feed_ob_del', 'DELETE', 'ap_outbox', 'OLD.site_slug', 'OLD.id', 'deleted'),
1050 // ap_interactions draagt geen slug: die hangt aan de POST. Vandaar de join,
1051 // en vandaar dat deze drie niet in de gewone vorm passen.
1052 //
1053 // De WHEN op kind='reply' is nodig omdat deze tabel ook likes en announces
1054 // draagt, en die schrijven object_uri = '' (zie recordInteraction). Zonder de
1055 // WHEN bumpte elke inkomende like de rev, werd elke wachter gewekt en kreeg
1056 // die de hele collectie opnieuw terwijl er niets aan veranderd was: precies de
1057 // kosten die de 304 moest wegnemen. Bovendien belandde er dan een rij op de
1058 // lege string in ap_feed_state, die feedChangesSince vervolgens uitdeelt.
1059 // De oude cursor filterde hier wel op kind; bij ap_timeline is dit ook gedaan
1060 // (de UPDATE OF sluit liked/boosted uit) en één tabel verder vergeten.
1061 zet('trg_feed_ia_ins', 'INSERT', 'ap_interactions', '', '', '', `${joinPosts('NEW.object_uri', 'new')} WHERE p.id = NEW.post_id`, "NEW.kind = 'reply'"),
1062 zet('trg_feed_ia_upd', 'UPDATE OF content, media_json, quote_json, embed_json', 'ap_interactions', '', '', '', `${joinPosts('NEW.object_uri', 'updated')} WHERE p.id = NEW.post_id`, "NEW.kind = 'reply'"),
1063 zet('trg_feed_ia_del', 'DELETE', 'ap_interactions', '', '', '', `${joinPosts('OLD.object_uri', 'deleted')} WHERE p.id = OLD.post_id`, "OLD.kind = 'reply'"),
1064 ].join('\n'));
1065 } catch (e) {
1066 // Niet fataal: zonder deze tabel valt het wachten terug op "altijd de tijd
1067 // volmaken", en dat is traag maar niet stuk.
1068 console.error('❌ feed-state triggers:', e.message);
1069 }
1070}
1071
1072function ensureColumn(table, column, definition) {
1073 try {
1074 db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${definition}`);
1075 console.log(`🔧 Added column ${table}.${column}`);
1076 } catch (e) {
1077 // "duplicate column name" → already there. Anything else, surface it.
1078 if (!/duplicate column/i.test(e.message)) {
1079 console.error(`❌ ensureColumn(${table}.${column}):`, e.message);
1080 }
1081 }
1082}
1083
1084export default db;
Note: See TracBrowser for help on using the repository browser.