// The buyer's conversations, kept in this browser only. There are no accounts: // a conversation is its thread key, which is also in the private link the // buyer can bookmark. Losing both loses the conversation, by design. // // Browser storage can be missing (private windows, blocked site data), so every // access is guarded and the page keeps working from memory. export interface StoredMessage { dir: "in" | "out"; n: number; sent_at: number; t: "text" | "grant"; text?: string; address?: string; phone?: string; email?: string; note?: string; valid_until?: number; /** Envelope hash, for messages received from the owner. */ hash?: string; } export interface Conversation { author: string; id: string; /** The thread secret, hex. */ secret: string; title: string; created: number; updated: number; messages: StoredMessage[]; /** Mailbox poll position (unix seconds). */ since: number; /** Envelopes already looked at (hash -> their expiry), so they are not verified again. */ seen: Record; } const KEY = "allmende.conversations.v1"; const cache = new Map(); let loaded = false; let persistent = true; const idOf = (author: string, id: string) => `${author}/${id}`; const HEX = (n: number) => new RegExp(`^[0-9a-f]{${n}}$`); function wellFormed(c: unknown): c is Conversation { if (typeof c !== "object" || c === null) return false; const x = c as Record; return ( typeof x.author === "string" && HEX(64).test(x.author) && typeof x.id === "string" && HEX(32).test(x.id) && typeof x.secret === "string" && HEX(64).test(x.secret) && typeof x.title === "string" && typeof x.created === "number" && typeof x.updated === "number" && Array.isArray(x.messages) && typeof x.since === "number" && typeof x.seen === "object" && x.seen !== null ); } function load(): void { if (loaded) return; loaded = true; try { const raw = localStorage.getItem(KEY); if (!raw) return; const parsed: unknown = JSON.parse(raw); if (typeof parsed !== "object" || parsed === null) return; for (const c of Object.values(parsed)) { if (wellFormed(c)) cache.set(idOf(c.author, c.id), c); } } catch { persistent = false; } } function flush(): void { try { localStorage.setItem(KEY, JSON.stringify(Object.fromEntries(cache))); } catch { persistent = false; } } /** False when this browser refuses to store anything, so conversations vanish with the page. */ export function isPersistent(): boolean { load(); if (persistent) { try { localStorage.setItem(KEY + ".probe", "1"); localStorage.removeItem(KEY + ".probe"); } catch { persistent = false; } } return persistent; } export function getConversation(author: string, id: string): Conversation | undefined { load(); return cache.get(idOf(author, id)); } export function saveConversation(c: Conversation): void { load(); cache.set(idOf(c.author, c.id), c); flush(); } export function deleteConversation(author: string, id: string): void { load(); cache.delete(idOf(author, id)); flush(); } /** Most recently active first. */ export function listConversations(): Conversation[] { load(); return [...cache.values()].sort((a, b) => b.updated - a.updated); } /** The private link to a conversation. The secret stays in the URL fragment, which is never sent to a server. */ export function conversationHash(author: string, id: string, secret: string): string { return `#/c/${author}/${id}/${secret}`; }