2023-10-22 23:28:39 -05:00
|
|
|
import { prisma } from "@/lib/api/db";
|
|
|
|
|
2023-10-23 14:24:22 -05:00
|
|
|
export default async function getPublicUserById(
|
2023-10-22 23:28:39 -05:00
|
|
|
targetId: number | string,
|
|
|
|
isId: boolean,
|
|
|
|
requestingUsername?: string
|
|
|
|
) {
|
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: isId
|
|
|
|
? {
|
|
|
|
id: Number(targetId) as number,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
username: targetId as string,
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
whitelistedUsers: {
|
|
|
|
select: {
|
|
|
|
username: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!user)
|
|
|
|
return { response: "User not found or profile is private.", status: 404 };
|
|
|
|
|
|
|
|
const whitelistedUsernames = user.whitelistedUsers?.map(
|
|
|
|
(usernames) => usernames.username
|
|
|
|
);
|
|
|
|
|
|
|
|
if (
|
|
|
|
user?.isPrivate &&
|
|
|
|
(!requestingUsername ||
|
|
|
|
!whitelistedUsernames.includes(requestingUsername?.toLowerCase()))
|
|
|
|
) {
|
|
|
|
return { response: "User not found or profile is private.", status: 404 };
|
|
|
|
}
|
|
|
|
|
|
|
|
const { password, ...lessSensitiveInfo } = user;
|
|
|
|
|
|
|
|
const data = {
|
2023-10-28 00:42:31 -05:00
|
|
|
id: lessSensitiveInfo.id,
|
2023-10-22 23:28:39 -05:00
|
|
|
name: lessSensitiveInfo.name,
|
|
|
|
username: lessSensitiveInfo.username,
|
2023-10-27 23:45:14 -05:00
|
|
|
image: lessSensitiveInfo.image,
|
2023-10-22 23:28:39 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return { response: data, status: 200 };
|
|
|
|
}
|