From 14b792802696c12c648da2bfbbcd660969a30050 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Thu, 28 May 2026 03:40:44 +0200 Subject: [PATCH] fix: list_mutual sort by follow created_at, apply pagination in TestStore --- crates/adapters/postgres/src/follow/mod.rs | 33 ++++++++++++++-------- crates/domain/src/testing/mod.rs | 10 +++++-- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/crates/adapters/postgres/src/follow/mod.rs b/crates/adapters/postgres/src/follow/mod.rs index 3c23930..22a038f 100644 --- a/crates/adapters/postgres/src/follow/mod.rs +++ b/crates/adapters/postgres/src/follow/mod.rs @@ -194,13 +194,14 @@ impl FollowRepository for PgFollowRepository { page: &PageParams, ) -> Result, DomainError> { let total: i64 = sqlx::query_scalar( - "SELECT COUNT(DISTINCT u.id) - FROM users u - WHERE EXISTS ( - SELECT 1 FROM follows f1 WHERE f1.follower_id=$1 AND f1.following_id=u.id AND f1.state='accepted' - ) AND EXISTS ( - SELECT 1 FROM follows f2 WHERE f2.following_id=$1 AND f2.follower_id=u.id AND f2.state='accepted' - )", + "SELECT COUNT(*) FROM follows f1 + WHERE f1.follower_id = $1 AND f1.state = 'accepted' + AND EXISTS ( + SELECT 1 FROM follows f2 + WHERE f2.follower_id = f1.following_id + AND f2.following_id = f1.follower_id + AND f2.state = 'accepted' + )", ) .bind(user_id.as_uuid()) .fetch_one(&self.pool) @@ -208,14 +209,22 @@ impl FollowRepository for PgFollowRepository { .into_domain()?; let rows = sqlx::query_as::<_, crate::user::UserRow>( - "SELECT u.id,u.username,u.email,u.password_hash,u.display_name,u.bio,u.avatar_url,u.header_url,u.custom_css,u.local,u.ap_id,u.inbox_url,u.created_at,u.updated_at + "SELECT u.id, u.username, u.email, u.password_hash, u.display_name, u.bio, + u.avatar_url, u.header_url, u.custom_css, u.local, + u.created_at, u.updated_at FROM users u + JOIN follows f1 + ON f1.follower_id = $1 + AND f1.following_id = u.id + AND f1.state = 'accepted' WHERE EXISTS ( - SELECT 1 FROM follows f1 WHERE f1.follower_id=$1 AND f1.following_id=u.id AND f1.state='accepted' - ) AND EXISTS ( - SELECT 1 FROM follows f2 WHERE f2.following_id=$1 AND f2.follower_id=u.id AND f2.state='accepted' + SELECT 1 FROM follows f2 + WHERE f2.follower_id = u.id + AND f2.following_id = $1 + AND f2.state = 'accepted' ) - ORDER BY u.created_at DESC LIMIT $2 OFFSET $3" + ORDER BY f1.created_at DESC + LIMIT $2 OFFSET $3", ) .bind(user_id.as_uuid()) .bind(page.limit()) diff --git a/crates/domain/src/testing/mod.rs b/crates/domain/src/testing/mod.rs index 4f2e4d2..9ad9518 100644 --- a/crates/domain/src/testing/mod.rs +++ b/crates/domain/src/testing/mod.rs @@ -475,12 +475,18 @@ impl FollowRepository for TestStore { following_ids.intersection(&follower_ids).cloned().collect(); drop(follows); let users = self.users.lock().unwrap(); - let items: Vec = users + let all_items: Vec = users .iter() .filter(|u| mutual_ids.contains(&u.id)) .cloned() .collect(); - let total = items.len() as i64; + let total = all_items.len() as i64; + let offset = page.offset() as usize; + let items: Vec = all_items + .into_iter() + .skip(offset) + .take(page.limit() as usize) + .collect(); Ok(Paginated { items, total,