From 245d4b02a6978683da5987e4c19b568f72363105 Mon Sep 17 00:00:00 2001 From: Akkiristo Date: Sat, 17 Feb 2024 22:26:09 +0100 Subject: [PATCH] Refactor socket event handlers and improve room filtering in ChatFooter --- backend/src/routes/socket.ts | 29 +++------------------ frontend/src/components/Chat/ChatFooter.tsx | 16 +++++++++--- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/backend/src/routes/socket.ts b/backend/src/routes/socket.ts index 1eca535..382c933 100644 --- a/backend/src/routes/socket.ts +++ b/backend/src/routes/socket.ts @@ -75,23 +75,7 @@ io.on("connection", (socket) => { } }); - socket.on("list", async (filter = "") => { - try { - const myRoomsIds = Object.keys(roomUsers).filter(roomId => roomUsers[roomId].includes(socket.id)); - let rooms = await Room.find({ - '_id': { $in: myRoomsIds }, - 'name': new RegExp(filter, 'i') - }, 'name'); - - const roomNames = rooms.map(room => room.name); - socket.emit("roomsList", roomNames); - console.log("Filtered rooms listed: ", roomNames); - } catch (error) { - console.log("Failed to list rooms: ", error); - socket.emit("error", "Failed to list rooms."); - } - }); - + //affiche les users dans la room socket.on("users", (roomId) => { if (roomUsers[roomId]) { const userIds = roomUsers[roomId]; @@ -104,6 +88,7 @@ io.on("connection", (socket) => { } }); + // Supprime la room socket.on("delete_room", async (roomId) => { try { await Room.findByIdAndDelete(roomId); @@ -138,33 +123,28 @@ io.on("connection", (socket) => { log(`User with ID: ${socket.id} joined room: ${roomId}`); }); + // Rejoindre une Room socket.on("join", async (roomId, username) => { // Vérifie si la salle existe let room = await Room.findById(roomId); if (!room) { - // Si la salle n'existe pas, vous pouvez choisir de la créer ou d'envoyer une erreur socket.emit("join_error", `Room ${roomId} does not exist.`); return; } - // Rejoint la salle socket.join(roomId); roomUsers[roomId] = [...(roomUsers[roomId] ?? []), socket.id]; - - // Informe les autres utilisateurs de la salle socket.to(roomId).emit("receive_message", { text: `${username} has joined the room.`, systemMessage: true, }); - // Confirme la jonction à l'utilisateur socket.emit("room_joined", { roomName: room.name, roomId: roomId }); }); socket.on( "send_private_message", async (roomId, nickname, username, userid, messageData) => { - // Récupérer l'ID du socket associé au surnom const userId = Object.keys(userNames).find( (key) => userNames[key] === nickname ); @@ -182,8 +162,7 @@ io.on("connection", (socket) => { roomName: roomName, roomId: roomId, }); - - // Envoyez le message à la salle + socket.emit("private_message_sent", { text: messageData, name: username, diff --git a/frontend/src/components/Chat/ChatFooter.tsx b/frontend/src/components/Chat/ChatFooter.tsx index 2c8a8fa..0b3a625 100644 --- a/frontend/src/components/Chat/ChatFooter.tsx +++ b/frontend/src/components/Chat/ChatFooter.tsx @@ -119,10 +119,18 @@ function ChatFooter({ roomId }: { roomId: string }) { }); localStorage.setItem("name", newName); break; - case "list": - const filter = args.join(" "); - socket.emit("list", filter); - break; + case "list": + const filter = args.join(" ").toLowerCase(); + const filteredRooms = myRooms.filter(room => + room.title.toLowerCase().includes(filter) + ); + if (filteredRooms.length > 0) { + const roomNames = filteredRooms.map(room => room.title).join(", "); + toast.info(`Accessible Rooms: ${roomNames}`); + } else { + toast.info("No accessible rooms found."); + } + break; case "create": if (args.length === 0) { console.error("No room name specified.");