- {/* Messages Container */}
-
+ {/*Chat area */}
+
+
+ {/* Messages Container and Participants Container */}
+
+
+
+
+
-
- {/* Participants Bar */}
- {currentContact?.type == 'group' ? (
-
- ) : null}
+
+ {currentContact && currentContact.username?.length >= 4 ? (
+
+ ) : null}
+
-
- {currentContact && currentContact.username?.length >= 4 ? (
-
- ) : null}
-
+ {/* Right Sidebar - Participants */}
+ {currentContact?.type === 'group' && (
+
+ )}
- >
+
);
}
diff --git a/server/db/db.js b/server/db/db.js
index 4757060..d97821a 100644
--- a/server/db/db.js
+++ b/server/db/db.js
@@ -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 [];
}
}
diff --git a/server/server.js b/server/server.js
index d627f4d..f25ba24 100644
--- a/server/server.js
+++ b/server/server.js
@@ -449,6 +449,12 @@ app.get(
},
);
+app.get(
+ "/api/chat/messages/lastMessage",
+ authorizeUser,
+ async (req, res) => {},
+);
+
initializeSocket(io);
server.listen(PORT, () => {