diff --git a/client/src/components/ui/NotificationDropdown.tsx b/client/src/components/ui/NotificationDropdown.tsx index 1cc1df7..0d96584 100644 --- a/client/src/components/ui/NotificationDropdown.tsx +++ b/client/src/components/ui/NotificationDropdown.tsx @@ -2,7 +2,6 @@ import React, { useState, useEffect, useRef } from "react"; import { Bell, X, - Check, CheckCheck, MessageSquare, Award, @@ -16,7 +15,7 @@ import { markAllNotificationsAsRead, } from "../../utils/api"; import { Notification } from "../../types/discussions"; -import useSocket from "../../hooks/useSocket"; +import { useSocket } from "../../contexts/SocketContext"; const NotificationDropdown: React.FC = () => { const [notifications, setNotifications] = useState([]); @@ -37,7 +36,9 @@ const NotificationDropdown: React.FC = () => { // Listen for real-time notifications useEffect(() => { - if (!socket) return; + if (!socket) { + return; + } socket.on("new_notification", (notification: Notification) => { setNotifications((prev) => [notification, ...prev]); diff --git a/server/package-lock.json b/server/package-lock.json index a9c4faf..0945f5e 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -2884,7 +2884,6 @@ "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", - "dev": true, "hasInstallScript": true, "optional": true, "os": [ diff --git a/server/routes/discussions.js b/server/routes/discussions.js index 8a95a3c..0b61142 100644 --- a/server/routes/discussions.js +++ b/server/routes/discussions.js @@ -342,29 +342,31 @@ router.post("/:id/answers", authenticateToken, async (req, res) => { // Handle mentions const mentions = extractMentions(content); + for (const mentionedUsername of mentions) { const mentionedUser = await prisma.user.findUnique({ where: { username: mentionedUsername }, }); - if (mentionedUser && mentionedUser.id !== req.user.id) { - await createNotification( - mentionedUser.id, - "mention", - "You were mentioned", - `${req.user.username} mentioned you in an answer`, - answer.id, - "answer", - req.user.id, - req.user.username, - req.io - ); + if (mentionedUser) { + if (mentionedUser.id !== req.user.id) { + await createNotification( + mentionedUser.id, + "mention", + "You were mentioned", + `${req.user.username} mentioned you in an answer`, + answer.id, + "answer", + req.user.id, + req.user.username, + req.io + ); + } } } // Emit real-time event if (req.io) { - console.log("๐Ÿš€ Emitting new answer event:", id); emitNewAnswer(req.io, id, { ...answer, author_username: req.user.username, @@ -396,7 +398,6 @@ router.post( "/answers/:answerId/replies", authenticateToken, async (req, res) => { - console.log("๐Ÿ”ฅ Reply route hit! AnswerId:", req.params.answerId); try { const { answerId } = req.params; const { content, images } = req.body; @@ -443,33 +444,31 @@ router.post( // Handle mentions const mentions = extractMentions(content); + for (const mentionedUsername of mentions) { const mentionedUser = await prisma.user.findUnique({ where: { username: mentionedUsername }, }); - if (mentionedUser && mentionedUser.id !== req.user.id) { - await createNotification( - mentionedUser.id, - "mention", - "You were mentioned", - `${req.user.username} mentioned you in a reply`, - reply.id, - "reply", - req.user.id, - req.user.username, - req.io - ); + if (mentionedUser) { + if (mentionedUser.id !== req.user.id) { + await createNotification( + mentionedUser.id, + "mention", + "You were mentioned", + `${req.user.username} mentioned you in a reply`, + reply.id, + "reply", + req.user.id, + req.user.username, + req.io + ); + } } } // Emit real-time event if (req.io) { - console.log( - "๐Ÿš€ Emitting new reply event:", - answer.discussionId, - answerId - ); emitNewReply(req.io, answer.discussionId, answerId, { ...reply, author_username: req.user.username, diff --git a/server/socket/socketHandlers.js b/server/socket/socketHandlers.js index 2a807e5..2f003a1 100644 --- a/server/socket/socketHandlers.js +++ b/server/socket/socketHandlers.js @@ -24,7 +24,6 @@ const authenticateSocket = async (socket, next) => { } if (!token) { - console.log("โŒ Socket auth: No token found in any location"); return next(new Error("Authentication error")); } @@ -39,11 +38,9 @@ const authenticateSocket = async (socket, next) => { }); if (!user) { - console.log("โŒ Socket auth: User not found for token"); return next(new Error("User not found")); } - console.log("โœ… Socket authenticated:", user.username); socket.user = user; next(); } catch (error) { @@ -57,42 +54,19 @@ export const setupSocketHandlers = (io) => { io.use(authenticateSocket); io.on("connection", (socket) => { - console.log( - `โœ… User ${socket.user.username} connected with socket ID: ${socket.id}` - ); - // Join user to their personal room for notifications - socket.join(`user_${socket.user.id}`); + const userRoom = `user_${socket.user.id}`; + socket.join(userRoom); // Join discussion room socket.on("join_discussion", (discussionId) => { const roomName = `discussion_${discussionId}`; socket.join(roomName); - console.log( - `โœ… User ${socket.user.username} joined discussion room: ${roomName}` - ); - - // Check room membership immediately after joining - setTimeout(() => { - const room = io.sockets.adapter.rooms.get(roomName); - const clientCount = room ? room.size : 0; - console.log(`๐Ÿ‘ฅ Clients in room ${roomName} after join:`, clientCount); - - // List all rooms this socket is in - const socketRooms = Array.from(socket.rooms); - console.log( - `๐Ÿ  Socket ${socket.user.username} is in rooms:`, - socketRooms - ); - }, 100); }); // Leave discussion room socket.on("leave_discussion", (discussionId) => { socket.leave(`discussion_${discussionId}`); - console.log( - `User ${socket.user.username} left discussion ${discussionId}` - ); }); // Handle typing indicators @@ -125,7 +99,6 @@ export const setupSocketHandlers = (io) => { // Test event handler socket.on("test_event", (data) => { - console.log(`๐Ÿงช Test event from ${socket.user.username}:`, data); socket.emit("test_event", { message: `Hello back from server, ${socket.user.username}!`, timestamp: new Date().toISOString(), @@ -134,14 +107,7 @@ export const setupSocketHandlers = (io) => { // Handle disconnect socket.on("disconnect", (reason) => { - console.log( - `โŒ User ${socket.user.username} disconnected. Reason:`, - reason - ); - console.log( - `๐Ÿ  Socket ${socket.id} was in rooms:`, - Array.from(socket.rooms) - ); + // Silent disconnect }); }); }; @@ -149,28 +115,11 @@ export const setupSocketHandlers = (io) => { // Helper functions to emit events from routes export const emitNewAnswer = (io, discussionId, answer) => { const roomName = `discussion_${discussionId}`; - console.log("๐Ÿ“ก Emitting new_answer to room:", roomName); - - // Check how many clients are in the room - const room = io.sockets.adapter.rooms.get(roomName); - const clientCount = room ? room.size : 0; - console.log(`๐Ÿ‘ฅ Clients in room ${roomName}:`, clientCount); - io.to(roomName).emit("new_answer", answer); }; export const emitNewReply = (io, discussionId, answerId, reply) => { const roomName = `discussion_${discussionId}`; - console.log("๐Ÿ“ก Emitting new_reply to room:", roomName, { - answerId, - reply, - }); - - // Check how many clients are in the room - const room = io.sockets.adapter.rooms.get(roomName); - const clientCount = room ? room.size : 0; - console.log(`๐Ÿ‘ฅ Clients in room ${roomName}:`, clientCount); - io.to(roomName).emit("new_reply", { answerId, reply, @@ -198,5 +147,6 @@ export const emitVoteUpdate = ( }; export const emitNotification = (io, userId, notification) => { - io.to(`user_${userId}`).emit("new_notification", notification); + const userRoom = `user_${userId}`; + io.to(userRoom).emit("new_notification", notification); };