xonecas.io
8058
Ir

irvc / commit 6f94343

feat: you can now see whos in voice without joining

xonecas2026-10-12 6f94343fee6e03b39cb875b7464b0f9ff7a1bade parent 0802cbe

5 files changed

web/src/components/Sidebar.tsx+20−1
@@ -31,6 +31,7 @@ export default function Sidebar({
3131 selected,
3232 onSelect,
3333 voice,
34+ voiceRooms,
3435 username,
3536 users,
3637 online,
@@ -44,6 +45,7 @@ export default function Sidebar({
4445 selected: Channel | null;
4546 onSelect: (c: Channel) => void;
4647 voice: Voice;
48+ voiceRooms: Record<string, string[]>;
4749 username: string;
4850 users: UserSummary[];
4951 online: Set<string>;
@@ -83,7 +85,13 @@ export default function Sidebar({
8385 {channels
8486 .filter((c) => c.type === "voice")
8587 .map((c) => (
86- <VoiceChannel key={c.id} channel={c} voice={voice} username={username} />
88+ <VoiceChannel
89+ key={c.id}
90+ channel={c}
91+ voice={voice}
92+ occupants={voiceRooms[String(c.id)] ?? []}
93+ username={username}
94+ />
8795 ))}
8896 </div>
8997 <div>
@@ -168,10 +176,12 @@ function Group({
168176 function VoiceChannel({
169177 channel,
170178 voice,
179+ occupants,
171180 username,
172181 }: {
173182 channel: Channel;
174183 voice: Voice;
184+ occupants: string[];
175185 username: string;
176186 }) {
177187 const active = voice.channel?.id === channel.id;
@@ -219,6 +229,15 @@ function VoiceChannel({
219229 </Button>
220230 </div>
221231 )}
232+ {!active && occupants.length > 0 && (
233+ <div className="flex flex-col gap-0.5 pb-0.5 pl-3.5 pr-2">
234+ {occupants.map((name) => (
235+ <span key={name} className="truncate text-xs text-sepia/70">
236+ {name}
237+ </span>
238+ ))}
239+ </div>
240+ )}
222241 </div>
223242 );
224243 }
web/src/lib/types.ts+1−0
@@ -30,4 +30,5 @@ export interface WSEvent {
3030 message?: Message;
3131 users?: string[];
3232 roster?: UserSummary[];
33+ voice?: Record<string, string[]>;
3334 }
web/src/lib/voice.ts+8−1
@@ -181,7 +181,10 @@ export interface Voice {
181181 // switching panes. Chat holds it and passes it to the sidebar controls.
182182 // Voice settings are read from a ref so `join` always uses the latest values,
183183 // while live changes (device, processing, quality) are applied via effects.
184-export function useVoice(settings: Settings): Voice {
184+export function useVoice(
185+ settings: Settings,
186+ onVoice?: (channelID: number | null) => void,
187+): Voice {
185188 const [channel, setChannel] = useState<Channel | null>(null);
186189 const [status, setStatus] = useState<VoiceStatus>("idle");
187190 const [error, setError] = useState("");
@@ -195,8 +198,10 @@ export function useVoice(settings: Settings): Voice {
195198 const mutedRef = useRef(false);
196199 const appliedRef = useRef("");
197200 const gateRef = useRef<MicGate | null>(null);
201+ const onVoiceRef = useRef(onVoice);
198202 settingsRef.current = settings;
199203 mutedRef.current = muted;
204+ onVoiceRef.current = onVoice;
200205
201206 if (!gateRef.current) gateRef.current = micGate();
202207 const gate = gateRef.current;
@@ -217,6 +222,7 @@ export function useVoice(settings: Settings): Voice {
217222 setPeers([]);
218223 setSpeaking([]);
219224 setMuted(false);
225+ onVoiceRef.current?.(null);
220226 }, []);
221227
222228 const join = useCallback(
@@ -266,6 +272,7 @@ export function useVoice(settings: Settings): Voice {
266272 }
267273 appliedRef.current = micSignature(s);
268274 setStatus("live");
275+ onVoiceRef.current?.(c.id);
269276 sync();
270277 } catch (err) {
271278 if (gen !== genRef.current) return;
web/src/views/Chat.tsx+15−2
@@ -20,8 +20,15 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
2020 const desktop = useRef(window.matchMedia("(min-width: 768px)").matches);
2121 const [sidebarOpen, setSidebarOpen] = useState(desktop.current);
2222 const selectedId = useRef<number | null>(null);
23+ const wsRef = useRef<WebSocket | null>(null);
2324 const { settings, update } = useSettings();
24- const voice = useVoice(settings);
25+ const voice = useVoice(settings, (channelId) => {
26+ const ws = wsRef.current;
27+ if (ws?.readyState === WebSocket.OPEN) {
28+ ws.send(JSON.stringify({ type: "voice", channelId: channelId ?? 0 }));
29+ }
30+ });
31+ const [voiceRooms, setVoiceRooms] = useState<Record<string, string[]>>({});
2532 const { theme, toggle } = useTheme();
2633
2734 useEffect(() => {
@@ -43,16 +50,21 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
4350 useEffect(() => {
4451 const proto = window.location.protocol === "https:" ? "wss" : "ws";
4552 const ws = new WebSocket(`${proto}://${window.location.host}/api/ws`);
53+ wsRef.current = ws;
4654 ws.onmessage = (e) => {
4755 const ev = JSON.parse(e.data as string) as WSEvent;
4856 if (ev.type === "presence") {
4957 setOnline(new Set(ev.users ?? []));
5058 if (ev.roster) setUsers(ev.roster);
59+ setVoiceRooms(ev.voice ?? {});
5160 } else if (ev.message && ev.message.channelId === selectedId.current) {
5261 setMessages((prev) => [...prev, ev.message!]);
5362 }
5463 };
55- return () => ws.close();
64+ return () => {
65+ wsRef.current = null;
66+ ws.close();
67+ };
5668 }, []);
5769
5870 useEffect(() => {
@@ -95,6 +107,7 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
95107 selected={selected}
96108 onSelect={selectChannel}
97109 voice={voice}
110+ voiceRooms={voiceRooms}
98111 username={user.username}
99112 users={users}
100113 online={online}
ws.go+76−7
@@ -6,6 +6,8 @@ import (
66 "net"
77 "net/http"
88 "net/url"
9+ "sort"
10+ "strconv"
911 "sync"
1012 "time"
1113
@@ -14,16 +16,24 @@ import (
1416 )
1517
1618 type event struct {
17- Type string `json:"type"`
18- Message *Message `json:"message,omitempty"`
19- Users []string `json:"users,omitempty"`
20- Roster []userSummary `json:"roster,omitempty"`
19+ Type string `json:"type"`
20+ Message *Message `json:"message,omitempty"`
21+ Users []string `json:"users,omitempty"`
22+ Roster []userSummary `json:"roster,omitempty"`
23+ Voice map[string][]string `json:"voice,omitempty"`
24+}
25+
26+// wsIn is a message sent from a client over the socket.
27+type wsIn struct {
28+ Type string `json:"type"`
29+ ChannelID int64 `json:"channelId"`
2130 }
2231
2332 type client struct {
2433 hub *hub
2534 userID int64
2635 username string
36+ voiceID int64
2737 conn *websocket.Conn
2838 send chan []byte
2939 }
@@ -31,11 +41,12 @@ type client struct {
3141 type hub struct {
3242 mu sync.Mutex
3343 clients map[int64]map[*client]bool
44+ voice map[int64]map[int64]string
3445 db *sql.DB
3546 }
3647
3748 func newHub(db *sql.DB) *hub {
38- return &hub{clients: map[int64]map[*client]bool{}, db: db}
49+ return &hub{clients: map[int64]map[*client]bool{}, voice: map[int64]map[int64]string{}, db: db}
3950 }
4051
4152 func (h *hub) register(c *client) {
@@ -56,11 +67,41 @@ func (h *hub) unregister(c *client) {
5667 delete(h.clients, c.userID)
5768 }
5869 }
70+ h.clearVoiceLocked(c)
5971 h.mu.Unlock()
6072 close(c.send)
6173 h.broadcastPresence()
6274 }
6375
76+// clearVoiceLocked drops a client's voice occupancy. Caller holds h.mu.
77+func (h *hub) clearVoiceLocked(c *client) {
78+ if c.voiceID == 0 {
79+ return
80+ }
81+ if set := h.voice[c.voiceID]; set != nil {
82+ delete(set, c.userID)
83+ if len(set) == 0 {
84+ delete(h.voice, c.voiceID)
85+ }
86+ }
87+ c.voiceID = 0
88+}
89+
90+// setVoice moves a client into a voice channel (0 for none) and rebroadcasts.
91+func (h *hub) setVoice(c *client, channelID int64) {
92+ h.mu.Lock()
93+ h.clearVoiceLocked(c)
94+ c.voiceID = channelID
95+ if channelID != 0 {
96+ if h.voice[channelID] == nil {
97+ h.voice[channelID] = map[int64]string{}
98+ }
99+ h.voice[channelID][c.userID] = c.username
100+ }
101+ h.mu.Unlock()
102+ h.broadcastPresence()
103+}
104+
64105 func (h *hub) users() []string {
65106 h.mu.Lock()
66107 defer h.mu.Unlock()
@@ -112,8 +153,28 @@ func (h *hub) roster() []userSummary {
112153 return users
113154 }
114155
156+func (h *hub) voiceUsers() map[string][]string {
157+ h.mu.Lock()
158+ defer h.mu.Unlock()
159+ out := map[string][]string{}
160+ for channelID, set := range h.voice {
161+ names := make([]string, 0, len(set))
162+ for _, name := range set {
163+ names = append(names, name)
164+ }
165+ sort.Strings(names)
166+ out[strconv.FormatInt(channelID, 10)] = names
167+ }
168+ return out
169+}
170+
115171 func (h *hub) broadcastPresence() {
116- h.broadcast(event{Type: "presence", Users: h.users(), Roster: h.roster()})
172+ h.broadcast(event{
173+ Type: "presence",
174+ Users: h.users(),
175+ Roster: h.roster(),
176+ Voice: h.voiceUsers(),
177+ })
117178 }
118179
119180 var upgrader = websocket.Upgrader{
@@ -166,9 +227,17 @@ func (c *client) readPump() {
166227 return nil
167228 })
168229 for {
169- if _, _, err := c.conn.ReadMessage(); err != nil {
230+ _, msg, err := c.conn.ReadMessage()
231+ if err != nil {
170232 return
171233 }
234+ var in wsIn
235+ if json.Unmarshal(msg, &in) != nil {
236+ continue
237+ }
238+ if in.Type == "voice" {
239+ c.hub.setVoice(c, in.ChannelID)
240+ }
172241 }
173242 }