ausername-12345 commited on
Commit
c89717a
·
1 Parent(s): a433909

fix read receipts and add WebRTC audio/video calling

Browse files

- Read receipts: sendReadReceipt now called when message arrives in
already-open DM (not just when opening chat), so ✓→✓✓ updates live
- Added call button in DM header with Video Call / Audio Call dropdown
- WebRTC signaling via WebSocket: call_offer/call_answer/ice_candidate/call_end
- Incoming call modal with accept/reject, active call modal with mute/end
- Local video preview (PIP), remote video full-size for video calls
- Audio call shows large avatar with call controls
- Call button hidden in group chats (DMs only for now)

Files changed (4) hide show
  1. src/main.py +8 -0
  2. src/static/app.js +253 -1
  3. src/static/index.html +53 -1
  4. src/ws_manager.py +34 -0
src/main.py CHANGED
@@ -57,6 +57,14 @@ async def websocket_endpoint(websocket: WebSocket, token: str, db: Session = Dep
57
  await manager.handle_typing(msg, user)
58
  elif msg_type == "read":
59
  await manager.handle_read(msg, user, db)
 
 
 
 
 
 
 
 
60
 
61
  except WebSocketDisconnect:
62
  manager.disconnect(user.id)
 
57
  await manager.handle_typing(msg, user)
58
  elif msg_type == "read":
59
  await manager.handle_read(msg, user, db)
60
+ elif msg_type == "call_offer":
61
+ await manager.handle_call_offer(msg, user)
62
+ elif msg_type == "call_answer":
63
+ await manager.handle_call_answer(msg, user)
64
+ elif msg_type == "ice_candidate":
65
+ await manager.handle_ice_candidate(msg, user)
66
+ elif msg_type == "call_end":
67
+ await manager.handle_call_end(msg, user)
68
 
69
  except WebSocketDisconnect:
70
  manager.disconnect(user.id)
src/static/app.js CHANGED
@@ -18,6 +18,22 @@ let typingTimeout = null;
18
  let selectedGroupMembers = []; // for create group modal
19
  let currentGroupData = null;
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  // ── Bootstrap ───────────────────────────────────────────────────────────────
22
 
23
  window.addEventListener("DOMContentLoaded", async () => {
@@ -327,6 +343,14 @@ async function handleWsMessage(evt) {
327
  showTyping(msg);
328
  } else if (msg.type === "read_receipt") {
329
  handleReadReceipt(msg);
 
 
 
 
 
 
 
 
330
  }
331
  }
332
 
@@ -364,6 +388,7 @@ async function handleIncomingDm(msg) {
364
  if (currentChat && currentChat.type === "dm" && currentChat.id === conversation_id) {
365
  appendMessage(withPlain, currentChat.other_user);
366
  scrollToBottom();
 
367
  }
368
 
369
  await loadChats();
@@ -448,6 +473,222 @@ function handleReadReceipt(msg) {
448
  }
449
  }
450
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
451
  async function loadChats() {
452
  [conversations, groups] = await Promise.all([
453
  apiFetch("/chat/conversations"),
@@ -519,6 +760,7 @@ async function openDmChat(conv) {
519
  document.getElementById("chat-avatar").style.background = full.other_user.avatar_color;
520
  document.getElementById("chat-name").textContent = full.other_user.display_name;
521
  document.getElementById("chat-sub").textContent = "@" + full.other_user.username;
 
522
  document.getElementById("group-settings-btn").classList.add("hidden");
523
 
524
  document.getElementById("empty-state").classList.add("hidden");
@@ -552,6 +794,8 @@ async function openGroupChat(group) {
552
  document.getElementById("chat-avatar").style.background = full.avatar_color;
553
  document.getElementById("chat-name").textContent = full.name;
554
  document.getElementById("chat-sub").textContent = `${full.members.length} members`;
 
 
555
  document.getElementById("group-settings-btn").classList.remove("hidden");
556
 
557
  document.getElementById("empty-state").classList.add("hidden");
@@ -776,6 +1020,8 @@ function showChatHeader(user) {
776
  document.getElementById("chat-avatar").style.background = user.avatar_color;
777
  document.getElementById("chat-name").textContent = user.display_name;
778
  document.getElementById("chat-sub").textContent = "@" + user.username;
 
 
779
  document.getElementById("group-settings-btn").classList.add("hidden");
780
  }
781
 
@@ -911,10 +1157,16 @@ function closeModal(id) {
911
 
912
  // Close on backdrop click
913
  document.addEventListener("click", (e) => {
914
- ["modal-new-chat", "modal-new-group", "modal-settings", "modal-group-settings"].forEach(id => {
915
  const el = document.getElementById(id);
916
  if (e.target === el) el.classList.add("hidden");
917
  });
 
 
 
 
 
 
918
  });
919
 
920
  // ── Utils ─────────────────────────────────────────────────────────────────────
 
18
  let selectedGroupMembers = []; // for create group modal
19
  let currentGroupData = null;
20
 
21
+ // ── Call State ────────────────────────────────────────────────────────────────
22
+ let callState = null;
23
+ let callType = null;
24
+ let callTarget = null;
25
+ let localStream = null;
26
+ let remoteStream = null;
27
+ let peerConnection = null;
28
+ let pendingCallOffer = null;
29
+
30
+ const ICE_SERVERS = {
31
+ iceServers: [
32
+ { urls: 'stun:stun.l.google.com:19302' },
33
+ { urls: 'stun:stun1.l.google.com:19302' }
34
+ ]
35
+ };
36
+
37
  // ── Bootstrap ───────────────────────────────────────────────────────────────
38
 
39
  window.addEventListener("DOMContentLoaded", async () => {
 
343
  showTyping(msg);
344
  } else if (msg.type === "read_receipt") {
345
  handleReadReceipt(msg);
346
+ } else if (msg.type === "call_offer") {
347
+ handleCallOffer(msg);
348
+ } else if (msg.type === "call_answer") {
349
+ handleCallAnswer(msg);
350
+ } else if (msg.type === "ice_candidate") {
351
+ handleIceCandidate(msg);
352
+ } else if (msg.type === "call_end") {
353
+ handleCallEnd(msg);
354
  }
355
  }
356
 
 
388
  if (currentChat && currentChat.type === "dm" && currentChat.id === conversation_id) {
389
  appendMessage(withPlain, currentChat.other_user);
390
  scrollToBottom();
391
+ sendReadReceipt(conversation_id);
392
  }
393
 
394
  await loadChats();
 
473
  }
474
  }
475
 
476
+ // ── Call Functions ────────────────────────────────────────────────────────────
477
+
478
+ function showCallDropdown() {
479
+ document.getElementById("call-dropdown").classList.toggle("hidden");
480
+ }
481
+
482
+ function showCallModal() {
483
+ document.getElementById("modal-call").classList.remove("hidden");
484
+ }
485
+
486
+ function hideCallModal() {
487
+ document.getElementById("modal-call").classList.add("hidden");
488
+ }
489
+
490
+ function showIncomingCall() {
491
+ document.getElementById("incoming-call").classList.remove("hidden");
492
+ }
493
+
494
+ function hideIncomingCall() {
495
+ document.getElementById("incoming-call").classList.add("hidden");
496
+ }
497
+
498
+ async function startCall(type) {
499
+ if (!currentChat || currentChat.type !== "dm" || !currentChat.other_user) return;
500
+ document.getElementById("call-dropdown").classList.add("hidden");
501
+ callType = type;
502
+ callTarget = currentChat.other_user;
503
+ callState = "calling";
504
+ try {
505
+ const constraints = { audio: true, video: type === "video" };
506
+ localStream = await navigator.mediaDevices.getUserMedia(constraints);
507
+ peerConnection = new RTCPeerConnection(ICE_SERVERS);
508
+ localStream.getTracks().forEach(t => peerConnection.addTrack(t, localStream));
509
+ remoteStream = new MediaStream();
510
+ peerConnection.ontrack = (event) => {
511
+ event.streams[0].getTracks().forEach(t => remoteStream.addTrack(t));
512
+ const el = document.getElementById("remote-video");
513
+ if (el) el.srcObject = remoteStream;
514
+ };
515
+ peerConnection.onicecandidate = (event) => {
516
+ if (event.candidate && ws && ws.readyState === WebSocket.OPEN) {
517
+ ws.send(JSON.stringify({ type: "ice_candidate", target_id: callTarget.id, candidate: event.candidate }));
518
+ }
519
+ };
520
+ peerConnection.oniceconnectionstatechange = () => {
521
+ if (peerConnection.connectionState === "disconnected" || peerConnection.connectionState === "failed") {
522
+ endCall();
523
+ }
524
+ };
525
+ const offer = await peerConnection.createOffer();
526
+ await peerConnection.setLocalDescription(offer);
527
+ ws.send(JSON.stringify({ type: "call_offer", target_id: callTarget.id, call_type: type, sdp: peerConnection.localDescription }));
528
+ showCallModal();
529
+ document.getElementById("call-status").textContent = "Calling…";
530
+ document.getElementById("call-peer-name").textContent = callTarget.display_name;
531
+ setupCallMedia();
532
+ } catch (e) {
533
+ console.error("startCall error:", e);
534
+ cleanupCall();
535
+ }
536
+ }
537
+
538
+ function handleCallOffer(msg) {
539
+ if (callState) {
540
+ ws.send(JSON.stringify({ type: "call_end", target_id: msg.from }));
541
+ return;
542
+ }
543
+ callType = msg.call_type;
544
+ callTarget = { id: msg.from, display_name: msg.from_display_name, avatar_color: msg.from_avatar_color };
545
+ pendingCallOffer = msg.sdp;
546
+ callState = "ringing";
547
+ document.getElementById("incoming-caller-name").textContent = msg.from_display_name;
548
+ document.getElementById("incoming-call-type").textContent = msg.call_type === "video" ? "Video Call" : "Audio Call";
549
+ const av = document.getElementById("incoming-avatar");
550
+ av.textContent = msg.from_display_name[0].toUpperCase();
551
+ av.style.background = msg.from_avatar_color || "#6366f1";
552
+ document.getElementById("incoming-call-type-icon").textContent = msg.call_type === "video" ? "📹" : "📞";
553
+ document.getElementById("call-dropdown").classList.add("hidden");
554
+ showIncomingCall();
555
+ }
556
+
557
+ async function acceptCall() {
558
+ hideIncomingCall();
559
+ callState = "connected";
560
+ try {
561
+ const constraints = { audio: true, video: callType === "video" };
562
+ localStream = await navigator.mediaDevices.getUserMedia(constraints);
563
+ peerConnection = new RTCPeerConnection(ICE_SERVERS);
564
+ localStream.getTracks().forEach(t => peerConnection.addTrack(t, localStream));
565
+ remoteStream = new MediaStream();
566
+ peerConnection.ontrack = (event) => {
567
+ event.streams[0].getTracks().forEach(t => remoteStream.addTrack(t));
568
+ const el = document.getElementById("remote-video");
569
+ if (el) el.srcObject = remoteStream;
570
+ };
571
+ peerConnection.onicecandidate = (event) => {
572
+ if (event.candidate && ws && ws.readyState === WebSocket.OPEN) {
573
+ ws.send(JSON.stringify({ type: "ice_candidate", target_id: callTarget.id, candidate: event.candidate }));
574
+ }
575
+ };
576
+ peerConnection.oniceconnectionstatechange = () => {
577
+ if (peerConnection.connectionState === "disconnected" || peerConnection.connectionState === "failed") {
578
+ endCall();
579
+ }
580
+ };
581
+ await peerConnection.setRemoteDescription(new RTCSessionDescription(pendingCallOffer));
582
+ pendingCallOffer = null;
583
+ const answer = await peerConnection.createAnswer();
584
+ await peerConnection.setLocalDescription(answer);
585
+ ws.send(JSON.stringify({ type: "call_answer", target_id: callTarget.id, sdp: peerConnection.localDescription }));
586
+ showCallModal();
587
+ document.getElementById("call-status").textContent = "Connected";
588
+ document.getElementById("call-peer-name").textContent = callTarget.display_name;
589
+ setupCallMedia();
590
+ } catch (e) {
591
+ console.error("acceptCall error:", e);
592
+ cleanupCall();
593
+ }
594
+ }
595
+
596
+ function rejectCall() {
597
+ hideIncomingCall();
598
+ if (ws && ws.readyState === WebSocket.OPEN && callTarget) {
599
+ ws.send(JSON.stringify({ type: "call_end", target_id: callTarget.id }));
600
+ }
601
+ cleanupCall();
602
+ }
603
+
604
+ function handleCallAnswer(msg) {
605
+ if (!peerConnection || callState !== "calling") return;
606
+ callState = "connected";
607
+ document.getElementById("call-status").textContent = "Connected";
608
+ peerConnection.setRemoteDescription(new RTCSessionDescription(msg.sdp)).catch(e => console.error("setRemoteDescription error:", e));
609
+ }
610
+
611
+ function handleIceCandidate(msg) {
612
+ if (peerConnection && msg.candidate) {
613
+ peerConnection.addIceCandidate(new RTCIceCandidate(msg.candidate)).catch(() => {});
614
+ }
615
+ }
616
+
617
+ function handleCallEnd(msg) {
618
+ if (callState === "ringing") hideIncomingCall();
619
+ if (callState) {
620
+ callState = null;
621
+ const status = document.getElementById("call-status");
622
+ if (status) {
623
+ status.textContent = "Call ended";
624
+ setTimeout(hideCallModal, 1500);
625
+ }
626
+ cleanupCall();
627
+ }
628
+ }
629
+
630
+ function endCall() {
631
+ if (ws && ws.readyState === WebSocket.OPEN && callTarget) {
632
+ ws.send(JSON.stringify({ type: "call_end", target_id: callTarget.id }));
633
+ }
634
+ cleanupCall();
635
+ hideCallModal();
636
+ hideIncomingCall();
637
+ }
638
+
639
+ function cleanupCall() {
640
+ if (peerConnection) { peerConnection.close(); peerConnection = null; }
641
+ if (localStream) { localStream.getTracks().forEach(t => t.stop()); localStream = null; }
642
+ remoteStream = null;
643
+ callState = null;
644
+ callType = null;
645
+ callTarget = null;
646
+ pendingCallOffer = null;
647
+ const lv = document.getElementById("local-video");
648
+ const rv = document.getElementById("remote-video");
649
+ if (lv) lv.srcObject = null;
650
+ if (rv) rv.srcObject = null;
651
+ }
652
+
653
+ function setupCallMedia() {
654
+ const lv = document.getElementById("local-video");
655
+ if (lv) lv.srcObject = localStream;
656
+ const videoContent = document.getElementById("call-video-content");
657
+ const audioContent = document.getElementById("call-audio-content");
658
+ if (callType === "video") {
659
+ if (videoContent) videoContent.classList.remove("hidden");
660
+ if (audioContent) audioContent.classList.add("hidden");
661
+ } else {
662
+ if (videoContent) videoContent.classList.add("hidden");
663
+ if (audioContent) audioContent.classList.remove("hidden");
664
+ const av = document.getElementById("call-audio-avatar");
665
+ if (av) {
666
+ av.textContent = callTarget.display_name[0].toUpperCase();
667
+ av.style.background = callTarget.avatar_color || "#6366f1";
668
+ }
669
+ }
670
+ }
671
+
672
+ function toggleMute() {
673
+ if (localStream) {
674
+ const t = localStream.getAudioTracks()[0];
675
+ if (t) {
676
+ t.enabled = !t.enabled;
677
+ document.getElementById("call-mute-btn").textContent = t.enabled ? "🔊 Mute" : "🔇 Unmute";
678
+ }
679
+ }
680
+ }
681
+
682
+ function toggleVideo() {
683
+ if (localStream && callType === "video") {
684
+ const t = localStream.getVideoTracks()[0];
685
+ if (t) {
686
+ t.enabled = !t.enabled;
687
+ document.getElementById("call-video-btn").textContent = t.enabled ? "📹 Video" : "🚫 Video Off";
688
+ }
689
+ }
690
+ }
691
+
692
  async function loadChats() {
693
  [conversations, groups] = await Promise.all([
694
  apiFetch("/chat/conversations"),
 
760
  document.getElementById("chat-avatar").style.background = full.other_user.avatar_color;
761
  document.getElementById("chat-name").textContent = full.other_user.display_name;
762
  document.getElementById("chat-sub").textContent = "@" + full.other_user.username;
763
+ document.getElementById("call-btn").classList.remove("hidden");
764
  document.getElementById("group-settings-btn").classList.add("hidden");
765
 
766
  document.getElementById("empty-state").classList.add("hidden");
 
794
  document.getElementById("chat-avatar").style.background = full.avatar_color;
795
  document.getElementById("chat-name").textContent = full.name;
796
  document.getElementById("chat-sub").textContent = `${full.members.length} members`;
797
+ document.getElementById("call-btn").classList.add("hidden");
798
+ document.getElementById("call-dropdown").classList.add("hidden");
799
  document.getElementById("group-settings-btn").classList.remove("hidden");
800
 
801
  document.getElementById("empty-state").classList.add("hidden");
 
1020
  document.getElementById("chat-avatar").style.background = user.avatar_color;
1021
  document.getElementById("chat-name").textContent = user.display_name;
1022
  document.getElementById("chat-sub").textContent = "@" + user.username;
1023
+ document.getElementById("call-btn").classList.remove("hidden");
1024
+ document.getElementById("call-dropdown").classList.add("hidden");
1025
  document.getElementById("group-settings-btn").classList.add("hidden");
1026
  }
1027
 
 
1157
 
1158
  // Close on backdrop click
1159
  document.addEventListener("click", (e) => {
1160
+ ["modal-new-chat", "modal-new-group", "modal-settings", "modal-group-settings", "modal-call"].forEach(id => {
1161
  const el = document.getElementById(id);
1162
  if (e.target === el) el.classList.add("hidden");
1163
  });
1164
+ // Close call dropdown when clicking outside
1165
+ const dd = document.getElementById("call-dropdown");
1166
+ const btn = document.getElementById("call-btn");
1167
+ if (dd && !dd.classList.contains("hidden") && !dd.contains(e.target) && !btn.contains(e.target)) {
1168
+ dd.classList.add("hidden");
1169
+ }
1170
  });
1171
 
1172
  // ── Utils ─────────────────────────────────────────────────────────────────────
src/static/index.html CHANGED
@@ -29,6 +29,8 @@
29
  .unread-badge { background: rgba(255,255,255,0.15); backdrop-filter: blur(4px); border: 1px solid rgba(255,255,255,0.2); }
30
  #app { display:none; }
31
  #auth { display:flex; }
 
 
32
  </style>
33
  </head>
34
  <body class="h-screen overflow-hidden">
@@ -208,11 +210,20 @@
208
  <p id="chat-sub" class="text-xs text-gray-500"></p>
209
  </div>
210
  <div class="flex items-center gap-1">
 
 
 
 
 
 
 
 
 
211
  <span class="text-xs text-gray-600 flex items-center gap-1">
212
  <svg xmlns="http://www.w3.org/2000/svg" class="w-3 h-3 text-green-400" viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M5 9V7a5 5 0 0110 0v2a2 2 0 012 2v5a2 2 0 01-2 2H5a2 2 0 01-2-2v-5a2 2 0 012-2zm8-2v2H7V7a3 3 0 016 0z" clip-rule="evenodd"/></svg>
213
  E2EE
214
  </span>
215
- <button id="group-settings-btn" onclick="showGroupSettings()" class="hidden ml-2 p-1.5 rounded-lg hover:bg-white/10 transition-colors">
216
  <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 5v.01M12 12v.01M12 19v.01M12 6a1 1 0 110-2 1 1 0 010 2zm0 7a1 1 0 110-2 1 1 0 010 2zm0 7a1 1 0 110-2 1 1 0 010 2z"/></svg>
217
  </button>
218
  </div>
@@ -315,6 +326,47 @@
315
  </div>
316
  </div>
317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  <script src="/static/crypto.js"></script>
319
  <script src="/static/app.js"></script>
320
  </body>
 
29
  .unread-badge { background: rgba(255,255,255,0.15); backdrop-filter: blur(4px); border: 1px solid rgba(255,255,255,0.2); }
30
  #app { display:none; }
31
  #auth { display:flex; }
32
+ #call-dropdown { bottom: 100%; margin-bottom: 0.5rem; }
33
+ #call-dropdown.hidden { display: none; }
34
  </style>
35
  </head>
36
  <body class="h-screen overflow-hidden">
 
210
  <p id="chat-sub" class="text-xs text-gray-500"></p>
211
  </div>
212
  <div class="flex items-center gap-1">
213
+ <div class="relative">
214
+ <button onclick="showCallDropdown()" id="call-btn" class="p-1.5 rounded-lg hover:bg-white/10 transition-colors hidden" title="Call">
215
+ <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"/></svg>
216
+ </button>
217
+ <div id="call-dropdown" class="hidden absolute right-0 bottom-full mb-2 glass rounded-xl py-1 min-w-[140px] shadow-2xl z-50">
218
+ <button onclick="startCall('video')" class="w-full flex items-center gap-2 px-3 py-2 text-sm text-gray-300 hover:text-white hover:bg-white/5 transition-colors">📹 Video Call</button>
219
+ <button onclick="startCall('audio')" class="w-full flex items-center gap-2 px-3 py-2 text-sm text-gray-300 hover:text-white hover:bg-white/5 transition-colors">📞 Audio Call</button>
220
+ </div>
221
+ </div>
222
  <span class="text-xs text-gray-600 flex items-center gap-1">
223
  <svg xmlns="http://www.w3.org/2000/svg" class="w-3 h-3 text-green-400" viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M5 9V7a5 5 0 0110 0v2a2 2 0 012 2v5a2 2 0 01-2 2H5a2 2 0 01-2-2v-5a2 2 0 012-2zm8-2v2H7V7a3 3 0 016 0z" clip-rule="evenodd"/></svg>
224
  E2EE
225
  </span>
226
+ <button id="group-settings-btn" onclick="showGroupSettings()" class="hidden p-1.5 rounded-lg hover:bg-white/10 transition-colors">
227
  <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 5v.01M12 12v.01M12 19v.01M12 6a1 1 0 110-2 1 1 0 010 2zm0 7a1 1 0 110-2 1 1 0 010 2zm0 7a1 1 0 110-2 1 1 0 010 2z"/></svg>
228
  </button>
229
  </div>
 
326
  </div>
327
  </div>
328
 
329
+ <!-- Incoming Call Modal -->
330
+ <div id="incoming-call" class="hidden fixed inset-0 bg-black/70 backdrop-blur-md flex items-center justify-center z-50">
331
+ <div class="glass rounded-3xl p-8 w-full max-w-sm mx-4 shadow-2xl text-center">
332
+ <div id="incoming-avatar" class="w-20 h-20 rounded-full flex items-center justify-center text-white text-3xl font-semibold mx-auto mb-4"></div>
333
+ <p id="incoming-caller-name" class="text-lg font-semibold mb-1"></p>
334
+ <p class="text-sm text-gray-400 mb-6"><span id="incoming-call-type-icon">📞</span> <span id="incoming-call-type">Audio Call</span></p>
335
+ <div class="flex gap-4 justify-center">
336
+ <button onclick="rejectCall()" class="w-14 h-14 rounded-full bg-red-500/80 hover:bg-red-500 flex items-center justify-center text-white text-xl transition-colors shadow-lg">✕</button>
337
+ <button onclick="acceptCall()" class="w-14 h-14 rounded-full bg-green-500/80 hover:bg-green-500 flex items-center justify-center text-white text-xl transition-colors shadow-lg">✓</button>
338
+ </div>
339
+ </div>
340
+ </div>
341
+
342
+ <!-- Active Call Modal -->
343
+ <div id="modal-call" class="hidden fixed inset-0 bg-black/85 flex flex-col z-50">
344
+ <!-- Call header -->
345
+ <div class="flex items-center justify-between px-6 py-4">
346
+ <div>
347
+ <p id="call-peer-name" class="text-lg font-semibold text-white"></p>
348
+ <p id="call-status" class="text-sm text-gray-400">Connecting…</p>
349
+ </div>
350
+ <div class="flex gap-3">
351
+ <button onclick="toggleMute()" id="call-mute-btn" class="px-3 py-1.5 rounded-xl glass-btn text-sm text-gray-300 hover:text-white transition-colors">🔊 Mute</button>
352
+ <button onclick="toggleVideo()" id="call-video-btn" class="px-3 py-1.5 rounded-xl glass-btn text-sm text-gray-300 hover:text-white transition-colors hidden">📹 Video</button>
353
+ <button onclick="endCall()" class="px-4 py-1.5 rounded-xl bg-red-500/80 hover:bg-red-500 text-white text-sm font-medium transition-colors">End Call</button>
354
+ </div>
355
+ </div>
356
+ <!-- Video content -->
357
+ <div id="call-video-content" class="hidden flex-1 relative flex items-center justify-center p-4">
358
+ <video id="remote-video" autoplay playsinline class="max-w-full max-h-full rounded-2xl object-contain bg-black/40"></video>
359
+ <div id="local-video-container" class="absolute top-6 right-6 w-48 h-36 rounded-xl overflow-hidden shadow-2xl border-2 border-white/20">
360
+ <video id="local-video" autoplay playsinline muted class="w-full h-full object-cover"></video>
361
+ </div>
362
+ </div>
363
+ <!-- Audio content -->
364
+ <div id="call-audio-content" class="hidden flex-1 flex flex-col items-center justify-center gap-4">
365
+ <div id="call-audio-avatar" class="w-28 h-28 rounded-full flex items-center justify-center text-white text-5xl font-semibold"></div>
366
+ <p class="text-gray-400 text-sm">Audio call in progress</p>
367
+ </div>
368
+ </div>
369
+
370
  <script src="/static/crypto.js"></script>
371
  <script src="/static/app.js"></script>
372
  </body>
src/ws_manager.py CHANGED
@@ -144,6 +144,40 @@ class ConnectionManager:
144
  # Would need to broadcast to all group members
145
  pass
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  async def handle_read(self, msg: dict, sender: User, db: Session):
148
  conversation_id = msg.get("conversation_id")
149
  if conversation_id:
 
144
  # Would need to broadcast to all group members
145
  pass
146
 
147
+ async def handle_call_offer(self, msg: dict, sender: User):
148
+ target_id = msg.get("target_id")
149
+ await self.send_to(target_id, {
150
+ "type": "call_offer",
151
+ "from": sender.id,
152
+ "from_display_name": sender.display_name,
153
+ "from_avatar_color": sender.avatar_color,
154
+ "call_type": msg.get("call_type", "audio"),
155
+ "sdp": msg.get("sdp")
156
+ })
157
+
158
+ async def handle_call_answer(self, msg: dict, sender: User):
159
+ target_id = msg.get("target_id")
160
+ await self.send_to(target_id, {
161
+ "type": "call_answer",
162
+ "from": sender.id,
163
+ "sdp": msg.get("sdp")
164
+ })
165
+
166
+ async def handle_ice_candidate(self, msg: dict, sender: User):
167
+ target_id = msg.get("target_id")
168
+ await self.send_to(target_id, {
169
+ "type": "ice_candidate",
170
+ "from": sender.id,
171
+ "candidate": msg.get("candidate")
172
+ })
173
+
174
+ async def handle_call_end(self, msg: dict, sender: User):
175
+ target_id = msg.get("target_id")
176
+ await self.send_to(target_id, {
177
+ "type": "call_end",
178
+ "from": sender.id
179
+ })
180
+
181
  async def handle_read(self, msg: dict, sender: User, db: Session):
182
  conversation_id = msg.get("conversation_id")
183
  if conversation_id: