Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Feb 2, 2025
1 parent 8fb0106 commit de579ee
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/services/User/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,24 +672,37 @@ export async function verifyPassword(accountId: string, password: string) {
}

export async function searchUsers(requesterUserId: string, query: string) {
const q = `%${query}%`;
//search for a user by username. order by followers, then by username
const result = await prisma.$queryRaw<{ id: string }[]>`
SELECT
u.id
FROM
"User" u
LEFT JOIN "Follower" f ON u.id = ${requesterUserId}
WHERE u.username ILIKE ${q}
ORDER BY
f DESC,
u.username
LIMIT 10;
`;
const userIds = result.map((user) => user.id);

const followedTo = await prisma.follower.findMany({
where: {
followedById: requesterUserId,
followedTo: {
username: { contains: query },
},
},
select: {
followedTo: {
select: {
username: true,
tag: true,
avatar: true,
hexColor: true,
id: true,
},
},
},
take: 10,
});

const followedToId = followedTo.map((f) => f.followedTo.id);
const followedUsers = followedTo.map((f) => f.followedTo);
if (followedUsers.length >= 10) return followedUsers;

const users = await prisma.user.findMany({
where: {
id: { in: userIds },
id: { notIn: followedToId },
username: { contains: query },
OR: [{ NOT: { account: null } }, { NOT: { application: null } }],
},
select: {
Expand All @@ -699,8 +712,9 @@ export async function searchUsers(requesterUserId: string, query: string) {
hexColor: true,
id: true,
},
orderBy: { joinedAt: 'desc' },
orderBy: { username: 'desc' },
take: 10 - followedUsers.length,
});
const orderedUsers = userIds.map((id) => users.find((user) => user.id === id)!);
return orderedUsers;

return [...followedUsers, ...users];
}

0 comments on commit de579ee

Please sign in to comment.