Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Merge branch 'main' into tim/multiple-tags-cleanup

Tim Wilson91dab6933570
diff --cc apps/web/app/api/links/[linkId]/route.ts
index dce88c747b,1b9f9a1506..fdcbce6efa
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@@ -95,10 -88,10 +94,10 @@@ export const PUT = withAuth(async ({ re
const [response, _] = await Promise.allSettled([
editLink({
- // link is guaranteed to exist because if not we will return 404
- domain: link!.domain,
- key: link!.key,
+ oldDomain: link.domain,
+ oldKey: link.key,
+ oldImage: link.image || undefined,
- updatedLink: processedLink as any, // TODO: fix types
+ updatedLink: processedLink,
}),
qstash.publishJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/event`,
diff --cc apps/web/lib/api/links.ts
index d6609096a6,bc920766b4..5fb3098a61
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@@ -469,39 -435,13 +469,38 @@@ export async function processLink<T ext
code: "unprocessable_entity",
};
}
+ } else if (tagNames && tagNames.length > 0) {
+ const tags = await prisma.tag.findMany({
+ select: {
+ name: true,
+ },
+ where: {
+ projectId: project?.id,
+ name: { in: tagNames },
+ },
+ });
+
+ if (tags.length !== tagNames.length) {
+ return {
+ link: payload,
+ error:
+ "Invalid tagNames detected: " +
+ tagNames
+ .filter(
+ (tagName) =>
+ tags.find(({ name }) => tagName === name) === undefined,
+ )
+ .join(", "),
+ status: 422,
+ };
+ }
}
- // custom social media image checks
- const uploadedImage = image && image.startsWith("data:image") ? true : false;
- if (uploadedImage && !process.env.CLOUDINARY_URL) {
+ // custom social media image checks (see if R2 is configured)
+ if (proxy && !process.env.STORAGE_SECRET_ACCESS_KEY) {
return {
link: payload,
- error: "Missing Cloudinary environment variable.",
+ error: "Missing storage access key.",
code: "bad_request",
};
}
@@@ -559,10 -502,8 +558,8 @@@ export function combineTagIds(
return tagId ? [tagId] : [];
}
-export async function addLink(link: LinkWithTagIdsProps) {
+export async function addLink(link: ProcessedLinkProps) {
- let { domain, key, url, expiresAt, title, description, image, proxy, geo } =
- link;
- const uploadedImage = image && image.startsWith("data:image") ? true : false;
+ let { key, url, expiresAt, title, description, image, proxy, geo } = link;
const combinedTagIds = combineTagIds(link);
@@@ -603,9 -535,42 +591,30 @@@
},
}),
},
- include: {
- tags: {
- select: {
- tag: {
- select: {
- id: true,
- name: true,
- color: true,
- },
- },
- },
- },
- },
+ include: includeTags,
});
+ const uploadedImageUrl = `${process.env.STORAGE_BASE_URL}/images/${response.id}`;
+
+ if (proxy && image && !isStored(image)) {
+ await Promise.all([
+ // upload image to R2
+ storage.upload(`images/${response.id}`, image, {
+ width: 1200,
+ height: 630,
+ }),
+ // update the null image we set earlier to the uploaded image URL
+ prisma.link.update({
+ where: {
+ id: response.id,
+ },
+ data: {
+ image: uploadedImageUrl,
+ },
+ }),
+ ]);
+ }
+
const shortLink = linkConstructor({
domain: response.domain,
key: response.key,
@@@ -740,14 -706,15 +752,16 @@@ export async function propagateBulkLink
}
export async function editLink({
- domain: oldDomain = SHORT_DOMAIN,
- key: oldKey,
+ oldDomain = SHORT_DOMAIN,
+ oldKey,
+ oldImage,
updatedLink,
}: {
- domain?: string;
- key: string;
+ oldDomain?: string;
+ oldKey: string;
+ oldImage?: string;
- updatedLink: LinkWithTagIdsProps;
+ updatedLink: ProcessedLinkProps &
+ Pick<LinkProps, "id" | "clicks" | "lastClicked" | "updatedAt">;
}) {
let {
id,
@@@ -830,22 -791,23 +839,10 @@@
})),
},
},
- include: {
- tags: {
- select: {
- tag: {
- select: {
- id: true,
- name: true,
- color: true,
- },
- },
- },
- },
- },
}),
- // if key is changed: rename resource in Cloudinary, delete the old key in Redis and change the clicks key name
- ...(changedDomain || changedKey
- ? [
- ...(process.env.CLOUDINARY_URL
- ? [
- cloudinary.v2.uploader
- .destroy(`${oldDomain}/${oldKey}`, {
- invalidate: true,
- })
- .catch(() => {}),
- ]
- : []),
- redis.hdel(oldDomain, oldKey.toLowerCase()),
- ]
- : []),
+ // if key is changed: delete the old key in Redis
+ (changedDomain || changedKey) &&
+ redis.hdel(oldDomain, oldKey.toLowerCase()),
]);
const shortLink = linkConstructor({