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.

if there is no title or description provided, try fetching and storing it

Steven Tey54970b3e7d03
diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.ts
index beab130461..40e2824a84 100644
--- a/apps/web/lib/api/links/create-link.ts
+++ b/apps/web/lib/api/links/create-link.ts
@@ -7,7 +7,11 @@ import { formatRedisLink, redis } from "@/lib/upstash";
import { APP_DOMAIN_WITH_NGROK, getParamsFromURL, truncate } from "@dub/utils";
import { Prisma } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
-import { combineTagIds, transformLink } from "./utils";
+import {
+ combineTagIds,
+ fetchMetatagsAndUpdateLink,
+ transformLink,
+} from "./utils";
export async function createLink(link: ProcessedLinkProps) {
let { key, url, expiresAt, title, description, image, proxy, geo } = link;
@@ -112,7 +116,15 @@ export async function createLink(link: ProcessedLinkProps) {
},
}),
]
- : []),
+ : // if there is no title or description provided, try fetching and storing it
+ !proxy && !title && !description
+ ? [
+ fetchMetatagsAndUpdateLink({
+ url,
+ linkId: response.id,
+ }),
+ ]
+ : []),
// delete public links after 30 mins
!response.userId &&
qstash.publishJSON({
diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.ts
index 0ac5ba5331..611e734979 100644
--- a/apps/web/lib/api/links/update-link.ts
+++ b/apps/web/lib/api/links/update-link.ts
@@ -6,7 +6,11 @@ import { formatRedisLink, redis } from "@/lib/upstash";
import { SHORT_DOMAIN, getParamsFromURL, truncate } from "@dub/utils";
import { Prisma } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
-import { combineTagIds, transformLink } from "./utils";
+import {
+ combineTagIds,
+ fetchMetatagsAndUpdateLink,
+ transformLink,
+} from "./utils";
export async function updateLink({
oldDomain = SHORT_DOMAIN,
@@ -132,14 +136,19 @@ export async function updateLink({
// if key is changed: delete the old key in Redis
(changedDomain || changedKey) &&
redis.hdel(oldDomain.toLowerCase(), oldKey.toLowerCase()),
- // only upload image if proxy is true and image is not stored in R2
- proxy &&
- image &&
- !isStored(image) &&
- storage.upload(`images/${id}`, image, {
- width: 1200,
- height: 630,
- }),
+ // if proxy is true and image is not stored in R2, upload image to R2
+ proxy && image && !isStored(image)
+ ? storage.upload(`images/${id}`, image, {
+ width: 1200,
+ height: 630,
+ })
+ : // if there is no title or description provided, try fetching and storing it
+ !proxy && !title && !description
+ ? fetchMetatagsAndUpdateLink({
+ url,
+ linkId: id,
+ })
+ : null,
]),
);
diff --git a/apps/web/lib/api/links/utils.ts b/apps/web/lib/api/links/utils.ts
index 5ad69a4682..4adad61a04 100644
--- a/apps/web/lib/api/links/utils.ts
+++ b/apps/web/lib/api/links/utils.ts
@@ -4,6 +4,7 @@ import {
isReservedUsername,
} from "@/lib/edge-config";
import { checkIfKeyExists } from "@/lib/planetscale";
+import prisma from "@/lib/prisma";
import { WorkspaceProps } from "@/lib/types";
import {
DEFAULT_REDIRECTS,
@@ -11,9 +12,11 @@ import {
isDubDomain,
linkConstructor,
punyEncode,
+ truncate,
validKeyRegex,
} from "@dub/utils";
import { Link, Tag } from "@prisma/client";
+import { getMetaTags } from "app/api/metatags/utils";
export type LinkWithTags = Link & {
tags: { tag: Pick<Tag, "id" | "name" | "color"> }[];
@@ -147,3 +150,24 @@ export const transformLink = (link: LinkWithTags) => {
workspaceId: `ws_${link.projectId}`,
};
};
+
+export async function fetchMetatagsAndUpdateLink({
+ url,
+ linkId,
+}: {
+ url: string;
+ linkId: string;
+}) {
+ const { title, description, image } = await getMetaTags(url);
+ console.log({ title, description, image });
+ await prisma.link.update({
+ where: {
+ id: linkId,
+ },
+ data: {
+ title: truncate(title, 120),
+ description: truncate(description, 240),
+ image: image || null,
+ },
+ });
+}