Repository Public
1// Operator CLI (runs on the node). It can issue invitations and show counts; it cannot read any vault content.2import { migrate, randomToken, sha256b64u, sql, config } from "./core.ts";34const [cmd, ...args] = process.argv.slice(2);5const opt = (name: string, d?: string) => { const i = args.indexOf(`--${name}`); return i >= 0 ? args[i + 1] : d; };6await migrate();7if (cmd === "invite") {8 const hours = Math.min(Number(opt("hours", "24")), 168), label = opt("for", "") ?? "";9 const token = randomToken(24);10 await sql`insert into invites (token_hash, label, expires_at) values (${sha256b64u(token)}, ${label}, now() + ${hours} * interval '1 hour')`;11 console.log(`Invitation for "${label}" (single use, ${hours} h):\n${config.origin}/#/join/${token}`);12} else if (cmd === "status") {13 const [r] = await sql`select (select count(*) from accounts)::int as accounts, (select count(*) from devices where revoked_at is null)::int as devices,14 (select count(*) from vaults where deleted_at is null)::int as vaults, (select count(*) from records)::int as records,15 (select coalesce(sum(size),0) from attachments)::bigint as attachment_bytes, (select count(*) from invites where used_at is null and expires_at > now())::int as open_invites`;16 console.log(r);17} else if (cmd === "revoke-sessions") {18 const [a] = await sql`select id from accounts where handle = ${args[0] ?? ""}`;19 if (!a) { console.error("unknown handle"); process.exit(1); }20 const r = await sql`delete from sessions where account_id = ${a.id} returning 1`;21 console.log(`${r.length} session(s) ended`);22} else {23 console.log("usage: cli invite --for <label> [--hours N] | status | revoke-sessions <handle>");24}25await sql.end();26