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.

stashed some changes

Steven Teyd77e37b495ad
diff --git a/lib/inngest.ts b/lib/inngest.ts
index 5613b3d1ee..81104d3f88 100644
--- a/lib/inngest.ts
+++ b/lib/inngest.ts
@@ -1,12 +1,83 @@
import { Inngest } from "inngest";
+import prisma from "#/lib/prisma";
+import { randomBadgeColor } from "@/components/app/links/tag-badge";
export const inngest = new Inngest({ name: "Dub" });
-export const helloWorld = inngest.createFunction(
- { name: "Hello World" },
- { event: "test/hello.world" },
+export const importLinks = inngest.createFunction(
+ { name: "Import Links" },
+ { event: "import-links" },
async ({ event, step }) => {
- await step.sleep("1s");
- return { event, body: "Hello, World!" };
+ const { source, projectId, preserveTags, bitlyGroup, bitlyApiKey } =
+ event.data;
+
+ await step.run(
+ `Importing links from ${source} for project ${projectId}`,
+ async () => {
+ if (source === "bitly") {
+ // while loop, check if pagination.searchAfter is not an empty string, else break
+ // rate limit for /groups/{group_guid}/bitlinks is 1500 per hour or 150 per minute
+ while (true) {
+ // get links from source
+ const data = await fetch(
+ `https://api-ssl.bitly.com/v4/groups/${bitlyGroup}/bitlinks?size=100`,
+ {
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${bitlyApiKey}`,
+ },
+ },
+ ).then((res) => res.json());
+ const { links, pagination } = data;
+ const { searchAfter } = pagination;
+ // convert links to format that can be imported into database
+ const linksToImport = links.map((link) => {
+ const { id, long_url: url, title, archived, tags } = link;
+ const [domain, key] = id.split("/");
+ const tag = tags[0];
+ return {
+ domain,
+ key,
+ url,
+ title,
+ archived,
+ ...(preserveTags && {
+ tag: {
+ connectOrCreate: {
+ where: {
+ name_projectId: {
+ name: tag,
+ projectId,
+ },
+ },
+ create: {
+ name: tag,
+ color: randomBadgeColor(),
+ projectId,
+ },
+ },
+ },
+ }),
+ };
+ });
+ // import links into database
+ await prisma.link.createMany({
+ data: linksToImport,
+ skipDuplicates: true,
+ });
+ await step.sleep(5000);
+ if (searchAfter === "") {
+ break;
+ }
+ }
+ }
+ },
+ );
+ return {
+ event,
+ body: {
+ message: `Done importing links from ${source} for project ${projectId}`,
+ },
+ };
},
);
diff --git a/package.json b/package.json
index 9f02ee8b36..ec9f4c5444 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
"private": true,
"license": "AGPL-3.0-or-later",
"scripts": {
- "dev": "concurrently --kill-others \"next dev\" \"prisma studio --browser none\" \"mailing\" \"inngest-cli dev\"",
+ "dev": "concurrently --kill-others \"next dev\" \"prisma studio --browser none\" \"mailing\"",
"build": "prisma generate & next build",
"format:write": "prettier --write \"**/*.{css,js,json,jsx,ts,tsx}\"",
"format": "prettier \"**/*.{css,js,json,jsx,ts,tsx}\"",
diff --git a/pages/api/inngest.ts b/pages/api/inngest.ts
index fdb8d98182..e7771b70b6 100644
--- a/pages/api/inngest.ts
+++ b/pages/api/inngest.ts
@@ -1,4 +1,4 @@
import { serve } from "inngest/next";
-import { helloWorld, inngest } from "#/lib/inngest";
+import { inngest, importLinks } from "#/lib/inngest";
-export default serve(inngest, [helloWorld]);
+export default serve(inngest, [importLinks]);
diff --git a/pages/api/projects/[slug]/import/bitly.ts b/pages/api/projects/[slug]/import/bitly.ts
index 50bd435d6b..315e9293ed 100644
--- a/pages/api/projects/[slug]/import/bitly.ts
+++ b/pages/api/projects/[slug]/import/bitly.ts
@@ -1,4 +1,18 @@
import { withProjectAuth } from "#/lib/auth";
import { inngest } from "#/lib/inngest";
-export default withProjectAuth(async (req, res, project) => {});
+export default withProjectAuth(async (req, res, project) => {
+ const { bitlyGroup, bitlyApiKey } = req.body;
+
+ await inngest.send({
+ name: "import-links",
+ data: {
+ source: "bitly",
+ projectId: project.id,
+ bitlyGroup,
+ bitlyApiKey,
+ },
+ });
+
+ res.status(200).json({ message: "Importing links from Bitly" });
+});