Skip to main content

max / makenotwork

1.6 KB · 67 lines History Blame Raw
1 use super::{PgPool, Uuid};
2
3 /// Track a thread (upsert).
4 #[tracing::instrument(skip_all)]
5 pub async fn track_thread(
6 pool: &PgPool,
7 user_id: Uuid,
8 thread_id: Uuid,
9 ) -> Result<(), sqlx::Error> {
10 sqlx::query!(
11 "INSERT INTO tracked_threads (user_id, thread_id)
12 VALUES ($1, $2)
13 ON CONFLICT (user_id, thread_id) DO NOTHING",
14 user_id,
15 thread_id,
16 )
17 .execute(pool)
18 .await?;
19 Ok(())
20 }
21
22 /// Untrack a thread.
23 #[tracing::instrument(skip_all)]
24 pub async fn untrack_thread(
25 pool: &PgPool,
26 user_id: Uuid,
27 thread_id: Uuid,
28 ) -> Result<(), sqlx::Error> {
29 sqlx::query!(
30 "DELETE FROM tracked_threads WHERE user_id = $1 AND thread_id = $2",
31 user_id,
32 thread_id,
33 )
34 .execute(pool)
35 .await?;
36 Ok(())
37 }
38
39 /// Stop tracking all threads for a user.
40 #[tracing::instrument(skip_all)]
41 pub async fn untrack_all(pool: &PgPool, user_id: Uuid) -> Result<(), sqlx::Error> {
42 sqlx::query!("DELETE FROM tracked_threads WHERE user_id = $1", user_id)
43 .execute(pool)
44 .await?;
45 Ok(())
46 }
47
48 /// Update the read position for a tracked thread (set last_read_post_id to the last post).
49 #[tracing::instrument(skip_all)]
50 pub async fn update_read_position(
51 pool: &PgPool,
52 user_id: Uuid,
53 thread_id: Uuid,
54 last_post_id: Uuid,
55 ) -> Result<(), sqlx::Error> {
56 sqlx::query!(
57 "UPDATE tracked_threads SET last_read_post_id = $3
58 WHERE user_id = $1 AND thread_id = $2",
59 user_id,
60 thread_id,
61 last_post_id,
62 )
63 .execute(pool)
64 .await?;
65 Ok(())
66 }
67