fixed insertContact again

This commit is contained in:
slawk0
2025-01-05 00:51:41 +01:00
parent 3439d4b9de
commit 892b394836

View File

@@ -514,58 +514,117 @@ async function insertContactById(senderId, conversation_id, read) {
async function insertContact(initiatorId, receiverId, contactUsername, read) { async function insertContact(initiatorId, receiverId, contactUsername, read) {
try { try {
const findConversationQuery = ` let conversation_id;
SELECT c.conversation_id
FROM Conversations c
JOIN Memberships m1 ON c.conversation_id = m1.conversation_id
JOIN Memberships m2 ON c.conversation_id = m2.conversation_id
WHERE c.conversation_type = 'direct'
AND (
(m1.user_id = $1 AND m2.user_id = $2)
OR
(m1.user_id = $2 AND m2.user_id = $1)
)
AND (
SELECT COUNT(DISTINCT user_id)
FROM Memberships
WHERE conversation_id = c.conversation_id
) = 1
LIMIT 1
`;
const conversationResult = await client.query(findConversationQuery, [
initiatorId,
receiverId,
]);
let conversation_id = conversationResult.rows[0]?.conversation_id;
// Create new conversation if none exists // Check if the user is creating a contact for themselves
if (!conversation_id) { const isSelfContact = initiatorId === receiverId;
const createConversationQuery = `
INSERT INTO Conversations (conversation_type) if (isSelfContact) {
VALUES ('direct') // For self-contacts, check if a conversation already exists for the user alone
RETURNING conversation_id const findSelfConversationQuery = `
SELECT c.conversation_id
FROM Conversations c
JOIN Memberships m ON c.conversation_id = m.conversation_id
WHERE c.conversation_type = 'direct'
AND m.user_id = $1
AND (
SELECT COUNT(*)
FROM Memberships
WHERE conversation_id = c.conversation_id
) = 1
LIMIT 1;
`; `;
const newConversationResult = await client.query(createConversationQuery);
if (!newConversationResult) { const selfConversationResult = await client.query(
console.error("Failed to create new conversation"); findSelfConversationQuery,
return null; [initiatorId],
);
conversation_id = selfConversationResult.rows[0]?.conversation_id;
if (!conversation_id) {
// Create a new conversation for the user alone
const createConversationQuery = `
INSERT INTO Conversations (conversation_type)
VALUES ('direct')
RETURNING conversation_id;
`;
const newConversationResult = await client.query(
createConversationQuery,
);
if (!newConversationResult) {
console.error("Failed to create new conversation for self-contact");
return null;
}
conversation_id = newConversationResult.rows[0].conversation_id;
// Add the user as the only member of the conversation
const createMembershipQuery = `
INSERT INTO Memberships (conversation_id, user_id)
VALUES ($1, $2)
ON CONFLICT (conversation_id, user_id) DO NOTHING;
`;
await client.query(createMembershipQuery, [
conversation_id,
initiatorId,
]);
} }
} else {
conversation_id = newConversationResult.rows[0].conversation_id; // For regular contacts, check if a conversation already exists between the two users
// Create memberships for users const findConversationQuery = `
const createMembershipsQuery = ` SELECT c.conversation_id
INSERT INTO Memberships (conversation_id, user_id) FROM Conversations c
VALUES ($1, $2), ($1, $3) JOIN Memberships m1 ON c.conversation_id = m1.conversation_id
ON CONFLICT (conversation_id, user_id) DO NOTHING JOIN Memberships m2 ON c.conversation_id = m2.conversation_id
WHERE c.conversation_type = 'direct'
AND (
(m1.user_id = $1 AND m2.user_id = $2)
OR
(m1.user_id = $2 AND m2.user_id = $1)
)
LIMIT 1;
`; `;
await client.query(createMembershipsQuery, [
conversation_id, const conversationResult = await client.query(findConversationQuery, [
initiatorId, initiatorId,
receiverId, receiverId,
]); ]);
conversation_id = conversationResult.rows[0]?.conversation_id;
if (!conversation_id) {
// Create a new conversation for the two users
const createConversationQuery = `
INSERT INTO Conversations (conversation_type)
VALUES ('direct')
RETURNING conversation_id;
`;
const newConversationResult = await client.query(
createConversationQuery,
);
if (!newConversationResult) {
console.error("Failed to create new conversation");
return null;
}
conversation_id = newConversationResult.rows[0].conversation_id;
// Add both users as members of the conversation
const createMembershipsQuery = `
INSERT INTO Memberships (conversation_id, user_id)
VALUES ($1, $2), ($1, $3)
ON CONFLICT (conversation_id, user_id) DO NOTHING;
`;
await client.query(createMembershipsQuery, [
conversation_id,
initiatorId,
receiverId,
]);
}
} }
// Insert the contact for the initiator
const contact = await insertContactById(initiatorId, conversation_id, read); const contact = await insertContactById(initiatorId, conversation_id, read);
if (!contact.contact_id) { if (!contact.contact_id) {
console.error("Failed to insert contact by id"); console.error("Failed to insert contact by id");