From abfe6e7d4f182285b8aad3ecde06c940d9042167 Mon Sep 17 00:00:00 2001 From: enricobuehler Date: Fri, 30 Oct 2026 17:46:16 +0100 Subject: [PATCH] fix(console): show profile pictures A host row names its picture and the shell asks for it with FetchAvatar. Both pickers need it, so the app reads it over the existing host path and hands the bytes to the wasm shell, and the DOM picker draws the same one. The pin moves to the punktfunk commit that adds the command. --- apps/web/src/app.ts | 27 ++++++++++++++++++++++----- apps/web/src/ui/console.ts | 21 +++++++++++++++++++++ apps/web/src/ui/sheets.tsx | 6 +++++- apps/web/src/ui/types.ts | 6 ++++-- packages/stream/Cargo.lock | 8 ++++---- packages/stream/Cargo.toml | 6 +++--- packages/stream/build.sh | 2 +- packages/stream/rust/host.rs | 24 ++++++++++++++++++++++++ packages/stream/src/emscripten.d.ts | 1 + packages/stream/src/engine.ts | 10 ++++++++++ packages/stream/src/profiles.test.ts | 7 ++++++- packages/stream/src/profiles.ts | 11 +++++++++-- server/Cargo.lock | 4 ++-- server/Cargo.toml | 2 +- 14 files changed, 113 insertions(+), 22 deletions(-) diff --git a/apps/web/src/app.ts b/apps/web/src/app.ts index eee4620..48a8874 100644 --- a/apps/web/src/app.ts +++ b/apps/web/src/app.ts @@ -96,6 +96,8 @@ class App { * is not connected, so the tab never opens blank on a shelf it has shown before. */ private entries: LibraryEntry[] = []; private readonly art = new Map(); + /** Data URLs of the profile pictures read so far, by their path on the host. */ + private readonly pictures = new Map(); private libraryFor: string | null = null; private libraryLoading = false; /** When the titles were last read. Not the same as "is the list empty": a host with nothing in @@ -1060,15 +1062,27 @@ class App { hosts.setProfile(origin, decision.remember); return void this.settleSeat(gate, decision.send); } - this.gateShow(gate, { + const picker: Extract = { kind: "profiles", origin, host: this.nameOf(origin), - cards: gate.rows.map(cardOf), + cards: gate.rows.map((p) => cardOf(p, this.pictures)), ...(saved ? { saved: saved.id } : {}), ...(decision?.gone ? { gone: decision.gone } : {}), ...(listed === undefined ? { error: "Couldn't load the profiles." } : {}), - }); + }; + this.gateShow(gate, picker); + void this.readPictures(gate, picker); + } + + /** Reads the pictures `gate`'s picker lacks, then shows them. One that won't load stays initials. */ + private async readPictures(gate: Gate, picker: Extract): Promise { + const missing = gate.rows.flatMap((p) => (p.avatar?.startsWith("/") && !this.pictures.has(p.avatar) ? [p.avatar] : [])); + const read = await Promise.all(missing.map((url) => gate.host.art(url).catch(() => null))); + read.forEach((shown, i) => shown && this.pictures.set(missing[i]!, shown)); + if (gate.screen === picker && read.some(Boolean)) { + this.gateShow(gate, { ...picker, cards: gate.rows.map((p) => cardOf(p, this.pictures)) }); + } } private pickProfile(id: string): void { @@ -1184,13 +1198,16 @@ function within(p: Promise, ms: number): Promise { }); } -/** A picker card for `p`. An accent that isn't `#RRGGBB` falls back to the brand's. */ -function cardOf(p: ListedProfile): ProfileCard { +/** A picker card for `p`, with its picture if `pictures` has it. An accent that isn't `#RRGGBB` + * falls back to the brand's. */ +function cardOf(p: ListedProfile, pictures: ReadonlyMap): ProfileCard { const note = profileNote(p); + const picture = p.avatar ? pictures.get(p.avatar) : undefined; return { id: p.id, name: p.display_name, initials: initials(p.display_name), + ...(picture ? { picture } : {}), ...(p.accent && /^#[0-9a-f]{6}$/i.test(p.accent) ? { accent: p.accent } : {}), ...(note ? { note } : {}), }; diff --git a/apps/web/src/ui/console.ts b/apps/web/src/ui/console.ts index 1c043ac..185027c 100644 --- a/apps/web/src/ui/console.ts +++ b/apps/web/src/ui/console.ts @@ -948,6 +948,12 @@ export class ConsoleUi implements Ui { this.actions?.connect(origin); return; } + case "FetchAvatar": { + // A host not reached yet is asked again once it lists its profiles. + const host = origin ? this.apiFor(origin) : undefined; + if (host) void this.answerAvatar(str("url"), host); + return; + } case "WakeProfile": { // No answer: the console reads the list again until the seat is up. const host = origin ? this.apiFor(origin) : undefined; @@ -987,6 +993,21 @@ export class ConsoleUi implements Ui { } } + /** A profile's picture for the console's faces. One that won't load leaves the initials. */ + private async answerAvatar(url: string, host: Host): Promise { + if (!url.startsWith("/")) return; + try { + const shown = await host.art(url); + if (!shown) return; + const blob = await (await fetch(shown)).blob(); + URL.revokeObjectURL(shown); + // The browser reads what the console's own decoders can't. + this.engine.console.avatar(url, (await coverBytes(blob)) ?? new Uint8Array(await blob.arrayBuffer())); + } catch (e) { + console.warn("punktfunk: profile picture did not load", e); + } + } + private pushProfiles(fp: string, answer: unknown): void { this.engine.console.push(CONSOLE_PUSH.PROFILES, { fp_hex: fp, answer }); } diff --git a/apps/web/src/ui/sheets.tsx b/apps/web/src/ui/sheets.tsx index 2ea4fe3..d5b3a4e 100644 --- a/apps/web/src/ui/sheets.tsx +++ b/apps/web/src/ui/sheets.tsx @@ -51,7 +51,11 @@ export function Profiles({ screen, actions }: Props<"profiles">): JSX.Element { style={c.accent ? { background: c.accent } : undefined} aria-hidden="true" > - {c.initials} + {c.picture ? ( + + ) : ( + c.initials + )} {c.name} {c.note && {c.note}} diff --git a/apps/web/src/ui/types.ts b/apps/web/src/ui/types.ts index 94cdac4..636628c 100644 --- a/apps/web/src/ui/types.ts +++ b/apps/web/src/ui/types.ts @@ -59,12 +59,14 @@ export interface HostCard extends KnownHost { waking?: boolean; } -/** One card on the profile picker: initials on the profile's accent, and what its seat is doing - * when that matters to the pick. */ +/** One card on the profile picker: its picture, else initials on the profile's accent, and what + * its seat is doing when that matters to the pick. */ export interface ProfileCard { id: string; name: string; initials: string; + /** Something an `` can show. */ + picture?: string; /** `#RRGGBB`; the brand's when absent. */ accent?: string; note?: string; diff --git a/packages/stream/Cargo.lock b/packages/stream/Cargo.lock index 54159eb..996f9a0 100644 --- a/packages/stream/Cargo.lock +++ b/packages/stream/Cargo.lock @@ -531,7 +531,7 @@ dependencies = [ [[package]] name = "fec-rs" version = "0.1.0" -source = "git+https://git.unom.io/unom/punktfunk?rev=eb990a68305046336025ba115c9fff2ba395238a#eb990a68305046336025ba115c9fff2ba395238a" +source = "git+https://git.unom.io/unom/punktfunk?rev=dd871ad7e22f02131efee64ef53225e5d3e48490#dd871ad7e22f02131efee64ef53225e5d3e48490" [[package]] name = "fiat-crypto" @@ -997,7 +997,7 @@ dependencies = [ [[package]] name = "pf-client-core" version = "0.45.0" -source = "git+https://git.unom.io/unom/punktfunk?rev=eb990a68305046336025ba115c9fff2ba395238a#eb990a68305046336025ba115c9fff2ba395238a" +source = "git+https://git.unom.io/unom/punktfunk?rev=dd871ad7e22f02131efee64ef53225e5d3e48490#dd871ad7e22f02131efee64ef53225e5d3e48490" dependencies = [ "anyhow", "cfg_aliases", @@ -1012,7 +1012,7 @@ dependencies = [ [[package]] name = "pf-console-ui" version = "0.45.0" -source = "git+https://git.unom.io/unom/punktfunk?rev=eb990a68305046336025ba115c9fff2ba395238a#eb990a68305046336025ba115c9fff2ba395238a" +source = "git+https://git.unom.io/unom/punktfunk?rev=dd871ad7e22f02131efee64ef53225e5d3e48490#dd871ad7e22f02131efee64ef53225e5d3e48490" dependencies = [ "anyhow", "pf-client-core", @@ -1105,7 +1105,7 @@ dependencies = [ [[package]] name = "punktfunk-core" version = "0.45.0" -source = "git+https://git.unom.io/unom/punktfunk?rev=eb990a68305046336025ba115c9fff2ba395238a#eb990a68305046336025ba115c9fff2ba395238a" +source = "git+https://git.unom.io/unom/punktfunk?rev=dd871ad7e22f02131efee64ef53225e5d3e48490#dd871ad7e22f02131efee64ef53225e5d3e48490" dependencies = [ "aes-gcm", "aws-lc-rs", diff --git a/packages/stream/Cargo.toml b/packages/stream/Cargo.toml index 92524f1..84a7090 100644 --- a/packages/stream/Cargo.toml +++ b/packages/stream/Cargo.toml @@ -23,7 +23,7 @@ anyhow = "1" sha2 = "0.11" # `transport::Transport`, the wire codecs and the SPAKE2 ceremony. `pake`, not `quic`: the # browser plane is WebTransport, so the ceremony is wanted and the quinn runtime under it is not. -punktfunk-core = { git = "https://git.unom.io/unom/punktfunk", rev = "eb990a68305046336025ba115c9fff2ba395238a", default-features = false, features = ["pake"] } +punktfunk-core = { git = "https://git.unom.io/unom/punktfunk", rev = "dd871ad7e22f02131efee64ef53225e5d3e48490", default-features = false, features = ["pake"] } # wasm only. Off it this is a bin that prints how to build it, so `cargo test` on a developer's # desktop stays green and does not resolve skia-safe for a target that wants a second Skia @@ -32,12 +32,12 @@ punktfunk-core = { git = "https://git.unom.io/unom/punktfunk", rev = "eb990a6830 # The shell itself. `default-features = false`: `vulkan-overlay` is the desktop session binary's, # and it would put `vulkan` on skia-safe and change the prebuilt key away from # `ganesh-gl-jpegd-jpege-pdf-textlayout`, the one build.sh downloads for this target. -pf-console-ui = { git = "https://git.unom.io/unom/punktfunk", rev = "eb990a68305046336025ba115c9fff2ba395238a", default-features = false } +pf-console-ui = { git = "https://git.unom.io/unom/punktfunk", rev = "dd871ad7e22f02131efee64ef53225e5d3e48490", default-features = false } # The bridge's JSON both ways: the model the page pushes, what the shell raises. serde = "1" serde_json = "1" # The portable half — `trust::Settings` for the snapshot store the shell loads from. -pf-client-core = { git = "https://git.unom.io/unom/punktfunk", rev = "eb990a68305046336025ba115c9fff2ba395238a", default-features = false } +pf-client-core = { git = "https://git.unom.io/unom/punktfunk", rev = "dd871ad7e22f02131efee64ef53225e5d3e48490", default-features = false } # The SAME line pf-console-ui's wasm block carries, features included: two feature sets in one # build resolve two prebuilt keys, and the second one puts a source build of Skia on the critical # path. Only `host.rs` names it, to build the context and wrap the canvas. diff --git a/packages/stream/build.sh b/packages/stream/build.sh index 7f66558..b224647 100755 --- a/packages/stream/build.sh +++ b/packages/stream/build.sh @@ -85,7 +85,7 @@ link=( -C link-arg=-sSTACK_SIZE=4MB # Every `_pf_*` the page calls, the ring's included: `src/carrier/feed.ts` writes datagrams # into it. - -C link-arg=-sEXPORTED_FUNCTIONS=_main,_pf_start,_pf_frame,_pf_key,_pf_console_text,_pf_console_pointer,_pf_console_pad,_pf_console_phase,_pf_console_state,_pf_console_leave_out,_pf_console_push,_pf_console_art,_pf_rx_base,_pf_rx_stride,_pf_rx_claim,_pf_rx_commit,_pf_rx_dropped,_pf_net_blast,_pf_net_drain,_pf_wt_close,_pf_wt_ctl_open,_pf_ctl_recv,_pf_uni_recv,_pf_session_hello,_pf_session_codecs,_pf_session_pump,_pf_session_phase,_pf_session_frames,_pf_device_init,_pf_device_set,_pf_device_sign,_pf_device_fingerprint_hex,_pf_cred_phase,_pf_cred_sign_ptr,_pf_cred_sign_len,_pf_cred_signed,_pf_pair_begin,_pf_input,_pf_gamepad,_pf_gamepad_arrival,_pf_gamepad_remove,_pf_audio_frames,_pf_audio_lost,_pf_session_audio_channels,_pf_session_audio,_pf_session_audio_layout,_pf_mic_send,_pf_session_access_seq,_pf_session_access_grants,_pf_session_access_secs,_pf_session_reset,_pf_session_reconfigure,_pf_hud_decoded,_pf_hud_presented,_pf_gate_decoded,_pf_gate_no_output,_pf_request_keyframe,_pf_hud_drain,_pf_hud_quality,_pf_video_decoder,_pf_hud_text,_pf_hud_text_ptr,_pf_session_host_caps,_pf_cursor_render,_pf_cursor_serial,_pf_cursor_flags,_malloc,_free + -C link-arg=-sEXPORTED_FUNCTIONS=_main,_pf_start,_pf_frame,_pf_key,_pf_console_text,_pf_console_pointer,_pf_console_pad,_pf_console_phase,_pf_console_state,_pf_console_leave_out,_pf_console_push,_pf_console_art,_pf_console_avatar,_pf_rx_base,_pf_rx_stride,_pf_rx_claim,_pf_rx_commit,_pf_rx_dropped,_pf_net_blast,_pf_net_drain,_pf_wt_close,_pf_wt_ctl_open,_pf_ctl_recv,_pf_uni_recv,_pf_session_hello,_pf_session_codecs,_pf_session_pump,_pf_session_phase,_pf_session_frames,_pf_device_init,_pf_device_set,_pf_device_sign,_pf_device_fingerprint_hex,_pf_cred_phase,_pf_cred_sign_ptr,_pf_cred_sign_len,_pf_cred_signed,_pf_pair_begin,_pf_input,_pf_gamepad,_pf_gamepad_arrival,_pf_gamepad_remove,_pf_audio_frames,_pf_audio_lost,_pf_session_audio_channels,_pf_session_audio,_pf_session_audio_layout,_pf_mic_send,_pf_session_access_seq,_pf_session_access_grants,_pf_session_access_secs,_pf_session_reset,_pf_session_reconfigure,_pf_hud_decoded,_pf_hud_presented,_pf_gate_decoded,_pf_gate_no_output,_pf_request_keyframe,_pf_hud_drain,_pf_hud_quality,_pf_video_decoder,_pf_hud_text,_pf_hud_text_ptr,_pf_session_host_caps,_pf_cursor_render,_pf_cursor_serial,_pf_cursor_flags,_malloc,_free # `stringToNewUTF8` is how the page hands a string across; `HEAPU8` is emscripten's view of # wasm memory, which `src/carrier/feed.ts` writes datagrams into. -C link-arg=-sEXPORTED_RUNTIME_METHODS=stringToNewUTF8,HEAPU8 diff --git a/packages/stream/rust/host.rs b/packages/stream/rust/host.rs index 596d24f..bba8a69 100644 --- a/packages/stream/rust/host.rs +++ b/packages/stream/rust/host.rs @@ -434,6 +434,30 @@ pub unsafe extern "C" fn pf_console_art(id: *const u8, id_len: u32, bytes: *cons with_app((), |app| app.art.push_back((id.to_string(), bytes))); } +/// A profile's picture, encoded, under the URL `ConsoleCmd::FetchAvatar` named. +/// +/// # Safety +/// `url` points to `url_len` readable bytes of UTF-8 and `bytes` to `len` readable bytes, for the +/// call. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn pf_console_avatar( + url: *const u8, + url_len: u32, + bytes: *const u8, + len: u32, +) { + // SAFETY: the caller guarantees `url_len` readable bytes at `url`. + let Some(url) = (unsafe { utf8(url, url_len) }) else { + return; + }; + if bytes.is_null() || len == 0 { + return; + } + // SAFETY: the caller guarantees `len` readable bytes at `bytes`. + let bytes = unsafe { std::slice::from_raw_parts(bytes, len as usize) }.to_vec(); + with_app((), |app| app.handles.console.set_avatar(url, bytes)); +} + /// The page's key table, by index. Order is the wire between `console.ts` and here; append only. const KEYS: [Key; 13] = [ Key::Left, diff --git a/packages/stream/src/emscripten.d.ts b/packages/stream/src/emscripten.d.ts index 1d34ead..a5f6a23 100644 --- a/packages/stream/src/emscripten.d.ts +++ b/packages/stream/src/emscripten.d.ts @@ -47,6 +47,7 @@ export interface PunktfunkModule { _pf_console_leave_out(what: number): void; _pf_console_push(kind: number, ptr: number, len: number): void; _pf_console_art(idPtr: number, idLen: number, bytesPtr: number, len: number): void; + _pf_console_avatar(urlPtr: number, urlLen: number, bytesPtr: number, len: number): void; // --- the transport --------------------------------------------------------------------- /** Close `__pfCarrier`, with the host's application close code when one is given. */ diff --git a/packages/stream/src/engine.ts b/packages/stream/src/engine.ts index 8776737..f3fd9bf 100644 --- a/packages/stream/src/engine.ts +++ b/packages/stream/src/engine.ts @@ -957,6 +957,16 @@ export class Engine { this.mod._free(p); } }, + /** A profile's picture, encoded, under the URL `FetchAvatar` named. */ + avatar: (url: string, bytes: Uint8Array): void => { + const p = this.mod._malloc(bytes.length); + try { + this.mod.HEAPU8.set(bytes, p); + withStr(this.mod, [url], (up, ul) => this.mod._pf_console_avatar(up, ul, p, bytes.length)); + } finally { + this.mod._free(p); + } + }, /** What the console raised: a bridge event or `{cmd}`, parsed. One listener. */ onEvent: (fn: (event: ConsoleEvent) => void): void => { this.mod.__pfOnConsole = (json) => { diff --git a/packages/stream/src/profiles.test.ts b/packages/stream/src/profiles.test.ts index 108ed6b..44ef35f 100644 --- a/packages/stream/src/profiles.test.ts +++ b/packages/stream/src/profiles.test.ts @@ -3,7 +3,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; -import { initials, pickerDecision, profileNote, seatGate, type ProfileRow } from "./profiles.ts"; +import { initials, pickOf, pickerDecision, profileNote, seatGate, type ProfileRow } from "./profiles.ts"; const row = (id: string, display_name: string, more: Partial = {}): ProfileRow => ({ id, display_name, ...more }); const MIA = row("a1", "Mia"); @@ -14,6 +14,11 @@ test("a box without profiles sends none, or the link's", () => { assert.deepEqual(pickerDecision(null, null, "Kid"), { picker: false, send: "Kid", remember: null }); }); +test("a pick carries the picture only when there is one", () => { + assert.equal(pickOf(row("a1", "Mia", { avatar: "/api/v1/profiles/a1/avatar?v=1" })).avatar, "/api/v1/profiles/a1/avatar?v=1"); + assert.equal("avatar" in pickOf(row("a1", "Mia", { avatar: null })), false); +}); + test("one profile plays without asking; a saved one still listed plays", () => { assert.deepEqual(pickerDecision([MIA], null), { picker: false, send: "a1", remember: null }); const saved = { id: "b2", display_name: "Kiddo", remember: true }; diff --git a/packages/stream/src/profiles.ts b/packages/stream/src/profiles.ts index df28532..b93cf37 100644 --- a/packages/stream/src/profiles.ts +++ b/packages/stream/src/profiles.ts @@ -28,10 +28,13 @@ export interface SeatRow { readonly steam_sign_in?: unknown; } -/** The pick this browser keeps for a box. It carries the name, which a card shows. */ +/** The pick this browser keeps for a box. It carries the name and the picture's URL, which a + * card shows. */ export interface ProfilePick { readonly id: string; readonly display_name: string; + /** Host-relative URL of the picture. */ + readonly avatar?: string; /** The player asked for it to be remembered. The console applies only such a pick. */ readonly remember?: boolean; } @@ -61,7 +64,11 @@ export const SEAT_POLL_MS = 2000; /** The host's refusal of another profile's seat (its 403), in its own words. */ export const WAKE_REFUSED = "This device isn't allowed to start another profile's seat."; -export const pickOf = (p: ProfileRow): ProfilePick => ({ id: p.id, display_name: p.display_name }); +export const pickOf = (p: ProfileRow): ProfilePick => ({ + id: p.id, + display_name: p.display_name, + ...(p.avatar ? { avatar: p.avatar } : {}), +}); /** The profile `wanted` names: its id, else its name in any case, when only one has it. */ export function findProfile(listed: readonly ProfileRow[], wanted: string): ProfileRow | undefined { diff --git a/server/Cargo.lock b/server/Cargo.lock index deb69ea..67d9124 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -544,7 +544,7 @@ checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" [[package]] name = "fec-rs" version = "0.1.0" -source = "git+https://git.unom.io/unom/punktfunk?rev=eb990a68305046336025ba115c9fff2ba395238a#eb990a68305046336025ba115c9fff2ba395238a" +source = "git+https://git.unom.io/unom/punktfunk?rev=dd871ad7e22f02131efee64ef53225e5d3e48490#dd871ad7e22f02131efee64ef53225e5d3e48490" [[package]] name = "find-msvc-tools" @@ -1403,7 +1403,7 @@ dependencies = [ [[package]] name = "punktfunk-core" version = "0.45.0" -source = "git+https://git.unom.io/unom/punktfunk?rev=eb990a68305046336025ba115c9fff2ba395238a#eb990a68305046336025ba115c9fff2ba395238a" +source = "git+https://git.unom.io/unom/punktfunk?rev=dd871ad7e22f02131efee64ef53225e5d3e48490#dd871ad7e22f02131efee64ef53225e5d3e48490" dependencies = [ "aes-gcm", "chacha20poly1305", diff --git a/server/Cargo.toml b/server/Cargo.toml index f157f30..c2bc9a4 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -31,7 +31,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] } wtransport = { version = "0.7", default-features = false, features = ["aws-lc-rs"] } # Pinned to the rev packages/stream builds against: the same PinVerify and address ranking the # native clients use, so this server trusts and reaches a host exactly the way they do. -punktfunk-core = { git = "https://git.unom.io/unom/punktfunk", rev = "eb990a68305046336025ba115c9fff2ba395238a", default-features = false, features = ["tls"] } +punktfunk-core = { git = "https://git.unom.io/unom/punktfunk", rev = "dd871ad7e22f02131efee64ef53225e5d3e48490", default-features = false, features = ["tls"] } [dev-dependencies] tower = { version = "0.5", features = ["util"] }