/** * THE ONE GATE FOR FIRST CONTACT (DISCOVERY OFF 3, 27 Nov 2026). * * Anything that makes account A reach a number, or an account that has not * accepted A, passes here: a chat message, a wait or ask, an add from Your * people, a group envelope, a keys lookup, a prekey claim, a call's ring, a * shared list's telling. No route carries its own copy of these rules; each * calls one of the functions below (test/first-contact-gate.test.ts lists * every route that takes a number from a signed-in caller and proves it). * * TWO HALVES, one on each object: * * - On A's object, `firstContact` / `firstContacts`, before anything is looked * up: a number that is not already one of A's people (nor asking A, nor, for * a group's word, in a group A is in) costs one of A's new numbers, at most * NEW_NUMBERS_PER_HOUR and NEW_NUMBERS_PER_DAY, counted by distinct number. * Over either, refused the same whatever the number is. Nothing here reads * the directory, so the answer and its timing are the same whether or not * the number is on Cotton, has accepted A, or refused A. * * - On the target's object, `admits` / `admitsAny` / `askAdmits`: sealed words * (a message, a receipt, a group's envelope, a ring, a list's telling) and * key material (keys, one-time keys) go only to and from someone this owner * accepted, or a member of a group this owner's own phone said they are in. * Everyone else reaches this owner by an ask alone, which is dropped unsaid * when the asker is blocked, when the owner deleted their request * (`chat_ask_refused`) until the owner accepts them, or when it is over the * strangers' limits. What a stranger's object is answered is built from * nothing real (chat.ts `madeUpDevices`, `madeUpPrekey`), as for a number * nobody holds. * * So the first sealed message is made after accept: A asks (the ask carries * A's number and the account name A volunteered, never sealed words), B * accepts, A's phone hears `reach`, asks B's keys (now real), claims one-time * keys, and seals. Before that A's phone holds what A wrote. */import { blocked, count, hit, keyOf } from"../calling/calls.ts";import { refuse } from"../http.ts";const HOUR = 3_600_000;const DAY = 24 * HOUR;/** * New numbers one person may reach in an hour and in a day (DISCOVERY OFF 2, 27 Nov 2026): counted by distinct * number, so reaching the same one again costs nothing, and refused the same, in the same time, whatever the number. */exportconst NEW_NUMBERS_PER_HOUR = 5;exportconst NEW_NUMBERS_PER_DAY = 20;/** Asks (and a stranger's envelopes taken as their ask) from one stranger in an hour. */exportconst ASKS_FROM_ONE_HOUR = 5;/** Asks from all strangers together in an hour. */exportconst ASKS_FROM_ALL_HOUR = 60;/** One-time keys one accepted person (or group member) claims of this owner's devices in an hour: no one drains another's. */exportconst CLAIMS_FROM_ONE_HOUR = 32;/** The words the owner reads when they are over (the owner's to change; the core shows them as given). */exportconst OVER_HOUR = `You can start ${NEW_NUMBERS_PER_HOUR} new chats an hour. Try again later.`;exportconst OVER_DAY = `You can start ${NEW_NUMBERS_PER_DAY} new chats a day. Try again tomorrow.`;/** How a number is reached: one to one (a message, a wait, an add, a call), or as a member of a group (its envelope, keys, a claim). */exporttypeReach = "one" | "member";interfaceHost { sql: SqlStorage; ownerPhone: string | null;}/** Whether this owner accepted `number` (wrote to them, waited for them, or accepted their ask), blocked or not. */exportasyncfunction accepted(sql: SqlStorage, number: string): Promise<boolean> {return sql.exec("SELECT 1 FROM chat_peer WHERE key = ?", await keyOf(sql, number)).toArray().length > 0;}/** One of this owner's people: accepted and not blocked. Send, invitations, secrets, shared lists and spaces ask this. */exportasyncfunction isPerson(sql: SqlStorage, number: string): Promise<boolean> {return (await accepted(sql, number)) && !(await blocked(sql, number));}/** Whether `number` asks this owner (an ask held, not yet answered). */asyncfunction asking(sql: SqlStorage, number: string): Promise<boolean> {return sql.exec("SELECT 1 FROM chat_ask WHERE key = ?", await keyOf(sql, number)).toArray().length > 0;}/** Whether this owner's phone said `number` is in a group with them: `group` that one, or any. */asyncfunction member(sql: SqlStorage, number: string, group: string | null): Promise<boolean> {const key = await keyOf(sql, number);const rows = group ? sql.exec("SELECT 1 FROM chat_group WHERE group_id = ? AND key = ? LIMIT 1", group, key) : sql.exec("SELECT 1 FROM chat_group WHERE key = ? LIMIT 1", key);return rows.toArray().length > 0;}/** Already reached: one of the owner's people, someone asking them, a number picked within the day, or (for a group's word) a group member. */asyncfunction free(sql: SqlStorage, number: string, how: Reach, key: string): Promise<boolean> {if (sql.exec("SELECT 1 FROM chat_pick WHERE key = ?", key).toArray().length) returntrue;if ((await accepted(sql, number)) || (await asking(sql, number))) returntrue;return how === "member" && (await member(sql, number, null));}/** * A's half: each number in `numbers` that is new to A costs one of A's new numbers, all or none: over the hour's or * the day's, refused before any is counted. Never looks anything up. A's own number is refused by the caller. */exportasyncfunction firstContacts(host: Host, numbers: string[], how: Reach): Promise<void> {const now = Date.now(); host.sql.exec("DELETE FROM chat_pick WHERE at < ?", now - DAY);const fresh: string[] = [];for (const number ofnewSet(numbers)) {if (number === host.ownerPhone) continue;const key = await keyOf(host.sql, number);if (!(await free(host.sql, number, how, key))) fresh.push(key); }if (!fresh.length) return;const inHour = host.sql.exec<{ n: number }>("SELECT count(*) AS n FROM chat_pick WHERE at >= ?", now - HOUR).one().n;const inDay = host.sql.exec<{ n: number }>("SELECT count(*) AS n FROM chat_pick").one().n;if (inDay + fresh.length > NEW_NUMBERS_PER_DAY) refuse(429, "too_many_numbers", OVER_DAY);if (inHour + fresh.length > NEW_NUMBERS_PER_HOUR) refuse(429, "too_many_numbers", OVER_HOUR);for (const key of fresh) host.sql.exec("INSERT OR IGNORE INTO chat_pick (key, at) VALUES (?, ?)", key, now);}/** A's half for one number. */exportasyncfunction firstContact(host: Host, number: string, how: Reach): Promise<void> {await firstContacts(host, [number], how);}/** * The target's half for sealed words: `from` is one of this owner's people, or (for a group's word) a member of that * group as this owner's own phone said it. Anyone else's envelope, ring or telling is dropped unsaid. */exportasyncfunction admits(sql: SqlStorage, from: string, group: string | null): Promise<boolean> {if (await blocked(sql, from)) returnfalse;if (await accepted(sql, from)) returntrue;return group !== null && (await member(sql, from, group));}/** The target's half for key material (keys, one-time keys): one of this owner's people, or in any group with them. */exportasyncfunction admitsAny(sql: SqlStorage, from: string): Promise<boolean> {if (await blocked(sql, from)) returnfalse;return (await accepted(sql, from)) || (await member(sql, from, null));}/** The target's half for a claim of one-time keys: admitted, and under CLAIMS_FROM_ONE_HOUR for that claimer. */exportasyncfunction claimAdmits(sql: SqlStorage, from: string, handles: number): Promise<boolean> {if (!(await admitsAny(sql, from))) returnfalse;const who = `chat-claimed:${await keyOf(sql, from)}`;if (count(sql, who, HOUR) + handles > CLAIMS_FROM_ONE_HOUR) returnfalse;for (let n = 0; n < handles; n++) hit(sql, who);returntrue;}/** * The target's half for an ask from someone not yet accepted: dropped when they are blocked, when the owner deleted * their request (until the owner accepts them), or over the strangers' limits. True when it may be held; counted. */exportasyncfunction askAdmits(host: Host, from: string): Promise<boolean> {if (from === host.ownerPhone || (await blocked(host.sql, from))) returnfalse;const key = await keyOf(host.sql, from);if (host.sql.exec("SELECT 1 FROM chat_ask_refused WHERE key = ?", key).toArray().length) returnfalse;const who = `chat-ask:${key}`;if (count(host.sql, who, HOUR) >= ASKS_FROM_ONE_HOUR || count(host.sql, "chat-asks", HOUR) >= ASKS_FROM_ALL_HOUR) returnfalse; hit(host.sql, who); hit(host.sql, "chat-asks");returntrue;}