/** * WHO THE OWNER IS: the person in People the personal object names as its * owner (owner, 27 Oct 2026: "since people sign in with phone number, can't * we automatically assign the contact of that phone number with them? Or if * that phone number is not present in the contact list, create a person * automatically?"). * * The object decides it, never a phone: the link is the meta `owner_person_id`, * served with the rest of the database's meta (`BusinessMeta`, on * `/v1/definitions` and `/v1/me`), and every phone's copy takes it from there. * * It is settled at the start of every request a personal object answers, in * one cheap read while it stands: * * 1. a link that names a person still held and not in the bin stands; * 2. otherwise the person whose phone handle is the sign-in number * (`owner_phone`, matched by the handle's `normalised` digits, the import's * own normalisation) is linked — the oldest, when more than one carries it; * 3. otherwise, once the owner has signed in, a person is made with that * number as their one phone handle (confirmed: the sign-in proved it), * named what sign-in knows — the name the owner gave at sign-in, else the * kit's line for an owner who gave none (`people.owner.unnamed`, "You") — * never the number, which stays the number, and never a name made up (the * 362 walk, 6 Nov 2026: a fresh account's card was "+1555…", made at the * code's request before sign-in brought the name). A handle a binned * person still holds for the number moves to them: one number is one * handle in a database. * * PEOPLE SEALED (e2e rulings R1, R2; 16 Nov 2026): the * object cannot read a sealed handle, so it cannot find a contact by its * number. A link that stands still stands, and a person whose handle is * still open is still found; otherwise the object makes the owner's person * and phone handle sealed at arrival, under ids the sign-in number derives * (`ownerIds`), so the same number always finds the same card. That a * contact the phone holds IS the owner is the phone's to say, as it says * any other two people are one (merge_people). * * So a person merged away (merge_people bins the duplicate and moves its * handles to the survivor) or binned is followed to whoever carries the number * on the next request, and a new database's owner is a person from its first. */ import people from "../../../crates/cotton-core/kits/shell/lines/people.json"; import type { Engine, Outcome } from "./engine.ts"; import { normalizePhone } from "./import/phone.ts"; import { derivedId } from "./import/ids.ts"; import type { Store } from "./store.ts"; /** The kit's name for an owner who gave sign-in none (`people.owner.unnamed`): read from the kit file, so it is the kit's words. */ export const UNNAMED_OWNER: string = people.owner.unnamed; /** The meta key the link is kept under, and its name in `BusinessMeta`. */ export const OWNER_PERSON = "owner_person_id"; const PERSON = "person"; const HANDLE = "person_handle"; /** The person the link names while that person is held and not in the bin, else null. */ export function linkedOwner(store: Store): string | null { const id = store.meta(OWNER_PERSON); if (!id) return null; const row = store.get(PERSON, id); return row && !row.data.trashed_at ? row.id : null; } /** The oldest person held and not in the bin whose live phone handle is `digits`, else null. */ export function personCarrying(store: Store, digits: string): string | null { const hit = store.query<{ id: string }>( `SELECT p.id AS id FROM rows h JOIN rows p ON p.table_id = '${PERSON}' AND p.deleted = 0 AND json_extract(p.data, '$.trashed_at') IS NULL AND p.id = json_extract(h.data, '$.person_id') WHERE h.table_id = '${HANDLE}' AND h.deleted = 0 AND json_extract(h.data, '$.trashed_at') IS NULL AND json_extract(h.data, '$.kind') = 'phone' AND json_extract(h.data, '$.normalised') = ? ORDER BY p.created_at, p.id LIMIT 1`, digits, )[0]; return hit?.id ?? null; } /** What linking needs of the object: its store, and one write in its own transaction and version. */ export interface OwnerHost { store: Store; /** Whether the object defines People and their handles: an object without them names no owner. */ defines(table: string): boolean; write(fn: (engine: Engine, version: number, outcome: Outcome) => void): void; } /** * Settle the link (see above). Answers the person linked now, or null when * the object cannot name one (no owner number, or no People). */ export function linkOwner(host: OwnerHost): string | null { const { store } = host; const held = linkedOwner(store); if (held) return held; const phone = store.meta("owner_phone"); const digits = phone ? normalizePhone(phone) : null; if (!phone || !digits || !host.defines(PERSON) || !host.defines(HANDLE)) return null; // Nobody carries the number and the owner has not signed in: nothing is made until sign-in says who they are. const signedIn = store.personByPhone(phone); if (!signedIn && !personCarrying(store, digits)) return null; let linked: string | null = null; host.write((engine, version, outcome) => { linked = personCarrying(store, digits); if (!linked) { const name = signedIn?.name?.trim() || UNNAMED_OWNER; const made = engine.create(PERSON, { full_name: name }, version, outcome); // One number is one handle in a database: a handle a binned person still holds moves to the person made, never a second beside it. const held = phoneHandle(store, digits); const handle = { person_id: made.id, verified_at: engine.clock.now }; if (held) engine.update(held, { ...handle, trashed_at: null }, version, outcome); else engine.create(HANDLE, { ...handle, kind: "phone", value: phone, normalised: digits, source: "manual", position: 0 }, version, outcome); linked = made.id; } store.setMeta(OWNER_PERSON, linked); }); return linked; } /** The held phone handle whose normalised digits are `digits`, whoever holds it, else null. */ function phoneHandle(store: Store, digits: string) { const hit = store.query<{ id: string }>( `SELECT id FROM rows WHERE table_id = '${HANDLE}' AND deleted = 0 AND json_extract(data, '$.kind') = 'phone' AND json_extract(data, '$.normalised') = ? ORDER BY created_at, id LIMIT 1`, digits, )[0]; return hit ? store.get(HANDLE, hit.id) : null; } /** The ids the owner's person and phone handle are made under when People are sealed: the sign-in number's own. */ export function ownerIds(digits: string): { person: string; handle: string } { return { person: derivedId(PERSON, ["owner", digits]), handle: derivedId(HANDLE, ["owner", digits]) }; } /** What linking needs when People are sealed: a write whose new rows land sealed at arrival (src/business.ts `writeSealing`). */ export interface SealedOwnerHost extends Omit { writeSealed(fn: (engine: Engine, version: number, outcome: Outcome) => void): Promise; } /** * Settle the link while People are sealed (see above): a link that stands, a * person whose open handle carries the number, else the owner's own person * and handle made sealed at arrival under `ownerIds`. Answers null while it * cannot name one yet (no owner number, not signed in, or no arrival key * held: the next request asks again). */ export async function linkOwnerSealed(host: SealedOwnerHost): Promise { const { store } = host; const held = linkedOwner(store); if (held) return held; const phone = store.meta("owner_phone"); const digits = phone ? normalizePhone(phone) : null; if (!phone || !digits || !host.defines(PERSON) || !host.defines(HANDLE)) return null; const signedIn = store.personByPhone(phone); const open = personCarrying(store, digits); const ids = ownerIds(digits); const made = store.get(PERSON, ids.person); if (!open && !(made && !made.data.trashed_at) && !signedIn) return null; let linked: string | null = null; await host.writeSealed((engine, version, outcome) => { linked = personCarrying(store, digits); const mine = store.get(PERSON, ids.person); if (!linked && mine && !mine.data.trashed_at) linked = mine.id; if (!linked) { const name = signedIn?.name?.trim() || UNNAMED_OWNER; linked = engine.create(PERSON, { full_name: name }, version, outcome, 0, { id: ids.person }).id; if (!store.get(HANDLE, ids.handle)) engine.create(HANDLE, { person_id: linked, verified_at: engine.clock.now, kind: "phone", value: phone, normalised: digits, source: "manual", position: 0 }, version, outcome, 0, { id: ids.handle }); } store.setMeta(OWNER_PERSON, linked); }); return linked; }