Address remaining PR comments

This commit is contained in:
mplorentz
2026-03-05 16:46:29 -05:00
parent d128eb6c7a
commit 378aeec7e5
5 changed files with 97 additions and 106 deletions
+2 -4
View File
@@ -7,6 +7,7 @@
import {isMobileViewport} from "@lib/html" import {isMobileViewport} from "@lib/html"
import ProfileCircle from "@app/components/ProfileCircle.svelte" import ProfileCircle from "@app/components/ProfileCircle.svelte"
import RoomName from "@app/components/RoomName.svelte" import RoomName from "@app/components/RoomName.svelte"
import {errorMessage} from "@lib/util"
import {pushToast} from "@app/util/toast" import {pushToast} from "@app/util/toast"
import { import {
deriveVoiceParticipants, deriveVoiceParticipants,
@@ -46,10 +47,7 @@
try { try {
await joinVoiceRoom(url, h, joinAbortController.signal) await joinVoiceRoom(url, h, joinAbortController.signal)
} catch (e) { } catch (e) {
if (e instanceof Error && e.message === "Join cancelled") return pushToast({theme: "error", message: `Failed to join voice room: ${errorMessage(e)}`})
if (e instanceof DOMException && e.name === "AbortError") return
const message = e instanceof Error ? e.message : String(e)
pushToast({theme: "error", message: `Failed to join voice room: ${message}`})
} finally { } finally {
isJoining = false isJoining = false
joinAbortController = undefined joinAbortController = undefined
+3 -6
View File
@@ -1,5 +1,4 @@
<script lang="ts"> <script lang="ts">
import {get} from "svelte/store"
import {displayRelayUrl} from "@welshman/util" import {displayRelayUrl} from "@welshman/util"
import Microphone from "@assets/icons/microphone.svg?dataurl" import Microphone from "@assets/icons/microphone.svg?dataurl"
import MicrophoneOff from "@assets/icons/microphone-off.svg?dataurl" import MicrophoneOff from "@assets/icons/microphone-off.svg?dataurl"
@@ -17,10 +16,8 @@
) )
const spaceName = $derived($currentVoiceSession ? displayRelayUrl($currentVoiceSession.url) : "") const spaceName = $derived($currentVoiceSession ? displayRelayUrl($currentVoiceSession.url) : "")
const handleDisconnect = () => leaveVoiceRoom()
const handleToggleMute = () => toggleMute()
const showRoomDetail = () => { const showRoomDetail = () => {
const session = get(currentVoiceSession) const session = $currentVoiceSession
if (session) pushModal(RoomDetail, {url: session.url, h: session.h}) if (session) pushModal(RoomDetail, {url: session.url, h: session.h})
} }
</script> </script>
@@ -41,10 +38,10 @@
<div class="flex items-center gap-1"> <div class="flex items-center gap-1">
<Button <Button
class="btn btn-sm btn-square {$currentVoiceSession.muted ? 'btn-error' : 'btn-ghost'}" class="btn btn-sm btn-square {$currentVoiceSession.muted ? 'btn-error' : 'btn-ghost'}"
onclick={handleToggleMute}> onclick={toggleMute}>
<Icon icon={$currentVoiceSession.muted ? MicrophoneOff : Microphone} size={4} /> <Icon icon={$currentVoiceSession.muted ? MicrophoneOff : Microphone} size={4} />
</Button> </Button>
<Button class="btn btn-sm btn-square btn-error" onclick={handleDisconnect}> <Button class="btn btn-sm btn-square btn-error" onclick={leaveVoiceRoom}>
<Icon icon={PhoneRounded} size={4} /> <Icon icon={PhoneRounded} size={4} />
</Button> </Button>
</div> </div>
+5 -4
View File
@@ -103,6 +103,7 @@ import {
getListTags, getListTags,
getPubkeyTagValues, getPubkeyTagValues,
getRelayTagValues, getRelayTagValues,
getTag,
getTagValues, getTagValues,
isRelayUrl, isRelayUrl,
normalizeRelayUrl, normalizeRelayUrl,
@@ -668,7 +669,7 @@ export const deriveRoomsWithLivekit = (url: string) =>
derived(roomsById, $roomsById => { derived(roomsById, $roomsById => {
const set = new Set<string>() const set = new Set<string>()
for (const room of $roomsById.values()) { for (const room of $roomsById.values()) {
if (room.url === url && room.event?.tags?.some(t => t[0] === "livekit")) { if (room.url === url && getTag("livekit", room.event?.tags ?? [])) {
set.add(room.h) set.add(room.h)
} }
} }
@@ -679,7 +680,7 @@ export const deriveRoomsNoText = (url: string) =>
derived(roomsById, $roomsById => { derived(roomsById, $roomsById => {
const set = new Set<string>() const set = new Set<string>()
for (const room of $roomsById.values()) { for (const room of $roomsById.values()) {
if (room.url === url && room.event?.tags?.some(t => t[0] === "no-text")) { if (room.url === url && getTag("no-text", room.event?.tags ?? [])) {
set.add(room.h) set.add(room.h)
} }
} }
@@ -688,12 +689,12 @@ export const deriveRoomsNoText = (url: string) =>
export const roomHasLivekit = (url: string, h: string) => { export const roomHasLivekit = (url: string, h: string) => {
const room = getRoom(makeRoomId(url, h)) const room = getRoom(makeRoomId(url, h))
return room?.event?.tags?.some(t => t[0] === "livekit") ?? false return !!getTag("livekit", room?.event?.tags ?? [])
} }
export const roomIsNoText = (url: string, h: string) => { export const roomIsNoText = (url: string, h: string) => {
const room = getRoom(makeRoomId(url, h)) const room = getRoom(makeRoomId(url, h))
return room?.event?.tags?.some(t => t[0] === "no-text") ?? false return !!getTag("no-text", room?.event?.tags ?? [])
} }
// User space/room lists // User space/room lists
+57 -92
View File
@@ -3,12 +3,12 @@
* (ICE candidate gathering fails). Use Chrome or test from deployed HTTPS. * (ICE candidate gathering fails). Use Chrome or test from deployed HTTPS.
*/ */
import {DisconnectReason, Room, RoomEvent, Track} from "livekit-client" import {DisconnectReason, Room, RoomEvent, Track} from "livekit-client"
import {getToken} from "nostr-tools/nip98"
import {derived, get, writable} from "svelte/store" import {derived, get, writable} from "svelte/store"
import {now} from "@welshman/lib" import {now} from "@welshman/lib"
import {makeEvent, getTagValue} from "@welshman/util" import {makeEvent, makeHttpAuth, makeHttpAuthHeader, getTagValue} from "@welshman/util"
import {signer, publishThunk} from "@welshman/app" import {signer, publishThunk} from "@welshman/app"
import {getLivekitEndpoint} from "$lib/livekit" import {getLivekitEndpoint} from "$lib/livekit"
import {AbortError, whenAborted, whenTimeout} from "$lib/util"
import {deriveEventsForUrl} from "@app/core/state" import {deriveEventsForUrl} from "@app/core/state"
import {pushToast} from "@app/util/toast" import {pushToast} from "@app/util/toast"
@@ -28,7 +28,7 @@ export type VoiceSession = {
export const currentVoiceSession = writable<VoiceSession | undefined>(undefined) export const currentVoiceSession = writable<VoiceSession | undefined>(undefined)
export const speakingPubkeys = writable<Set<string>>(new Set()) export const speakingPubkeys = writable(new Set<string>())
const fetchLivekitToken = async ( const fetchLivekitToken = async (
url: string, url: string,
@@ -40,31 +40,16 @@ const fetchLivekitToken = async (
const $signer = signer.get() const $signer = signer.get()
if (!$signer) throw new Error("No signer available") if (!$signer) throw new Error("No signer available")
if (signal?.aborted) throw new Error("Join cancelled") if (signal?.aborted) throw new DOMException("Aborted", "AbortError")
const authHeader = await getToken( const template = await makeHttpAuth(endpoint, "GET")
endpoint, const signedEvent = await $signer.sign(template)
"GET", const authHeader = makeHttpAuthHeader(signedEvent)
template =>
$signer.sign(
makeEvent(template.kind, {
tags: template.tags,
content: template.content ?? "",
}),
),
true,
)
let response: Response const response = await fetch(endpoint, {
try { headers: {Authorization: authHeader},
response = await fetch(endpoint, { signal,
headers: {Authorization: authHeader}, })
signal,
})
} catch (e) {
if (e instanceof DOMException && e.name === "AbortError") throw new Error("Join cancelled")
throw e
}
if (!response.ok) { if (!response.ok) {
const text = await response.text() const text = await response.text()
@@ -119,6 +104,36 @@ const stopPresenceHeartbeat = () => {
} }
} }
const onRoomDisconnected = (reason?: DisconnectReason) => {
speakingPubkeys.set(new Set())
currentVoiceSession.set(undefined)
stopPresenceHeartbeat()
if (reason !== undefined && reason !== DisconnectReason.CLIENT_INITIATED) {
const message =
reason === DisconnectReason.JOIN_FAILURE
? "Could not connect to voice room. Please try again."
: "Voice connection lost."
pushToast({theme: "error", message})
}
}
const onTrackSubscribed = (track: Track) => {
if (track.kind === Track.Kind.Audio) {
const element = track.attach()
element.style.display = "none"
document.body.appendChild(element)
element.play().catch(() => {})
}
}
const onTrackUnsubscribed = (track: Track) => {
track.detach().forEach(el => el.remove())
}
const onActiveSpeakersChanged = (participants: {identity: string}[]) => {
speakingPubkeys.set(new Set(participants.map(p => p.identity)))
}
export const joinVoiceRoom = async ( export const joinVoiceRoom = async (
url: string, url: string,
h: string, h: string,
@@ -126,83 +141,33 @@ export const joinVoiceRoom = async (
): Promise<void> => { ): Promise<void> => {
const session = get(currentVoiceSession) const session = get(currentVoiceSession)
if (session) { if (session) await leaveVoiceRoom()
if (session.url === url && session.h === h) return
await leaveVoiceRoom()
}
const {server_url, participant_token} = await fetchLivekitToken(url, h, signal) const {server_url, participant_token} = await fetchLivekitToken(url, h, signal)
if (signal?.aborted) throw new Error("Join cancelled") if (signal?.aborted) return
const room = new Room({ const room = new Room({adaptiveStream: true, dynacast: true})
adaptiveStream: true,
dynacast: true, room.on(RoomEvent.Disconnected, onRoomDisconnected)
room.on(RoomEvent.TrackSubscribed, onTrackSubscribed)
room.on(RoomEvent.TrackUnsubscribed, onTrackUnsubscribed)
room.on(RoomEvent.ActiveSpeakersChanged, onActiveSpeakersChanged)
const connect = room.connect(server_url, participant_token, {maxRetries: 0})
const timeout = whenTimeout(5_000, {
message: "Connection timed out. Please check your network and try again.",
}) })
const abort = whenAborted(signal)
room.on(RoomEvent.Disconnected, (reason?: DisconnectReason) => {
speakingPubkeys.set(new Set())
currentVoiceSession.set(undefined)
stopPresenceHeartbeat()
if (reason !== undefined && reason !== DisconnectReason.CLIENT_INITIATED) {
const message =
reason === DisconnectReason.JOIN_FAILURE
? "Could not connect to voice room. Please try again."
: "Voice connection lost."
pushToast({theme: "error", message})
}
})
room.on(RoomEvent.TrackSubscribed, (track, _publication, _participant) => {
if (track.kind === Track.Kind.Audio) {
const element = track.attach()
element.style.display = "none"
document.body.appendChild(element)
element.play().catch(() => {})
}
})
room.on(RoomEvent.TrackUnsubscribed, track => {
track.detach().forEach(el => el.remove())
})
room.on(RoomEvent.ActiveSpeakersChanged, participants => {
speakingPubkeys.set(new Set(participants.map(p => p.identity)))
})
const onAbort = () => {
room.disconnect()
}
if (signal) {
if (signal.aborted) {
room.disconnect()
throw new Error("Join cancelled")
}
signal.addEventListener("abort", onAbort, {once: true})
}
const CONNECT_TIMEOUT_MS = 5_000
try { try {
await Promise.race([ await Promise.race([connect, timeout, abort])
room.connect(server_url, participant_token, {maxRetries: 0}),
new Promise<never>((_, reject) =>
setTimeout(
() => reject(new Error("Connection timed out. Please check your network and try again.")),
CONNECT_TIMEOUT_MS,
),
),
])
} catch (e) { } catch (e) {
room.disconnect() room.disconnect()
if (signal?.aborted) { if (e instanceof AbortError) return
throw new Error("Join cancelled")
}
throw e throw e
} finally {
signal?.removeEventListener("abort", onAbort)
} }
if (signal?.aborted) throw new Error("Join cancelled")
await room.localParticipant.setMicrophoneEnabled(true) await room.localParticipant.setMicrophoneEnabled(true)
currentVoiceSession.set({url, h, room, muted: false}) currentVoiceSession.set({url, h, room, muted: false})
+30
View File
@@ -19,6 +19,36 @@ export const ucFirst = (s: string) => s.slice(0, 1).toUpperCase() + s.slice(1)
export const errorMessage = (err: unknown) => String(err).replace(/^.*Error: /, "") export const errorMessage = (err: unknown) => String(err).replace(/^.*Error: /, "")
export class AbortError extends Error {
constructor() {
super("Aborted")
this.name = "AbortError"
}
}
export class TimeoutError extends Error {
constructor(message = "Timed out") {
super(message)
this.name = "TimeoutError"
}
}
/** Returns a promise that rejects with AbortError when signal aborts. Use with Promise.race. */
export const whenAborted = (signal?: AbortSignal) => {
if (!signal) return new Promise<never>(() => {})
return new Promise<never>((_, reject) => {
const onAborted = () => reject(new AbortError())
if (signal.aborted) onAborted()
else signal.addEventListener("abort", onAborted, {once: true})
})
}
/** Returns a promise that rejects with TimeoutError after ms. Use with Promise.race. */
export const whenTimeout = (ms: number, opts: {message?: string} = {}) => {
return new Promise<never>((_, reject) => setTimeout(() => reject(new TimeoutError()), ms))
}
export const buildUrl = (base: string | URL, ...pathname: string[]) => { export const buildUrl = (base: string | URL, ...pathname: string[]) => {
const url = new URL(base) const url = new URL(base)