/** * A person's own object's side of shared spaces (src/space/space.ts): which spaces the person is in, who they are to * a space (their database, and the keys and name their own phones gave, never what an adder says), whether numbers * are their people, and the bytes they added to spaces. Shared bytes are their own tally (`space_bytes`), counted * beside `media_bytes` against the person's space (src/business.ts `mediaBudget`), so a media recount never loses * them. The space itself never lives here. */import { isPersonOf, ownKeys } from"../send/send.ts";import { LIMITS } from"./space.ts";exportconst SCHEMA = `CREATE TABLE IF NOT EXISTS space_in (space TEXT PRIMARY KEY, founded INTEGER NOT NULL DEFAULT 0, bytes INTEGER NOT NULL DEFAULT 0, at INTEGER NOT NULL, serial INTEGER NOT NULL DEFAULT 0);CREATE TABLE IF NOT EXISTS space_bytes (space TEXT PRIMARY KEY, bytes INTEGER NOT NULL);`;const SPACE_ID = /^[A-Za-z0-9_-]{22}$/;/** A `space_in` made before SHARED SPACE 3 gains its serial (a table only gains columns). */exportfunction upgrade(sql: SqlStorage): void {const columns = sql.exec<{ name: string }>("PRAGMA table_info(space_in)").toArray().map((c) => c.name);if (!columns.includes("serial")) sql.exec("ALTER TABLE space_in ADD COLUMN serial INTEGER NOT NULL DEFAULT 0");// SPACES 1: the app a space belongs to, so each app lists its own (a space joined before says Albums).if (!columns.includes("app")) sql.exec("ALTER TABLE space_in ADD COLUMN app TEXT NOT NULL DEFAULT 'albums'");}exportinterfacePersonHost { sql: SqlStorage; ownerPhone: string | null; databaseId: string | null; ownerName: string | null;/** What the person's space may still grow by, shared bytes counted (src/business.ts `mediaBudget`). */ left(): number;}/** Who a person is to a space: the database a membership is bound to for ever, and what their own phones posted. */exportinterfaceIdentity { database: string; key: string; sign: string; name: string | null;}exporttypeJoinAnswer = { ok: Identity } | { refused: "not_your_person" | "too_many_spaces" | "no_key" };/** Every byte this person added to spaces, still counted. */exportfunction sharedBytes(sql: SqlStorage): number {return sql.exec<{ n: number | null }>("SELECT SUM(bytes) AS n FROM space_bytes").one().n ?? 0;}/** The spaces this person is in, newest first. */exportfunction spacesIn(host: Pick<PersonHost, "sql">): { space: string; at: number; app: string }[] {return host.sql.exec<{ space: string; at: number; app: string }>("SELECT space, at, app FROM space_in ORDER BY at DESC").toArray();}/** * A space asks to take this person in, as its membership `serial`: only from one of their people (or themselves * founding), within the limits, once their phone posted both its sharing and its signing key. */exportasyncfunction join(host: PersonHost, space: string, from: string, founding: boolean, serial: number, app = "albums"): Promise<JoinAnswer> {if (!/^[a-z]{1,16}$/.test(app)) return { refused: "not_your_person" };if (!SPACE_ID.test(space) || !host.databaseId || !Number.isInteger(serial) || serial < 1) return { refused: "not_your_person" };if (founding ? from !== host.ownerPhone : !(await isPersonOf(host.sql, from))) return { refused: "not_your_person" };const keys = ownKeys(host.sql);if (!keys.share || !keys.sign) return { refused: "no_key" };const identity: Identity = { database: host.databaseId, key: keys.share, sign: keys.sign, name: host.ownerName };if (host.sql.exec("SELECT 1 FROM space_in WHERE space = ?", space).toArray().length) { host.sql.exec("UPDATE space_in SET serial = MAX(serial, ?) WHERE space = ?", serial, space);return { ok: identity }; }if (founding && host.sql.exec<{ n: number }>("SELECT COUNT(*) AS n FROM space_in WHERE founded = 1").one().n >= LIMITS.founded) return { refused: "too_many_spaces" };if (host.sql.exec<{ n: number }>("SELECT COUNT(*) AS n FROM space_in").one().n >= LIMITS.joined) return { refused: "too_many_spaces" }; host.sql.exec("INSERT INTO space_in (space, founded, bytes, at, serial, app) VALUES (?, ?, 0, ?, ?, ?)", space, founding ? 1 : 0, Date.now(), serial, app);return { ok: identity };}/** A space says this person is out of the membership `serial` names: a telling for an earlier membership than the one held now is let go. */exportfunction left(host: Pick<PersonHost, "sql">, space: string, serial: number): void { host.sql.exec("DELETE FROM space_in WHERE space = ? AND serial <= ?", space, serial);}/** Bytes this person added to a space (or took back): false, and nothing counted, when they would not fit. */exportfunction charge(host: PersonHost, space: string, delta: number): boolean {if (!SPACE_ID.test(space) || !Number.isFinite(delta) || !Number.isInteger(delta)) returnfalse;if (delta > 0 && delta > host.left()) returnfalse; host.sql.exec("INSERT INTO space_bytes (space, bytes) VALUES (?, MAX(0, ?)) ON CONFLICT (space) DO UPDATE SET bytes = MAX(0, bytes + ?)", space, delta, delta); host.sql.exec("DELETE FROM space_bytes WHERE bytes = 0");returntrue;}/** Which of these numbers are this person's people (a member is shown another's number only when they already have it). */exportasyncfunction peers(host: Pick<PersonHost, "sql">, numbers: string[]): Promise<string[]> {const out: string[] = [];for (const n of numbers.slice(0, LIMITS.members)) if (await isPersonOf(host.sql, n)) out.push(n);return out;}