Skip to content

Commit

Permalink
fix: get/inbox for MariaDB
Browse files Browse the repository at this point in the history
  • Loading branch information
Celesca committed Nov 25, 2024
1 parent 97aef86 commit 81d267c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@ server.exe

# Frontend folder
frontend/node_modules
frontend

bun.lockb
24 changes: 19 additions & 5 deletions src/controllers/messageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,25 @@ export const messageController = new Elysia({ prefix: "/message" })

// Get the latest message from each conversation
const latestMessages = await prisma.$queryRaw`
SELECT DISTINCT ON (LEAST("SenderID", "ReceiverID"), GREATEST("SenderID", "ReceiverID"))
"MessageID", "SenderID", "ReceiverID", "MessageContent", "Timestamp"
FROM "Message"
WHERE "SenderID" = ${userID} OR "ReceiverID" = ${userID}
ORDER BY LEAST("SenderID", "ReceiverID"), GREATEST("SenderID", "ReceiverID"), "Timestamp" DESC
SELECT
m1.*
FROM
Message m1
INNER JOIN (
SELECT
LEAST(SenderID, ReceiverID) AS user1,
GREATEST(SenderID, ReceiverID) AS user2,
MAX(Timestamp) AS maxTimestamp
FROM
Message
WHERE
SenderID = ${userID} OR ReceiverID = ${userID}
GROUP BY
user1, user2
) m2 ON
LEAST(m1.SenderID, m1.ReceiverID) = m2.user1 AND
GREATEST(m1.SenderID, m1.ReceiverID) = m2.user2 AND
m1.Timestamp = m2.maxTimestamp
`;

return latestMessages;
Expand Down

0 comments on commit 81d267c

Please sign in to comment.