From f66c68b4aaf42afe3be54aa738e9268ae28495ba Mon Sep 17 00:00:00 2001 From: Luis Date: Sat, 1 Feb 2025 18:03:15 -0300 Subject: [PATCH] fix: change some sql queries and add 'viewed' field to messages --- rest-server/migrations/13_messages.sql | 3 +- rest-server/src/api/v1/users/me/chats/get.rs | 15 ++++++---- .../src/api/v1/users/me/chats/messages/get.rs | 12 +++++++- rest-server/src/models/chats.rs | 1 + rest-server/tests/fixtures/12_chats.sql | 10 +++++++ rest-server/tests/fixtures/13_messages.sql | 28 +++++++++++++++++++ 6 files changed, 61 insertions(+), 8 deletions(-) diff --git a/rest-server/migrations/13_messages.sql b/rest-server/migrations/13_messages.sql index f73343f..ea50905 100644 --- a/rest-server/migrations/13_messages.sql +++ b/rest-server/migrations/13_messages.sql @@ -5,7 +5,8 @@ CREATE TABLE IF NOT EXISTS "messages" content TEXT NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, edited BOOLEAN DEFAULT FALSE, - chat BIGINT REFERENCES "chats" (id) + chat BIGINT REFERENCES "chats" (id), + viewed BOOLEAN DEFAULT FALSE ); CREATE INDEX chating ON "messages" (chat); diff --git a/rest-server/src/api/v1/users/me/chats/get.rs b/rest-server/src/api/v1/users/me/chats/get.rs index c589b79..345f4ed 100644 --- a/rest-server/src/api/v1/users/me/chats/get.rs +++ b/rest-server/src/api/v1/users/me/chats/get.rs @@ -12,13 +12,16 @@ pub async fn chats( let user = auth_session.user.unwrap(); let chats = sqlx::query_as::<_, ChatPreview>( r#" - SELECT u.id AS user_id, u.avatar as user_avatar, u.name as user_name, - m.content AS last_message, c.id + SELECT DISTINCT ON (u.id) + u.id AS user_id, + u.avatar AS user_avatar, + u.name AS user_name, + m.content AS last_message, + c.id FROM chats c - LEFT JOIN users u ON u.id = CASE WHEN c.user1 != $1 THEN c.user1 ELSE c.user2 END - JOIN messages m ON m.chat = c.id - WHERE (c.user1 = 1 OR c.user2 = 1) AND c.hidden = false - ORDER BY m.created_at DESC + LEFT JOIN users u ON (u.id = c.user1 OR u.id = c.user2) AND u.id <> $1 + LEFT JOIN messages m ON m.chat = c.id + ORDER BY u.id, m.created_at DESC; "# ) .bind(user.id) diff --git a/rest-server/src/api/v1/users/me/chats/messages/get.rs b/rest-server/src/api/v1/users/me/chats/messages/get.rs index 847b7f5..f90d42d 100644 --- a/rest-server/src/api/v1/users/me/chats/messages/get.rs +++ b/rest-server/src/api/v1/users/me/chats/messages/get.rs @@ -16,7 +16,7 @@ pub async fn messages( let messages = sqlx::query_as::<_, Message>( r#" SELECT - (m.author_id = $1) AS is_author, m.content, m.created_at + (m.author_id = $1) AS is_author, m.content, m.created_at, m.viewed FROM messages m INNER JOIN chats c ON m.chat = c.id WHERE m.chat = $2 AND (c.user1 = $1 OR c.user2 = $1) @@ -30,6 +30,16 @@ pub async fn messages( .bind((query.page - 1) * query.per_page) .fetch_all(&state.pool) .await?; + + sqlx::query(r#" + UPDATE messages + SET viewed = TRUE + WHERE chat = $2 AND author_id != $1 AND viewed = FALSE; + "#) + .bind(user.id) + .bind(chat_id) + .execute(&state.pool) + .await?; Ok(Json(messages)) } \ No newline at end of file diff --git a/rest-server/src/models/chats.rs b/rest-server/src/models/chats.rs index 6652466..b42ae25 100644 --- a/rest-server/src/models/chats.rs +++ b/rest-server/src/models/chats.rs @@ -18,4 +18,5 @@ pub struct Message { content: String, #[serde(serialize_with = "serialize_timestamp")] created_at: NaiveDateTime, + viewed: bool, } \ No newline at end of file diff --git a/rest-server/tests/fixtures/12_chats.sql b/rest-server/tests/fixtures/12_chats.sql index e69de29..bfde79a 100644 --- a/rest-server/tests/fixtures/12_chats.sql +++ b/rest-server/tests/fixtures/12_chats.sql @@ -0,0 +1,10 @@ +INSERT INTO chats (user1, user2) +VALUES (2, 1), + (2, 6), + (2, 7), + (2, 8), + (2, 11), + (2, 13), + (2, 16), + (2, 19), + (2, 20); \ No newline at end of file diff --git a/rest-server/tests/fixtures/13_messages.sql b/rest-server/tests/fixtures/13_messages.sql index e69de29..967fb85 100644 --- a/rest-server/tests/fixtures/13_messages.sql +++ b/rest-server/tests/fixtures/13_messages.sql @@ -0,0 +1,28 @@ +INSERT INTO messages (author_id, content, created_at, chat, viewed) +VALUES + (2, 'Olá, como você está?', NOW() - INTERVAL '10 minutes', 1, TRUE), + (1, 'Estou bem, e você?', NOW() - INTERVAL '9 minutes', 1, FALSE), + + (2, 'Oi, tudo certo?', NOW() - INTERVAL '20 minutes', 2, TRUE), + (6, 'Sim, e aí?', NOW() - INTERVAL '19 minutes', 2, FALSE), + + (2, 'E aí, novidades?', NOW() - INTERVAL '30 minutes', 3, TRUE), + (7, 'Nada de novo por aqui.', NOW() - INTERVAL '29 minutes', 3, FALSE), + + (2, 'Como foi o dia?', NOW() - INTERVAL '40 minutes', 4, TRUE), + (8, 'Trabalhei bastante.', NOW() - INTERVAL '39 minutes', 4, FALSE), + + (2, 'Oi!', NOW() - INTERVAL '50 minutes', 5, TRUE), + (11, 'Oi, tudo bem?', NOW() - INTERVAL '49 minutes', 5, FALSE), + + (2, 'Alguma novidade?', NOW() - INTERVAL '1 hour', 6, TRUE), + (13, 'Nada demais.', NOW() - INTERVAL '59 minutes', 6, FALSE), + + (2, 'Oi, tudo certo?', NOW() - INTERVAL '1 hour 10 minutes', 7, TRUE), + (16, 'Tudo sim, e você?', NOW() - INTERVAL '1 hour 9 minutes', 7, FALSE), + + (2, 'Bora marcar algo?', NOW() - INTERVAL '1 hour 20 minutes', 8, TRUE), + (19, 'Bora sim!', NOW() - INTERVAL '1 hour 19 minutes', 8, FALSE), + + (2, 'Bom dia!', NOW() - INTERVAL '1 hour 30 minutes', 9, TRUE), + (20, 'Bom dia! Como vai?', NOW() - INTERVAL '1 hour 29 minutes', 9, FALSE); \ No newline at end of file