added displaying last message under contact, UI improvement

This commit is contained in:
slawk0
2025-01-01 22:09:19 +01:00
parent 8558b9ac90
commit 867e465a23
13 changed files with 194 additions and 134 deletions

View File

@@ -586,6 +586,16 @@ async function insertContact(userUsername, receiverUsername, read) {
async function getContacts(user_id) {
const contactsQuery = `
WITH LastMessages AS (
SELECT DISTINCT ON (m.conversation_id)
m.conversation_id,
m.content as last_message,
m.sent_at as last_message_time,
a.username as last_message_sender
FROM Messages m
JOIN Accounts a ON m.user_id = a.user_id
ORDER BY m.conversation_id, m.sent_at DESC
)
SELECT DISTINCT ON (c.conversation_id)
c.contact_id AS id,
CASE
@@ -599,11 +609,15 @@ async function getContacts(user_id) {
conv.last_active,
c.conversation_id,
conv.conversation_type AS type,
c.read
c.read,
lm.last_message,
lm.last_message_time,
lm.last_message_sender
FROM Contacts c
JOIN Conversations conv ON c.conversation_id = conv.conversation_id
JOIN Memberships m ON m.conversation_id = c.conversation_id
JOIN Accounts a2 ON a2.user_id = m.user_id
LEFT JOIN LastMessages lm ON c.conversation_id = lm.conversation_id
WHERE c.user_id = $1
AND (
conv.conversation_type = 'direct'
@@ -614,7 +628,10 @@ async function getContacts(user_id) {
`;
try {
// Execute the query with the user_id parameter
const contactsResult = await client.query(contactsQuery, [user_id]);
console.error(contactsResult.rows);
// Map the results to a more friendly format
const contacts = contactsResult.rows.map((row) => ({
id: row.id,
user_id: row.user_id,
@@ -623,10 +640,14 @@ async function getContacts(user_id) {
conversation_id: row.conversation_id,
type: row.type,
read: row.read,
last_message: row.last_message,
last_message_time: row.last_message_time,
last_message_sender: row.last_message_sender,
}));
return contacts;
} catch (e) {
console.error("Failed to get contacts:", e);
} catch (error) {
console.error("Failed to get contacts:", error);
return [];
}
}

View File

@@ -449,6 +449,12 @@ app.get(
},
);
app.get(
"/api/chat/messages/lastMessage",
authorizeUser,
async (req, res) => {},
);
initializeSocket(io);
server.listen(PORT, () => {