diff --git a/lib/services/rocketchat-call-listener.ts b/lib/services/rocketchat-call-listener.ts index fb1a81a..59b455e 100644 --- a/lib/services/rocketchat-call-listener.ts +++ b/lib/services/rocketchat-call-listener.ts @@ -97,7 +97,9 @@ export class RocketChatCallListener { this.isDdpConnected = false; // Reset DDP connection state this.reconnectAttempts = 0; // First, establish DDP connection + console.log('[ROCKETCHAT_CALL_LISTENER] About to call connectDDP()'); this.connectDDP(); + console.log('[ROCKETCHAT_CALL_LISTENER] connectDDP() called'); }; this.ws.onmessage = (event) => { @@ -249,7 +251,7 @@ export class RocketChatCallListener { } /** - * Subscribe to webrtc events for incoming calls + * Subscribe to webrtc events and notifications for incoming calls */ private subscribeToCalls(): void { if (!this.ws || !this.userId) { @@ -257,9 +259,25 @@ export class RocketChatCallListener { return; } - this.subscriptionId = `call-sub-${Date.now()}`; + // Subscribe to notifications (calls come via notifications with message.t === 'videoconf') + const notificationsSubId = `notifications-${Date.now()}`; + const notificationsMessage = { + msg: 'sub', + id: notificationsSubId, + name: 'stream-notify-user', + params: [`${this.userId}/notification`, false], + }; - // Subscribe to webrtc events + console.log('[ROCKETCHAT_CALL_LISTENER] 📢 Subscribing to notifications (for calls)', { + subscriptionId: notificationsSubId, + userId: this.userId, + message: notificationsMessage, + }); + + this.ws.send(JSON.stringify(notificationsMessage)); + + // Also subscribe to webrtc events (backup) + this.subscriptionId = `call-sub-${Date.now()}`; const subscribeMessage = { msg: 'sub', id: this.subscriptionId, @@ -267,10 +285,9 @@ export class RocketChatCallListener { params: [`${this.userId}/webrtc`, false], }; - console.log('[ROCKETCHAT_CALL_LISTENER] Subscribing to webrtc events', { + console.log('[ROCKETCHAT_CALL_LISTENER] Subscribing to webrtc events (backup)', { subscriptionId: this.subscriptionId, userId: this.userId, - message: subscribeMessage, }); logger.debug('[ROCKETCHAT_CALL_LISTENER] Subscribing to webrtc events', { @@ -279,18 +296,6 @@ export class RocketChatCallListener { }); this.ws.send(JSON.stringify(subscribeMessage)); - - // Also subscribe to all user notifications to catch any call-related events - const allNotificationsSubId = `all-notifications-${Date.now()}`; - const allNotificationsMessage = { - msg: 'sub', - id: allNotificationsSubId, - name: 'stream-notify-user', - params: [`${this.userId}/notification`, false], - }; - - console.log('[ROCKETCHAT_CALL_LISTENER] Also subscribing to all notifications'); - this.ws.send(JSON.stringify(allNotificationsMessage)); } /** @@ -361,7 +366,7 @@ export class RocketChatCallListener { return; } - // Handle call events (webrtc) + // Handle call events - check notifications for videoconf messages if (message.msg === 'changed' && message.collection === 'stream-notify-user') { const eventName = message.fields?.eventName; const args = message.fields?.args || []; @@ -372,6 +377,31 @@ export class RocketChatCallListener { message: JSON.stringify(message, null, 2), }); + // Check if this is a notification event (calls come via notifications!) + if (eventName?.includes('/notification') && args.length > 0) { + const notification = args[0]; + const payload = notification.payload; + + // Check if it's a videoconf (video call) message + if (payload?.message?.t === 'videoconf' || payload?.message?.t === 'audio' || payload?.message?.t === 'video') { + console.log('[ROCKETCHAT_CALL_LISTENER] ✅ VIDEO/AUDIO CALL DETECTED in notification!', { + type: payload.message.t, + sender: payload.sender, + roomId: payload.rid, + }); + + // Handle as call event + this.handleCallEvent({ + type: 'call', + action: 'ringing', + from: payload.sender, + roomId: payload.rid, + roomName: payload.name || notification.title, + message: payload.message, + }); + } + } + // Check if this is a webrtc event if (eventName?.includes('/webrtc')) { console.log('[ROCKETCHAT_CALL_LISTENER] ✅ This is a webrtc event!'); @@ -421,12 +451,11 @@ export class RocketChatCallListener { * Handle call event from RocketChat */ private handleCallEvent(eventData: any): void { - console.log('[ROCKETCHAT_CALL_LISTENER] Received webrtc event - FULL DATA:', JSON.stringify(eventData, null, 2)); + console.log('[ROCKETCHAT_CALL_LISTENER] Received call event - FULL DATA:', JSON.stringify(eventData, null, 2)); logger.debug('[ROCKETCHAT_CALL_LISTENER] Received call event', { eventData }); try { - // RocketChat webrtc events can have different formats - // Let's check multiple possible structures + // RocketChat call events can come in different formats let callType: string | null = null; let from: any = {}; let roomId: string | null = null; @@ -441,13 +470,16 @@ export class RocketChatCallListener { callType = eventData.event; } else if (eventData.callType) { callType = eventData.callType; + } else if (eventData.message?.t) { + // This is a notification with message type (videoconf, audio, video) + callType = eventData.message.t; } // Extract room info - roomId = eventData.roomId || eventData.rid || eventData.room?._id || eventData.room?._id; - roomName = eventData.roomName || eventData.room?.name || eventData.room?.fname; + roomId = eventData.roomId || eventData.rid || eventData.room?._id; + roomName = eventData.roomName || eventData.name || eventData.room?.name || eventData.room?.fname; - // Extract caller info + // Extract caller info - can be in different places from = eventData.from || eventData.caller || eventData.user || eventData.sender || {}; // Log all possible fields for debugging @@ -456,21 +488,24 @@ export class RocketChatCallListener { roomId, roomName, from, + messageType: eventData.message?.t, allKeys: Object.keys(eventData), }); // Check if this is an incoming call event - // RocketChat might send: 'call', 'ringing', 'incoming', 'offer', etc. + // RocketChat sends calls via notifications with message.t === 'videoconf' or 'audio' const isIncomingCall = callType === 'call' || callType === 'ringing' || callType === 'incoming' || callType === 'offer' || + callType === 'videoconf' || // Video call callType === 'video' || callType === 'audio' || eventData.action === 'ringing' || - eventData.type === 'video' || - eventData.type === 'audio' || + eventData.message?.t === 'videoconf' || + eventData.message?.t === 'audio' || + eventData.message?.t === 'video' || (eventData.type === 'call' && eventData.status === 'ringing'); if (isIncomingCall && roomId) { @@ -486,15 +521,17 @@ export class RocketChatCallListener { timestamp: new Date(), }; - console.log('[ROCKETCHAT_CALL_LISTENER] ✅ Incoming call detected!', { + console.log('[ROCKETCHAT_CALL_LISTENER] 🎉 INCOMING CALL DETECTED!', { from: callEvent.from.username, roomId: callEvent.roomId, roomName: callEvent.roomName, + callType, }); logger.info('[ROCKETCHAT_CALL_LISTENER] Incoming call detected', { from: callEvent.from.username, roomId, + callType, }); // Notify all handlers @@ -511,6 +548,7 @@ export class RocketChatCallListener { callType, roomId, isIncomingCall, + messageType: eventData.message?.t, }); } } catch (error) {