members with edit permission can now refresh preservation as well + bug fix

This commit is contained in:
daniel31x13 2024-09-12 15:30:15 -04:00
parent 8bb6e32bfa
commit aaf3590542
5 changed files with 58 additions and 36 deletions

View File

@ -440,7 +440,7 @@ export default function LinkDetails({
{mode === "view" ? (
<div className="flex gap-2 flex-wrap rounded-md p-2 bg-base-200 border border-base-200 w-full text-xs">
{link.tags[0] ? (
{link.tags && link.tags[0] ? (
link.tags.map((tag) =>
isPublicRoute ? (
<div

View File

@ -127,21 +127,22 @@ export default function LinkActions({ link, btnStyle }: Props) {
</div>
</li>
)}
{link.type === "url" && permissions === true && (
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
updateArchive();
}}
className="whitespace-nowrap"
>
{t("refresh_preserved_formats")}
</div>
</li>
)}
{link.type === "url" &&
(permissions === true || permissions?.canUpdate) && (
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
updateArchive();
}}
className="whitespace-nowrap"
>
{t("refresh_preserved_formats")}
</div>
</li>
)}
{(permissions === true || permissions?.canDelete) && (
<li>
<div

View File

@ -108,21 +108,22 @@ export default function LinkModal({
</div>
</li>
}
{link.type === "url" && permissions === true && (
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
onUpdateArchive();
}}
className="whitespace-nowrap"
>
{t("refresh_preserved_formats")}
</div>
</li>
)}
{link.type === "url" &&
(permissions === true || permissions?.canUpdate) && (
<li>
<div
role="button"
tabIndex={0}
onClick={() => {
(document?.activeElement as HTMLElement)?.blur();
onUpdateArchive();
}}
className="whitespace-nowrap"
>
{t("refresh_preserved_formats")}
</div>
</li>
)}
{(permissions === true || permissions?.canDelete) && (
<li>
<div

View File

@ -96,9 +96,18 @@ export default async function updateLinkById(
},
});
if (oldLink && oldLink?.url !== data.url) {
if (
data.url &&
oldLink &&
oldLink?.url !== data.url &&
isValidUrl(data.url)
) {
await removeFiles(oldLink.id, oldLink.collectionId);
}
} else
return {
response: "Invalid URL.",
status: 401,
};
const updatedLink = await prisma.link.update({
where: {

View File

@ -2,8 +2,10 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { prisma } from "@/lib/api/db";
import verifyUser from "@/lib/api/verifyUser";
import isValidUrl from "@/lib/shared/isValidUrl";
import { Collection, Link } from "@prisma/client";
import { removeFiles } from "@/lib/api/manageLinkFiles";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import { UsersAndCollections } from "@prisma/client";
import getPermission from "@/lib/api/getPermission";
import { moveFiles, removeFiles } from "@/lib/api/manageLinkFiles";
const RE_ARCHIVE_LIMIT = Number(process.env.RE_ARCHIVE_LIMIT) || 5;
@ -23,7 +25,16 @@ export default async function links(req: NextApiRequest, res: NextApiResponse) {
response: "Link not found.",
});
if (link.collection.ownerId !== user.id)
const collectionIsAccessible = await getPermission({
userId: user.id,
collectionId: link.collectionId,
});
const memberHasAccess = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === user.id && e.canUpdate
);
if (!(collectionIsAccessible?.ownerId === user.id || memberHasAccess))
return res.status(401).json({
response: "Permission denied.",
});