refactor: make profile picture cooldown 7 days instead of 30

This commit is contained in:
trafficlunar 2025-05-10 14:35:40 +01:00
parent 4987c2e59a
commit 09c2448116
2 changed files with 7 additions and 7 deletions

View file

@ -25,13 +25,13 @@ export async function PATCH(request: NextRequest) {
const check = await rateLimit.handle(); const check = await rateLimit.handle();
if (check) return check; if (check) return check;
// Check if profile picture was updated in the last 30 days // Check if profile picture was updated in the last 7 days
const user = await prisma.user.findUnique({ where: { id: Number(session.user.id) } }); const user = await prisma.user.findUnique({ where: { id: Number(session.user.id) } });
if (user && user.imageUpdatedAt) { if (user && user.imageUpdatedAt) {
const timePeriod = dayjs().subtract(30, "days"); const timePeriod = dayjs().subtract(7, "days");
const lastUpdate = dayjs(user.imageUpdatedAt); const lastUpdate = dayjs(user.imageUpdatedAt);
if (lastUpdate.isAfter(timePeriod)) return rateLimit.sendResponse({ error: "Profile picture was changed in the last 30 days" }, 400); if (lastUpdate.isAfter(timePeriod)) return rateLimit.sendResponse({ error: "Profile picture was changed in the last 7 days" }, 400);
} }
// Parse data // Parse data
@ -62,7 +62,7 @@ export async function PATCH(request: NextRequest) {
try { try {
const buffer = Buffer.from(await image.arrayBuffer()); const buffer = Buffer.from(await image.arrayBuffer());
const webpBuffer = await sharp(buffer).resize({ width: 128, height: 128 }).webp({ quality: 85 }).toBuffer(); const webpBuffer = await sharp(buffer, { animated: true }).resize({ width: 128, height: 128 }).webp({ quality: 85 }).toBuffer();
const fileLocation = path.join(uploadsDirectory, `${session.user.id}.webp`); const fileLocation = path.join(uploadsDirectory, `${session.user.id}.webp`);
await fs.writeFile(fileLocation, webpBuffer); await fs.writeFile(fileLocation, webpBuffer);

View file

@ -15,7 +15,7 @@ export default function ProfilePictureSettings() {
const [error, setError] = useState<string | undefined>(undefined); const [error, setError] = useState<string | undefined>(undefined);
const [newPicture, setNewPicture] = useState<FileWithPath | undefined>(); const [newPicture, setNewPicture] = useState<FileWithPath | undefined>();
const changeDate = dayjs().add(30, "days"); const changeDate = dayjs().add(7, "days");
const handleSubmit = async (close: () => void) => { const handleSubmit = async (close: () => void) => {
const formData = new FormData(); const formData = new FormData();
@ -53,7 +53,7 @@ export default function ProfilePictureSettings() {
<div className="grid grid-cols-2"> <div className="grid grid-cols-2">
<div> <div>
<label className="font-semibold">Profile Picture</label> <label className="font-semibold">Profile Picture</label>
<p className="text-sm text-zinc-500">Manage your profile picture. Can only be changed once every 30 days.</p> <p className="text-sm text-zinc-500">Manage your profile picture. Can only be changed once every 7 days.</p>
</div> </div>
<div className="flex flex-col"> <div className="flex flex-col">
@ -98,7 +98,7 @@ export default function ProfilePictureSettings() {
)} )}
<SubmitDialogButton <SubmitDialogButton
title="Confirm Profile Picture Change" title="Confirm Profile Picture Change"
description="Are you sure? Your profile picture can only be changed every 30 days." description="Are you sure? Your profile picture can only be changed every 7 days."
error={error} error={error}
onSubmit={handleSubmit} onSubmit={handleSubmit}
> >