From 3226cf5a2d909c814a5f3d6812f8702481f578d4 Mon Sep 17 00:00:00 2001 From: slawk0 Date: Mon, 16 Dec 2024 23:44:12 +0100 Subject: [PATCH] adjusted getConversationsForUser function to return only group conversations --- server/db/db.js | 12 +++++++----- server/socket/socket.js | 27 +++++++++++++++------------ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/server/db/db.js b/server/db/db.js index a709235..5ba0a16 100644 --- a/server/db/db.js +++ b/server/db/db.js @@ -803,16 +803,18 @@ async function updateConversationLastActive(conversation_id, userId) { async function getConversationsForUser(user_id) { const query = ` - SELECT conversation_id - FROM Memberships - WHERE user_id = $1; + SELECT DISTINCT m.conversation_id + FROM Memberships m + JOIN Conversations c ON m.conversation_id = c.conversation_id + WHERE m.user_id = $1 + AND c.conversation_type = 'group'; `; try { const result = await client.query(query, [user_id]); return result.rows.map((row) => row.conversation_id); - } catch (e) { - console.error("Failed to get conversations for user ", e); + } catch (error) { + console.error("Failed to get group conversations for user:", error); return []; } } diff --git a/server/socket/socket.js b/server/socket/socket.js index 2539b14..4f70c58 100644 --- a/server/socket/socket.js +++ b/server/socket/socket.js @@ -73,6 +73,7 @@ function initializeSocket(io) { conversations = await getConversationsForUser(socket.user_id); conversations.push(socket.user_id); socket.join(conversations); + console.log(`User: ${socket.username} joined to: ${conversations}`); } catch (e) { console.error("(socket) Failed to get user conversations"); } @@ -92,7 +93,7 @@ function initializeSocket(io) { } else { return callback({ status: "error", - message: "You are not member of this conversation", + message: "You are not member of this group", }); } }); @@ -139,8 +140,7 @@ function initializeSocket(io) { insertedMessage; console.log("(socket) received from chat message", msg); - console.log(`io to ${username}, ${recipient}, ${recipient_id}`); - io.to(username).to(recipient).to(recipient_id).emit("chat message", { + io.to(recipient).to(recipient_id).emit("chat message", { sender, message: content, attachment_urls, @@ -150,15 +150,18 @@ function initializeSocket(io) { sent_at, conversation_id, }); - console.log("(socket) sent on 'chat message' socket: ", { - sender, - message, - attachment_urls, - recipient, - sent_at, - message_id, - sender_id, - }); + console.log( + `(socket) sent on 'chat message' socket to: recipient: ${recipient}, recipient_id: ${recipient_id} `, + { + sender, + message, + attachment_urls, + recipient, + sent_at, + message_id, + sender_id, + }, + ); callback({ message_id,