Refactor code structure for improved readability and maintainability; optimize performance across multiple modules.
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s
This commit is contained in:
18
backend/utils/socket.js
Normal file → Executable file
18
backend/utils/socket.js
Normal file → Executable file
@@ -1,5 +1,6 @@
|
||||
import { Server } from 'socket.io';
|
||||
import BaseService from '../services/BaseService.js';
|
||||
import { dispatchRealtimeEvent } from '../services/pushNotificationService.js';
|
||||
|
||||
const baseService = new BaseService();
|
||||
|
||||
@@ -46,23 +47,22 @@ export function getUserSockets() {
|
||||
|
||||
export async function notifyUser(recipientHashedUserId, event, data) {
|
||||
const io = getIo();
|
||||
if (!io) {
|
||||
if (DEBUG_SOCKETS) console.warn('[socket.io] notifyUser übersprungen: Socket.io nicht initialisiert');
|
||||
return;
|
||||
}
|
||||
const userSockets = getUserSockets();
|
||||
try {
|
||||
const recipientUser = await baseService.getUserByHashedId(recipientHashedUserId);
|
||||
if (recipientUser) {
|
||||
const socketId = userSockets[recipientUser.hashedId];
|
||||
if (socketId) {
|
||||
const recipientUser = await baseService.getUserByHashedId(recipientHashedUserId);
|
||||
if (recipientUser) {
|
||||
const socketId = userSockets[recipientUser.hashedId];
|
||||
if (socketId && io) {
|
||||
if (DEBUG_SOCKETS) console.log('[socket.io] notifyUser → emit:', event, 'to socket', socketId, 'user', recipientUser.hashedId, 'data:', JSON.stringify(data));
|
||||
setTimeout(() => {
|
||||
io.to(socketId).emit(event, data);
|
||||
}, 250);
|
||||
} else {
|
||||
} else if (DEBUG_SOCKETS) {
|
||||
if (DEBUG_SOCKETS) console.warn('[socket.io] notifyUser: no socket registered for user', recipientUser.hashedId);
|
||||
}
|
||||
void dispatchRealtimeEvent(recipientUser.hashedId, event, data).catch((error) => {
|
||||
console.error('[push] Realtime-Ereignis konnte nicht zugestellt werden:', error.message);
|
||||
});
|
||||
} else {
|
||||
if (DEBUG_SOCKETS) console.log(`Benutzer mit gehashter ID ${recipientHashedUserId} nicht gefunden.`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user