Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Sign out, and every page shows who you are, even when the arm is off #587
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
quill/arm:quill/580-sign-out-and-every-page-shows-who-you-ar → main 2 commits fork 1 thread · 1 unresolved
13 files
changed +383
−18
Software/so101/README.md +11
| @@ -17,6 +17,17 @@ is calibrated again, one at a time. Then the leader drives the follower, 60 Hz, | ||
| 17 | 17 | skips the questions (`bun so101 teleop --leader P --follower P [--save]` names the ports, and `--save` remembers them). `bun so101 setup --dry-run` shows the plan without touching an arm. Assembly and calibration background: |
| 18 | 18 | https://huggingface.co/docs/lerobot/so101 |
| 19 | 19 | |
| 20 | ## Who you are, and signing out | |
| 21 | ||
| 22 | Every page says who you are, the arm's page, the resting card and `/admin` alike: **Signed in as *name* (this app)** for a name picked here, **Signed in as *name* (forge)** for a forge account, each with **Sign out**; a guest sees **Watching as a guest · Sign in** (the forge's sign-in, back to the same page). Sign out is a same-origin form post to `POST /signout` (src/relay.ts, src/signed-in.ts), and works while the arm is closed: | |
| 23 | ||
| 24 | - **A name picked here:** the session ends by its token's hash (the name stays taken, so nobody else can pass for it), the `arm_session` cookie comes back expired (`Max-Age=0`, the same Path and SameSite), and you land on `/` as a guest. The forge is not touched. That name is gone for this browser: pick a new one, or sign in with the forge. | |
| 25 | - **A forge account:** the same, then on to the forge's own `/__forge/signout`, which ends the forge's sign-in for this app. **Switch forge account** links to the forge itself, where its own Sign out is (the forge refuses a sign-out posted from another site). | |
| 26 | ||
| 27 | In the terminal, `/whoami` says the same thing in words ("Signed in as trevato (forge)", or "Watching as a guest"). The terminal keeps no session to end: the forge signs whoever your SSH key belongs to again on every connection, so `/signout` says so, and you leave with `/quit` and come back with another key to be someone else. | |
| 28 | ||
| 29 | Signed out, the Queue tab offers **Sign in with the forge** first and picking a name second, so an owner who was stuck in another name gets back to `/admin` in one click. | |
| 30 | ||
| 20 | 31 | ## Open / Closed |
| 21 | 32 | |
| 22 | 33 | The owner can close the whole app when the shop is dark: on `/admin`, **Open / Closed** has **Close the arm** with an optional note for visitors ("Back Saturday 10am CT", at most 140 characters) and an optional "back around" time (the owner's local time); **Open the arm** undoes it. In the terminal: `/close [note]` and `/open`. Over `/live`, the owner sends `{"type": "open", "open": false, "note": "…", "reopenAt": "2026-10-31T10:00-05:00"}`. Every change is in the admin log; the switch is kept in `so101/open.json` under `FORGE_APP_DATA` (src/opening.ts). |
Software/so101/src/accounts.ts +12
| @@ -101,6 +101,9 @@ export function sessionToken(cookie: string | undefined): string | null { | ||
| 101 | 101 | /** The Set-Cookie header for a session: a year, this site only, never readable by script. */ |
| 102 | 102 | export const sessionCookie = (token: string) => `${COOKIE}=${token}; Path=/; Max-Age=${COOKIE_MAX_AGE_S}; HttpOnly; Secure; SameSite=Lax`; |
| 103 | 103 | |
| 104 | /** The Set-Cookie header that ends a session: the same cookie, the same Path and SameSite, empty and already expired. */ | |
| 105 | export const expiredSessionCookie = `${COOKIE}=; Path=/; Max-Age=0; HttpOnly; Secure; SameSite=Lax`; | |
| 106 | ||
| 104 | 107 | interface UserRow { |
| 105 | 108 | id: string; |
| 106 | 109 | name: string; |
| @@ -161,6 +164,15 @@ export class Accounts { | ||
| 161 | 164 | return { id: row.id, name: row.name, owner: false }; |
| 162 | 165 | } |
| 163 | 166 | |
| 167 | /** | |
| 168 | * End the session this token opens, found by its hash. The row stays with a token nobody holds, so the name stays | |
| 169 | * taken (nobody else can pass for whoever used it) and still counts toward its address's hourly limit. True when it held one. | |
| 170 | */ | |
| 171 | signOut(token: string | null): boolean { | |
| 172 | if (!token || !TOKEN.test(token)) return false; | |
| 173 | return this.db.query("UPDATE users SET token = ? WHERE token = ?").run(hash(randomBytes(32).toString("base64url")), hash(token)).changes > 0; | |
| 174 | } | |
| 175 | ||
| 164 | 176 | /** A forge user's name is theirs here too: nobody may join under it. */ |
| 165 | 177 | reserve(name: string): void { |
| 166 | 178 | this.db.query("INSERT OR IGNORE INTO reserved (key, name) VALUES (?, ?)").run(name.toLowerCase(), name); |
Software/so101/src/admin-page.ts +4
| @@ -17,6 +17,7 @@ import { ALIGNED_DEG, REFERENCES, alignment } from "./joint-reference.ts"; | ||
| 17 | 17 | import { servosSection, type ServoPanel } from "./servo-panel.ts"; |
| 18 | 18 | import { videoSection, type VideoCard } from "./video-panel.ts"; |
| 19 | 19 | import { NOTE_CHARS, OFFLINE_AFTER_MS, type OpenState, type Resting } from "./opening.ts"; |
| 20 | import { renderSignedIn, SIGNED_IN_STYLE, type SignedIn } from "./signed-in.ts"; | |
| 20 | 21 | |
| 21 | 22 | export const esc = (text: string): string => text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`); |
| 22 | 23 | |
| @@ -200,6 +201,8 @@ export interface Target { id: string; name: string } | ||
| 200 | 201 | |
| 201 | 202 | export interface AdminPage { |
| 202 | 203 | who: Who; |
| 204 | /** The identity line at the top (src/signed-in.ts); absent shows none. */ | |
| 205 | signedIn?: SignedIn | null; | |
| 203 | 206 | /** The owner may change things; a moderator only looks. */ |
| 204 | 207 | canEdit: boolean; |
| 205 | 208 | settings: AdminSettings; |
| @@ -356,6 +359,7 @@ export function renderAdmin(p: AdminPage): string { | ||
| 356 | 359 | `<header> |
| 357 | 360 | <h1>Admin</h1> |
| 358 | 361 | <div class="state"><span class="pill${s.paused || p.resting ? " paused" : ""}">${p.resting ? (p.resting.reason === "offline" ? "Offline" : "Closed") : s.paused ? "Paused" : "Open"}</span><span>${d ? `<b>${esc(d.name)}</b> has control` : "Nobody has control"}</span><span><b>${p.snapshot.queue.length}</b> waiting</span><a href="/admin/data">Data</a><a href="/admin/camera-list">Cameras</a><a href="/">Back to the arm</a></div> |
| 362 | ${p.signedIn ? renderSignedIn(p.signedIn, "/admin") : ""} | |
| 359 | 363 | </header> |
| 360 | 364 | ${flash} |
| 361 | 365 | ${openSection(p)} |
Software/so101/src/admin-routes.ts +1 −1
| @@ -63,7 +63,7 @@ export async function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: Se | ||
| 63 | 63 | const servos = canEdit && (req.method === "GET" || req.method === "HEAD") && askServos ? await askServos() : undefined; |
| 64 | 64 | const video = askVideo?.(); |
| 65 | 65 | const show = (status: number, done: Done | null, error: string | null) => |
| 66 | html(status, renderAdmin({ precision, servos, video, versions: versions?.(), who, canEdit, settings: lobby.admin.settings, opening: lobby.opening.state, resting: lobby.resting(), snapshot: lobby.turns.snapshot(), log: lobby.admin.log, removed: lobby.chat.removed, muted: lobby.chat.mutedList(), banned: lobby.accounts.bans(), health: lobby.reports.rows(), reports: lobby.lineReports.list(), visitors: canEdit ? { peak: lobby.visitors.peakToday(), hours: lobby.visitors.joinsPerHour() } : undefined, done, error }), canEdit); | |
| 66 | html(status, renderAdmin({ precision, servos, video, versions: versions?.(), who, signedIn: lobby.signedIn(req), canEdit, settings: lobby.admin.settings, opening: lobby.opening.state, resting: lobby.resting(), snapshot: lobby.turns.snapshot(), log: lobby.admin.log, removed: lobby.chat.removed, muted: lobby.chat.mutedList(), banned: lobby.accounts.bans(), health: lobby.reports.rows(), reports: lobby.lineReports.list(), visitors: canEdit ? { peak: lobby.visitors.peakToday(), hours: lobby.visitors.joinsPerHour() } : undefined, done, error }), canEdit); | |
| 67 | 67 | if (req.method === "GET" || req.method === "HEAD") { |
| 68 | 68 | const done = url.searchParams.get("done"); |
| 69 | 69 | show(200, done && Object.hasOwn(DONE, done) ? (done as Done) : null, null); |
Software/so101/src/lobby.ts +9 −2
| @@ -32,7 +32,7 @@ | ||
| 32 | 32 | * {"type": "clock", "offsetMs", "readBack"} the page's offset to the relay and the read-back hops it saw (src/page-clock.ts) |
| 33 | 33 | * {"type": "pong"} the answer to {"type": "ping"} |
| 34 | 34 | * {"type": "history", "before": 12} anyone: up to HISTORY_PAGE lines older than that one |
| 35 | * Server → client: hello (who you are, and whether you may moderate: delete a line), you (an anonymous socket only: its guest name and colour, once), state (the turn, the line, pause, the announcement, the speed cap, and `resting`: null, or why the arm rests), | |
| 35 | * Server → client: hello (who you are, by which sign-in ("via": "app", "forge", "local" or null), and whether you may moderate: delete a line), you (an anonymous socket only: its guest name and colour, once), state (the turn, the line, pause, the announcement, the speed cap, and `resting`: null, or why the arm rests), | |
| 36 | 36 | * chat (the last CHAT_FIRST lines of the log, and whether there are more), history (an older page, the same way), |
| 37 | 37 | * said, unsaid, muted, admin (owners only: the settings and the admin log), crew (owners only: |
| 38 | 38 | * settings, hidden lines, mute suggestions), banned (owners and moderators), words (owners only), reports (the owner and verified moderators: the |
| @@ -62,6 +62,7 @@ import { Turns, type TurnEvent } from "./turns.ts"; | ||
| 62 | 62 | import { parseReport, ViewerReports } from "./viewer-reports.ts"; |
| 63 | 63 | import { wallMs } from "./clock.ts"; |
| 64 | 64 | import { parseClock } from "./page-clock.ts"; |
| 65 | import { signedIn, type SignedIn } from "./signed-in.ts"; | |
| 65 | 66 | import { Opening, parseOpenChange, resting, restingWords, type ArmSeen, type Resting } from "./opening.ts"; |
| 66 | 67 | |
| 67 | 68 | /** How long a mute lasts, by the name the page and the terminal send; null is for good. */ |
| @@ -235,6 +236,12 @@ export class Lobby { | ||
| 235 | 236 | return this.local ? LOCAL : identify(req.headers, this.key); |
| 236 | 237 | } |
| 237 | 238 | |
| 239 | /** Who this request is, for the identity line on every page (src/signed-in.ts): null for a guest. */ | |
| 240 | signedIn(req: IncomingMessage): SignedIn | null { | |
| 241 | const who = this.who(req); | |
| 242 | return signedIn(who, this.verified(req), this.local); | |
| 243 | } | |
| 244 | ||
| 238 | 245 | /** The one moderator test: the owner, or a listed moderator who holds that forge identity (a guest who types the same name is not them). */ |
| 239 | 246 | mayModerate(who: Person | null, verified: boolean): boolean { |
| 240 | 247 | return Boolean(who && (who.owner || (verified && this.admin.isModerator(who.name)))); |
| @@ -818,7 +825,7 @@ export class Lobby { | ||
| 818 | 825 | |
| 819 | 826 | private hello(seat: Seat) { |
| 820 | 827 | const who = seat.who; |
| 821 | return { type: "hello", you: who, moderator: Boolean(who && !who.owner && seat.verified && this.admin.isModerator(who.name)), signin: !this.local, muted: who ? this.chat.muted.has(who.id) : false, banned: who ? this.barred(who, seat.from) : false }; | |
| 828 | return { type: "hello", you: who, moderator: Boolean(who && !who.owner && seat.verified && this.admin.isModerator(who.name)), signin: !this.local, via: signedIn(who, seat.verified ? who : null, this.local)?.via ?? null, muted: who ? this.chat.muted.has(who.id) : false, banned: who ? this.barred(who, seat.from) : false }; | |
| 822 | 829 | } |
| 823 | 830 | |
| 824 | 831 | private bannedList() { |
Software/so101/src/relay.ts +31 −3
| @@ -35,6 +35,8 @@ | ||
| 35 | 35 | * command in and out with every safety clamp, pushed as it happens (src/machine-feed.ts) |
| 36 | 36 | * POST /join {"name": "…"}: become someone here, no forge account needed (src/accounts.ts); the answer |
| 37 | 37 | * sets the HttpOnly session cookie and names you: {"you": {"id", "name", "owner"}} |
| 38 | * POST /signout [forge=1]: end this browser's session here, expire its cookie, 303 to / (with forge=1, to the | |
| 39 | * forge's /__forge/signout, which ends its sign-in for this app); works while resting too | |
| 38 | 40 | * GET /admin, POST /admin the owner's page: pause, end a turn, take control, the settings, the log |
| 39 | 41 | * GET /precision?hours=1-720 owner and moderators: the arm's precision scorecard (src/precision-history.ts), with the relay's |
| 40 | 42 | * H.264 numbers per camera under "video" (src/video-stats.ts); 404 to anyone else |
| @@ -83,7 +85,7 @@ import { perServo } from "./machine-log.ts"; | ||
| 83 | 85 | import { parseFrame } from "./safety.ts"; |
| 84 | 86 | import { acceptsGzip, GzipCache, worthCompressing } from "./compress.ts"; |
| 85 | 87 | import { browserCookie, browserId } from "./browser-id.ts"; |
| 86 | import { AccountError, sessionCookie, visitor } from "./accounts.ts"; | |
| 88 | import { AccountError, expiredSessionCookie, sessionCookie, visitor } from "./accounts.ts"; | |
| 87 | 89 | import { CameraSocket } from "./camera-socket.ts"; |
| 88 | 90 | import { OFFLINE_FRAME } from "./offline-frame.ts"; |
| 89 | 91 | import { ServoBaseline } from "./servo-baseline.ts"; |
| @@ -110,6 +112,7 @@ import { Clock, CommandGate, HELD_CLOSE, isRelease, parseCommand } from "./comma | ||
| 110 | 112 | import { FLOOD_CLOSE, FrameRate, frameBytes, MAX_FRAME_BYTES, TOO_BIG_CLOSE } from "./frame-limit.ts"; |
| 111 | 113 | import { restingWords, statusFields, type Resting } from "./opening.ts"; |
| 112 | 114 | import { RESTING_CSP, renderResting } from "./resting-page.ts"; |
| 115 | import { renderSignedIn } from "./signed-in.ts"; | |
| 113 | 116 | |
| 114 | 117 | export interface Remote { |
| 115 | 118 | /** The arm server's HTTP base, no trailing slash. */ |
| @@ -477,7 +480,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 477 | 480 | const restingPage = (req: IncomingMessage, res: ServerResponse, rest: Resting) => { |
| 478 | 481 | const who = lobby?.who(req); |
| 479 | 482 | res.writeHead(200, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store", "content-security-policy": RESTING_CSP }); |
| 480 | res.end(renderResting(rest, who ? (who.owner ? "owner" : "user") : "anonymous")); | |
| 483 | res.end(renderResting(rest, who ? (who.owner ? "owner" : "user") : "anonymous", lobby ? lobby.signedIn(req) : null)); | |
| 481 | 484 | }; |
| 482 | 485 | /** |
| 483 | 486 | * The arm's /status, saying which arm it is: from the link while it is live, the last one it had while reconnecting, |
| @@ -681,7 +684,9 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 681 | 684 | const page = async (req: IncomingMessage, res: ServerResponse, { response }: Awaited<ReturnType<typeof ask>>) => { |
| 682 | 685 | const html = await response.text(); |
| 683 | 686 | const boot = JSON.stringify({ link: linkBody(), cameras: cameras.list().map(({ id, name, online }) => ({ id, name, online })) }).replace(/</g, "\\u003c"); |
| 684 | const body = Buffer.from(response.status === 200 ? html.replace("</head>", `<script type="application/json" id="boot">${boot}</script>\n</head>`) : html); | |
| 687 | // Who is looking goes in server-side (#580), so the first paint already says it; the page keeps it current after a join. | |
| 688 | const line = lobby && !lobby.local ? renderSignedIn(lobby.signedIn(req), "/") : ""; | |
| 689 | const body = Buffer.from(response.status === 200 ? html.replace("</head>", `<script type="application/json" id="boot">${boot}</script>\n</head>`).replace("<!--signed-in-->", line) : html); | |
| 685 | 690 | const headers: Record<string, string> = { "content-type": response.headers.get("content-type") ?? "text/html; charset=utf-8", "cache-control": "no-store", vary: "accept-encoding" }; |
| 686 | 691 | const browser = browserCookie(req.headers.cookie); |
| 687 | 692 | if (browser) headers["set-cookie"] = browser; |
| @@ -770,6 +775,24 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 770 | 775 | } |
| 771 | 776 | }; |
| 772 | 777 | |
| 778 | /** | |
| 779 | * Sign out: end this browser's session here (by its token's hash) and expire its cookie, then go home. With forge=1 | |
| 780 | * the forge's own /__forge/signout comes next, which drops its sign-in for this app and then goes home itself. | |
| 781 | * Works whether the arm is open or resting; nothing about it reaches an arm. | |
| 782 | */ | |
| 783 | const signOut = async (req: IncomingMessage, res: ServerResponse) => { | |
| 784 | let form: URLSearchParams; | |
| 785 | try { | |
| 786 | form = new URLSearchParams((await readBody(req, MAX_JOIN_BODY)).toString("utf8")); | |
| 787 | } catch { | |
| 788 | res.setHeader("connection", "close"); | |
| 789 | return json(res, 413, { error: "that form is too large" }); | |
| 790 | } | |
| 791 | lobby?.accounts.signOut(visitor(req).token); | |
| 792 | res.writeHead(303, { location: form.get("forge") === "1" ? "/__forge/signout" : "/", "set-cookie": expiredSessionCookie, "cache-control": "no-store" }); | |
| 793 | res.end(); | |
| 794 | }; | |
| 795 | ||
| 773 | 796 | /** Resume once the arm server has: one that is still held would refuse the stream anyway. One too old for /resume (404) has no hold of its own. */ |
| 774 | 797 | const resume = async (res: ServerResponse, by: string) => { |
| 775 | 798 | const real = Boolean(remote && onArm()); |
| @@ -815,6 +838,11 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 815 | 838 | if (url.pathname === "/precision/trace") return await traceRead(req, res, url); |
| 816 | 839 | if (url.pathname === "/precision") return await precisionRead(req, res, url); |
| 817 | 840 | if (url.pathname === LEADER) return await leaderCalibration(req, res); |
| 841 | if (url.pathname === "/signout") { | |
| 842 | if (req.method === "POST") return await signOut(req, res); | |
| 843 | res.setHeader("allow", "POST"); | |
| 844 | return json(res, 405, { error: "POST to sign out" }); | |
| 845 | } | |
| 818 | 846 | if (url.pathname === "/join") { |
| 819 | 847 | if (req.method === "POST") return await join(req, res); |
| 820 | 848 | res.setHeader("allow", "POST"); |
Software/so101/src/resting-page.ts +16 −6
| @@ -2,9 +2,10 @@ | ||
| 2 | 2 | * The page a visitor gets at / while the arm rests (src/opening.ts): one calm card where the camera would be, saying |
| 3 | 3 | * why (the owner closed it, or the arm is offline), the owner's note, when it should be back and when it was last |
| 4 | 4 | * open, with a way to be told when it is back, the story and the code. No camera, no controls, no error banner. |
| 5 | * The owner is never locked out (#578): an anonymous visitor gets a quiet "Owner? Sign in to open" link to the forge's | |
| 6 | * sign-in, which comes back to /admin; a signed-in owner gets an Open now button right on the card (a same-origin POST | |
| 7 | * to /admin, which checks it is the owner again) and a link to /admin. | |
| 5 | * The owner is never locked out (#578): an anonymous visitor gets "Watching as a guest · Owner? Sign in to open", a link | |
| 6 | * to the forge's sign-in, which comes back to /admin; anyone signed in sees who they are and Sign out (#580); the owner | |
| 7 | * also gets an Open now button right on the card (a same-origin POST to /admin, which checks it is the owner again) and | |
| 8 | * a link to /admin. | |
| 8 | 9 | * |
| 9 | 10 | * Server-rendered from plain data, every value escaped; times go out as UTC text and the script rewrites them in the |
| 10 | 11 | * visitor's own zone. The script asks GET /status every 20 s: when it says open, it shows the notification the visitor |
| @@ -15,6 +16,7 @@ | ||
| 15 | 16 | import { createHash } from "node:crypto"; |
| 16 | 17 | import { esc } from "./admin-page.ts"; |
| 17 | 18 | import { restingWords, type Resting } from "./opening.ts"; |
| 19 | import { renderSignedIn, SIGNED_IN_STYLE, type SignedIn } from "./signed-in.ts"; | |
| 18 | 20 | |
| 19 | 21 | /** The public repository, for "the code". */ |
| 20 | 22 | export const REPO_URL = "https://forge.open-platform.sh/trevato/arm"; |
| @@ -45,6 +47,7 @@ a { color: var(--accent); display: inline-flex; align-items: center; min-height: | ||
| 45 | 47 | .owner { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem 1.25rem; } |
| 46 | 48 | .quiet { color: var(--dim); font-size: .9rem; } |
| 47 | 49 | form { margin: 0; } |
| 50 | ${SIGNED_IN_STYLE} | |
| 48 | 51 | @media (min-width: 720px) { |
| 49 | 52 | main { padding: 3rem 2rem; } |
| 50 | 53 | .card { max-width: 40rem; padding: 2.5rem 2.75rem; gap: 1.25rem; } |
| @@ -96,16 +99,22 @@ export const OWNER_SIGNIN = "/__forge/signin?to=%2Fadmin"; | ||
| 96 | 99 | /** Who is looking at the card: anonymous, signed in, or the owner. */ |
| 97 | 100 | export type Viewer = "anonymous" | "user" | "owner"; |
| 98 | 101 | |
| 99 | /** The owner's way back in: sign in (anonymous), nothing (a signed-in visitor who is not the owner), or Open now. */ | |
| 102 | /** The owner's Open now, for the owner alone; anyone else's way in is on the identity line. */ | |
| 100 | 103 | function ownerRow(viewer: Viewer, r: Resting): string { |
| 101 | if (viewer === "anonymous") return `<p class="owner quiet">Owner? <a href="${OWNER_SIGNIN}">Sign in to open</a></p>`; | |
| 102 | 104 | if (viewer !== "owner") return ""; |
| 103 | 105 | const label = r.reason === "offline" ? "Open anyway" : "Open now"; |
| 104 | 106 | return `<div class="owner"><form method="post" action="/admin"><input type="hidden" name="action" value="open"><input type="hidden" name="then" value="home"><button>${label}</button></form><a href="/admin">Admin</a></div>`; |
| 105 | 107 | } |
| 106 | 108 | |
| 109 | /** | |
| 110 | * Who is looking, with Sign out (#580): a guest is offered the owner's sign-in, which comes back to /admin. Signed in as | |
| 111 | * anyone, the line says so, so someone signed in as the wrong name can always get out, closed or not. | |
| 112 | */ | |
| 113 | const identityRow = (viewer: Viewer, signedIn: SignedIn | null): string => | |
| 114 | viewer !== "anonymous" && !signedIn ? "" : renderSignedIn(viewer === "anonymous" ? null : signedIn, "/", `<span>Owner? <a href="${OWNER_SIGNIN}">Sign in to open</a></span>`); | |
| 115 | ||
| 107 | 116 | /** The resting card as a whole page. */ |
| 108 | export function renderResting(r: Resting, viewer: Viewer = "anonymous"): string { | |
| 117 | export function renderResting(r: Resting, viewer: Viewer = "anonymous", signedIn: SignedIn | null = null): string { | |
| 109 | 118 | const title = restingWords(r); |
| 110 | 119 | const description = r.note || (r.reason === "offline" ? "The arm can't be reached from here just now; it comes back by itself." : "The owner has closed the arm for now."); |
| 111 | 120 | const when = [ |
| @@ -134,6 +143,7 @@ ${r.note ? `<p class="note">${esc(r.note)}</p>` : `<p class="note">${esc(descrip | ||
| 134 | 143 | ${when ? `<div class="when">${when}</div>` : ""} |
| 135 | 144 | <div class="notify"><button type="button" id="notify" hidden>Get notified when it's back</button><p class="said" id="said" role="status"></p></div> |
| 136 | 145 | ${ownerRow(viewer, r)} |
| 146 | ${identityRow(viewer, signedIn)} | |
| 137 | 147 | <nav aria-label="While you wait"><a href="${STORY_PATH}">Read the story of the arm</a><a href="${REPO_URL}">See the code</a></nav> |
| 138 | 148 | </section> |
| 139 | 149 | </main> |
Software/so101/src/signed-in.ts added +64
| @@ -0,0 +1,64 @@ | ||
| 1 | /** | |
| 2 | * Who you are, on every page (#580): "Signed in as **name** (this app)" or "(forge)", with Sign out beside it, or | |
| 3 | * "Watching as a guest · Sign in". Rendered from plain data, the name escaped; each page adds SIGNED_IN_STYLE to its | |
| 4 | * own pinned style (the arm's page, viewer.html, styles it itself), and its CSP must allow `form-action 'self'` for the | |
| 5 | * Sign out form. | |
| 6 | * | |
| 7 | * Sign out is a same-origin form post to /signout (src/relay.ts). For a name made here it ends that session alone; for | |
| 8 | * a forge identity it carries forge=1, and /signout hands on to the forge's own /__forge/signout, which drops the | |
| 9 | * forge's sign-in for this app. The forge's own session lives on the forge's site and refuses a cross-site sign-out, | |
| 10 | * so a forge identity also gets "Switch forge account", a link to the forge, where its own Sign out is. | |
| 11 | */ | |
| 12 | ||
| 13 | import { esc } from "./story-markdown.ts"; | |
| 14 | import type { Person } from "./identity.ts"; | |
| 15 | ||
| 16 | /** Where a person's identity came from: a name made here, the forge's signed header, or a forge-less local run. */ | |
| 17 | export type Via = "app" | "forge" | "local"; | |
| 18 | ||
| 19 | export interface SignedIn { | |
| 20 | name: string; | |
| 21 | via: Via; | |
| 22 | } | |
| 23 | ||
| 24 | /** What the line says about this request: null for a guest. A forge-signed identity wins over a name made here. */ | |
| 25 | export function signedIn(who: Person | null, verified: Person | null, local = false): SignedIn | null { | |
| 26 | if (!who) return null; | |
| 27 | return { name: who.name, via: local ? "local" : verified?.id === who.id ? "forge" : "app" }; | |
| 28 | } | |
| 29 | ||
| 30 | /** The same line as text, for the terminal (src/terminal.ts): "Signed in as NAME (forge)", or a guest's. */ | |
| 31 | export function signedInText(s: SignedIn | null): string { | |
| 32 | if (!s) return "Watching as a guest"; | |
| 33 | return `Signed in as ${s.name} (${s.via === "forge" ? "forge" : s.via === "local" ? "this machine" : "this app"})`; | |
| 34 | } | |
| 35 | ||
| 36 | /** A `via` from the wire, or null when it is not one. */ | |
| 37 | export const readVia = (x: unknown): Via | null => (x === "app" || x === "forge" || x === "local" ? x : null); | |
| 38 | ||
| 39 | /** The forge's sign-in, coming back to `to` (a path on this site). */ | |
| 40 | export const forgeSignin = (to: string): string => `/__forge/signin?to=${encodeURIComponent(to.startsWith("/") && !to.startsWith("//") ? to : "/")}`; | |
| 41 | ||
| 42 | /** The forge's own site, where a forge account signs out of the forge itself. */ | |
| 43 | export const FORGE_HOME = "https://forge.open-platform.sh/"; | |
| 44 | ||
| 45 | export const SIGNED_IN_STYLE = ` | |
| 46 | .signed-in { display: flex; flex-wrap: wrap; align-items: center; gap: .25rem .75rem; margin: 0; font-size: .9rem; color: var(--dim); } | |
| 47 | .signed-in b { color: var(--fg); font-weight: 600; overflow-wrap: anywhere; } | |
| 48 | .signed-in form { margin: 0; display: inline; } | |
| 49 | .signed-in .link { font: inherit; font-weight: 400; min-height: 44px; padding: 0; border: 0; background: none; color: var(--accent, var(--link, #58a6ff)); text-decoration: underline; cursor: pointer; } | |
| 50 | `; | |
| 51 | ||
| 52 | const signOut = (label: string, forge: boolean) => | |
| 53 | `<form method="post" action="/signout">${forge ? `<input type="hidden" name="forge" value="1">` : ""}<button class="link">${label}</button></form>`; | |
| 54 | ||
| 55 | /** | |
| 56 | * The line itself. `guest` is what a guest is offered after "Watching as a guest" (by default a sign-in link back to | |
| 57 | * `back`); a local run, where everyone is the owner, has no line. | |
| 58 | */ | |
| 59 | export function renderSignedIn(s: SignedIn | null, back = "/", guest?: string): string { | |
| 60 | if (!s) return `<div class="signed-in" id="signedIn">Watching as a guest · ${guest ?? `<a href="${esc(forgeSignin(back))}">Sign in</a>`}</div>`; | |
| 61 | if (s.via === "local") return ""; | |
| 62 | const where = s.via === "forge" ? "forge" : "this app"; | |
| 63 | return `<div class="signed-in" id="signedIn"><span>Signed in as <b>${esc(s.name)}</b> (${where})</span>${signOut("Sign out", s.via === "forge")}${s.via === "forge" ? `<a href="${FORGE_HOME}">Switch forge account</a>` : ""}</div>`; | |
| 64 | } |
Software/so101/src/signout.test.ts added +166
| @@ -0,0 +1,166 @@ | ||
| 1 | import { afterEach, describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { Accounts, expiredSessionCookie, sessionCookie, type Visitor } from "./accounts.ts"; | |
| 6 | import { FeetechBus } from "./bus.ts"; | |
| 7 | import { FakeBus } from "./fake-bus.ts"; | |
| 8 | import { sign } from "./identity.ts"; | |
| 9 | import { Lobby } from "./lobby.ts"; | |
| 10 | import { startRelay, type RunningRelay } from "./relay.ts"; | |
| 11 | import { startServer, type RunningServer } from "./server.ts"; | |
| 12 | import { FORGE_HOME, forgeSignin, renderSignedIn, signedIn } from "./signed-in.ts"; | |
| 13 | import { signedInParts } from "./ui/lobby-panel.js"; | |
| 14 | ||
| 15 | const KEY = "k".repeat(64); | |
| 16 | const later = Date.now() + 600_000; | |
| 17 | const as = (name: string, role: "user" | "admin") => { | |
| 18 | const identity = `id-${name}|${name}|${role}|${later}`; | |
| 19 | return { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) }; | |
| 20 | }; | |
| 21 | const owner = as("trevato", "admin"); | |
| 22 | const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); | |
| 23 | const shut = (s: { close(): Promise<void> } | undefined) => (s ? Promise.race([s.close(), sleep(500)]) : undefined); | |
| 24 | ||
| 25 | let dir = ""; | |
| 26 | let relay: RunningRelay | undefined; | |
| 27 | let lobby: Lobby | undefined; | |
| 28 | let sim: RunningServer | undefined; | |
| 29 | afterEach(async () => { | |
| 30 | await shut(relay); | |
| 31 | await shut(sim); | |
| 32 | lobby?.close(); | |
| 33 | if (dir) rmSync(dir, { recursive: true, force: true }); | |
| 34 | dir = ""; | |
| 35 | relay = lobby = sim = undefined; | |
| 36 | }); | |
| 37 | ||
| 38 | async function boot() { | |
| 39 | dir = mkdtempSync(join(tmpdir(), "so101-signout-")); | |
| 40 | sim = await startServer(new FeetechBus(new FakeBus([1, 2, 3, 4, 5, 6])), { host: "127.0.0.1", port: 0, posesFile: join(dir, "poses.json") }); | |
| 41 | lobby = new Lobby({ identityKey: KEY, adminFile: join(dir, "admin.json"), openFile: join(dir, "open.json") }); | |
| 42 | relay = await startRelay({ port: 0, simulation: `http://127.0.0.1:${sim.port}`, remote: null, lobby, link: { backoffMs: [20], connectTimeoutMs: 50, offlineAfterMs: 60_000 } }); | |
| 43 | return `http://127.0.0.1:${relay.port}`; | |
| 44 | } | |
| 45 | ||
| 46 | /** Join under a name; the cookie the answer sets, as a Cookie header. */ | |
| 47 | async function join_(base: string, name: string): Promise<string> { | |
| 48 | const r = await fetch(`${base}/join`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ name }) }); | |
| 49 | expect(r.status).toBe(200); | |
| 50 | return r.headers.get("set-cookie")!.split(";")[0]!; | |
| 51 | } | |
| 52 | const signOut = (base: string, cookie: string, body = "", headers: Record<string, string> = {}) => | |
| 53 | fetch(`${base}/signout`, { method: "POST", headers: { cookie, "content-type": "application/x-www-form-urlencoded", ...headers }, body, redirect: "manual" }); | |
| 54 | const close = (base: string) => | |
| 55 | fetch(`${base}/admin`, { method: "POST", headers: { ...owner, "content-type": "application/x-www-form-urlencoded" }, body: "action=close¬e=Lights+off", redirect: "manual" }); | |
| 56 | ||
| 57 | describe("POST /signout (#580)", () => { | |
| 58 | test("signed in as a name made here, sign out: the cookie is expired, the next request is a guest, the old cookie opens nothing", async () => { | |
| 59 | const base = await boot(); | |
| 60 | const cookie = await join_(base, "altmia"); | |
| 61 | expect(await (await fetch(`${base}/`, { headers: { cookie } })).text()).toContain("Signed in as <b>altmia</b> (this app)"); | |
| 62 | ||
| 63 | const out = await signOut(base, cookie); | |
| 64 | expect(out.status).toBe(303); | |
| 65 | expect(out.headers.get("location")).toBe("/"); | |
| 66 | expect(out.headers.get("set-cookie")).toBe(expiredSessionCookie); | |
| 67 | // the same cookie as when it was set: name, Path, SameSite, Secure, HttpOnly; only the value and the age differ | |
| 68 | expect(expiredSessionCookie).toBe(sessionCookie("").replace(/Max-Age=\d+/, "Max-Age=0")); | |
| 69 | ||
| 70 | // a browser that kept the old cookie anyway is nobody now | |
| 71 | const page = await (await fetch(`${base}/`, { headers: { cookie } })).text(); | |
| 72 | expect(page).toContain("Watching as a guest"); | |
| 73 | expect(page).not.toContain("altmia"); | |
| 74 | // and may make a new name | |
| 75 | expect((await fetch(`${base}/join`, { method: "POST", headers: { cookie, "content-type": "application/json" }, body: '{"name":"mia2"}' })).status).toBe(200); | |
| 76 | }); | |
| 77 | ||
| 78 | test("Sign out of a forge identity goes on to the forge's /__forge/signout; a bare one never touches the forge", async () => { | |
| 79 | const base = await boot(); | |
| 80 | const forge = await signOut(base, "", "forge=1", owner); | |
| 81 | expect(forge.status).toBe(303); | |
| 82 | expect(forge.headers.get("location")).toBe("/__forge/signout"); | |
| 83 | expect(forge.headers.get("set-cookie")).toBe(expiredSessionCookie); | |
| 84 | expect((await signOut(base, "", "")).headers.get("location")).toBe("/"); | |
| 85 | }); | |
| 86 | ||
| 87 | test("only this site may sign you out, only by POST, and it works while the arm is closed", async () => { | |
| 88 | const base = await boot(); | |
| 89 | const cookie = await join_(base, "altbob"); | |
| 90 | expect((await signOut(base, cookie, "", { origin: "https://elsewhere.example" })).status).toBe(403); | |
| 91 | expect((await fetch(`${base}/signout`, { headers: { cookie } })).status).toBe(405); | |
| 92 | expect(await (await fetch(`${base}/`, { headers: { cookie } })).text()).toContain("altbob"); | |
| 93 | ||
| 94 | expect((await close(base)).status).toBe(303); | |
| 95 | const card = await (await fetch(`${base}/`, { headers: { cookie } })).text(); | |
| 96 | expect(card).toContain("The arm is resting"); | |
| 97 | expect(card).toContain('Signed in as <b>altbob</b> (this app)</span><form method="post" action="/signout"><button class="link">Sign out</button></form>'); | |
| 98 | ||
| 99 | const out = await signOut(base, cookie); | |
| 100 | expect(out.status).toBe(303); | |
| 101 | expect(out.headers.get("set-cookie")).toBe(expiredSessionCookie); | |
| 102 | const after = await (await fetch(`${base}/`, { headers: { cookie } })).text(); | |
| 103 | expect(after).toContain("Watching as a guest"); | |
| 104 | expect(after).toContain("Owner? <a href=\"/__forge/signin?to=%2Fadmin\">Sign in to open</a>"); | |
| 105 | }); | |
| 106 | ||
| 107 | test("the closed card and /admin say the owner is signed in through the forge, with a Sign out that ends the forge's sign-in", async () => { | |
| 108 | const base = await boot(); | |
| 109 | await close(base); | |
| 110 | const form = '<form method="post" action="/signout"><input type="hidden" name="forge" value="1"><button class="link">Sign out</button></form>'; | |
| 111 | for (const path of ["/", "/admin"]) { | |
| 112 | const html = await (await fetch(`${base}${path}`, { headers: owner })).text(); | |
| 113 | expect(html).toContain(`Signed in as <b>trevato</b> (forge)</span>${form}<a href="${FORGE_HOME}">Switch forge account</a>`); | |
| 114 | } | |
| 115 | }); | |
| 116 | ||
| 117 | test("after signing out, the page offers the forge's sign-in first, ahead of picking a name", async () => { | |
| 118 | const base = await boot(); | |
| 119 | const html = await (await fetch(`${base}/`)).text(); | |
| 120 | expect(html).toContain('Watching as a guest · <a href="/__forge/signin?to=%2F">Sign in</a>'); | |
| 121 | const forge = html.indexOf('id="forgeSignin"'); | |
| 122 | expect(forge).toBeGreaterThan(0); | |
| 123 | expect(forge).toBeLessThan(html.indexOf('id="joinForm"')); | |
| 124 | expect(html).toContain("Sign in with the forge"); | |
| 125 | }); | |
| 126 | }); | |
| 127 | ||
| 128 | describe("the identity line", () => { | |
| 129 | test("names the person escaped, says where they signed in, and a guest gets sign-in back to where they were", () => { | |
| 130 | expect(renderSignedIn({ name: "<b>x</b>", via: "app" })).toContain("Signed in as <b><b>x</b></b> (this app)"); | |
| 131 | expect(renderSignedIn({ name: "trevato", via: "forge" })).toContain('name="forge" value="1"'); | |
| 132 | expect(renderSignedIn({ name: "local", via: "local" })).toBe(""); | |
| 133 | expect(renderSignedIn(null, "/admin")).toContain('<a href="/__forge/signin?to=%2Fadmin">Sign in</a>'); | |
| 134 | expect(forgeSignin("//evil.example")).toBe("/__forge/signin?to=%2F"); | |
| 135 | const person = { id: "a-1", name: "mia", owner: false }; | |
| 136 | expect(signedIn(person, null)).toEqual({ name: "mia", via: "app" }); | |
| 137 | expect(signedIn(person, person)).toEqual({ name: "mia", via: "forge" }); | |
| 138 | expect(signedIn(null, null)).toBeNull(); | |
| 139 | }); | |
| 140 | ||
| 141 | test("the page keeps it current after a join, with the name as text", () => { | |
| 142 | const el = (tag: string, props: Record<string, unknown> = {}, ...children: unknown[]) => ({ tag, ...props, children }); | |
| 143 | const parts = signedInParts(el, { name: "<i>mia</i>" }, "app", "/") as any[]; | |
| 144 | expect(parts[0].children).toEqual(["Signed in as ", { tag: "b", textContent: "<i>mia</i>", children: [] }, " (this app)"]); | |
| 145 | expect(parts[1]).toMatchObject({ tag: "form", method: "post", action: "/signout" }); | |
| 146 | expect(parts[1].children.map((c: any) => c.tag)).toEqual(["button"]); | |
| 147 | const forge = signedInParts(el, { name: "t" }, "forge", "/") as any[]; | |
| 148 | expect(forge[1].children[0]).toMatchObject({ tag: "input", name: "forge", value: "1" }); | |
| 149 | expect(forge[2]).toMatchObject({ tag: "a", href: FORGE_HOME, textContent: "Switch forge account" }); | |
| 150 | expect((signedInParts(el, null, null, "/?x=1") as any[])[1]).toMatchObject({ tag: "a", href: "/__forge/signin?to=%2F%3Fx%3D1" }); | |
| 151 | }); | |
| 152 | }); | |
| 153 | ||
| 154 | describe("Accounts.signOut", () => { | |
| 155 | test("ends the session by its token; the name stays taken and still counts toward the hourly limit", () => { | |
| 156 | const accounts = new Accounts({ perIpPerHour: 1 }); | |
| 157 | const from: Visitor = { ip: "10.0.0.9", ua: "ua", token: null }; | |
| 158 | const { token } = accounts.create("Mia", from); | |
| 159 | expect(accounts.signOut(token)).toBe(true); | |
| 160 | expect(accounts.byToken(token)).toBeNull(); | |
| 161 | expect(accounts.signOut(token)).toBe(false); | |
| 162 | expect(accounts.signOut(null)).toBe(false); | |
| 163 | expect(() => accounts.create("Mia", { ...from, ip: "10.0.0.10" })).toThrow("Mia is taken"); | |
| 164 | expect(() => accounts.create("Other", from)).toThrow("at most 1 new names"); | |
| 165 | }); | |
| 166 | }); |
Software/so101/src/terminal.test.ts +23
| @@ -436,6 +436,20 @@ describe("terminal door", () => { | ||
| 436 | 436 | await alice.printedLine(/^bob: hello terminal$/); |
| 437 | 437 | }); |
| 438 | 438 | |
| 439 | test("/whoami says who the terminal is signed in as, the page's identity line in words; /signout says there is no session to end (#580)", async () => { | |
| 440 | const port = await start(); | |
| 441 | const alice = await terminal(port, ttyEnv("u-alice", "alice", "admin")); | |
| 442 | expect(alice.door.room.via).toBe("forge"); | |
| 443 | alice.door.input("/whoami"); | |
| 444 | await alice.printedLine(/^Signed in as alice \(forge\)$/); | |
| 445 | alice.door.input("/signout"); | |
| 446 | await alice.printedLine(/^the terminal keeps no session: .*\/quit to leave/); | |
| 447 | expect(alice.door.room.you?.name).toBe("alice"); // nothing ended | |
| 448 | const guest = await terminal(port, { FORGE_APP_IDENTITY_KEY: KEY }); | |
| 449 | guest.door.input("/whoami"); | |
| 450 | await guest.printedLine(/^Watching as a guest: connect with a key/); | |
| 451 | }); | |
| 452 | ||
| 439 | 453 | test("/health at the terminal: the owner sees each viewer's report, a guest and a plain user see nothing", async () => { |
| 440 | 454 | const port = await start(); |
| 441 | 455 | const page = await viewer(port, web("u-bob", "bob")); |
| @@ -499,6 +513,15 @@ describe("terminal door", () => { | ||
| 499 | 513 | await page.heardOne((m) => m.type === "said" && /alice's turn ended \(handed back\)/.test(m.message.text), "the hand-back on the page"); |
| 500 | 514 | }); |
| 501 | 515 | |
| 516 | test("/whoami reads the hello's via, and a forged via never makes a guest someone", () => { | |
| 517 | const room = emptyRoom(); | |
| 518 | hear(room, { type: "hello", you: { id: "a-1", name: "mia" }, via: "app", signin: true }); | |
| 519 | expect(typed(room, "/whoami")).toEqual({ print: "Signed in as mia (this app)" }); | |
| 520 | hear(room, { type: "hello", you: null, via: "forge", signin: true }); | |
| 521 | expect(room.via).toBeNull(); | |
| 522 | expect((typed(room, "/whoami") as { print: string }).print).toStartWith("Watching as a guest"); | |
| 523 | }); | |
| 524 | ||
| 502 | 525 | test("a terminal is told once when it is next and once when it is called", () => { |
| 503 | 526 | const room = emptyRoom(); |
| 504 | 527 | hear(room, { type: "hello", you: { id: "u-bob", name: "bob" }, signin: true }); |
Software/so101/src/terminal.ts +24 −2
| @@ -44,6 +44,8 @@ | ||
| 44 | 44 | * /tune owner: the run's progress and the last result (GET /tuning, src/tuning-panel.ts) |
| 45 | 45 | * /machine [off] the Machine panel (/machine, src/machine-feed.ts): the joints, the link and the server as they are, |
| 46 | 46 | * then every command in and out as it happens, in the page's own words (src/ui/machine.js); off stops it |
| 47 | * /whoami who you are signed in as, the page's identity line in words: "Signed in as NAME (forge)" or a guest's | |
| 48 | * /signout says why there is nothing to end: the terminal keeps no session (each connection is signed afresh) | |
| 47 | 49 | * /who /help /quit |
| 48 | 50 | * <who> is a name the door has seen (driving, in line, in the chat) or a user id. |
| 49 | 51 | */ |
| @@ -53,6 +55,7 @@ import { REFERENCES, referenceLines, verdictLine } from "./joint-reference.ts"; | ||
| 53 | 55 | import { UNPRINTABLE } from "./chat.ts"; |
| 54 | 56 | import { dataLines, RANGES, turnLines, type Range } from "./data-lines.ts"; |
| 55 | 57 | import { ID, NAME, sign } from "./identity.ts"; |
| 58 | import { readVia, signedInText, type Via } from "./signed-in.ts"; | |
| 56 | 59 | import { readServos, type ServoPanel } from "./servo-panel.ts"; |
| 57 | 60 | import { readTuning, tuningSentence, type TuningView } from "./tuning-panel.ts"; |
| 58 | 61 | import { JOINTS } from "./sts3215.ts"; |
| @@ -331,6 +334,8 @@ interface Someone { | ||
| 331 | 334 | /** What the door knows of the room, from what the server told it. */ |
| 332 | 335 | export interface Room { |
| 333 | 336 | you: (Someone & { owner: boolean; moderator: boolean }) | null; |
| 337 | /** How you are signed in, as the server's hello said (src/signed-in.ts); null for a guest. */ | |
| 338 | via: Via | null; | |
| 334 | 339 | muted: boolean; |
| 335 | 340 | banned: boolean; |
| 336 | 341 | driver: Someone | null; |
| @@ -354,7 +359,7 @@ export interface Room { | ||
| 354 | 359 | presence: string | null; |
| 355 | 360 | } |
| 356 | 361 | |
| 357 | export const emptyRoom = (): Room => ({ you: null, muted: false, banned: false, driver: null, queue: [], paused: false, resting: null, held: undefined, told: { called: false, next: false }, seen: new Map(), latest: new Map(), reports: null, refused: null, presence: null }); | |
| 362 | export const emptyRoom = (): Room => ({ you: null, via: null, muted: false, banned: false, driver: null, queue: [], paused: false, resting: null, held: undefined, told: { called: false, next: false }, seen: new Map(), latest: new Map(), reports: null, refused: null, presence: null }); | |
| 358 | 363 | |
| 359 | 364 | const someone = (x: unknown): Someone | null => { |
| 360 | 365 | if (typeof x !== "object" || x === null) return null; |
| @@ -381,7 +386,19 @@ const said = (room: Room, m: unknown): string => { | ||
| 381 | 386 | const TUNE_USAGE = "usage: /tune <joint, e.g. shoulder_pan or 1> <P, 1 to 64>, or /tune alone for the run so far"; |
| 382 | 387 | const ZERO_USAGE = "usage: /zero <joint, e.g. shoulder_pan> [degrees the model should read, default 0]"; |
| 383 | 388 | const DATA_USAGE = "usage: /data [hour|day|week|all] or /data #<turn>"; |
| 384 | export const HELP = "type to chat · /join /leave /ready /extend · /stop /resume (driver, owner) · /end [who] /table here|touch 1|2|3|<mm> /zero <joint> [deg] /align [joint] (owner) · /mute <who> /unmute <who> (owner, moderator) · /delete <who|#id> (owner, moderator) · /report <who|#id> · /reports /dismiss #<id> (owner, moderator) · /removed /restore #<id> /visitors (owner) · /close [note] /open (owner) · /health /precision /trace <session> /data [hour|day|week|all|#<turn>] /versions (owner, moderator) · /servos /tune [<joint> <P>] (owner) · /machine [off] · /who · /quit"; | |
| 389 | /** | |
| 390 | * Who you are here, as the page's identity line says it (#580): the server's own signedIn() reading, from hello. A guest | |
| 391 | * hears how to become someone. | |
| 392 | */ | |
| 393 | export function whoami(room: Room): string { | |
| 394 | if (!room.you) return `${signedInText(null)}: connect with a key your forge account knows to chat and drive`; | |
| 395 | return plain(signedInText({ name: room.you.name, via: room.via ?? "forge" })); | |
| 396 | } | |
| 397 | ||
| 398 | /** The terminal holds no session to end: the forge signs who is at the keyboard afresh on every connection. */ | |
| 399 | export const SIGNOUT_NOTE = "the terminal keeps no session: you are whoever your SSH key signs in as, each time you connect; /quit to leave, and connect with another key to be someone else"; | |
| 400 | ||
| 401 | export const HELP = "type to chat · /join /leave /ready /extend · /stop /resume (driver, owner) · /end [who] /table here|touch 1|2|3|<mm> /zero <joint> [deg] /align [joint] (owner) · /mute <who> /unmute <who> (owner, moderator) · /delete <who|#id> (owner, moderator) · /report <who|#id> · /reports /dismiss #<id> (owner, moderator) · /removed /restore #<id> /visitors (owner) · /close [note] /open (owner) · /health /precision /trace <session> /data [hour|day|week|all|#<turn>] /versions (owner, moderator) · /servos /tune [<joint> <P>] (owner) · /machine [off] · /who /whoami · /quit"; | |
| 385 | 402 | |
| 386 | 403 | /** The Machine feed's socket beside the lobby's: same app, same port. */ |
| 387 | 404 | export const machineUrl = (live: string): string => live.replace(/\/live$/, "/machine"); |
| @@ -436,6 +453,7 @@ export function hear(room: Room, frame: Frame): string[] { | ||
| 436 | 453 | const you = someone(frame.you); |
| 437 | 454 | const was = { you: room.you, muted: room.muted, banned: room.banned }; |
| 438 | 455 | room.you = you ? { ...you, owner: (frame.you as { owner?: unknown }).owner === true, moderator: frame.moderator === true } : null; |
| 456 | room.via = room.you ? readVia(frame.via) : null; | |
| 439 | 457 | room.muted = frame.muted === true; |
| 440 | 458 | room.banned = frame.banned === true; |
| 441 | 459 | see(room, you); |
| @@ -644,6 +662,10 @@ export function typed(room: Room, input: string): Typed { | ||
| 644 | 662 | return { machine: arg.toLowerCase() !== "off" }; |
| 645 | 663 | case "who": |
| 646 | 664 | return { print: who(room) }; |
| 665 | case "whoami": | |
| 666 | return { print: whoami(room) }; | |
| 667 | case "signout": | |
| 668 | return { print: SIGNOUT_NOTE }; | |
| 647 | 669 | case "help": |
| 648 | 670 | return { print: HELP }; |
| 649 | 671 | case "quit": |
Software/so101/src/ui/lobby-panel.js +15
| @@ -35,6 +35,19 @@ export function modActions(you, moderator, author) { | ||
| 35 | 35 | return { delete: true, mute: other, ban: other }; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | /** | |
| 39 | * The identity line (#580), as the server first renders it (src/signed-in.ts): kept current after a join on this page. | |
| 40 | * The name goes in as text. @param {(tag: string, props?: Record<string, unknown>, ...children: any[]) => any} el | |
| 41 | * @param {{ name: string } | null} me @param {"app" | "forge" | "local" | null | undefined} via @param {string} back a path here | |
| 42 | */ | |
| 43 | export function signedInParts(el, me, via, back) { | |
| 44 | if (!me) return ["Watching as a guest · ", el("a", { href: `/__forge/signin?to=${encodeURIComponent(back)}`, textContent: "Sign in" })]; | |
| 45 | if (via === "local") return []; | |
| 46 | const out = el("form", { method: "post", action: "/signout" }, ...(via === "forge" ? [el("input", { type: "hidden", name: "forge", value: "1" })] : []), el("button", { className: "link", textContent: "Sign out" })); | |
| 47 | const forge = via === "forge" ? [el("a", { href: "https://forge.open-platform.sh/", textContent: "Switch forge account" })] : []; | |
| 48 | return [el("span", {}, "Signed in as ", el("b", { textContent: me.name }), ` (${via === "forge" ? "forge" : "this app"})`), out, ...forge]; | |
| 49 | } | |
| 50 | ||
| 38 | 51 | /** Only a signed-in person reports, and only what someone else said. @param {any} m a chat message @param {any} me hello.you */ |
| 39 | 52 | export const mayReport = (m, me) => Boolean(me && m.from && m.from.id !== me.id); |
| 40 | 53 | |
| @@ -157,6 +170,8 @@ export function createRoom(page) { | ||
| 157 | 170 | $("joinForm").hidden = $("forgeSignin").hidden = !joining; |
| 158 | 171 | if (!joining) $("joinErr").textContent = ""; |
| 159 | 172 | $("forgeSignin").href = `/__forge/signin?to=${encodeURIComponent(location.pathname + location.search)}`; |
| 173 | const line = $("signedIn"); | |
| 174 | if (line) line.replaceChildren(...signedInParts(el, me, lobby.hello.via, location.pathname + location.search)); | |
| 160 | 175 | $("waiting").textContent = st.queue.length ? String(st.queue.length) : ""; |
| 161 | 176 | $("queue").replaceChildren(...st.queue.map((/** @type {any} */ x) => { |
| 162 | 177 | const mine = me && x.id === me.id; |
Software/so101/src/viewer.html +7 −4
| @@ -177,6 +177,9 @@ | ||
| 177 | 177 | #say input { flex: 1; min-width: 0; } |
| 178 | 178 | #sayNote { color: var(); font-size: 13px; padding: 10px calc(12px + var()) 10px calc(12px + var()); border-top: 1px solid var(); } #sayNote:empty { display: none; } |
| 179 | 179 | #storyLink { font-size: 13px; padding: 4px calc(12px + var()) 6px calc(12px + var()); border-top: 1px solid var(); } #storyLink a { display: inline-flex; align-items: center; min-height: 32px; color: var(); } #shell.driving #storyLink { display: none; } |
| 180 | #tab-chat > .signed-in { font-size: 13px; padding: 4px calc(12px + var()) 0 calc(12px + var()); border-top: 1px solid var(); } #tab-chat > .signed-in + #storyLink { border-top: 0; } #shell.driving #tab-chat > .signed-in { display: none; } | |
| 181 | .signed-in { display: flex; flex-wrap: wrap; align-items: center; gap: 0 10px; margin: 0; color: var(); } .signed-in b { color: var(); font-weight: 600; overflow-wrap: anywhere; } | |
| 182 | .signed-in form { margin: 0; display: inline; } .signed-in .link, #storyLink .signed-in a { font: inherit; min-height: 32px; padding: 0; border: 0; background: none; color: var(); text-decoration: underline; cursor: pointer; } | |
| 180 | 183 | |
| 181 | 184 | /* queue */ |
| 182 | 185 | #queue { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; } |
| @@ -186,7 +189,7 @@ | ||
| 186 | 189 | #queue:empty::after { content: "Nobody is waiting."; color: var(); font-size: 14px; } |
| 187 | 190 | #joinForm { display: flex; gap: 8px; } #joinForm input { flex: 1; min-width: 0; } |
| 188 | 191 | #joinErr { margin: 0; color: var(); font-size: 13px; } #joinErr:empty { display: none; } |
| 189 | #forgeSignin { color: var(); font-size: 13px; align-self: flex-start; } | |
| 192 | #forgeSignin { display: inline-flex; align-items: center; justify-content: center; min-height: var(); padding: 0 16px; border-radius: 10px; background: var(); color: var(); font-weight: 600; text-decoration: none; align-self: flex-start; } | |
| 190 | 193 | |
| 191 | 194 | /* controls */ |
| 192 | 195 | #joints { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 6px 14px; } |
| @@ -331,15 +334,15 @@ | ||
| 331 | 334 | <button type="button" id="newMsgs" hidden>New messages ↓</button> |
| 332 | 335 | <form id="say" hidden><input id="sayText" type="text" maxlength="500" autocomplete="off" enterkeyhint="send" placeholder="Say something" aria-label="Message"><button class="go">Send</button></form> |
| 333 | 336 | <div id="sayNote"></div> |
| 334 | <footer id="storyLink"><a href="/story">How this arm works, and how to build your own</a></footer> | |
| 337 | <!--signed-in--><footer id="storyLink"><a href="/story">How this arm works, and how to build your own</a></footer> | |
| 335 | 338 | </div> |
| 336 | 339 | <div class="tab" id="tab-queue" role="tabpanel" aria-labelledby="t-queue" hidden> |
| 337 | 340 | <h2>Driving</h2> |
| 338 | 341 | <div class="row" id="queueActions"></div> |
| 339 | 342 | <label class="row" style="font-size:13px"><input type="checkbox" id="quiet"> Mute the "you're next" tone</label> |
| 340 | <form id="joinForm" hidden><input id="joinName" type="text" required minlength="3" maxlength="20" pattern="[A-Za-z0-9_\-]{3,20}" autocomplete="nickname" autocapitalize="off" spellcheck="false" enterkeyhint="go" placeholder="Pick a name" aria-label="Your name" title="3-20 letters, digits, _ or -"><button class="go">Join</button></form> | |
| 343 | <a id="forgeSignin" hidden>Sign in with the forge</a> | |
| 344 | <form id="joinForm" hidden><input id="joinName" type="text" required minlength="3" maxlength="20" pattern="[A-Za-z0-9_\-]{3,20}" autocomplete="nickname" autocapitalize="off" spellcheck="false" enterkeyhint="go" placeholder="or pick a name" aria-label="Or pick a name" title="3-20 letters, digits, _ or -"><button class="go">Join</button></form> | |
| 341 | 345 | <p id="joinErr" role="alert"></p> |
| 342 | <a id="forgeSignin" hidden>Have a forge account? Sign in instead</a> | |
| 343 | 346 | <h2>Waiting</h2> |
| 344 | 347 | <ol id="queue" aria-label="Queue"></ol> |
| 345 | 348 | <p class="note">A turn lasts a few minutes and ends early when the arm sits idle. When yours comes, the Controls tab appears.</p> |
new above
offline
Sign out ends the session for the web only. The terminal has no matching command in src/tty.ts, so the 'both doors' rule is not met.