2023-03-08 15:31:24 -06:00
|
|
|
import { prisma } from "@/lib/api/db";
|
|
|
|
|
2023-07-17 22:31:26 -05:00
|
|
|
export default async function getUser({
|
|
|
|
params,
|
|
|
|
isSelf,
|
|
|
|
username,
|
|
|
|
}: {
|
|
|
|
params: {
|
|
|
|
lookupUsername?: string;
|
|
|
|
lookupId?: number;
|
|
|
|
};
|
|
|
|
isSelf: boolean;
|
|
|
|
username: string;
|
|
|
|
}) {
|
2023-04-26 15:40:48 -05:00
|
|
|
const user = await prisma.user.findUnique({
|
2023-03-08 15:31:24 -06:00
|
|
|
where: {
|
2023-07-17 22:31:26 -05:00
|
|
|
id: params.lookupId,
|
|
|
|
username: params.lookupUsername?.toLowerCase(),
|
2023-03-08 15:31:24 -06:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-05-22 23:08:16 -05:00
|
|
|
if (!user) return { response: "User not found.", status: 404 };
|
|
|
|
|
|
|
|
if (
|
|
|
|
!isSelf &&
|
|
|
|
user?.isPrivate &&
|
2023-07-08 05:35:43 -05:00
|
|
|
!user.whitelistedUsers.includes(username.toLowerCase())
|
2023-05-22 23:08:16 -05:00
|
|
|
) {
|
|
|
|
return { response: "This profile is private.", status: 401 };
|
|
|
|
}
|
2023-05-20 14:25:00 -05:00
|
|
|
|
2023-07-19 11:14:52 -05:00
|
|
|
const { password, ...lessSensitiveInfo } = user;
|
2023-05-20 14:25:00 -05:00
|
|
|
|
2023-05-18 13:02:17 -05:00
|
|
|
const data = isSelf
|
2023-05-20 14:25:00 -05:00
|
|
|
? // If user is requesting its own data
|
2023-07-19 11:14:52 -05:00
|
|
|
lessSensitiveInfo
|
2023-05-18 13:02:17 -05:00
|
|
|
: {
|
|
|
|
// If user is requesting someone elses data
|
2023-07-19 11:14:52 -05:00
|
|
|
id: lessSensitiveInfo.id,
|
|
|
|
name: lessSensitiveInfo.name,
|
|
|
|
username: lessSensitiveInfo.username,
|
2023-05-18 13:02:17 -05:00
|
|
|
};
|
2023-04-26 15:40:48 -05:00
|
|
|
|
2023-05-20 14:25:00 -05:00
|
|
|
return { response: data || null, status: 200 };
|
2023-04-26 15:40:48 -05:00
|
|
|
}
|