Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
/live answers {type:'who'} with who is here: usernames, guest names with their colour, and driving, queued or watching - lobby and test only #424
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/397-live-answers-type-who-with-who-is-here-u → main 1 commit fork
2 files
changed +58
−0
Software/so101/src/lobby.test.ts +30
| @@ -91,6 +91,36 @@ describe("lobby", () => { | ||
| 91 | 91 | expect((await tab.first((m) => m.type === "you")).guest).toEqual(mine); |
| 92 | 92 | }); |
| 93 | 93 | |
| 94 | test("who answers the asking socket alone with driving, queued and watching, guests with colour, one row per person, once a second", async () => { | |
| 95 | let clock = 1_000_000; | |
| 96 | lobby = new Lobby({ identityKey: KEY, turns: new Turns(), chat: new Chat(), admin: new Admin(), accounts: new Accounts(), now: () => clock }); | |
| 97 | relay = await startRelay({ port: 0, simulation: `http://127.0.0.1:${simulation.port}`, lobby }); | |
| 98 | const alice = await join(relay.port, ALICE); | |
| 99 | const alice2 = await join(relay.port, ALICE); | |
| 100 | const bob = await join(relay.port, BOB); | |
| 101 | const guest = await join(relay.port, { cookie: `arm_browser=${"a".repeat(32)}` }); | |
| 102 | alice.send({ type: "join" }); | |
| 103 | bob.send({ type: "join" }); | |
| 104 | await bob.next((m) => m.type === "state" && m.queue?.length === 1); | |
| 105 | const seen = guest.first((m) => m.type === "you"); | |
| 106 | guest.send({ type: "who" }); | |
| 107 | const people = (await guest.next((m) => m.type === "who")).people as Msg[]; | |
| 108 | const you = (await seen).guest; | |
| 109 | expect(people.filter((p) => p.name === "alice")).toEqual([{ name: "alice", guest: false, role: "driving" }]); | |
| 110 | expect(people.find((p) => p.name === "bob")).toEqual({ name: "bob", guest: false, role: "queued" }); | |
| 111 | expect(people.find((p) => p.guest)).toEqual({ name: you.name, colour: you.colour, guest: true, role: "watching" }); | |
| 112 | expect(people).toHaveLength(3); | |
| 113 | expect(JSON.stringify(people)).not.toMatch(/u-alice|127\.0\.0\.1|agent/i); | |
| 114 | expect(alice2.heard.some((m) => m.type === "who")).toBe(false); | |
| 115 | guest.heard.length = 0; | |
| 116 | guest.send({ type: "who" }); | |
| 117 | await sleep(100); | |
| 118 | expect(guest.heard.some((m) => m.type === "who")).toBe(false); | |
| 119 | clock += 1000; | |
| 120 | guest.send({ type: "who" }); | |
| 121 | await guest.next((m) => m.type === "who"); | |
| 122 | }); | |
| 123 | ||
| 94 | 124 | test("the page sets an HttpOnly browser-id cookie when absent, and not when a good one comes", async () => { |
| 95 | 125 | const port = await start(); |
| 96 | 126 | const set = (await fetch(`http://127.0.0.1:${port}/`)).headers.get("set-cookie") ?? ""; |
Software/so101/src/lobby.ts +28
| @@ -25,6 +25,7 @@ | ||
| 25 | 25 | * {"type": "dismissReport", "id": 12} owner or moderator: close a chat line's reports and nothing else |
| 26 | 26 | * {"type": "report", "id": 12} signed in, not banned: report a chat line; answered to you alone with {"type": "reported", "id"}, a repeat too |
| 27 | 27 | * {"type": "report", …} anyone: the page's own timings (src/viewer-reports.ts), every 5 s (no "id") |
| 28 | * {"type": "who"} anyone: who is here, answered to that socket alone, at most once a second (people: name, colour?, guest, role) | |
| 28 | 29 | * {"type": "pong"} the answer to {"type": "ping"} |
| 29 | 30 | * {"type": "history", "before": 12} anyone: up to HISTORY_PAGE lines older than that one |
| 30 | 31 | * 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 and the speed cap), |
| @@ -100,6 +101,8 @@ interface Seat { | ||
| 100 | 101 | heard: number; |
| 101 | 102 | /** An anonymous viewer's browser id, and the name it holds while any of its sockets is open. */ |
| 102 | 103 | guest?: { id: string; name: GuestName }; |
| 104 | /** When this socket's last "who" was answered. */ | |
| 105 | whoAt?: number; | |
| 103 | 106 | } |
| 104 | 107 | |
| 105 | 108 | const MAX_FRAME = 4096; |
| @@ -388,6 +391,12 @@ export class Lobby { | ||
| 388 | 391 | this.reports.set(seat, who?.name ?? "watcher", report); |
| 389 | 392 | return; |
| 390 | 393 | } |
| 394 | if (m.type === "who") { | |
| 395 | const at = this.now(); | |
| 396 | if (seat.whoAt !== undefined && at - seat.whoAt < 1000) return; | |
| 397 | seat.whoAt = at; | |
| 398 | return this.send(seat, { type: "who", people: this.people() }); | |
| 399 | } | |
| 391 | 400 | if (m.type === "history") { |
| 392 | 401 | if (!Number.isSafeInteger(m.before)) return this.send(seat, { type: "error", error: "history before which line?" }); |
| 393 | 402 | return this.send(seat, { type: "history", ...this.page(m.before as number, HISTORY_PAGE) }); |
| @@ -733,6 +742,25 @@ export class Lobby { | ||
| 733 | 742 | return { type: "presence", here: this.seats.size, signedIn: people.size, watching }; |
| 734 | 743 | } |
| 735 | 744 | |
| 745 | /** Who is here, for a "who" answer: a signed-in person once however many tabs, a guest by name and colour. No address or id. */ | |
| 746 | private people(): Array<{ name: string; colour?: string; guest: boolean; role: "driving" | "queued" | "watching" }> { | |
| 747 | const out: Array<{ name: string; colour?: string; guest: boolean; role: "driving" | "queued" | "watching" }> = []; | |
| 748 | const seen = new Set<string>(); | |
| 749 | for (const s of this.seats) { | |
| 750 | if (s.who) { | |
| 751 | if (seen.has(s.who.id)) continue; | |
| 752 | seen.add(s.who.id); | |
| 753 | const role = this.turns.isDriver(s.who.id) ? "driving" : this.turns.queue.some((p) => p.id === s.who!.id) ? "queued" : "watching"; | |
| 754 | out.push({ name: s.who.name, guest: false, role }); | |
| 755 | } else if (s.guest) { | |
| 756 | if (seen.has(s.guest.id)) continue; | |
| 757 | seen.add(s.guest.id); | |
| 758 | out.push({ name: s.guest.name.name, colour: s.guest.name.colour, guest: true, role: "watching" }); | |
| 759 | } | |
| 760 | } | |
| 761 | return out; | |
| 762 | } | |
| 763 | ||
| 736 | 764 | /** One frame per window however many connects and disconnects happened in it. */ |
| 737 | 765 | private presenceChanged(): void { |
| 738 | 766 | if (this.presenceTimer) return; |
new above
offline