"""SQLite drafts store, shared with other vortex components via DRAFTS_DB.""" import os import sqlite3 from pathlib import Path SCHEMA = """ CREATE TABLE IF NOT EXISTS drafts( draft_id TEXT PRIMARY KEY, room_id TEXT NOT NULL, body TEXT NOT NULL, reply_to_event_id TEXT, created_by TEXT NOT NULL, created_at TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'draft', sent_event_id TEXT ); """ def connect(path: str | None = None) -> sqlite3.Connection: db = Path(path or os.environ.get("DRAFTS_DB", "/shared/drafts/drafts.db")) db.parent.mkdir(parents=True, exist_ok=True) con = sqlite3.connect(db) con.row_factory = sqlite3.Row con.execute("PRAGMA journal_mode=WAL") con.execute(SCHEMA) con.commit() return con def pending(con: sqlite3.Connection) -> list[sqlite3.Row]: return con.execute( "SELECT * FROM drafts WHERE status='draft' ORDER BY created_at" ).fetchall() def update_body(con: sqlite3.Connection, draft_id: str, body: str) -> None: con.execute("UPDATE drafts SET body=? WHERE draft_id=?", (body, draft_id)) con.commit() def mark( con: sqlite3.Connection, draft_id: str, status: str, sent_event_id: str | None = None, ) -> None: con.execute( "UPDATE drafts SET status=?, sent_event_id=? WHERE draft_id=?", (status, sent_event_id, draft_id), ) con.commit()