Skip to content

Commit

Permalink
Refactor socket event handlers and improve room filtering in ChatFooter
Browse files Browse the repository at this point in the history
  • Loading branch information
loicBRAVO committed Feb 17, 2024
1 parent 3469093 commit 245d4b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 29 deletions.
29 changes: 4 additions & 25 deletions backend/src/routes/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -104,6 +88,7 @@ io.on("connection", (socket) => {
}
});

// Supprime la room
socket.on("delete_room", async (roomId) => {
try {
await Room.findByIdAndDelete(roomId);
Expand Down Expand Up @@ -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
);
Expand All @@ -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,
Expand Down
16 changes: 12 additions & 4 deletions frontend/src/components/Chat/ChatFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down

0 comments on commit 245d4b0

Please sign in to comment.