fix:logic of friends and users search
This commit is contained in:
@@ -24,30 +24,46 @@ export const GET: RequestHandler = async ({ url, locals }) => {
|
|||||||
|
|
||||||
if (type === 'friends') {
|
if (type === 'friends') {
|
||||||
// Get accepted friendships where user is either sender or receiver
|
// Get accepted friendships where user is either sender or receiver
|
||||||
friendships = await db
|
const friendshipsRaw = await db
|
||||||
.select({
|
.select({
|
||||||
id: friendship.id,
|
id: friendship.id,
|
||||||
userId: friendship.userId,
|
userId: friendship.userId,
|
||||||
friendId: friendship.friendId,
|
friendId: friendship.friendId,
|
||||||
status: friendship.status,
|
status: friendship.status,
|
||||||
createdAt: friendship.createdAt,
|
createdAt: friendship.createdAt
|
||||||
friendUsername: user.username,
|
|
||||||
friendProfilePictureUrl: user.profilePictureUrl
|
|
||||||
})
|
})
|
||||||
.from(friendship)
|
.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(
|
.where(
|
||||||
and(
|
and(
|
||||||
eq(friendship.status, status),
|
eq(friendship.status, status),
|
||||||
or(eq(friendship.userId, locals.user.id), eq(friendship.friendId, locals.user.id))
|
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') {
|
} else if (type === 'sent') {
|
||||||
// Get friend requests sent by current user
|
// Get friend requests sent by current user
|
||||||
friendships = await db
|
friendships = await db
|
||||||
|
|||||||
@@ -38,25 +38,22 @@ export const GET: RequestHandler = async ({ url, locals }) => {
|
|||||||
}> = [];
|
}> = [];
|
||||||
|
|
||||||
if (userIds.length > 0) {
|
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({
|
.select({
|
||||||
userId: friendship.userId,
|
userId: friendship.userId,
|
||||||
friendId: friendship.friendId,
|
friendId: friendship.friendId,
|
||||||
status: friendship.status
|
status: friendship.status
|
||||||
})
|
})
|
||||||
.from(friendship)
|
.from(friendship)
|
||||||
.where(
|
.where(or(eq(friendship.userId, locals.user.id), eq(friendship.friendId, locals.user.id)));
|
||||||
or(
|
|
||||||
and(
|
// Filter to only include friendships with users in our search results
|
||||||
eq(friendship.userId, locals.user.id),
|
existingFriendships = allUserFriendships.filter((f) => {
|
||||||
or(...userIds.map((id) => eq(friendship.friendId, id)))
|
const otherUserId = f.userId === locals.user!.id ? f.friendId : f.userId;
|
||||||
),
|
return userIds.includes(otherUserId);
|
||||||
and(
|
});
|
||||||
eq(friendship.friendId, locals.user.id),
|
|
||||||
or(...userIds.map((id) => eq(friendship.userId, id)))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a map of friendship statuses
|
// Create a map of friendship statuses
|
||||||
|
|||||||
@@ -134,6 +134,8 @@
|
|||||||
// Search users when query changes with debounce
|
// Search users when query changes with debounce
|
||||||
let searchTimeout: ReturnType<typeof setTimeout>;
|
let searchTimeout: ReturnType<typeof setTimeout>;
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
|
// Track searchQuery dependency explicitly
|
||||||
|
searchQuery;
|
||||||
clearTimeout(searchTimeout);
|
clearTimeout(searchTimeout);
|
||||||
searchTimeout = setTimeout(searchUsers, 300);
|
searchTimeout = setTimeout(searchUsers, 300);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user