chat drin

This commit is contained in:
th committed 2026-04-10 22:49:25 +02:00
1 parent ef238f3b9c
commit 988a5410f7
12 files changed
+1115 -4

No files matched your search

+19
View File
@@ -164,6 +164,23 @@ $db->exec('CREATE TABLE IF NOT EXISTS attachments (
FOREIGN KEY (recipient_id) REFERENCES tenants(id) ON DELETE CASCADE
)');
// Chat: separate from `messages` so chat traffic doesnt land in card inboxes.
// Same decrypt-reencrypt hop as messages: arrives encrypted with senders
// secret, stored at-rest under the inter-tenant key, returned re-encrypted
// with the recipients secret.
$db->exec('CREATE TABLE IF NOT EXISTS chat_messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
recipient_id INTEGER NOT NULL,
sender_id INTEGER,
sender_username TEXT,
body TEXT NOT NULL,
fetched INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (recipient_id) REFERENCES tenants(id) ON DELETE CASCADE,
FOREIGN KEY (sender_id) REFERENCES tenants(id) ON DELETE SET NULL
)');
$db->exec('CREATE INDEX IF NOT EXISTS idx_chat_recipient ON chat_messages(recipient_id, fetched, id)');
$db->exec('CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
tenant_id INTEGER NOT NULL,
@@ -181,6 +198,8 @@ $db->exec('CREATE TABLE IF NOT EXISTS rate_limits (
// Cleanup: 24h ephemeral storage, expired sessions, old rate limits
$db->exec("DELETE FROM messages WHERE created_at < datetime('now', '-24 hours')");
$db->exec("DELETE FROM attachments WHERE created_at < datetime('now', '-24 hours')");
// Chat: 7 days, longer than card sync but still ephemeral
$db->exec("DELETE FROM chat_messages WHERE created_at < datetime('now', '-7 days')");
$db->exec("DELETE FROM sessions WHERE expires_at < datetime('now')");
$db->exec("DELETE FROM rate_limits WHERE attempted_at < datetime('now', '-5 minutes')");