refactor Notifications rc

This commit is contained in:
alma 2026-01-16 01:36:35 +01:00
parent 58b022f738
commit d44dff8d34

View File

@ -97,7 +97,9 @@ export class RocketChatCallListener {
this.isDdpConnected = false; // Reset DDP connection state this.isDdpConnected = false; // Reset DDP connection state
this.reconnectAttempts = 0; this.reconnectAttempts = 0;
// First, establish DDP connection // First, establish DDP connection
console.log('[ROCKETCHAT_CALL_LISTENER] About to call connectDDP()');
this.connectDDP(); this.connectDDP();
console.log('[ROCKETCHAT_CALL_LISTENER] connectDDP() called');
}; };
this.ws.onmessage = (event) => { 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 { private subscribeToCalls(): void {
if (!this.ws || !this.userId) { if (!this.ws || !this.userId) {
@ -257,9 +259,25 @@ export class RocketChatCallListener {
return; 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 = { const subscribeMessage = {
msg: 'sub', msg: 'sub',
id: this.subscriptionId, id: this.subscriptionId,
@ -267,10 +285,9 @@ export class RocketChatCallListener {
params: [`${this.userId}/webrtc`, false], 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, subscriptionId: this.subscriptionId,
userId: this.userId, userId: this.userId,
message: subscribeMessage,
}); });
logger.debug('[ROCKETCHAT_CALL_LISTENER] Subscribing to webrtc events', { logger.debug('[ROCKETCHAT_CALL_LISTENER] Subscribing to webrtc events', {
@ -279,18 +296,6 @@ export class RocketChatCallListener {
}); });
this.ws.send(JSON.stringify(subscribeMessage)); 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; return;
} }
// Handle call events (webrtc) // Handle call events - check notifications for videoconf messages
if (message.msg === 'changed' && message.collection === 'stream-notify-user') { if (message.msg === 'changed' && message.collection === 'stream-notify-user') {
const eventName = message.fields?.eventName; const eventName = message.fields?.eventName;
const args = message.fields?.args || []; const args = message.fields?.args || [];
@ -372,6 +377,31 @@ export class RocketChatCallListener {
message: JSON.stringify(message, null, 2), 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 // Check if this is a webrtc event
if (eventName?.includes('/webrtc')) { if (eventName?.includes('/webrtc')) {
console.log('[ROCKETCHAT_CALL_LISTENER] ✅ This is a webrtc event!'); console.log('[ROCKETCHAT_CALL_LISTENER] ✅ This is a webrtc event!');
@ -421,12 +451,11 @@ export class RocketChatCallListener {
* Handle call event from RocketChat * Handle call event from RocketChat
*/ */
private handleCallEvent(eventData: any): void { 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 }); logger.debug('[ROCKETCHAT_CALL_LISTENER] Received call event', { eventData });
try { try {
// RocketChat webrtc events can have different formats // RocketChat call events can come in different formats
// Let's check multiple possible structures
let callType: string | null = null; let callType: string | null = null;
let from: any = {}; let from: any = {};
let roomId: string | null = null; let roomId: string | null = null;
@ -441,13 +470,16 @@ export class RocketChatCallListener {
callType = eventData.event; callType = eventData.event;
} else if (eventData.callType) { } else if (eventData.callType) {
callType = 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 // Extract room info
roomId = eventData.roomId || eventData.rid || eventData.room?._id || eventData.room?._id; roomId = eventData.roomId || eventData.rid || eventData.room?._id;
roomName = eventData.roomName || eventData.room?.name || eventData.room?.fname; 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 || {}; from = eventData.from || eventData.caller || eventData.user || eventData.sender || {};
// Log all possible fields for debugging // Log all possible fields for debugging
@ -456,21 +488,24 @@ export class RocketChatCallListener {
roomId, roomId,
roomName, roomName,
from, from,
messageType: eventData.message?.t,
allKeys: Object.keys(eventData), allKeys: Object.keys(eventData),
}); });
// Check if this is an incoming call event // 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 = const isIncomingCall =
callType === 'call' || callType === 'call' ||
callType === 'ringing' || callType === 'ringing' ||
callType === 'incoming' || callType === 'incoming' ||
callType === 'offer' || callType === 'offer' ||
callType === 'videoconf' || // Video call
callType === 'video' || callType === 'video' ||
callType === 'audio' || callType === 'audio' ||
eventData.action === 'ringing' || eventData.action === 'ringing' ||
eventData.type === 'video' || eventData.message?.t === 'videoconf' ||
eventData.type === 'audio' || eventData.message?.t === 'audio' ||
eventData.message?.t === 'video' ||
(eventData.type === 'call' && eventData.status === 'ringing'); (eventData.type === 'call' && eventData.status === 'ringing');
if (isIncomingCall && roomId) { if (isIncomingCall && roomId) {
@ -486,15 +521,17 @@ export class RocketChatCallListener {
timestamp: new Date(), timestamp: new Date(),
}; };
console.log('[ROCKETCHAT_CALL_LISTENER] ✅ Incoming call detected!', { console.log('[ROCKETCHAT_CALL_LISTENER] 🎉 INCOMING CALL DETECTED!', {
from: callEvent.from.username, from: callEvent.from.username,
roomId: callEvent.roomId, roomId: callEvent.roomId,
roomName: callEvent.roomName, roomName: callEvent.roomName,
callType,
}); });
logger.info('[ROCKETCHAT_CALL_LISTENER] Incoming call detected', { logger.info('[ROCKETCHAT_CALL_LISTENER] Incoming call detected', {
from: callEvent.from.username, from: callEvent.from.username,
roomId, roomId,
callType,
}); });
// Notify all handlers // Notify all handlers
@ -511,6 +548,7 @@ export class RocketChatCallListener {
callType, callType,
roomId, roomId,
isIncomingCall, isIncomingCall,
messageType: eventData.message?.t,
}); });
} }
} catch (error) { } catch (error) {