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 redis

Kiran Ka241b973b790
diff --cc apps/web/app/api/domains/[domain]/route.ts
index bf08451ff4,df1932e193..baab433d76
--- a/apps/web/app/api/domains/[domain]/route.ts
+++ b/apps/web/app/api/domains/[domain]/route.ts
@@@ -10,6 -9,9 +10,7 @@@ import { DubApiError } from "@/lib/api/
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
+ import { storage } from "@/lib/storage";
-import { recordLink } from "@/lib/tinybird";
-import { redis } from "@/lib/upstash";
import {
DomainSchema,
updateDomainBodySchema,
@@@ -102,22 -126,46 +125,29 @@@ export const PATCH = withWorkspace
},
});
- if (domainUpdated) {
- waitUntil(
- Promise.all([
- // remove old domain from Vercel
- removeDomainFromVercel(domain),
-
- // trigger the queue to rename the redis keys and update the links in Tinybird
- queueDomainUpdate({
- workspaceId: workspace.id,
- oldDomain: domain,
- newDomain: newDomain,
- page: 1,
- }),
- ]),
- );
- }
+ waitUntil(
+ (async () => {
+ // remove old logo
+ if (oldLogo && (logo === null || logoUploaded)) {
+ await storage.delete(oldLogo.replace(`${R2_URL}/`, ""));
+ }
+
+ if (domainUpdated) {
+ await Promise.all([
+ // remove old domain from Vercel
+ removeDomainFromVercel(domain),
- // rename redis key
- redis.rename(domain.toLowerCase(), newDomain.toLowerCase()),
- ]);
+
- const allLinks = await prisma.link.findMany({
- where: {
- domain: newDomain,
- },
- include: {
- tags: true,
- },
- });
-
- // update all links in Tinybird
- recordLink(
- allLinks.map((link) => ({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: link.tags.map((tag) => tag.tagId),
- program_id: link.programId ?? "",
- workspace_id: link.projectId,
- created_at: link.createdAt,
- })),
- );
++ // trigger the queue to rename the redis keys and update the links in Tinybird
++ queueDomainUpdate({
++ workspaceId: workspace.id,
++ oldDomain: domain,
++ newDomain: newDomain,
++ page: 1,
++ }),
++ ]);
+ }
+ })(),
+ );
return NextResponse.json(DomainSchema.parse(domainRecord));
},
diff --cc apps/web/lib/api/domains/delete-domain-links.ts
index 5041c4b7ed,ec9f70cd18..3e100dbacd
--- a/apps/web/lib/api/domains/delete-domain-links.ts
+++ b/apps/web/lib/api/domains/delete-domain-links.ts
@@@ -1,121 -1,15 +1,125 @@@
import { prisma } from "@/lib/prisma";
-import { redis } from "@/lib/upstash";
import { R2_URL } from "@dub/utils";
-import { waitUntil } from "@vercel/functions";
import { storage } from "../../storage";
import { recordLink } from "../../tinybird";
+import { linkCache } from "../links/cache";
+import { queueDomainDeletion } from "./queue";
import { removeDomainFromVercel } from "./remove-domain-vercel";
-/* Delete a domain and all links & images associated with it */
-export async function deleteDomainAndLinks(domain: string) {
- const [domainData, allLinks] = await Promise.all([
- prisma.domain.findUnique({
+// Delete a domain and all links & images associated with it
+export async function deleteDomainAndLinks({
+ domain,
+ workspaceId,
+}: {
+ domain: string;
+ workspaceId: string;
+}) {
+ const links = await prisma.link.findMany({
+ where: {
+ domain,
+ },
+ include: {
+ tags: true,
+ },
+ take: 100, // TODO: We can adjust this number based on the performance
+ });
+
+ if (links.length === 0) {
+ return;
+ }
+
+ const response = await Promise.allSettled([
+ // Record link in the Tinybird
+ recordLink(
+ links.map((link) => ({
+ link_id: link.id,
+ domain: link.domain,
+ key: link.key,
+ url: link.url,
+ tag_ids: link.tags.map((tag) => tag.id),
+ workspace_id: workspaceId,
+ program_id: link.programId ?? "",
+ created_at: link.createdAt,
+ deleted: true,
+ })),
+ ),
+
+ // Remove image from R2 storage if it exists
+ links
+ .filter((link) => link.image?.startsWith(`${R2_URL}/images/${link.id}`))
+ .map((link) => storage.delete(link.image!.replace(`${R2_URL}/`, ""))),
+
+ // Remove the link from Redis
+ linkCache.deleteMany(links),
+
+ // Remove the link from MySQL
+ prisma.link.deleteMany({
+ where: {
+ id: { in: links.map((link) => link.id) },
+ },
+ }),
+ ]);
+
+ response.forEach((promise) => {
+ if (promise.status === "rejected") {
+ console.error("deleteDomainAndLinks", {
+ reason: promise.reason,
+ domain,
+ workspaceId,
+ });
+ }
+ });
+
+ const remainingLinks = await prisma.link.count({
+ where: {
+ domain,
+ },
+ });
+
+ if (remainingLinks > 0) {
+ return await queueDomainDeletion({
+ workspaceId,
+ domain,
+ delay: 2,
+ });
+ }
+
- // After all links are deleted, delete the domain
- await prisma.domain.delete({
++ // After all links are deleted, delete the domain and image
++ const domainRecord = await prisma.domain.delete({
+ where: {
+ slug: domain,
+ },
+ });
++
++ if (domainRecord.logo) {
++ await storage.delete(domainRecord.logo.replace(`${R2_URL}/`, ""));
++ }
+}
+
+// Mark the domain as deleted
+// We'll delete the domain and its links via a cron job
+export async function markDomainAsDeleted({
+ domain,
+ workspaceId,
+ delay,
+}: {
+ domain: string;
+ workspaceId: string;
+ delay?: number; // delay the cron job to avoid hitting rate limits
+}) {
+ const links = await prisma.link.updateMany({
+ where: {
+ domain,
+ },
+ data: {
+ projectId: null,
+ },
+ });
+
+ const response = await Promise.allSettled([
+ removeDomainFromVercel(domain),
+
+ prisma.domain.update({
where: {
slug: domain,
},