contact deletion is now handled by socket, added left group socket to delete user from participants bar

This commit is contained in:
slawk0
2024-12-11 22:45:32 +01:00
parent 8a1a487fed
commit 374e64044c
6 changed files with 71 additions and 25 deletions

View File

@@ -534,7 +534,6 @@ async function getContacts(user_id) {
type: row.type,
read: row.read,
}));
console.log("Get contacts for: ", user_id, contacts);
return contacts;
} catch (e) {
console.error("Failed to get contacts:", e);
@@ -542,7 +541,7 @@ async function getContacts(user_id) {
}
}
async function deleteContact(user_id, contact_id, conversation_id) {
async function deleteContact(user_id, conversation_id) {
// Check if the conversation is a group
const checkConversationTypeQuery = `
SELECT conversation_type FROM Conversations WHERE conversation_id = $1;
@@ -558,7 +557,10 @@ async function deleteContact(user_id, contact_id, conversation_id) {
"No conversation found with conversation_id: ",
conversation_id,
);
return;
return {
message: "No conversation found with conversation_id: ",
conversation_id,
};
}
const conversationType = conversationTypeResult.rows[0].conversation_type;
@@ -582,6 +584,9 @@ async function deleteContact(user_id, contact_id, conversation_id) {
conversation_id,
user_id,
});
return {
message: `No matching contact found with conversation id: ${conversation_id}, user id: ${user_id}`,
};
}
const removeConversationResult = await client.query(
@@ -593,17 +598,26 @@ async function deleteContact(user_id, contact_id, conversation_id) {
conversation_id,
user_id,
});
return {
message: `No matching membership found with conversation id: ${conversation_id} , user id: ${user_id}`,
};
} else {
console.log("Successfully removed user from group for: ", user_id);
}
} else {
// For direct conversations, proceed with deleting the contact
const query = `
DELETE FROM Contacts WHERE (user_id = $1 AND contact_id = $2);
DELETE FROM Contacts WHERE (user_id = $1 AND conversation_id = $2);
`;
const result = await client.query(query, [user_id, contact_id]);
const result = await client.query(query, [user_id, conversation_id]);
if (result.rowCount === 0) {
console.log("No matching contact found with:", { user_id, contact_id });
console.log("No matching contact found with:", {
user_id,
conversation_id,
});
return {
message: `No matching contact found with user id: ${user_id}, conversation id: ${conversation_id}`,
};
} else {
console.log("Successfully deleted contact for: ", user_id);
}

View File

@@ -181,6 +181,23 @@ function initializeSocket(io) {
}
});
socket.on("delete contact", async (conversation_id, callback) => {
console.log(
"(socket) delete contact, conversation_id: ",
conversation_id,
);
const user_id = socket.user_id;
if (!conversation_id) {
callback({ status: "error", message: "No conversation id provided" });
}
const result = await deleteContact(user_id, conversation_id);
if (result?.message) {
callback({ status: "error", message: result.message });
}
callback({ status: "ok", message: "Successfully deleted contact" });
io.to(conversation_id).emit("left group", { conversation_id, user_id });
});
socket.on("disconnect", (reason) => {
console.log("(socket)", socket.id, " disconnected due to: ", reason);
});