2023-10-22 23:28:39 -05:00
|
|
|
import { prisma } from "@/lib/api/db";
|
|
|
|
|
2023-11-15 12:12:06 -06:00
|
|
|
export default async function getPublicUser(
|
2023-10-22 23:28:39 -05:00
|
|
|
targetId: number | string,
|
|
|
|
isId: boolean,
|
2023-11-06 07:25:57 -06:00
|
|
|
requestingId?: number
|
2023-10-22 23:28:39 -05:00
|
|
|
) {
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
2023-11-24 12:28:47 -06:00
|
|
|
const isInAPublicCollection = await prisma.collection.findFirst({
|
|
|
|
where: {
|
|
|
|
["OR"]: [
|
|
|
|
{ ownerId: user.id },
|
|
|
|
{
|
|
|
|
members: {
|
|
|
|
some: {
|
|
|
|
userId: user.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
isPublic: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user?.isPrivate && !isInAPublicCollection) {
|
2023-11-06 07:25:57 -06:00
|
|
|
if (requestingId) {
|
2023-11-20 14:58:32 -06:00
|
|
|
const requestingUser = await prisma.user.findUnique({
|
|
|
|
where: { id: requestingId },
|
|
|
|
});
|
2023-11-06 07:25:57 -06:00
|
|
|
|
|
|
|
if (
|
2023-11-20 14:58:32 -06:00
|
|
|
requestingUser?.id !== requestingId &&
|
|
|
|
(!requestingUser?.username ||
|
|
|
|
!whitelistedUsernames.includes(
|
|
|
|
requestingUser.username?.toLowerCase()
|
|
|
|
))
|
2023-11-06 07:25:57 -06:00
|
|
|
) {
|
|
|
|
return {
|
|
|
|
response: "User not found or profile is private.",
|
|
|
|
status: 404,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
return { response: "User not found or profile is private.", status: 404 };
|
2023-10-22 23:28:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-19 10:50:43 -06:00
|
|
|
archiveAsScreenshot: lessSensitiveInfo.archiveAsScreenshot,
|
2024-03-15 13:41:41 -05:00
|
|
|
archiveAsSinglefile: lessSensitiveInfo.archiveAsSinglefile,
|
2023-12-19 16:20:09 -06:00
|
|
|
archiveAsPDF: lessSensitiveInfo.archiveAsPDF,
|
2023-10-22 23:28:39 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return { response: data, status: 200 };
|
|
|
|
}
|