refactored/cleaned up API + added support for renaming tags
This commit is contained in:
@@ -1,54 +0,0 @@
|
||||
import { prisma } from "@/lib/api/db";
|
||||
|
||||
export default async function getUser({
|
||||
params,
|
||||
isSelf,
|
||||
username,
|
||||
}: {
|
||||
params: {
|
||||
lookupUsername?: string;
|
||||
lookupId?: number;
|
||||
};
|
||||
isSelf: boolean;
|
||||
username: string;
|
||||
}) {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: params.lookupId,
|
||||
username: params.lookupUsername?.toLowerCase(),
|
||||
},
|
||||
include: {
|
||||
whitelistedUsers: {
|
||||
select: {
|
||||
username: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!user) return { response: "User not found.", status: 404 };
|
||||
|
||||
const whitelistedUsernames = user.whitelistedUsers?.map(usernames => usernames.username);
|
||||
|
||||
if (
|
||||
!isSelf &&
|
||||
user?.isPrivate &&
|
||||
!whitelistedUsernames.includes(username.toLowerCase())
|
||||
) {
|
||||
return { response: "This profile is private.", status: 401 };
|
||||
}
|
||||
|
||||
const { password, ...lessSensitiveInfo } = user;
|
||||
|
||||
const data = isSelf
|
||||
? // If user is requesting its own data
|
||||
{...lessSensitiveInfo, whitelistedUsers: whitelistedUsernames}
|
||||
: {
|
||||
// If user is requesting someone elses data
|
||||
id: lessSensitiveInfo.id,
|
||||
name: lessSensitiveInfo.name,
|
||||
username: lessSensitiveInfo.username,
|
||||
};
|
||||
|
||||
return { response: data || null, status: 200 };
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
import { prisma } from "@/lib/api/db";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import bcrypt from "bcrypt";
|
||||
|
||||
const emailEnabled =
|
||||
process.env.EMAIL_FROM && process.env.EMAIL_SERVER ? true : false;
|
||||
|
||||
interface Data {
|
||||
response: string | object;
|
||||
}
|
||||
|
||||
interface User {
|
||||
name: string;
|
||||
username?: string;
|
||||
email?: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export default async function postUser(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<Data>
|
||||
) {
|
||||
if (process.env.NEXT_PUBLIC_DISABLE_REGISTRATION === "true") {
|
||||
return res.status(400).json({ response: "Registration is disabled." });
|
||||
}
|
||||
|
||||
const body: User = req.body;
|
||||
|
||||
const checkHasEmptyFields = emailEnabled
|
||||
? !body.password || !body.name || !body.email
|
||||
: !body.username || !body.password || !body.name;
|
||||
|
||||
if (checkHasEmptyFields)
|
||||
return res
|
||||
.status(400)
|
||||
.json({ response: "Please fill out all the fields." });
|
||||
|
||||
// Check email (if enabled)
|
||||
const checkEmail =
|
||||
/^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
|
||||
if (emailEnabled && !checkEmail.test(body.email?.toLowerCase() || ""))
|
||||
return res.status(400).json({
|
||||
response: "Please enter a valid email.",
|
||||
});
|
||||
|
||||
// Check username (if email was disabled)
|
||||
const checkUsername = RegExp("^[a-z0-9_-]{3,31}$");
|
||||
if (!emailEnabled && !checkUsername.test(body.username?.toLowerCase() || ""))
|
||||
return res.status(400).json({
|
||||
response:
|
||||
"Username has to be between 3-30 characters, no spaces and special characters are allowed.",
|
||||
});
|
||||
|
||||
const checkIfUserExists = await prisma.user.findFirst({
|
||||
where: emailEnabled
|
||||
? {
|
||||
email: body.email?.toLowerCase(),
|
||||
}
|
||||
: {
|
||||
username: (body.username as string).toLowerCase(),
|
||||
},
|
||||
});
|
||||
|
||||
if (!checkIfUserExists) {
|
||||
const saltRounds = 10;
|
||||
|
||||
const hashedPassword = bcrypt.hashSync(body.password, saltRounds);
|
||||
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
name: body.name,
|
||||
username: emailEnabled
|
||||
? undefined
|
||||
: (body.username as string).toLowerCase(),
|
||||
email: emailEnabled ? body.email?.toLowerCase() : undefined,
|
||||
password: hashedPassword,
|
||||
},
|
||||
});
|
||||
|
||||
return res.status(201).json({ response: "User successfully created." });
|
||||
} else if (checkIfUserExists) {
|
||||
return res.status(400).json({
|
||||
response: `${emailEnabled ? "Email" : "Username"} already exists.`,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { prisma } from "@/lib/api/db";
|
||||
|
||||
export default async function getUser(
|
||||
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 = {
|
||||
name: lessSensitiveInfo.name,
|
||||
username: lessSensitiveInfo.username,
|
||||
};
|
||||
|
||||
return { response: data, status: 200 };
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { prisma } from "@/lib/api/db";
|
||||
|
||||
export default async function getUser(userId: number) {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
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
|
||||
);
|
||||
|
||||
const { password, ...lessSensitiveInfo } = user;
|
||||
|
||||
const data = {
|
||||
...lessSensitiveInfo,
|
||||
whitelistedUsers: whitelistedUsernames,
|
||||
};
|
||||
|
||||
return { response: data, status: 200 };
|
||||
}
|
||||
+24
-24
@@ -10,20 +10,20 @@ const emailEnabled =
|
||||
process.env.EMAIL_FROM && process.env.EMAIL_SERVER ? true : false;
|
||||
|
||||
export default async function updateUser(
|
||||
user: AccountSettings,
|
||||
sessionUser: {
|
||||
id: number;
|
||||
username: string;
|
||||
email: string;
|
||||
isSubscriber: boolean;
|
||||
}
|
||||
},
|
||||
data: AccountSettings
|
||||
) {
|
||||
if (emailEnabled && !user.email)
|
||||
if (emailEnabled && !data.email)
|
||||
return {
|
||||
response: "Email invalid.",
|
||||
status: 400,
|
||||
};
|
||||
else if (!user.username)
|
||||
else if (!data.username)
|
||||
return {
|
||||
response: "Username invalid.",
|
||||
status: 400,
|
||||
@@ -31,7 +31,7 @@ export default async function updateUser(
|
||||
|
||||
const checkUsername = RegExp("^[a-z0-9_-]{3,31}$");
|
||||
|
||||
if (!checkUsername.test(user.username.toLowerCase()))
|
||||
if (!checkUsername.test(data.username.toLowerCase()))
|
||||
return {
|
||||
response:
|
||||
"Username has to be between 3-30 characters, no spaces and special characters are allowed.",
|
||||
@@ -44,15 +44,15 @@ export default async function updateUser(
|
||||
OR: emailEnabled
|
||||
? [
|
||||
{
|
||||
username: user.username.toLowerCase(),
|
||||
username: data.username.toLowerCase(),
|
||||
},
|
||||
{
|
||||
email: user.email?.toLowerCase(),
|
||||
email: data.email?.toLowerCase(),
|
||||
},
|
||||
]
|
||||
: [
|
||||
{
|
||||
username: user.username.toLowerCase(),
|
||||
username: data.username.toLowerCase(),
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -66,10 +66,10 @@ export default async function updateUser(
|
||||
|
||||
// Avatar Settings
|
||||
|
||||
const profilePic = user.profilePic;
|
||||
const profilePic = data.profilePic;
|
||||
|
||||
if (profilePic.startsWith("data:image/jpeg;base64")) {
|
||||
if (user.profilePic.length < 1572864) {
|
||||
if (data.profilePic.length < 1572864) {
|
||||
try {
|
||||
const base64Data = profilePic.replace(/^data:image\/jpeg;base64,/, "");
|
||||
|
||||
@@ -97,22 +97,22 @@ export default async function updateUser(
|
||||
// Other settings
|
||||
|
||||
const saltRounds = 10;
|
||||
const newHashedPassword = bcrypt.hashSync(user.newPassword || "", saltRounds);
|
||||
const newHashedPassword = bcrypt.hashSync(data.newPassword || "", saltRounds);
|
||||
|
||||
const updatedUser = await prisma.user.update({
|
||||
where: {
|
||||
id: sessionUser.id,
|
||||
},
|
||||
data: {
|
||||
name: user.name,
|
||||
username: user.username.toLowerCase(),
|
||||
email: user.email?.toLowerCase(),
|
||||
isPrivate: user.isPrivate,
|
||||
archiveAsScreenshot: user.archiveAsScreenshot,
|
||||
archiveAsPDF: user.archiveAsPDF,
|
||||
archiveAsWaybackMachine: user.archiveAsWaybackMachine,
|
||||
name: data.name,
|
||||
username: data.username.toLowerCase(),
|
||||
email: data.email?.toLowerCase(),
|
||||
isPrivate: data.isPrivate,
|
||||
archiveAsScreenshot: data.archiveAsScreenshot,
|
||||
archiveAsPDF: data.archiveAsPDF,
|
||||
archiveAsWaybackMachine: data.archiveAsWaybackMachine,
|
||||
password:
|
||||
user.newPassword && user.newPassword !== ""
|
||||
data.newPassword && data.newPassword !== ""
|
||||
? newHashedPassword
|
||||
: undefined,
|
||||
},
|
||||
@@ -124,11 +124,11 @@ export default async function updateUser(
|
||||
const { whitelistedUsers, password, ...userInfo } = updatedUser;
|
||||
|
||||
// If user.whitelistedUsers is not provided, we will assume the whitelistedUsers should be removed
|
||||
const newWhitelistedUsernames: string[] = user.whitelistedUsers || [];
|
||||
const newWhitelistedUsernames: string[] = data.whitelistedUsers || [];
|
||||
|
||||
// Get the current whitelisted usernames
|
||||
const currentWhitelistedUsernames: string[] = whitelistedUsers.map(
|
||||
(user) => user.username
|
||||
(data) => data.username
|
||||
);
|
||||
|
||||
// Find the usernames to be deleted (present in current but not in new)
|
||||
@@ -164,17 +164,17 @@ export default async function updateUser(
|
||||
|
||||
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
||||
|
||||
if (STRIPE_SECRET_KEY && emailEnabled && sessionUser.email !== user.email)
|
||||
if (STRIPE_SECRET_KEY && emailEnabled && sessionUser.email !== data.email)
|
||||
await updateCustomerEmail(
|
||||
STRIPE_SECRET_KEY,
|
||||
sessionUser.email,
|
||||
user.email as string
|
||||
data.email as string
|
||||
);
|
||||
|
||||
const response: Omit<AccountSettings, "password"> = {
|
||||
...userInfo,
|
||||
whitelistedUsers: newWhitelistedUsernames,
|
||||
profilePic: `/api/avatar/${userInfo.id}?${Date.now()}`,
|
||||
profilePic: `/api/v1/avatar/${userInfo.id}?${Date.now()}`,
|
||||
};
|
||||
|
||||
return { response, status: 200 };
|
||||
Reference in New Issue
Block a user