import { COLLECTIONS } from "../../../lib/collections.ts"; import { getDb } from "../index.ts"; // Collections whose row is uniquely identified by its AT-URI (no dedup on write). const TABLE_BY_ATURI: Record = { [COLLECTIONS.wiki]: "wikis", [COLLECTIONS.note]: "notes", [COLLECTIONS.noteRevision]: "revisions", }; function existsByAtUri(table: string, atUri: string): boolean { return ( getDb() .query(`SELECT 1 FROM ${table} WHERE at_uri = ? LIMIT 1`) .get(atUri) !== null ); } function existsByDidWiki( table: string, did: string, wikiAtUri: string, ): boolean { return ( getDb() .query(`SELECT 1 FROM ${table} WHERE did = ? AND wiki_at_uri = ? LIMIT 1`) .get(did, wikiAtUri) !== null ); } // Did a dispatched record actually land in the DB? The firehose handler silently // drops records it can't place or authorize, so "dispatched" never equals // "ingested" on its own. // // AT-URI is the right key for wiki/note/revision (unique, no replace). The // peripheral collections instead dedup by their logical identity — memberships by // (wiki, member), requests/bookmarks by (wiki, author) — and rewrite at_uri on // conflict, so they must be checked by that identity, not by at_uri. export function recordLanded( collection: string, did: string, atUri: string, value: unknown, ): boolean { const byAtUri = TABLE_BY_ATURI[collection]; if (byAtUri) return existsByAtUri(byAtUri, atUri); const v = (value ?? {}) as { wikiRef?: unknown; memberDid?: unknown }; const wikiRef = typeof v.wikiRef === "string" ? v.wikiRef : null; if (!wikiRef) return false; switch (collection) { case COLLECTIONS.membership: { const memberDid = typeof v.memberDid === "string" ? v.memberDid : null; return ( memberDid !== null && existsByDidWiki("memberships", memberDid, wikiRef) ); } case COLLECTIONS.memberRequest: return existsByDidWiki("requests", did, wikiRef); case COLLECTIONS.bookmark: return existsByDidWiki("bookmarks", did, wikiRef); default: return false; } }