adjusted getConversationsForUser function to return only group conversations
This commit is contained in:
@@ -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 [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user