el.xwx.moe/lib/api/controllers/users/getUsers.ts

38 lines
887 B
TypeScript
Raw Normal View History

import { prisma } from "@/lib/api/db";
export default async function getUser(
2023-07-08 05:35:43 -05:00
lookupUsername: string,
isSelf: boolean,
2023-07-08 05:35:43 -05:00
username: string
) {
const user = await prisma.user.findUnique({
where: {
2023-07-08 05:35:43 -05:00
username: lookupUsername.toLowerCase(),
},
});
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())
) {
return { response: "This profile is private.", status: 401 };
}
2023-05-20 14:25:00 -05:00
const { password, ...unsensitiveInfo } = user;
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
unsensitiveInfo
2023-05-18 13:02:17 -05:00
: {
// If user is requesting someone elses data
2023-05-20 14:25:00 -05:00
id: unsensitiveInfo.id,
name: unsensitiveInfo.name,
2023-07-08 05:35:43 -05:00
username: unsensitiveInfo.username,
2023-05-18 13:02:17 -05:00
};
2023-05-20 14:25:00 -05:00
return { response: data || null, status: 200 };
}