Skip to content

Commit

Permalink
fix: change some sql queries and add 'viewed' field to messages
Browse files Browse the repository at this point in the history
  • Loading branch information
luisfbl committed Feb 1, 2025
1 parent 2a863ce commit f66c68b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
3 changes: 2 additions & 1 deletion rest-server/migrations/13_messages.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 9 additions & 6 deletions rest-server/src/api/v1/users/me/chats/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion rest-server/src/api/v1/users/me/chats/messages/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
}
1 change: 1 addition & 0 deletions rest-server/src/models/chats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ pub struct Message {
content: String,
#[serde(serialize_with = "serialize_timestamp")]
created_at: NaiveDateTime,
viewed: bool,
}
10 changes: 10 additions & 0 deletions rest-server/tests/fixtures/12_chats.sql
Original file line number Diff line number Diff line change
@@ -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);
28 changes: 28 additions & 0 deletions rest-server/tests/fixtures/13_messages.sql
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit f66c68b

Please sign in to comment.