refactor Notifications

This commit is contained in:
alma 2026-01-16 00:48:12 +01:00
parent b926597a48
commit 2d35dbe074
2 changed files with 158 additions and 30 deletions

View File

@ -19,45 +19,52 @@ export function useRocketChatCalls() {
*/
const getRocketChatCredentials = useCallback(async () => {
if (!session?.user?.id) {
console.log('[useRocketChatCalls] No session user ID');
return null;
}
try {
// Get user token from RocketChat API
const response = await fetch('/api/rocket-chat/messages?refresh=true', {
credentials: 'include',
});
if (!response.ok) {
logger.error('[useRocketChatCalls] Failed to get RocketChat credentials');
return null;
}
console.log('[useRocketChatCalls] Getting RocketChat credentials...');
// Extract base URL from environment
const baseUrl = process.env.NEXT_PUBLIC_IFRAME_PAROLE_URL?.split('/channel')[0];
if (!baseUrl) {
console.error('[useRocketChatCalls] RocketChat base URL not configured');
logger.error('[useRocketChatCalls] RocketChat base URL not configured');
return null;
}
// Get user's RocketChat ID and token
// We need to call the API to get the user token
const tokenResponse = await fetch('/api/rocket-chat/user-token', {
credentials: 'include',
});
if (!tokenResponse.ok) {
logger.error('[useRocketChatCalls] Failed to get user token');
const errorText = await tokenResponse.text();
console.error('[useRocketChatCalls] Failed to get user token', {
status: tokenResponse.status,
error: errorText,
});
logger.error('[useRocketChatCalls] Failed to get user token', {
status: tokenResponse.status,
});
return null;
}
const tokenData = await tokenResponse.json();
console.log('[useRocketChatCalls] ✅ Got credentials', {
userId: tokenData.userId,
hasToken: !!tokenData.authToken,
baseUrl,
});
return {
userId: tokenData.userId,
authToken: tokenData.authToken,
baseUrl,
};
} catch (error) {
console.error('[useRocketChatCalls] Error getting credentials', error);
logger.error('[useRocketChatCalls] Error getting credentials', { error });
return null;
}
@ -68,11 +75,15 @@ export function useRocketChatCalls() {
*/
const initializeListener = useCallback(async () => {
if (initializedRef.current || listenerRef.current) {
console.log('[useRocketChatCalls] Already initialized, skipping');
return;
}
console.log('[useRocketChatCalls] Initializing call listener...');
const credentials = await getRocketChatCredentials();
if (!credentials) {
console.warn('[useRocketChatCalls] Could not get credentials, skipping initialization');
logger.warn('[useRocketChatCalls] Could not get credentials, skipping initialization');
return;
}
@ -85,12 +96,20 @@ export function useRocketChatCalls() {
credentials.baseUrl
);
console.log('[useRocketChatCalls] Listener initialization result:', success);
if (success) {
listenerRef.current = listener;
initializedRef.current = true;
// Subscribe to call events
const unsubscribe = listener.onCall((callEvent: CallEvent) => {
console.log('[useRocketChatCalls] 🎉 INCOMING CALL DETECTED!', {
from: callEvent.from.username,
roomId: callEvent.roomId,
roomName: callEvent.roomName,
});
logger.info('[useRocketChatCalls] Incoming call detected', {
from: callEvent.from.username,
roomId: callEvent.roomId,
@ -114,13 +133,21 @@ export function useRocketChatCalls() {
},
},
],
}).then(() => {
console.log('[useRocketChatCalls] ✅ Notification triggered successfully');
}).catch((error) => {
console.error('[useRocketChatCalls] ❌ Error triggering notification', error);
});
});
unsubscribeRef.current = unsubscribe;
console.log('[useRocketChatCalls] ✅ Call listener initialized and subscribed');
logger.debug('[useRocketChatCalls] Call listener initialized');
} else {
console.error('[useRocketChatCalls] ❌ Failed to initialize listener');
}
} catch (error) {
console.error('[useRocketChatCalls] Error initializing listener', error);
logger.error('[useRocketChatCalls] Error initializing listener', { error });
}
}, [getRocketChatCredentials, triggerNotification]);

View File

@ -152,11 +152,13 @@ export class RocketChatCallListener {
*/
private subscribeToCalls(): void {
if (!this.ws || !this.userId) {
console.warn('[ROCKETCHAT_CALL_LISTENER] Cannot subscribe - missing ws or userId');
return;
}
this.subscriptionId = `call-sub-${Date.now()}`;
// Subscribe to webrtc events
const subscribeMessage = {
msg: 'sub',
id: this.subscriptionId,
@ -164,12 +166,30 @@ export class RocketChatCallListener {
params: [`${this.userId}/webrtc`, false],
};
console.log('[ROCKETCHAT_CALL_LISTENER] Subscribing to webrtc events', {
subscriptionId: this.subscriptionId,
userId: this.userId,
message: subscribeMessage,
});
logger.debug('[ROCKETCHAT_CALL_LISTENER] Subscribing to webrtc events', {
subscriptionId: this.subscriptionId,
userId: this.userId,
});
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));
}
/**
@ -198,9 +218,42 @@ export class RocketChatCallListener {
const eventName = message.fields?.eventName;
const args = message.fields?.args || [];
if (eventName?.includes('/webrtc') && args.length > 0) {
this.handleCallEvent(args[0]);
console.log('[ROCKETCHAT_CALL_LISTENER] Received changed message:', {
eventName,
argsCount: args.length,
message: JSON.stringify(message, null, 2),
});
// Check if this is a webrtc event
if (eventName?.includes('/webrtc')) {
console.log('[ROCKETCHAT_CALL_LISTENER] ✅ This is a webrtc event!');
if (args.length > 0) {
this.handleCallEvent(args[0]);
} else {
// Sometimes the event data might be in the message itself
this.handleCallEvent(message.fields);
}
}
// Also check for other possible call-related events
if (eventName?.includes('/call') || eventName?.includes('/video') || eventName?.includes('/audio')) {
console.log('[ROCKETCHAT_CALL_LISTENER] ✅ This might be a call event!');
if (args.length > 0) {
this.handleCallEvent(args[0]);
} else {
this.handleCallEvent(message.fields);
}
}
}
// Log all messages for debugging
if (message.msg && message.collection === 'stream-notify-user') {
console.log('[ROCKETCHAT_CALL_LISTENER] Stream notify user message:', {
msg: message.msg,
collection: message.collection,
eventName: message.fields?.eventName,
hasArgs: !!(message.fields?.args && message.fields.args.length > 0),
});
}
}
@ -208,37 +261,77 @@ 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));
logger.debug('[ROCKETCHAT_CALL_LISTENER] Received call event', { eventData });
try {
// Parse the call event data
// RocketChat webrtc events typically contain:
// - type: 'call', 'ringing', 'answered', 'ended'
// - from: user info
// - roomId: room identifier
const callType = eventData.type || eventData.action;
const from = eventData.from || eventData.caller || {};
const roomId = eventData.roomId || eventData.rid;
// RocketChat webrtc events can have different formats
// Let's check multiple possible structures
let callType: string | null = null;
let from: any = {};
let roomId: string | null = null;
let roomName: string | null = null;
if (!callType || !roomId) {
logger.warn('[ROCKETCHAT_CALL_LISTENER] Invalid call event data', { eventData });
return;
// Try different event structures
if (eventData.type) {
callType = eventData.type;
} else if (eventData.action) {
callType = eventData.action;
} else if (eventData.event) {
callType = eventData.event;
} else if (eventData.callType) {
callType = eventData.callType;
}
// Detect incoming call
if (callType === 'call' || callType === 'ringing' || eventData.action === 'ringing') {
// Extract room info
roomId = eventData.roomId || eventData.rid || eventData.room?._id || eventData.room?._id;
roomName = eventData.roomName || eventData.room?.name || eventData.room?.fname;
// Extract caller info
from = eventData.from || eventData.caller || eventData.user || eventData.sender || {};
// Log all possible fields for debugging
console.log('[ROCKETCHAT_CALL_LISTENER] Parsed event:', {
callType,
roomId,
roomName,
from,
allKeys: Object.keys(eventData),
});
// Check if this is an incoming call event
// RocketChat might send: 'call', 'ringing', 'incoming', 'offer', etc.
const isIncomingCall =
callType === 'call' ||
callType === 'ringing' ||
callType === 'incoming' ||
callType === 'offer' ||
callType === 'video' ||
callType === 'audio' ||
eventData.action === 'ringing' ||
eventData.type === 'video' ||
eventData.type === 'audio' ||
(eventData.type === 'call' && eventData.status === 'ringing');
if (isIncomingCall && roomId) {
const callEvent: CallEvent = {
type: 'call-incoming',
from: {
userId: from._id || from.userId || '',
username: from.username || '',
name: from.name || from.username,
userId: from._id || from.userId || from.id || '',
username: from.username || from.name || 'Unknown',
name: from.name || from.username || 'Unknown',
},
roomId,
roomName: eventData.roomName || from.name,
roomName: roomName || from.name || 'Chat',
timestamp: new Date(),
};
console.log('[ROCKETCHAT_CALL_LISTENER] ✅ Incoming call detected!', {
from: callEvent.from.username,
roomId: callEvent.roomId,
roomName: callEvent.roomName,
});
logger.info('[ROCKETCHAT_CALL_LISTENER] Incoming call detected', {
from: callEvent.from.username,
roomId,
@ -249,11 +342,19 @@ export class RocketChatCallListener {
try {
handler(callEvent);
} catch (error) {
console.error('[ROCKETCHAT_CALL_LISTENER] Error in call handler', error);
logger.error('[ROCKETCHAT_CALL_LISTENER] Error in call handler', { error });
}
});
} else {
console.log('[ROCKETCHAT_CALL_LISTENER] ⚠️ Event not recognized as incoming call', {
callType,
roomId,
isIncomingCall,
});
}
} catch (error) {
console.error('[ROCKETCHAT_CALL_LISTENER] Error handling call event', error);
logger.error('[ROCKETCHAT_CALL_LISTENER] Error handling call event', {
error: error instanceof Error ? error.message : String(error),
eventData,