el.xwx.moe/lib/api/controllers/links/postLink.ts

212 lines
5.5 KiB
TypeScript
Raw Normal View History

import { prisma } from "@/lib/api/db";
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";
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-12-19 16:20:09 -06:00
const MAX_LINKS_PER_USER = Number(process.env.MAX_LINKS_PER_USER) || 30000;
export default async function postLink(
link: LinkIncludingShortenedCollectionAndTags,
2023-05-27 11:29:39 -05:00
userId: number
) {
2024-03-20 08:56:14 -05:00
if (link.url || link.type === "url") {
try {
new URL(link.url || "");
} catch (error) {
return {
response:
"Please enter a valid Address for the Link. (It should start with http/https)",
status: 400,
};
}
2023-07-24 08:39:51 -05:00
}
2024-02-19 13:37:07 -06:00
if (!link.collection.id && link.collection.name) {
link.collection.name = link.collection.name.trim();
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;
2024-03-17 18:07:51 -05:00
link.collection.ownerId = findCollection.ownerId;
2024-02-19 13:37:07 -06:00
} else {
const collection = await prisma.collection.create({
data: {
name: link.collection.name,
ownerId: userId,
},
});
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,
collectionId: link.collection.id,
2023-11-19 15:22:27 -06:00
});
2023-03-28 02:31:50 -05:00
const memberHasAccess = collectionIsAccessible?.members.some(
(e: UsersAndCollections) => e.userId === userId && e.canCreate
);
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 };
}
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) {
const existingLink = await prisma.link.findFirst({
where: {
url: link.url?.trim(),
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();
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";
}
const newLink = await prisma.link.create({
data: {
2024-03-05 08:03:04 -06:00
url: link.url?.trim(),
2023-07-24 08:39:51 -05:00
name: link.name,
description,
2023-11-25 02:19:02 -06:00
type: linkType,
collection: {
2024-02-19 13:37:07 -06:00
connect: {
id: link.collection.id,
},
},
tags: {
2023-03-28 02:31:50 -05:00
connectOrCreate: link.tags.map((tag) => ({
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,
},
},
create: {
2023-07-24 08:39:51 -05:00
name: tag.name.trim(),
2023-03-28 02:31:50 -05:00
owner: {
connect: {
2023-03-28 02:31:50 -05:00
id: link.collection.ownerId,
},
},
},
})),
},
},
2023-03-10 13:25:33 -06:00
include: { tags: true, collection: true },
});
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 };
}