diff --git a/crates/adapters/postgres/src/thought.rs b/crates/adapters/postgres/src/thought.rs index 3e2b074..a21ac8e 100644 --- a/crates/adapters/postgres/src/thought.rs +++ b/crates/adapters/postgres/src/thought.rs @@ -116,9 +116,19 @@ impl ThoughtRepository for PgThoughtRepository { } async fn get_thread(&self, id: &ThoughtId) -> Result, DomainError> { - sqlx::query_as::<_, ThoughtRow>(&format!( - "{THOUGHT_SELECT} WHERE id=$1 OR in_reply_to_id=$1 ORDER BY created_at ASC" - )) + // Recursive CTE: fetches the root thought and all nested replies at any depth. + sqlx::query_as::<_, ThoughtRow>( + "WITH RECURSIVE thread AS ( + SELECT id,user_id,content,in_reply_to_id,in_reply_to_url,ap_id, + visibility,content_warning,sensitive,local,created_at,updated_at + FROM thoughts WHERE id = $1 + UNION ALL + SELECT t.id,t.user_id,t.content,t.in_reply_to_id,t.in_reply_to_url,t.ap_id, + t.visibility,t.content_warning,t.sensitive,t.local,t.created_at,t.updated_at + FROM thoughts t JOIN thread ON t.in_reply_to_id = thread.id + ) + SELECT * FROM thread ORDER BY created_at ASC", + ) .bind(id.as_uuid()) .fetch_all(&self.pool) .await