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) {
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 [];
}
}

View File

@@ -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,7 +150,9 @@ function initializeSocket(io) {
sent_at,
conversation_id,
});
console.log("(socket) sent on 'chat message' socket: ", {
console.log(
`(socket) sent on 'chat message' socket to: recipient: ${recipient}, recipient_id: ${recipient_id} `,
{
sender,
message,
attachment_urls,
@@ -158,7 +160,8 @@ function initializeSocket(io) {
sent_at,
message_id,
sender_id,
});
},
);
callback({
message_id,