use super::{PgPool, Uuid}; /// Track a thread (upsert). #[tracing::instrument(skip_all)] pub async fn track_thread( pool: &PgPool, user_id: Uuid, thread_id: Uuid, ) -> Result<(), sqlx::Error> { sqlx::query!( "INSERT INTO tracked_threads (user_id, thread_id) VALUES ($1, $2) ON CONFLICT (user_id, thread_id) DO NOTHING", user_id, thread_id, ) .execute(pool) .await?; Ok(()) } /// Untrack a thread. #[tracing::instrument(skip_all)] pub async fn untrack_thread( pool: &PgPool, user_id: Uuid, thread_id: Uuid, ) -> Result<(), sqlx::Error> { sqlx::query!( "DELETE FROM tracked_threads WHERE user_id = $1 AND thread_id = $2", user_id, thread_id, ) .execute(pool) .await?; Ok(()) } /// Stop tracking all threads for a user. #[tracing::instrument(skip_all)] pub async fn untrack_all(pool: &PgPool, user_id: Uuid) -> Result<(), sqlx::Error> { sqlx::query!("DELETE FROM tracked_threads WHERE user_id = $1", user_id) .execute(pool) .await?; Ok(()) } /// Update the read position for a tracked thread (set last_read_post_id to the last post). #[tracing::instrument(skip_all)] pub async fn update_read_position( pool: &PgPool, user_id: Uuid, thread_id: Uuid, last_post_id: Uuid, ) -> Result<(), sqlx::Error> { sqlx::query!( "UPDATE tracked_threads SET last_read_post_id = $3 WHERE user_id = $1 AND thread_id = $2", user_id, thread_id, last_post_id, ) .execute(pool) .await?; Ok(()) }