nie wiem co, cos zepsulem z nieprzeczytanymi wiadomosci i nie wiem jak naprawic

This commit is contained in:
slawek
2025-01-11 00:02:06 +01:00
parent dc1e5c1748
commit e9763084f4
9 changed files with 98 additions and 31 deletions

View File

@@ -86,6 +86,7 @@ async function createTables() {
conversation_id UUID NOT NULL REFERENCES Conversations(conversation_id) ON DELETE CASCADE,
user_id UUID REFERENCES Accounts(user_id) ON DELETE CASCADE,
joined_at TIMESTAMPTZ DEFAULT NOW(),
last_read_message_id INT REFERENCES Messages(message_id) ON DELETE SET NULL,
PRIMARY KEY (conversation_id, user_id)
);
CREATE INDEX IF NOT EXISTS idx_memberships_conversation_id ON Memberships (conversation_id);
@@ -100,7 +101,6 @@ async function createTables() {
contact_id SERIAL PRIMARY KEY,
user_id UUID REFERENCES Accounts(user_id) ON DELETE CASCADE,
conversation_id UUID NOT NULL REFERENCES Conversations(conversation_id) ON DELETE CASCADE,
read BOOLEAN NOT NULL DEFAULT FALSE,
last_active TIMESTAMPTZ DEFAULT NOW(),
CONSTRAINT unique_contact UNIQUE (user_id, conversation_id)
);
@@ -237,6 +237,8 @@ async function insertMessage(
attachmentUrls,
]);
updateConversationLastActive(conversation_id, senderId);
updateContactStatus(senderId, conversation_id, result.rows[0].message_id);
return result.rows[0];
} catch (e) {
console.error("Failed to insert message ", e);
@@ -473,10 +475,10 @@ async function changePassword(username, newPasswordHash) {
}
}
async function insertContactById(senderId, conversation_id, read) {
async function insertContactById(senderId, conversation_id) {
// First check if contact already exists
const checkQuery = `
SELECT contact_id, conversation_id, user_id, read, last_active
SELECT contact_id, conversation_id, user_id, last_active
FROM Contacts
WHERE user_id = $1 AND conversation_id = $2
`;
@@ -494,16 +496,12 @@ async function insertContactById(senderId, conversation_id, read) {
// If contact doesn't exist, create new one
const insertQuery = `
INSERT INTO Contacts (user_id, conversation_id, read)
VALUES($1, $2, $3)
RETURNING contact_id, conversation_id, user_id, read, last_active
INSERT INTO Contacts (user_id, conversation_id)
VALUES($1, $2)
RETURNING contact_id, conversation_id, user_id, last_active
`;
const result = await client.query(insertQuery, [
senderId,
conversation_id,
read,
]);
const result = await client.query(insertQuery, [senderId, conversation_id]);
console.log("Insert contact by id:", senderId, conversation_id);
return result.rows[0] || null;
} catch (error) {
@@ -512,7 +510,7 @@ async function insertContactById(senderId, conversation_id, read) {
}
}
async function insertContact(initiatorId, receiverId, contactUsername, read) {
async function insertContact(initiatorId, receiverId, contactUsername) {
try {
let conversation_id;
@@ -625,7 +623,7 @@ async function insertContact(initiatorId, receiverId, contactUsername, read) {
}
// Insert the contact for the initiator
const contact = await insertContactById(initiatorId, conversation_id, read);
const contact = await insertContactById(initiatorId, conversation_id);
if (!contact.contact_id) {
console.error("Failed to insert contact by id");
return null;
@@ -642,7 +640,6 @@ async function insertContact(initiatorId, receiverId, contactUsername, read) {
last_active: contact.last_active,
conversation_id: contact.conversation_id,
type: "direct",
read: contact.read,
last_message_id: latestMessage.last_message_id,
last_message: latestMessage.last_message,
last_message_time: latestMessage.last_message_time,
@@ -717,10 +714,10 @@ async function getContacts(user_id) {
conv.last_active,
c.conversation_id,
conv.conversation_type AS type,
c.read
m.last_read_message_id
FROM Contacts c
JOIN Conversations conv ON c.conversation_id = conv.conversation_id
JOIN Memberships m ON m.conversation_id = c.conversation_id
JOIN Memberships m ON m.conversation_id = conv.conversation_id
JOIN Accounts a ON a.user_id = m.user_id
WHERE c.user_id = $1
AND conv.conversation_type = 'direct'
@@ -735,9 +732,10 @@ async function getContacts(user_id) {
conv.last_active,
c.conversation_id,
conv.conversation_type AS type,
c.read
m.last_read_message_id -- We need to add Memberships join here too
FROM Contacts c
JOIN Conversations conv ON c.conversation_id = conv.conversation_id
JOIN Memberships m ON m.conversation_id = conv.conversation_id -- Added this JOIN
WHERE c.user_id = $1
AND conv.conversation_type = 'group'
ORDER BY c.conversation_id, conv.last_active DESC
@@ -762,15 +760,14 @@ async function getContacts(user_id) {
last_active: row.last_active,
conversation_id: row.conversation_id,
type: row.type,
read: row.read,
last_message_id: latestMessage.message_id,
last_read_message_id: row.last_read_message_id,
last_message_id: latestMessage.last_message_id,
last_message: latestMessage.last_message,
last_message_time: latestMessage.last_message_time,
last_message_sender: latestMessage.last_message_sender,
};
}),
);
return contacts;
} catch (error) {
console.error("Failed to get contacts:", error);
@@ -864,23 +861,48 @@ async function deleteContact(user_id, conversation_id) {
}
}
async function updateContactStatus(user_id, conversation_id, read) {
async function updateContactStatus(
user_id,
conversation_id,
last_read_message_id,
) {
const query = `
UPDATE Contacts SET read = $1
UPDATE Memberships
SET last_read_message_id = $1
WHERE user_id = $2
AND conversation_id = $3;
`;
try {
await client.query(query, [read, user_id, conversation_id]);
// await updateContactLastActive(user_id, conversation_id);
await client.query(query, [last_read_message_id, user_id, conversation_id]);
console.log(
`Successfully updated contact status, user_id: ${user_id}, conversation_id: ${conversation_id}, read: ${read}: `,
`Successfully updated contact status, user_id: ${user_id}, conversation_id: ${conversation_id}, last_read_message_id: ${last_read_message_id}`,
);
} catch (e) {
console.error("Failed to update contact status ", e);
}
}
async function getLastReadMessageId(user_id, conversation_id) {
const query = `
SELECT last_read_message_id
FROM Memberships
WHERE user_id = $1
AND conversation_id = $2;
`;
try {
const result = await client.query(query, [user_id, conversation_id]);
if (result.rows.length > 0) {
return result.rows[0].last_read_message_id;
} else {
console.log("No read message found for user in conversation");
return null;
}
} catch (e) {
console.error("Failed to get last read message ID ", e);
return null;
}
}
async function updateContactLastActive(user_id, contact_id) {
const query = `
UPDATE Contacts

View File

@@ -247,7 +247,7 @@ app.put(
}
const read = req.body.status;
await updateContactStatus(req.user.user_id, conversation_id, read);
//await updateContactStatus(req.user.user_id, conversation_id, read);
return res
.status(200)

View File

@@ -8,6 +8,7 @@ const {
isAdmin,
addAdministrator,
removeAdministrator,
updateContactStatus,
} = require("../db/db");
const { isValidUsername } = require("../utils/filter");
const { verifyJwtToken } = require("../auth/jwt");
@@ -342,6 +343,15 @@ function initializeSocket(io) {
});
});
socket.on("message read", async (msg) => {
const { conversation_id, message_id } = msg;
if (!conversation_id || !message_id) {
return;
}
console.error("MESSGE READ: ", conversation_id, message_id);
await updateContactStatus(socket.user_id, conversation_id, message_id);
});
socket.on("disconnect", (reason) => {
console.log("(socket)", socket.id, " disconnected due to: ", reason);
});