Skip to content

Commit

Permalink
Refactor socket.on("list") to include a filter parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
loicBRAVO committed Feb 16, 2024
1 parent c42fbc9 commit 26e68a2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 9 additions & 4 deletions backend/src/routes/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,17 @@ io.on("connection", (socket) => {
}
});

socket.on("list", async () => {
socket.on("list", async (filter = "") => {
try {
const rooms = await Room.find({}, "name");
const roomNames = rooms.map((room) => room.name);
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("Rooms listed: ", roomNames);
console.log("Filtered rooms listed: ", roomNames);
} catch (error) {
console.log("Failed to list rooms: ", error);
socket.emit("error", "Failed to list rooms.");
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/Chat/ChatFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ function ChatFooter({ roomId }: { roomId: string }) {
localStorage.setItem("name", newName);
break;
case "list":
socket.emit("list", args.join(" "));
const filter = args.join(" ");
socket.emit("list", filter);
break;
case "create":
if (args.length === 0) {
Expand Down

0 comments on commit 26e68a2

Please sign in to comment.