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) {
try {
const findConversationQuery = `
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;
let conversation_id;
// Create new conversation if none exists
if (!conversation_id) {
const createConversationQuery = `
INSERT INTO Conversations (conversation_type)
VALUES ('direct')
RETURNING conversation_id
// Check if the user is creating a contact for themselves
const isSelfContact = initiatorId === receiverId;
if (isSelfContact) {
// For self-contacts, check if a conversation already exists for the user alone
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) {
console.error("Failed to create new conversation");
return null;
const selfConversationResult = await client.query(
findSelfConversationQuery,
[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,
]);
}
conversation_id = newConversationResult.rows[0].conversation_id;
// Create memberships for users
const createMembershipsQuery = `
INSERT INTO Memberships (conversation_id, user_id)
VALUES ($1, $2), ($1, $3)
ON CONFLICT (conversation_id, user_id) DO NOTHING
} else {
// For regular contacts, check if a conversation already exists between the two users
const findConversationQuery = `
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)
)
LIMIT 1;
`;
await client.query(createMembershipsQuery, [
conversation_id,
const conversationResult = await client.query(findConversationQuery, [
initiatorId,
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);
if (!contact.contact_id) {
console.error("Failed to insert contact by id");