adjusted getConversationsForUser function to return only group conversations

This commit is contained in:
slawk0
2024-12-16 23:44:12 +01:00
parent 097102554a
commit 3226cf5a2d
2 changed files with 22 additions and 17 deletions

View File

@@ -803,16 +803,18 @@ async function updateConversationLastActive(conversation_id, userId) {
async function getConversationsForUser(user_id) { async function getConversationsForUser(user_id) {
const query = ` const query = `
SELECT conversation_id SELECT DISTINCT m.conversation_id
FROM Memberships FROM Memberships m
WHERE user_id = $1; JOIN Conversations c ON m.conversation_id = c.conversation_id
WHERE m.user_id = $1
AND c.conversation_type = 'group';
`; `;
try { try {
const result = await client.query(query, [user_id]); const result = await client.query(query, [user_id]);
return result.rows.map((row) => row.conversation_id); return result.rows.map((row) => row.conversation_id);
} catch (e) { } catch (error) {
console.error("Failed to get conversations for user ", e); console.error("Failed to get group conversations for user:", error);
return []; return [];
} }
} }

View File

@@ -73,6 +73,7 @@ function initializeSocket(io) {
conversations = await getConversationsForUser(socket.user_id); conversations = await getConversationsForUser(socket.user_id);
conversations.push(socket.user_id); conversations.push(socket.user_id);
socket.join(conversations); socket.join(conversations);
console.log(`User: ${socket.username} joined to: ${conversations}`);
} catch (e) { } catch (e) {
console.error("(socket) Failed to get user conversations"); console.error("(socket) Failed to get user conversations");
} }
@@ -92,7 +93,7 @@ function initializeSocket(io) {
} else { } else {
return callback({ return callback({
status: "error", 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; insertedMessage;
console.log("(socket) received from chat message", msg); console.log("(socket) received from chat message", msg);
console.log(`io to ${username}, ${recipient}, ${recipient_id}`); io.to(recipient).to(recipient_id).emit("chat message", {
io.to(username).to(recipient).to(recipient_id).emit("chat message", {
sender, sender,
message: content, message: content,
attachment_urls, attachment_urls,
@@ -150,15 +150,18 @@ function initializeSocket(io) {
sent_at, sent_at,
conversation_id, conversation_id,
}); });
console.log("(socket) sent on 'chat message' socket: ", { console.log(
sender, `(socket) sent on 'chat message' socket to: recipient: ${recipient}, recipient_id: ${recipient_id} `,
message, {
attachment_urls, sender,
recipient, message,
sent_at, attachment_urls,
message_id, recipient,
sender_id, sent_at,
}); message_id,
sender_id,
},
);
callback({ callback({
message_id, message_id,