From 634ce8adf8b0466f412e43f53364e9f693888ecf Mon Sep 17 00:00:00 2001 From: Zias van Nes Date: Mon, 20 Oct 2025 11:35:44 +0200 Subject: [PATCH] fix:logic of friends and users search --- src/routes/api/friends/+server.ts | 38 ++++++++++++++++++++++--------- src/routes/api/users/+server.ts | 23 ++++++++----------- src/routes/friends/+page.svelte | 2 ++ 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/routes/api/friends/+server.ts b/src/routes/api/friends/+server.ts index e8ff518..4ed719d 100644 --- a/src/routes/api/friends/+server.ts +++ b/src/routes/api/friends/+server.ts @@ -24,30 +24,46 @@ export const GET: RequestHandler = async ({ url, locals }) => { if (type === 'friends') { // Get accepted friendships where user is either sender or receiver - friendships = await db + const friendshipsRaw = await db .select({ id: friendship.id, userId: friendship.userId, friendId: friendship.friendId, status: friendship.status, - createdAt: friendship.createdAt, - friendUsername: user.username, - friendProfilePictureUrl: user.profilePictureUrl + createdAt: friendship.createdAt }) .from(friendship) - .innerJoin( - user, - or( - and(eq(friendship.friendId, user.id), eq(friendship.userId, locals.user.id)), - and(eq(friendship.userId, user.id), eq(friendship.friendId, locals.user.id)) - ) - ) .where( and( eq(friendship.status, status), or(eq(friendship.userId, locals.user.id), eq(friendship.friendId, locals.user.id)) ) ); + + // Get friend details for each friendship + friendships = await Promise.all( + friendshipsRaw.map(async (f) => { + const friendUserId = f.userId === locals.user!.id ? f.friendId : f.userId; + const friendUser = await db + .select({ + username: user.username, + profilePictureUrl: user.profilePictureUrl + }) + .from(user) + .where(eq(user.id, friendUserId)) + .limit(1); + + return { + id: f.id, + userId: f.userId, + friendId: f.friendId, + status: f.status, + createdAt: f.createdAt, + friendUsername: friendUser[0]?.username || '', + friendProfilePictureUrl: friendUser[0]?.profilePictureUrl || null + }; + }) + ); } else if (type === 'sent') { // Get friend requests sent by current user friendships = await db diff --git a/src/routes/api/users/+server.ts b/src/routes/api/users/+server.ts index 0f0e5b5..e3dceb0 100644 --- a/src/routes/api/users/+server.ts +++ b/src/routes/api/users/+server.ts @@ -38,25 +38,22 @@ export const GET: RequestHandler = async ({ url, locals }) => { }> = []; if (userIds.length > 0) { - existingFriendships = await db + // Use a simpler approach: get all friendships involving the current user + // and then filter for the relevant user IDs + const allUserFriendships = await db .select({ userId: friendship.userId, friendId: friendship.friendId, status: friendship.status }) .from(friendship) - .where( - or( - and( - eq(friendship.userId, locals.user.id), - or(...userIds.map((id) => eq(friendship.friendId, id))) - ), - and( - eq(friendship.friendId, locals.user.id), - or(...userIds.map((id) => eq(friendship.userId, id))) - ) - ) - ); + .where(or(eq(friendship.userId, locals.user.id), eq(friendship.friendId, locals.user.id))); + + // Filter to only include friendships with users in our search results + existingFriendships = allUserFriendships.filter((f) => { + const otherUserId = f.userId === locals.user!.id ? f.friendId : f.userId; + return userIds.includes(otherUserId); + }); } // Create a map of friendship statuses diff --git a/src/routes/friends/+page.svelte b/src/routes/friends/+page.svelte index 03f4d83..5c09019 100644 --- a/src/routes/friends/+page.svelte +++ b/src/routes/friends/+page.svelte @@ -134,6 +134,8 @@ // Search users when query changes with debounce let searchTimeout: ReturnType; $effect(() => { + // Track searchQuery dependency explicitly + searchQuery; clearTimeout(searchTimeout); searchTimeout = setTimeout(searchUsers, 300); });