diff --git a/parakeet/src/db.rs b/parakeet/src/db.rs index d1965ba0..ea07c0b1 100644 --- a/parakeet/src/db.rs +++ b/parakeet/src/db.rs @@ -13,3 +13,33 @@ pub async fn get_actor_status( .await .optional() } + +#[derive(Debug, QueryableByName)] +#[diesel(check_for_backend(diesel::pg::Pg))] +#[allow(unused)] +pub struct ThreadItem { + #[diesel(sql_type = diesel::sql_types::Text)] + pub at_uri: String, + #[diesel(sql_type = diesel::sql_types::Nullable)] + pub parent_uri: Option, + #[diesel(sql_type = diesel::sql_types::Nullable)] + pub root_uri: Option, + #[diesel(sql_type = diesel::sql_types::Integer)] + pub depth: i32, +} + +pub async fn get_thread_children(conn: &mut AsyncPgConnection, uri: &str, depth: i32) -> QueryResult> { + diesel::sql_query(include_str!("sql/thread.sql")) + .bind::(uri) + .bind::(depth) + .load(conn) + .await +} + +pub async fn get_thread_parents(conn: &mut AsyncPgConnection, uri: &str, height: i32) -> QueryResult> { + diesel::sql_query(include_str!("sql/thread_parent.sql")) + .bind::(uri) + .bind::(height) + .load(conn) + .await +} diff --git a/parakeet/src/xrpc/app_bsky/feed/posts.rs b/parakeet/src/xrpc/app_bsky/feed/posts.rs index 8e359ccc..24f94020 100644 --- a/parakeet/src/xrpc/app_bsky/feed/posts.rs +++ b/parakeet/src/xrpc/app_bsky/feed/posts.rs @@ -321,19 +321,6 @@ pub struct GetPostThreadRes { pub threadgate: Option, } -#[derive(Debug, QueryableByName)] -#[diesel(check_for_backend(diesel::pg::Pg))] -struct ThreadItem { - #[diesel(sql_type = diesel::sql_types::Text)] - at_uri: String, - #[diesel(sql_type = diesel::sql_types::Nullable)] - parent_uri: Option, - // #[diesel(sql_type = diesel::sql_types::Nullable)] - // root_uri: Option, - #[diesel(sql_type = diesel::sql_types::Integer)] - depth: i32, -} - pub async fn get_post_thread( State(state): State, AtpAcceptLabelers(labelers): AtpAcceptLabelers, @@ -347,17 +334,8 @@ pub async fn get_post_thread( let depth = query.depth.unwrap_or(6).clamp(0, 1000); let parent_height = query.parent_height.unwrap_or(80).clamp(0, 1000); - let replies = diesel::sql_query(include_str!("../../../sql/thread.sql")) - .bind::(&uri) - .bind::(depth as i32) - .load::(&mut conn) - .await?; - - let parents = diesel::sql_query(include_str!("../../../sql/thread_parent.sql")) - .bind::(&uri) - .bind::(parent_height as i32) - .load::(&mut conn) - .await?; + let replies = crate::db::get_thread_children(&mut conn, &uri, depth as i32).await?; + let parents = crate::db::get_thread_parents(&mut conn, &uri, parent_height as i32).await?; let reply_uris = replies.iter().map(|item| item.at_uri.clone()).collect(); let parent_uris = parents.iter().map(|item| item.at_uri.clone()).collect();