diff --git a/README.md b/README.md index 2613595..02dcece 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,7 @@ All optional; add them under `environment:` in your compose override next to `PO | `DEFAULT_SERVER` / `DEFAULT_CHANNEL` | unset | Server (and optionally channel) every visitor auto-connects to — see [Bake in a default server](#2-bake-in-a-default-server). | | `BROADCAST_TOKEN` | unset | Bearer token required to call `/api/broadcast` (send an in-app notice to all connected clients before a maintenance restart). Endpoint 404s while unset. | | `TLS_CERT` / `TLS_KEY` | unset | Paths to serve the gateway over HTTPS/WSS directly instead of behind a reverse proxy/tunnel. Both must be set together. | -| `STORE_ENABLED` | off | Set to `1` to turn this gateway's own `/api/store/themes` endpoint on. Only needed if you want to run your own Design Store instead of using the maintainer's shared one (see `STORE_URL` build arg above) — the endpoint 404s while unset. | +| `STORE_ENABLED` | off | Set to `1` to turn this gateway's own `/api/store/themes` endpoint on. Only needed if you want to run your own Design Store instead of using the maintainer's shared one (see `STORE_URL` build arg above) — the endpoint 404s while unset. The store can also run as its own process (`node dist/store-server.js`, same image) so redeploying it never drops voice connections; route `/api/store/*` to it in your reverse proxy. | | `STORE_DATA_FILE` | `store-themes.json` in the container's working dir | Where submitted themes (including screenshots) are stored. Mount a volume over this path if they should survive a container recreate. | | `STORE_ALLOWED_ORIGIN` | `*` | CORS origin allowed to call the store endpoint, if you want to restrict it to your own frontend's origin. | | `STORE_ADMIN_TOKEN` | unset | Bearer token for moderation. New submissions wait as "pending" until approved on the review page at `/api/store/admin` (enter the token there); the token holder's own submissions and the reserved author name(s) skip the queue. Without it, nothing ever gets published. Submitted CSS is also checked server-side: no `@import`/`@font-face`, no external `url()` — only inline `data:` images. | diff --git a/gateway/src/store-server.ts b/gateway/src/store-server.ts new file mode 100644 index 0000000..0e8c43f --- /dev/null +++ b/gateway/src/store-server.ts @@ -0,0 +1,32 @@ +// Standalone Design Store: the same /api/store/* routes index.ts serves, in a +// process of its own. Lets the store be redeployed or restarted without +// dropping anyone's voice connection. Same image, different command: +// node dist/store-server.js (with STORE_ENABLED=1) +// The gateway in front of the same hostname then runs with STORE_ENABLED off +// and the reverse proxy routes /api/store/* here. +import { createServer } from "node:http"; +import { handleStore } from "./store.js"; + +const PORT = Number(process.env.PORT ?? 8080); + +createServer((req, res) => { + res.setHeader("X-Content-Type-Options", "nosniff"); + const { pathname } = new URL(req.url ?? "/", "http://localhost"); + if (pathname === "/healthz") { + res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" }); + res.end("ok\n"); + return; + } + if (pathname === "/api/store/admin" || pathname === "/api/store/themes" || pathname.startsWith("/api/store/themes/")) { + handleStore(req, res, pathname).catch((err) => { + console.error("[store] Request failed:", err); + if (!res.headersSent) { + res.writeHead(500, { "Content-Type": "application/json" }); + res.end(JSON.stringify({ error: "internal" })); + } + }); + return; + } + res.writeHead(404, { "Content-Type": "application/json" }); + res.end(JSON.stringify({ error: "not_found" })); +}).listen(PORT, () => console.log(`WebSpeak3 store listening on http://localhost:${PORT}`)); diff --git a/gateway/src/store.ts b/gateway/src/store.ts index af8e0c8..73124b2 100644 --- a/gateway/src/store.ts +++ b/gateway/src/store.ts @@ -367,6 +367,13 @@ async function handleStoreSubmit(req: IncomingMessage, res: ServerResponse) { } const themes = await loadStoreThemes(); + // Re-uploading an installed store theme unchanged would just duplicate it. + const cssKey = normalizeCss(css).replace(/\s+/g, ""); + if (themes.some((t) => normalizeCss(t.css).replace(/\s+/g, "") === cssKey)) { + res.writeHead(409, { "Content-Type": "application/json" }); + res.end(JSON.stringify({ error: "duplicate" })); + return; + } if (themes.length >= STORE_MAX_THEMES) { res.writeHead(507, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: "store_full" })); diff --git a/web/src/App.css b/web/src/App.css index 7090b40..468e846 100644 --- a/web/src/App.css +++ b/web/src/App.css @@ -1734,6 +1734,28 @@ margin: 0 0.2rem; } +.ts-ptt-button { + /* Holding it on a phone must not scroll, zoom or pop the long-press menu */ + touch-action: none; + -webkit-touch-callout: none; +} + +.ts-ptt-key-listening { + outline: 2px solid var(--accent); +} + +.ts-hotkeys-table td { + padding: 0.2rem 1rem 0.2rem 0; +} + +.ts-hotkeys-table kbd { + font-family: inherit; + padding: 0.05rem 0.4rem; + border: 1px solid var(--border); + border-radius: 4px; + white-space: nowrap; +} + .ts-icon-slider, .ts-icon-select { display: flex; diff --git a/web/src/App.tsx b/web/src/App.tsx index 3f908d0..d26a66e 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -391,6 +391,8 @@ const ECHO_CANCELLATION_KEY = "webspeak3:echo-cancellation"; const AUTO_GAIN_CONTROL_KEY = "webspeak3:auto-gain-control"; const AEC3_ENABLED_KEY = "webspeak3:aec3-enabled"; const VAD_HANGOVER_KEY = "webspeak3:vad-hangover"; +const PUSH_TO_TALK_KEY = "webspeak3:push-to-talk"; +const PTT_BINDING_KEY = "webspeak3:ptt-key"; const DESIGN_THEME_KEY = "webspeak3:design-theme"; // The user's actual pick - "standard" | "nova" | "greenteaspeak" | "custom:". // DESIGN_THEME_KEY above keeps tracking just the resolved structural base, since a @@ -505,6 +507,29 @@ function loadBoolPref(key: string, fallback: boolean): boolean { return raw === null ? fallback : raw === "1"; } +interface PttBinding { + code: string; + label: string; +} + +function loadPttBinding(): PttBinding | null { + try { + const v = JSON.parse(localStorage.getItem(PTT_BINDING_KEY) ?? "null"); + return v && typeof v.code === "string" && typeof v.label === "string" ? v : null; + } catch { + return null; + } +} + +function keyBindingLabel(e: KeyboardEvent): string { + return e.key.length === 1 && e.key !== " " ? e.key.toUpperCase() : e.code; +} + +function isTypingTarget(target: EventTarget | null): boolean { + const el = target as HTMLElement | null; + return !!el && (el.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(el.tagName)); +} + function loadNumberPref(key: string, fallback: number): number { const raw = localStorage.getItem(key); const parsed = raw === null ? NaN : Number(raw); @@ -5289,6 +5314,10 @@ interface AudioSettings { onVadThresholdChange: (v: number) => void; vadHangover: number; onVadHangoverChange: (v: number) => void; + pushToTalk: boolean; + onPushToTalkChange: (on: boolean) => void; + pttBinding: PttBinding | null; + onPttBindingChange: (binding: PttBinding) => void; noiseSuppressionEnabled: boolean; onToggleNoiseSuppression: () => void; echoCancellationEnabled: boolean; @@ -5300,6 +5329,88 @@ interface AudioSettings { onToggleAec3: () => void; } +function PttKeyButton({ binding, onChange }: { binding: PttBinding | null; onChange: (b: PttBinding) => void }) { + const t = useT(); + const [listening, setListening] = useState(false); + useEffect(() => { + if (!listening) return; + // Capture phase on window: the next key or mouse button wins wherever the + // pointer is, and never reaches the dialog (Escape) or the live PTT listener. + const take = (e: Event, next?: PttBinding) => { + e.preventDefault(); + e.stopPropagation(); + setListening(false); + if (next) onChange(next); + }; + const onKey = (e: KeyboardEvent) => { + if (e.key === "Tab") return setListening(false); + take(e, e.key === "Escape" ? undefined : { code: e.code, label: keyBindingLabel(e) }); + }; + const onMouse = (e: MouseEvent) => { + // Left click is how you started (and how you click away), so it can't be PTT. + if (e.button === 0) return setListening(false); + take(e, { code: `Mouse${e.button}`, label: `Mouse ${e.button + 1}` }); + // The back/forward side buttons navigate on release; keep the page. + window.addEventListener("mouseup", (up) => up.preventDefault(), { capture: true, once: true }); + }; + window.addEventListener("keydown", onKey, true); + window.addEventListener("mousedown", onMouse, true); + return () => { + window.removeEventListener("keydown", onKey, true); + window.removeEventListener("mousedown", onMouse, true); + }; + }, [listening, onChange]); + return ( + + ); +} + +function HotkeysPanel({ audio }: { audio: AudioSettings }) { + const t = useT(); + return ( + <> +

{t("options.section.hotkeys")}

+

{t("hotkeys.subtitle")}

+
+ {t("recording.pushToTalk")} + +
+ {t("recording.pttKey")} + +
+

{t("recording.pttHint")}

+
+
+ {t("hotkeys.shortcuts")} + + + {( + [ + ["Ctrl + S", "menu.connections.connect"], + ["Ctrl + D", "menu.connections.disconnectCurrent"], + ["Ctrl + B", "menu.favorites.add"], + ] as const + ).map(([keys, label]) => ( + + + + + ))} + +
+ {keys} + {t(label)}
+

{t("hotkeys.shortcutsHint")}

+
+ + ); +} + function MicLevelBar({ levelRef, active }: { levelRef: React.MutableRefObject; active: boolean }) { const [level, setLevel] = useState(0); useEffect(() => { @@ -5423,15 +5534,34 @@ function AufnahmePanel({ audio }: { audio: AudioSettings }) {
{t("recording.activation")} + {audio.pushToTalk && ( + <> +
+ {t("recording.pttKey")} + +
+

{t("recording.pttHint")}

+ + )}
@@ -5838,7 +5968,7 @@ function DesignStorePanel({ () => localStorage.getItem(STORE_ADMIN_TOKEN_KEY) ?? "" ); const [publishing, setPublishing] = useState(false); - const [publishResult, setPublishResult] = useState<"ok" | "pending" | "error" | null>(null); + const [publishResult, setPublishResult] = useState<"ok" | "pending" | "duplicate" | "error" | null>(null); const [ratedIds, setRatedIds] = useState>(new Set()); const refreshThemes = () => @@ -5917,6 +6047,10 @@ function DesignStorePanel({ screenshot: publishScreenshot ?? undefined, }), }); + if (res.status === 409) { + setPublishResult("duplicate"); + return; + } if (!res.ok) throw new Error(String(res.status)); const body = (await res.json().catch(() => ({}))) as { status?: string }; setPublishResult(body.status === "pending" ? "pending" : "ok"); @@ -6068,6 +6202,7 @@ function DesignStorePanel({ {publishResult === "ok" &&

{t("design.store.publish.success")}

} {publishResult === "pending" &&

{t("design.store.publish.pending")}

} + {publishResult === "duplicate" &&

{t("design.store.publish.duplicate")}

} {publishResult === "error" &&

{t("design.store.publish.error")}

} )} @@ -6419,6 +6554,8 @@ function OptionsDialog({ ) : active.id === "aufnahme" ? ( + ) : active.id === "hotkeys" ? ( + ) : active.id === "sounds" ? ( ) : active.id === "design" ? ( @@ -6913,6 +7050,12 @@ function AppInner() { const [selfActive, setSelfActive] = useState(false); const [vadThreshold, setVadThreshold] = useState(0.02); const [vadHangover, setVadHangover] = useState(() => loadNumberPref(VAD_HANGOVER_KEY, 0.3)); + const [pushToTalk, setPushToTalk] = useState(() => loadBoolPref(PUSH_TO_TALK_KEY, false)); + const [pttBinding, setPttBinding] = useState(loadPttBinding); + const [pttPressed, setPttPressed] = useState(false); + // Read when a new MicCapture is created, which happens outside render. + const pushToTalkRef = useRef(pushToTalk); + const pttPressedRef = useRef(false); const [outputDevices, setOutputDevices] = useState([]); const [outputDeviceId, setOutputDeviceId] = useState(""); const [inputDevices, setInputDevices] = useState([]); @@ -7132,6 +7275,64 @@ function AppInner() { localStorage.setItem(VAD_HANGOVER_KEY, String(vadHangover)); }, [vadHangover]); + useEffect(() => { + pushToTalkRef.current = pushToTalk; + if (micCaptureRef.current) micCaptureRef.current.pushToTalk = pushToTalk; + localStorage.setItem(PUSH_TO_TALK_KEY, pushToTalk ? "1" : "0"); + }, [pushToTalk]); + + useEffect(() => { + pttPressedRef.current = pttPressed; + if (micCaptureRef.current) micCaptureRef.current.pttPressed = pttPressed; + }, [pttPressed]); + + useEffect(() => { + if (pttBinding) localStorage.setItem(PTT_BINDING_KEY, JSON.stringify(pttBinding)); + }, [pttBinding]); + + // Browsers only deliver keys while this tab has focus - there's no global + // hotkey for websites, so PTT can't work while another program is in front. + useEffect(() => { + if (!pushToTalk || !pttBinding) return; + const onKeyDown = (e: KeyboardEvent) => { + if (e.code !== pttBinding.code || e.defaultPrevented) return; + // A letter key still has to type in chat; F-keys or modifiers work anywhere. + if (e.key.length === 1 && isTypingTarget(e.target)) return; + if (!isTypingTarget(e.target)) e.preventDefault(); + setPttPressed(true); + }; + const onKeyUp = (e: KeyboardEvent) => { + if (e.code === pttBinding.code) setPttPressed(false); + }; + // preventDefault: no middle-click autoscroll, no back/forward navigation + // from the side buttons (which fires on release). + const onMouseDown = (e: MouseEvent) => { + if (`Mouse${e.button}` !== pttBinding.code) return; + e.preventDefault(); + setPttPressed(true); + }; + const onMouseUp = (e: MouseEvent) => { + if (`Mouse${e.button}` !== pttBinding.code) return; + e.preventDefault(); + setPttPressed(false); + }; + // The keyup goes to whatever has focus next; don't leave the mic open. + const release = () => setPttPressed(false); + window.addEventListener("keydown", onKeyDown); + window.addEventListener("keyup", onKeyUp); + window.addEventListener("mousedown", onMouseDown); + window.addEventListener("mouseup", onMouseUp); + window.addEventListener("blur", release); + return () => { + window.removeEventListener("keydown", onKeyDown); + window.removeEventListener("keyup", onKeyUp); + window.removeEventListener("mousedown", onMouseDown); + window.removeEventListener("mouseup", onMouseUp); + window.removeEventListener("blur", release); + release(); + }; + }, [pushToTalk, pttBinding]); + useEffect(() => { audioPlayerRef.current?.setVolume(playbackVolume); localStorage.setItem(PLAYBACK_VOLUME_KEY, String(playbackVolume)); @@ -8863,6 +9064,8 @@ function AppInner() { } micCaptureRef.current?.stop(); micCaptureRef.current = mic; + mic.pushToTalk = pushToTalkRef.current; + mic.pttPressed = pttPressedRef.current; mic.setExtraAudio(tabAudioRef.current); setMicOn(true); refreshOutputDevices(); @@ -10695,18 +10898,45 @@ function AppInner() { > {micOn && !inputMuted ? "🎤" : "🔇"} - + {pushToTalk ? ( + + ) : ( + + )}