From 5c9acdecc1b3ea4f79f1c0b3b5c29bb75ecacfb0 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Thu, 14 May 2026 17:56:03 +0200 Subject: [PATCH] =?UTF-8?q?fix(postgres):=20get=5Fthread=20uses=20recursiv?= =?UTF-8?q?e=20CTE=20=E2=80=94=20fetches=20all=20nested=20replies=20not=20?= =?UTF-8?q?just=20direct=20ones?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/adapters/postgres/src/thought.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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