/** * THE MESSAGE IMPORT ENGINE: cotton-rules' `import::messages_engine` (with * `message_changes`), the phone's own, run as WASM over the object * (`driver.ts`). One answer to "who is this message from, which thread is it * in, which contact does it resolve to, and what does a re-import skip". * * Two passes, as the phone runs them: `planImport` reads the object and * returns the thread, its contacts, its participants, its messages and its * reactions as changes, which land; then `planReplyLinks` reads the rows that * landed and links each reply whose quoted line is now present. */importtype { Change } from"./driver.ts";import { objectChanges, runEngine } from"./driver.ts";importtype { ImportBatch } from"./envelope.ts";importtype { Reader } from"./reader.ts";exportinterfaceChatResult { phone: string | null; contactId: string | null; threadId: string; inserted: number; skipped: number;/** Always 0 per chat: the reply pass is per run. */ unresolvedReplies: number;}exportinterfaceGroupResult { threadId: string; title: string; externalKey: string; participantCount: number; threadCreated: boolean;}exportinterfaceImportPlan { changes: Change[]; chats: ChatResult[]; group: GroupResult | null;}exportinterfaceImportArgs { batch: ImportBatch;/** What every derived contact, handle and thread id is made under: the object's namespace, never the batch's `workspaceId`. */ namespace: string;/** The batch's one stamp for every row it creates. */ now: string; newId: () => string;}/** Everything one batch owes the object, in the order it must land. */exportfunction planImport(db: Reader, args: ImportArgs): ImportPlan {const plan = runEngine<ImportPlan>("import", { batch: args.batch, namespace: args.namespace, now: args.now }, db, args.newId);return { ...plan, changes: objectChanges(plan.changes) };}/** Link every reply in this thread whose quoted line is now present; a message quoting itself stays unlinked. */exportfunction planReplyLinks(db: Reader, threadId: string, channel: string): { changes: Change[]; unresolved: number } {const links = runEngine<{ changes: Change[]; unresolved: number }>("replies", { threadId, channel }, db);return { ...links, changes: objectChanges(links.changes) };}// ── changes to messages already held ───────────────────────────────────────exporttypeMessageChange = | { kind: "edit"; externalId: string; body: string; at: string } | { kind: "revoke"; externalId: string; at: string } | { kind: "reaction"; externalId: string; actor: string; emoji: string; at: string };/** At most this many changes in one call. */exportconst CHANGES_PER_CALL = 1000;/** Why a `message_changes` body is not one, or null. */exportfunction changesProblem(body: Record<string, unknown>): string | null {if (typeof body.workspaceId !== "string" || !body.workspaceId) return"workspaceId is required";if (typeof body.channel !== "string" || !body.channel) return"channel is required";if (!Array.isArray(body.changes)) return"changes must be a list";if (body.changes.length > CHANGES_PER_CALL) return`send at most ${CHANGES_PER_CALL} changes at a time`;const str = (v: unknown): boolean => typeof v === "string";for (const [i, c] of (body.changes as unknown[]).entries()) {const at = `changes[${i}]`;if (!c || typeof c !== "object" || Array.isArray(c)) return`${at} must be an object`;const change = c asRecord<string, unknown>;if (!str(change.externalId) || !change.externalId) return`${at}.externalId is required`;if (!str(change.at) || Number.isNaN(Date.parse(change.at as string))) return`${at}.at must be an ISO-8601 time`;if (change.kind === "edit") {if (!str(change.body)) return`${at}.body is required for an edit`; } elseif (change.kind === "reaction") {if (!str(change.actor) || !change.actor) return`${at}.actor is required for a reaction`;if (!str(change.emoji)) return`${at}.emoji is required for a reaction ("" takes it off)`; } elseif (change.kind !== "revoke") return`${at}.kind is edit, revoke or reaction`; }returnnull;}exportinterfaceChangesReport { edited: number; revoked: number; reacted: number; unreacted: number; missing: number;}/** An edit, a delete for everyone, a reaction on or off, on the row the message's own id names; a change to a message not held is counted, never kept. */exportfunction planMessageChanges(db: Reader, workspaceId: string, channel: string, changes: MessageChange[]): { changes: Change[]; report: ChangesReport } {// The route's wire names the message `externalId`; the engine's `MessageChange` names its fields in snake case.const asRust = changes.map(({ externalId, ...rest }) => ({ ...rest, external_id: externalId }));const plan = runEngine<{ changes: Change[]; report: ChangesReport }>("changes", { workspaceId, channel, changes: asRust }, db);return { ...plan, changes: objectChanges(plan.changes) };}