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.

improve bitly import to include custom bitlinks

Steven Tey4a61df3e6b01
diff --git a/apps/web/app/api/cron/import/bitly/utils.ts b/apps/web/app/api/cron/import/bitly/utils.ts
index 10eb0c6ee4..6dc40c7922 100644
--- a/apps/web/app/api/cron/import/bitly/utils.ts
+++ b/apps/web/app/api/cron/import/bitly/utils.ts
@@ -42,19 +42,31 @@ export const importLinksFromBitly = async ({
const pipeline = redis.pipeline();
// convert links to format that can be imported into database
- const importedLinks = links
- .map(({ id, long_url: url, title, archived, created_at, tags }) => {
+ const importedLinks = links.flatMap(
+ ({
+ id,
+ long_url: url,
+ title,
+ archived,
+ created_at,
+ custom_bitlinks,
+ tags,
+ }) => {
const [domain, key] = id.split("/");
// if domain is not in project domains, skip (could be a bit.ly link or old short domain)
if (!domains.includes(domain)) {
- return null;
+ return [];
}
- pipeline.set(`${domain}:${key}`, {
- url: encodeURIComponent(url),
- });
+ pipeline.set(
+ `${domain}:${key}`,
+ {
+ url: encodeURIComponent(url),
+ },
+ { nx: true },
+ );
const createdAt = new Date(created_at).toISOString();
const tagId = tagsToId ? tagsToId[tags[0]] : null;
- return {
+ const linkDetails = {
projectId,
userId,
domain,
@@ -65,8 +77,39 @@ export const importLinksFromBitly = async ({
createdAt,
...(tagId ? { tagId } : {}),
};
- })
- .filter(Boolean);
+
+ return [
+ linkDetails,
+ // if link has custom bitlinks, add them to the list of links to import
+ ...(custom_bitlinks
+ ?.filter((customBitlink: string) => {
+ const customDomain = new URL(customBitlink).hostname;
+ // only import custom bitlinks that have the same domain as the domains
+ // that were previously imported into the project from bitly
+ return domains.includes(customDomain);
+ })
+ .map((customBitlink: string) => {
+ // here we are getting the customDomain again just in case
+ // the custom bitlink doesn't have the same domain as the
+ // original bitlink, but it should
+ const customDomain = new URL(customBitlink).hostname;
+ const customKey = new URL(customBitlink).pathname.slice(1);
+ pipeline.set(
+ `${customDomain}:${customKey}`,
+ {
+ url: encodeURIComponent(url),
+ },
+ { nx: true },
+ );
+ return {
+ ...linkDetails,
+ domain: customDomain,
+ key: customKey,
+ };
+ }) ?? []),
+ ];
+ },
+ );
// import links into database
await Promise.all([