unread messages handling works
This commit is contained in:
@@ -707,49 +707,56 @@ async function checkMembership(user_id, conversation_id) {
|
||||
async function getContacts(user_id) {
|
||||
const contactsQuery = `
|
||||
WITH DirectContacts AS (
|
||||
SELECT
|
||||
c.contact_id AS id,
|
||||
a.user_id AS user_id,
|
||||
a.username AS username,
|
||||
conv.last_active,
|
||||
c.conversation_id,
|
||||
conv.conversation_type AS type,
|
||||
m.last_read_message_id
|
||||
FROM Contacts c
|
||||
SELECT DISTINCT ON (c.conversation_id)
|
||||
c.contact_id AS id,
|
||||
a.user_id AS user_id,
|
||||
a.username AS username,
|
||||
conv.last_active,
|
||||
c.conversation_id,
|
||||
conv.conversation_type AS type,
|
||||
requesting_member.last_read_message_id
|
||||
FROM Contacts c
|
||||
JOIN Conversations conv ON c.conversation_id = conv.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'
|
||||
AND (a.user_id != $1 OR (SELECT COUNT(*) FROM Memberships WHERE conversation_id = c.conversation_id) = 1)
|
||||
ORDER BY c.conversation_id, conv.last_active DESC
|
||||
),
|
||||
GroupContacts AS (
|
||||
SELECT
|
||||
c.contact_id AS id,
|
||||
NULL::uuid AS user_id,
|
||||
conv.name AS username,
|
||||
conv.last_active,
|
||||
c.conversation_id,
|
||||
conv.conversation_type AS type,
|
||||
m.last_read_message_id -- We need to add Memberships join here too
|
||||
FROM Contacts c
|
||||
JOIN Memberships requesting_member
|
||||
ON requesting_member.conversation_id = conv.conversation_id
|
||||
AND requesting_member.user_id = $1
|
||||
JOIN Memberships other_member
|
||||
ON other_member.conversation_id = conv.conversation_id
|
||||
JOIN Accounts a ON a.user_id = other_member.user_id
|
||||
WHERE c.user_id = $1
|
||||
AND conv.conversation_type = 'direct'
|
||||
AND (
|
||||
a.user_id != $1
|
||||
OR (SELECT COUNT(*) FROM Memberships WHERE conversation_id = c.conversation_id) = 1
|
||||
)
|
||||
),
|
||||
GroupContacts AS (
|
||||
SELECT DISTINCT ON (c.conversation_id)
|
||||
c.contact_id AS id,
|
||||
NULL::uuid AS user_id,
|
||||
conv.name AS username,
|
||||
conv.last_active,
|
||||
c.conversation_id,
|
||||
conv.conversation_type AS type,
|
||||
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 = 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
|
||||
)
|
||||
JOIN Memberships m
|
||||
ON m.conversation_id = conv.conversation_id
|
||||
AND m.user_id = $1
|
||||
WHERE c.user_id = $1
|
||||
AND conv.conversation_type = 'group'
|
||||
)
|
||||
SELECT * FROM DirectContacts
|
||||
UNION ALL
|
||||
SELECT * FROM GroupContacts
|
||||
ORDER BY last_active DESC;
|
||||
ORDER BY last_active DESC NULLS LAST;
|
||||
`;
|
||||
|
||||
try {
|
||||
const contactsResult = await client.query(contactsQuery, [user_id]);
|
||||
|
||||
// Map the results to include the latest message and message_id
|
||||
// Map the results to include the latest message info
|
||||
const contacts = await Promise.all(
|
||||
contactsResult.rows.map(async (row) => {
|
||||
const latestMessage = await getLatestMessage(row.conversation_id);
|
||||
|
||||
Reference in New Issue
Block a user