getContacts function now can return contact with owner id
This commit is contained in:
@@ -498,13 +498,13 @@ async function getContacts(user_id) {
|
||||
const contactsQuery = `
|
||||
SELECT DISTINCT ON (c.conversation_id)
|
||||
c.contact_id AS id,
|
||||
CASE
|
||||
CASE
|
||||
WHEN conv.conversation_type = 'group' THEN NULL
|
||||
ELSE a2.user_id
|
||||
ELSE a2.user_id
|
||||
END AS user_id,
|
||||
CASE
|
||||
CASE
|
||||
WHEN conv.conversation_type = 'group' THEN conv.name
|
||||
ELSE a2.username
|
||||
ELSE a2.username
|
||||
END AS username,
|
||||
conv.last_active,
|
||||
c.conversation_id,
|
||||
@@ -516,9 +516,9 @@ async function getContacts(user_id) {
|
||||
JOIN Accounts a2 ON a2.user_id = m.user_id
|
||||
WHERE c.user_id = $1
|
||||
AND (
|
||||
(conv.conversation_type = 'direct' AND a2.user_id != $1)
|
||||
conv.conversation_type = 'direct'
|
||||
OR
|
||||
(conv.conversation_type = 'group')
|
||||
conv.conversation_type = 'group'
|
||||
)
|
||||
ORDER BY c.conversation_id, conv.last_active DESC;
|
||||
`;
|
||||
@@ -534,7 +534,7 @@ async function getContacts(user_id) {
|
||||
type: row.type,
|
||||
read: row.read,
|
||||
}));
|
||||
console.log("Get contacts: ", contacts);
|
||||
console.log("Get contacts for: ", user_id, contacts);
|
||||
return contacts;
|
||||
} catch (e) {
|
||||
console.error("Failed to get contacts:", e);
|
||||
@@ -564,15 +564,31 @@ async function deleteContact(user_id, contact_id, conversation_id) {
|
||||
const conversationType = conversationTypeResult.rows[0].conversation_type;
|
||||
|
||||
if (conversationType === "group") {
|
||||
// Remove the user from the group in the Memberships table
|
||||
console.log(" Remove the user from the group in the Memberships table");
|
||||
const removeUserFromGroupQuery = `
|
||||
DELETE FROM Memberships WHERE conversation_id = $1 AND user_id = $2;
|
||||
`;
|
||||
const removeResult = await client.query(removeUserFromGroupQuery, [
|
||||
|
||||
const removeConversationContactQuery = `
|
||||
DELETE FROM Contacts WHERE conversation_id = $1 AND user_id = $2
|
||||
`;
|
||||
|
||||
const removeContact = await client.query(removeConversationContactQuery, [
|
||||
conversation_id,
|
||||
user_id,
|
||||
]);
|
||||
if (removeResult.rowCount === 0) {
|
||||
if (removeContact.rowCount === 0) {
|
||||
console.log("No matching contact found with: ", {
|
||||
conversation_id,
|
||||
user_id,
|
||||
});
|
||||
}
|
||||
|
||||
const removeConversationResult = await client.query(
|
||||
removeUserFromGroupQuery,
|
||||
[conversation_id, user_id],
|
||||
);
|
||||
if (removeConversationResult.rowCount === 0) {
|
||||
console.log("No matching membership found with:", {
|
||||
conversation_id,
|
||||
user_id,
|
||||
|
||||
Reference in New Issue
Block a user