The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Merge branch 'main' into tim/multiple-tags-cleanup
Tim Wilsondff1c09b2152
diff --cc apps/web/app/api/cron/import/bitly/utils.tsindex ad5638e98a,947ff920d7..38d34cb475--- a/apps/web/app/api/cron/import/bitly/utils.ts+++ b/apps/web/app/api/cron/import/bitly/utils.ts@@@ -162,8 -162,8 +162,8 @@@ export const importLinksFromBitly = asy// delete tags that have no linksprisma.tag.deleteMany({where: {- projectId,+ projectId: workspaceId,- linksNew: {+ links: {none: {},},},diff --cc apps/web/app/api/cron/import/csv/utils.tsindex bfeddf7de4,da340dcdb2..99555380bb--- a/apps/web/app/api/cron/import/csv/utils.ts+++ b/apps/web/app/api/cron/import/csv/utils.ts@@@ -1,7 -1,7 +1,7 @@@import { bulkCreateLinks, processLink } from "@/lib/api/links";import { qstash } from "@/lib/cron";import prisma from "@/lib/prisma";- import { NewLinkProps, ProcessedLinkProps, ProjectProps } from "@/lib/types";-import { LinkWithTagIdsProps, WorkspaceProps } from "@/lib/types";++import { NewLinkProps, WorkspaceProps } from "@/lib/types";import { redis } from "@/lib/upstash";import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";import { sendEmail } from "emails";@@@ -19,15 -19,19 +19,15 @@@ export const importLinksFromCSV = asyncount?: number;}) => {const [links, totalCount] = await Promise.all([- redis.lrange<NewLinkProps>(`import:csv:${projectId}`, count, count + 100),- redis.llen(`import:csv:${projectId}`),- redis.lrange<LinkWithTagIdsProps>(- `import:csv:${workspaceId}`,- count,- count + 100,- ),++ redis.lrange<NewLinkProps>(`import:csv:${workspaceId}`, count, count + 100),+ redis.llen(`import:csv:${workspaceId}`),]);- const project = (await prisma.project.findUnique({+ const workspace = (await prisma.project.findUnique({where: {- id: projectId,+ id: workspaceId,},- })) as unknown as ProjectProps;+ })) as unknown as WorkspaceProps;const processedLinks = await Promise.all(links.map(async (link) =>diff --cc apps/web/app/api/cron/import/rebrandly/utils.tsindex 4d73967e11,dca813dac0..f1903dd49f--- a/apps/web/app/api/cron/import/rebrandly/utils.ts+++ b/apps/web/app/api/cron/import/rebrandly/utils.ts@@@ -138,8 -138,8 +138,8 @@@ export const importLinksFromRebrandly// delete tags that have no linksprisma.tag.deleteMany({where: {- projectId,+ projectId: workspaceId,- linksNew: {+ links: {none: {},},},diff --cc apps/web/app/api/links/[linkId]/route.tsindex fdcbce6efa,bf0fa43542..9b20154e81--- a/apps/web/app/api/links/[linkId]/route.ts+++ b/apps/web/app/api/links/[linkId]/route.ts@@@ -77,8 -72,8 +78,8 @@@ export const PUT = withAuth(async ({ reerror,code,} = await processLink({- payload: updatedLink as LinkWithTagIdsProps,+ payload: updatedLink,- project,+ workspace,// if domain and key are the same, we don't need to check if the key existsskipKeyChecks:link.domain === updatedLink.domain &&diff --cc apps/web/app/api/links/bulk/route.tsindex 36a6f015fe,d5b5621c13..9ae487b84e--- a/apps/web/app/api/links/bulk/route.ts+++ b/apps/web/app/api/links/bulk/route.ts@@@ -51,8 -51,8 +51,8 @@@ export const POST = withAuthconst processedLinks = await Promise.all(links.map(async (link) =>processLink({- payload: link as LinkWithTagIdsProps,+ payload: link,- project,+ workspace,userId: session.user.id,bulk: true,}),diff --cc apps/web/app/api/links/route.tsindex 9881ee8612,fdbc3ba37a..79a2834e6d--- a/apps/web/app/api/links/route.ts+++ b/apps/web/app/api/links/route.ts@@@ -1,7 -1,8 +1,7 @@@import { DubApiError, ErrorCodes } from "@/lib/api/errors";- import { addLink, getLinksForProject, processLink } from "@/lib/api/links";+ import { addLink, getLinksForWorkspace, processLink } from "@/lib/api/links";import { withAuth } from "@/lib/auth";import { qstash } from "@/lib/cron";-import { LinkWithTagIdsProps } from "@/lib/types";import { ratelimit } from "@/lib/upstash";import {createLinkBodySchema,@@@ -71,8 -71,8 +72,8 @@@ export const POST = withAuth}const { link, error, code } = await processLink({- payload: body as LinkWithTagIdsProps,+ payload: body,- project,+ workspace,...(session && { userId: session.user.id }),});diff --cc apps/web/app/api/tags/[id]/route.tsindex 9f302325ba,68689002da..91fb39cf0d--- a/apps/web/app/api/tags/[id]/route.ts+++ b/apps/web/app/api/tags/[id]/route.ts@@@ -53,15 -54,23 +54,23 @@@ export const DELETE = withAuth(async (const response = await prisma.tag.delete({where: {id,+ projectId: workspace.id,},include: {- linksNew: true,+ links: true,},});+ if (!response) {+ throw new DubApiError({+ code: "not_found",+ message: "Tag not found.",+ });+ }+// update links metadata in tinybird after deleting a tagawait Promise.all(- response.linksNew.map(async ({ linkId }) => {+ response.links.map(async ({ linkId }) => {const link = await prisma.link.findUnique({where: {id: linkId,diff --cc apps/web/lib/api/links.tsindex 5fb3098a61,2714061db3..553b1adc03--- a/apps/web/lib/api/links.ts+++ b/apps/web/lib/api/links.ts@@@ -28,29 -28,14 +28,29 @@@ import { checkIfKeyExists, getRandomKeimport { recordLink } from "../tinybird";import {LinkProps,- LinkWithTagIdsProps,+ NewLinkProps,+ ProcessedLinkProps,- ProjectProps,RedisLinkProps,+ WorkspaceProps,} from "../types";import z from "../zod";-export async function getLinksForWorkspace({+const includeTags = {+ tags: {+ include: {+ tag: {+ select: {+ id: true,+ name: true,+ color: true,+ },+ },+ },+ },+};++export async function getLinksForProject({- projectId,+ workspaceId,domain,tagId,tagIds,@@@ -240,22 -221,17 +241,22 @@@ export async function keyChecks(}if (isDubDomain(domain) && process.env.NEXT_PUBLIC_IS_DUB) {- if (- domain === SHORT_DOMAIN &&- (DEFAULT_REDIRECTS[key] || (await isReservedKey(key)))- ) {- return {- error: "Duplicate key: This short link already exists.",- code: "conflict",- };+ if (domain === SHORT_DOMAIN) {+ if (DEFAULT_REDIRECTS[key] || (await isReservedKey(key))) {+ return {+ error: "Duplicate key: This short link already exists.",+ code: "conflict",+ };+ }+ if (await isBlacklistedKey(key)) {+ return {+ error: "Invalid key.",+ code: "unprocessable_entity",+ };+ }}- if (key.length <= 3 && (!project || project.plan === "free")) {+ if (key.length <= 3 && (!workspace || workspace.plan === "free")) {return {error: `You can only use keys that are 3 characters or less on a Pro plan and above. Upgrade to Pro to register a ${key.length}-character key.`,code: "forbidden",@@@ -292,15 -268,15 +293,15 @@@ export function processKey(key: stringreturn key;}-export async function processLink({+export async function processLink<T extends Record<string, any>>({payload,- project,+ workspace,userId,bulk = false,skipKeyChecks = false, // only skip when key doesn't change (e.g. when editing a link)}: {- payload: LinkWithTagIdsProps;+ payload: NewLinkProps & T;- project?: ProjectProps;+ workspace?: WorkspaceProps;userId?: string;bulk?: boolean;skipKeyChecks?: boolean;@@@ -450,10 -420,7 +451,10 @@@select: {id: true,},- where: { projectId: workspace?.id, id: { in: tagIds } },+ where: {- projectId: project?.id,++ projectId: workspace?.id,+ id: { in: tagIds },+ },});if (tags.length !== tagIds.length) {@@@ -469,31 -436,6 +470,31 @@@code: "unprocessable_entity",};}+ } else if (tagNames && tagNames.length > 0) {+ const tags = await prisma.tag.findMany({+ select: {+ name: true,+ },+ where: {- projectId: project?.id,++ projectId: workspace?.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 (see if R2 is configured)@@@ -536,9 -478,12 +537,9 @@@domain,key,url: processedUrl,- // make sure projectId is set to the current workspace+ userId: userId || null,+ // make sure projectId is set to the current project- projectId: project?.id || null,+ projectId: workspace?.id || null,- // if userId is passed, set it (we don't change the userId if it's already set, e.g. when editing a link)- ...(userId && {- userId,- }),},error: null,};diff --cc apps/web/lib/zod/schemas/links.tsindex eaaa2127c7,b3a52fcb20..fbda47d646--- a/apps/web/lib/zod/schemas/links.ts+++ b/apps/web/lib/zod/schemas/links.ts@@@ -1,14 -1,8 +1,9 @@@import z from "@/lib/zod";import { booleanQuerySchema } from ".";import { TagSchema } from "./tags";+import { getRandomKey } from "@/lib/planetscale";- const LinksQuerySchema = {- projectSlug: z- .string()- .describe(- "The slug for the project that the link belongs to. E.g. for `app.dub.co/acme`, the projectSlug is `acme`.",- ),+ const LinksQuerySchema = z.object({domain: z.string().optional()