el.xwx.moe/lib/api/controllers/users/getUsers.ts
2023-07-08 14:05:43 +03:30

38 lines
887 B
TypeScript

import { prisma } from "@/lib/api/db";
export default async function getUser(
lookupUsername: string,
isSelf: boolean,
username: string
) {
const user = await prisma.user.findUnique({
where: {
username: lookupUsername.toLowerCase(),
},
});
if (!user) return { response: "User not found.", status: 404 };
if (
!isSelf &&
user?.isPrivate &&
!user.whitelistedUsers.includes(username.toLowerCase())
) {
return { response: "This profile is private.", status: 401 };
}
const { password, ...unsensitiveInfo } = user;
const data = isSelf
? // If user is requesting its own data
unsensitiveInfo
: {
// If user is requesting someone elses data
id: unsensitiveInfo.id,
name: unsensitiveInfo.name,
username: unsensitiveInfo.username,
};
return { response: data || null, status: 200 };
}