2023-02-24 11:32:28 -06:00
|
|
|
import { prisma } from "@/lib/api/db";
|
2023-06-14 17:34:54 -05:00
|
|
|
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
|
2023-12-06 15:13:11 -06:00
|
|
|
import getTitle from "@/lib/shared/getTitle";
|
2023-11-19 15:22:27 -06:00
|
|
|
import { UsersAndCollections } from "@prisma/client";
|
2023-04-26 15:40:48 -05:00
|
|
|
import getPermission from "@/lib/api/getPermission";
|
2023-07-18 10:59:57 -05:00
|
|
|
import createFolder from "@/lib/api/storage/createFolder";
|
2023-11-25 02:19:02 -06:00
|
|
|
import validateUrlSize from "../../validateUrlSize";
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-12-19 16:20:09 -06:00
|
|
|
const MAX_LINKS_PER_USER = Number(process.env.MAX_LINKS_PER_USER) || 30000;
|
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
export default async function postLink(
|
2023-06-14 17:34:54 -05:00
|
|
|
link: LinkIncludingShortenedCollectionAndTags,
|
2023-05-27 11:29:39 -05:00
|
|
|
userId: number
|
|
|
|
) {
|
2023-07-24 08:39:51 -05:00
|
|
|
try {
|
2023-12-07 11:29:45 -06:00
|
|
|
new URL(link.url || "");
|
2023-07-24 08:39:51 -05:00
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
response:
|
|
|
|
"Please enter a valid Address for the Link. (It should start with http/https)",
|
|
|
|
status: 400,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-02-19 13:37:07 -06:00
|
|
|
if (!link.collection.id && link.collection.name) {
|
|
|
|
link.collection.name = link.collection.name.trim();
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2024-02-19 13:37:07 -06:00
|
|
|
// find the collection with the name and the user's id
|
|
|
|
const findCollection = await prisma.collection.findFirst({
|
|
|
|
where: {
|
|
|
|
name: link.collection.name,
|
2023-12-19 16:20:09 -06:00
|
|
|
ownerId: userId,
|
2024-02-19 13:37:07 -06:00
|
|
|
parentId: link.collection.parentId,
|
2023-12-19 16:20:09 -06:00
|
|
|
},
|
2024-02-19 13:37:07 -06:00
|
|
|
});
|
2023-12-19 16:20:09 -06:00
|
|
|
|
2024-02-19 13:37:07 -06:00
|
|
|
if (findCollection) {
|
|
|
|
const collectionIsAccessible = await getPermission({
|
|
|
|
userId,
|
|
|
|
collectionId: findCollection.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
const memberHasAccess = collectionIsAccessible?.members.some(
|
|
|
|
(e: UsersAndCollections) => e.userId === userId && e.canCreate
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
|
|
|
|
return { response: "Collection is not accessible.", status: 401 };
|
|
|
|
|
|
|
|
link.collection.id = findCollection.id;
|
|
|
|
} else {
|
|
|
|
const collection = await prisma.collection.create({
|
|
|
|
data: {
|
|
|
|
name: link.collection.name,
|
|
|
|
ownerId: userId,
|
|
|
|
},
|
|
|
|
});
|
2023-08-03 23:33:51 -05:00
|
|
|
|
2024-02-19 13:37:07 -06:00
|
|
|
link.collection.id = collection.id;
|
2024-03-05 08:11:56 -06:00
|
|
|
|
|
|
|
await prisma.user.update({
|
|
|
|
where: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
collectionOrder: {
|
|
|
|
push: link.collection.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2024-02-19 13:37:07 -06:00
|
|
|
}
|
|
|
|
} else if (link.collection.id) {
|
2023-11-19 15:22:27 -06:00
|
|
|
const collectionIsAccessible = await getPermission({
|
2023-03-28 02:31:50 -05:00
|
|
|
userId,
|
2023-10-22 23:28:39 -05:00
|
|
|
collectionId: link.collection.id,
|
2023-11-19 15:22:27 -06:00
|
|
|
});
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
const memberHasAccess = collectionIsAccessible?.members.some(
|
|
|
|
(e: UsersAndCollections) => e.userId === userId && e.canCreate
|
|
|
|
);
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
if (!(collectionIsAccessible?.ownerId === userId || memberHasAccess))
|
|
|
|
return { response: "Collection is not accessible.", status: 401 };
|
2024-02-19 13:37:07 -06:00
|
|
|
} else if (!link.collection.id) {
|
|
|
|
link.collection.name = "Unorganized";
|
|
|
|
link.collection.parentId = null;
|
|
|
|
|
|
|
|
// find the collection with the name "Unorganized" and the user's id
|
|
|
|
const unorganizedCollection = await prisma.collection.findFirst({
|
|
|
|
where: {
|
|
|
|
name: "Unorganized",
|
|
|
|
ownerId: userId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
link.collection.id = unorganizedCollection?.id;
|
2024-03-05 08:11:56 -06:00
|
|
|
|
|
|
|
await prisma.user.update({
|
|
|
|
where: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
collectionOrder: {
|
|
|
|
push: link.collection.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2023-03-28 02:31:50 -05:00
|
|
|
} else {
|
2024-02-19 13:37:07 -06:00
|
|
|
return { response: "Uncaught error.", status: 500 };
|
2023-02-24 11:32:28 -06:00
|
|
|
}
|
|
|
|
|
2024-03-04 23:24:30 -06:00
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-03-05 08:03:04 -06:00
|
|
|
if (user?.preventDuplicateLinks) {
|
2024-03-08 05:34:56 -06:00
|
|
|
const trimmedUrl = link.url?.trim();
|
|
|
|
const wwwPrefix = 'www.';
|
|
|
|
const hasWwwPrefix = trimmedUrl?.includes(`://${wwwPrefix}`);
|
|
|
|
const urlWithoutWww = hasWwwPrefix ? trimmedUrl?.replace(`://${wwwPrefix}`, '://') : trimmedUrl;
|
|
|
|
const urlWithWww = hasWwwPrefix ? trimmedUrl : trimmedUrl?.replace('://', `://${wwwPrefix}`);
|
|
|
|
|
2024-03-05 08:03:04 -06:00
|
|
|
const existingLink = await prisma.link.findFirst({
|
|
|
|
where: {
|
2024-03-08 05:34:56 -06:00
|
|
|
OR: [
|
|
|
|
{ url: trimmedUrl },
|
|
|
|
{ url: hasWwwPrefix ? urlWithoutWww : urlWithWww }, // Toggling "www."
|
|
|
|
],
|
2024-03-05 08:03:04 -06:00
|
|
|
collection: {
|
|
|
|
ownerId: userId,
|
|
|
|
},
|
2024-03-04 23:24:30 -06:00
|
|
|
},
|
2024-03-05 08:03:04 -06:00
|
|
|
});
|
2024-03-04 23:24:30 -06:00
|
|
|
|
2024-03-05 08:03:04 -06:00
|
|
|
if (existingLink)
|
|
|
|
return {
|
|
|
|
response: "Link already exists",
|
|
|
|
status: 409,
|
|
|
|
};
|
2024-03-04 23:24:30 -06:00
|
|
|
}
|
|
|
|
|
2024-02-19 13:37:07 -06:00
|
|
|
const numberOfLinksTheUserHas = await prisma.link.count({
|
|
|
|
where: {
|
|
|
|
collection: {
|
|
|
|
ownerId: userId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (numberOfLinksTheUserHas + 1 > MAX_LINKS_PER_USER)
|
|
|
|
return {
|
|
|
|
response: `Error: Each user can only have a maximum of ${MAX_LINKS_PER_USER} Links.`,
|
|
|
|
status: 400,
|
|
|
|
};
|
|
|
|
|
|
|
|
link.collection.name = link.collection.name.trim();
|
|
|
|
|
2023-06-20 09:39:03 -05:00
|
|
|
const description =
|
|
|
|
link.description && link.description !== ""
|
|
|
|
? link.description
|
2023-11-25 02:19:02 -06:00
|
|
|
: link.url
|
2023-12-29 11:21:22 -06:00
|
|
|
? await getTitle(link.url)
|
|
|
|
: undefined;
|
2023-11-25 02:19:02 -06:00
|
|
|
|
|
|
|
const validatedUrl = link.url ? await validateUrlSize(link.url) : undefined;
|
|
|
|
|
|
|
|
const contentType = validatedUrl?.get("content-type");
|
|
|
|
let linkType = "url";
|
|
|
|
let imageExtension = "png";
|
|
|
|
|
|
|
|
if (!link.url) linkType = link.type;
|
|
|
|
else if (contentType === "application/pdf") linkType = "pdf";
|
|
|
|
else if (contentType?.startsWith("image")) {
|
|
|
|
linkType = "image";
|
|
|
|
if (contentType === "image/jpeg") imageExtension = "jpeg";
|
|
|
|
else if (contentType === "image/png") imageExtension = "png";
|
|
|
|
}
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-08-10 11:16:44 -05:00
|
|
|
const newLink = await prisma.link.create({
|
2023-02-24 11:32:28 -06:00
|
|
|
data: {
|
2024-03-05 08:03:04 -06:00
|
|
|
url: link.url?.trim(),
|
2023-07-24 08:39:51 -05:00
|
|
|
name: link.name,
|
2023-06-20 09:39:03 -05:00
|
|
|
description,
|
2023-11-25 02:19:02 -06:00
|
|
|
type: linkType,
|
2023-02-24 11:32:28 -06:00
|
|
|
collection: {
|
2024-02-19 13:37:07 -06:00
|
|
|
connect: {
|
|
|
|
id: link.collection.id,
|
2023-02-24 11:32:28 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
tags: {
|
2023-03-28 02:31:50 -05:00
|
|
|
connectOrCreate: link.tags.map((tag) => ({
|
2023-02-24 11:32:28 -06:00
|
|
|
where: {
|
2023-03-28 02:31:50 -05:00
|
|
|
name_ownerId: {
|
2023-07-24 08:39:51 -05:00
|
|
|
name: tag.name.trim(),
|
2023-03-28 02:31:50 -05:00
|
|
|
ownerId: link.collection.ownerId,
|
2023-02-24 11:32:28 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
create: {
|
2023-07-24 08:39:51 -05:00
|
|
|
name: tag.name.trim(),
|
2023-03-28 02:31:50 -05:00
|
|
|
owner: {
|
2023-02-24 11:32:28 -06:00
|
|
|
connect: {
|
2023-03-28 02:31:50 -05:00
|
|
|
id: link.collection.ownerId,
|
2023-02-24 11:32:28 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
},
|
2023-03-10 13:25:33 -06:00
|
|
|
include: { tags: true, collection: true },
|
2023-02-24 11:32:28 -06:00
|
|
|
});
|
|
|
|
|
2023-07-18 10:59:57 -05:00
|
|
|
createFolder({ filePath: `archives/${newLink.collectionId}` });
|
|
|
|
|
2023-06-13 09:30:52 -05:00
|
|
|
return { response: newLink, status: 200 };
|
2023-02-24 11:32:28 -06:00
|
|
|
}
|