'use strict'; /* * Reports: one `reports` row shared by every participant (`report_recipients`, with a per-user read flag). Kept * MILITARY.reportRetentionMs (real time); the `reportCleanup` event deletes expired ones and re-arms itself only while * reports remain. */ const D = require('../../public/js/data.js'); // o: { worldId, type, now, summary, data, recipients: [{ userId, role }] } -> report id function createReport(ctx, o) { const { db } = ctx; const r = db.run('INSERT INTO reports (world_id, type, created_at, expires_at, summary, data) VALUES (?, ?, ?, ?, ?, ?)', o.worldId, o.type, o.now, o.now + D.MILITARY.reportRetentionMs, JSON.stringify(o.summary || {}), JSON.stringify(o.data || {})); const id = Number(r.lastInsertRowid); const seen = new Set(); for (const rc of o.recipients) { if (!rc.userId || seen.has(rc.userId)) continue; seen.add(rc.userId); db.run('INSERT INTO report_recipients (report_id, user_id, role) VALUES (?, ?, ?)', id, rc.userId, rc.role); ctx.notify(rc.userId, o.worldId, 'report', { reportId: id, type: o.type, role: rc.role }); } ensureCleanup(ctx, o.worldId, o.now); return id; } function ensureCleanup(ctx, worldId, now) { const { db } = ctx; const pending = db.get("SELECT 1 AS x FROM events WHERE type = 'reportCleanup' AND world_id = ? AND status = 'pending' LIMIT 1", worldId); if (!pending) ctx.enqueue({ worldId, dueAt: now + D.MILITARY.reportRetentionMs, type: 'reportCleanup', payload: { worldId } }); } module.exports = { createReport, ensureCleanup };