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.

Add banned links implementation

Steven Teye9129fc67f51
diff --git a/apps/web/app/admin.dub.co/actions.ts b/apps/web/app/admin.dub.co/actions.ts
index 275ed7271f..7ef8737515 100644
--- a/apps/web/app/admin.dub.co/actions.ts
+++ b/apps/web/app/admin.dub.co/actions.ts
@@ -1,9 +1,15 @@
"use server";
-import { deleteLink, deleteUserLinks } from "@/lib/api/links";
+import { reassignUserLinks } from "@/lib/api/links";
import { deleteProject } from "@/lib/api/project";
import { getSession, hashToken } from "@/lib/auth";
import prisma from "@/lib/prisma";
-import { DUB_PROJECT_ID, getDomainWithoutWWW } from "@dub/utils";
+import { redis } from "@/lib/upstash";
+import {
+ DUB_PROJECT_ID,
+ LEGAL_USER_ID,
+ SHORT_DOMAIN,
+ getDomainWithoutWWW,
+} from "@dub/utils";
import { get } from "@vercel/edge-config";
import { randomBytes } from "crypto";
@@ -238,7 +244,7 @@ export async function banUser(data: FormData) {
const blacklistedEmails = (await get("emails")) as string[];
const ban = await Promise.allSettled([
- deleteUserLinks(user.id),
+ reassignUserLinks(user.id),
fetch(
`https://api.vercel.com/v1/edge-config/${process.env.EDGE_CONFIG_ID}/items?teamId=${process.env.TEAM_ID_VERCEL}`,
{
@@ -329,6 +335,7 @@ export async function getLinkByKey(data: FormData) {
export async function deleteAndBlacklistLink(data: FormData) {
const key = data.get("key") as string;
+ const url = data.get("url") as string;
const hostnames = data.getAll("hostname") as string[];
if (!(await isAdmin())) {
@@ -340,7 +347,21 @@ export async function deleteAndBlacklistLink(data: FormData) {
const blacklistedDomains = (await get("domains")) as string[];
const response = await Promise.allSettled([
- deleteLink({ key }),
+ redis.set(`${SHORT_DOMAIN}:${key}`, {
+ url: encodeURIComponent(url),
+ banned: true,
+ }),
+ prisma.link.update({
+ where: {
+ domain_key: {
+ domain: SHORT_DOMAIN,
+ key,
+ },
+ },
+ data: {
+ userId: LEGAL_USER_ID,
+ },
+ }),
fetch(
`https://api.vercel.com/v1/edge-config/${process.env.EDGE_CONFIG_ID}/items?teamId=${process.env.TEAM_ID_VERCEL}`,
{
diff --git a/apps/web/app/admin.dub.co/components/delete-link.tsx b/apps/web/app/admin.dub.co/components/delete-link.tsx
index b281d58d87..e6e4a7b9bf 100644
--- a/apps/web/app/admin.dub.co/components/delete-link.tsx
+++ b/apps/web/app/admin.dub.co/components/delete-link.tsx
@@ -51,9 +51,14 @@ export default function DeleteLink() {
readOnly
className="max-w-fit rounded-md border-gray-300 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
/>
- <div className="w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900">
- {data.url}
- </div>
+ <input
+ type="url"
+ name="url"
+ id={data.url}
+ value={data.url}
+ readOnly
+ className="w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500"
+ />
</div>
<div key={data.domain} className="space-x-2">
<input
diff --git a/apps/web/app/banned/[domain]/[key]/page.tsx b/apps/web/app/banned/[domain]/[key]/page.tsx
new file mode 100644
index 0000000000..c6e7772ad2
--- /dev/null
+++ b/apps/web/app/banned/[domain]/[key]/page.tsx
@@ -0,0 +1,52 @@
+import { getLinkViaEdge } from "@/lib/planetscale";
+import { Background, Footer, Nav } from "@dub/ui";
+import { constructMetadata } from "@dub/utils";
+import { ShieldBan } from "lucide-react";
+import { notFound } from "next/navigation";
+
+export const runtime = "edge";
+
+export const metadata = constructMetadata({
+ title: "Banned Link – Dub.co",
+ description: "This link has been banned for violating our terms of service.",
+ noIndex: true,
+});
+
+export default async function BannedPage({
+ params,
+}: {
+ params: { domain: string; key: string };
+}) {
+ const domain = params.domain;
+ const key = decodeURIComponent(params.key);
+
+ const data = await getLinkViaEdge(domain, key);
+
+ // if the link doesn't exist
+ if (!data) {
+ notFound();
+ }
+
+ return (
+ <main className="flex min-h-screen flex-col justify-between">
+ <Nav />
+ <div className="mx-2 my-10 flex max-w-md flex-col items-center space-y-5 px-2.5 text-center sm:mx-auto sm:max-w-lg sm:px-0 lg:mb-16">
+ <div className="mx-auto flex h-20 w-20 items-center justify-center rounded-full border border-gray-300 bg-white/30">
+ <ShieldBan className="h-6 w-6 text-gray-400" />
+ </div>
+ <h1 className="font-display text-5xl font-bold">Banned Link</h1>
+ <p className="text-lg text-gray-600">
+ This link has been banned for violating our terms of service.
+ </p>
+ <a
+ href="https://dub.co"
+ className="rounded-full bg-gray-800 px-10 py-2 font-medium text-white transition-colors hover:bg-black"
+ >
+ Create Your Free Branded Link
+ </a>
+ </div>
+ <Footer />
+ <Background />
+ </main>
+ );
+}
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 57ce409e4b..1e168719bc 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -11,6 +11,7 @@ import {
DEFAULT_REDIRECTS,
DUB_DOMAINS,
DUB_PROJECT_ID,
+ LEGAL_USER_ID,
SHORT_DOMAIN,
getDomainWithoutWWW,
getParamsFromURL,
@@ -22,7 +23,7 @@ import {
} from "@dub/utils";
import cloudinary from "cloudinary";
import { isIframeable } from "../middleware/utils";
-import { LinkProps, ProjectProps } from "../types";
+import { LinkProps, ProjectProps, SimpleLinkProps } from "../types";
import { Session } from "../auth";
import { qstash } from "../cron";
@@ -792,3 +793,43 @@ export async function deleteUserLinks(userId: string) {
deletePrisma,
};
}
+
+/* Reassign user links to another user when a user is deleted */
+export async function reassignUserLinks(userId: string) {
+ const links = await prisma.link.findMany({
+ where: {
+ userId,
+ domain: SHORT_DOMAIN,
+ },
+ select: {
+ domain: true,
+ key: true,
+ },
+ });
+
+ const redisLinks = await redis.mget<SimpleLinkProps[]>(
+ ...links.map(({ domain, key }) => `${domain}:${key}`),
+ );
+
+ const pipeline = redis.pipeline();
+ redisLinks.forEach((link, index) => {
+ const { domain, key } = links[index];
+ pipeline.set(`${domain}:${key}`, {
+ ...link,
+ banned: true,
+ });
+ });
+
+ await Promise.all([
+ pipeline.exec(),
+ prisma.link.updateMany({
+ where: {
+ userId,
+ domain: SHORT_DOMAIN,
+ },
+ data: {
+ userId: LEGAL_USER_ID,
+ },
+ }),
+ ]);
+}
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 28aa47408d..4a08da43bb 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -52,6 +52,7 @@ export default async function LinkMiddleware(
ios?: string;
android?: string;
geo?: object;
+ banned?: boolean;
}>(
// if inspect mode is enabled, remove the trailing `+` from the key
`${domain}:${inspectMode ? key.slice(0, -1) : key}`,
@@ -67,6 +68,7 @@ export default async function LinkMiddleware(
ios,
android,
geo,
+ banned,
} = response || {};
if (target) {
@@ -95,10 +97,15 @@ export default async function LinkMiddleware(
}
}
- // if the link has expired
- if (expired) {
+ // if the link has expired or is banned
+ if (expired || banned) {
return NextResponse.rewrite(
- new URL(`/expired/${domain}/${encodeURIComponent(key)}`, req.url),
+ new URL(
+ `/${banned ? "banned" : "expired"}/${domain}/${encodeURIComponent(
+ key,
+ )}`,
+ req.url,
+ ),
);
}
diff --git a/apps/web/package.json b/apps/web/package.json
index b7ace537a0..be2e472d71 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -52,7 +52,7 @@
"he": "^1.2.0",
"html-escaper": "^3.0.3",
"js-cookie": "^3.0.5",
- "lucide-react": "^0.269.0",
+ "lucide-react": "^0.299.0",
"nanoid": "^5.0.1",
"next": "14.0.4-canary.39",
"next-auth": "^4.24.4",
diff --git a/apps/web/ui/links/link-card.tsx b/apps/web/ui/links/link-card.tsx
index 8466f2e65a..e40087697f 100644
--- a/apps/web/ui/links/link-card.tsx
+++ b/apps/web/ui/links/link-card.tsx
@@ -234,7 +234,7 @@ export default function LinkCard({
content={
archived
? "This link is archived. It will still work, but won't be shown in your dashboard."
- : "This link has expired. It will still show up in your dashboard, but users will get an 'Expired' page when they click on it."
+ : "This link has expired. It will still show up in your dashboard, but users will get an 'Expired Link' page when they click on it."
}
>
<div className="flex h-8 w-8 items-center justify-center rounded-full bg-gray-300 px-0 sm:h-10 sm:w-10">
diff --git a/packages/utils/src/constants/index.ts b/packages/utils/src/constants/index.ts
index c8fa8f1480..d771f5de06 100644
--- a/packages/utils/src/constants/index.ts
+++ b/packages/utils/src/constants/index.ts
@@ -222,6 +222,7 @@ export const DEFAULT_LINK_PROPS = {
};
export const DUB_PROJECT_ID = "cl7pj5kq4006835rbjlt2ofka";
+export const LEGAL_USER_ID = "clqei1lgc0000vsnzi01pbf47";
export const SAML_PROVIDERS = [
{
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 60f21ba742..f8dc219610 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -155,8 +155,8 @@ importers:
specifier: ^3.0.5
version: 3.0.5
lucide-react:
- specifier: ^0.269.0
- version: 0.269.0([email protected])
+ specifier: ^0.299.0
+ version: 0.299.0([email protected])
nanoid:
specifier: ^5.0.1
version: 5.0.1
@@ -12399,6 +12399,14 @@ packages:
react: 18.2.0
dev: false
+ resolution: {integrity: sha512-59MiDzDzFI/efHhb4n0vGdXelMNwou7JlAFvVS4boA1G/7aYU7garPciYo73CODzkhrhz0JOgdtSTPSe5dMrlQ==}
+ peerDependencies:
+ react: ^16.5.1 || ^17.0.0 || ^18.0.0
+ dependencies:
+ react: 18.2.0
+ dev: false
+
resolution: {integrity: sha512-Q/TKtsC5BPm0kGqgBIF9oXAs/xEf2vRKiIB4wCRQTJOQIByZ1d+NnUOotvJOvNpi5RNIgVOMC3pOuaP1ZTDlVg==}
engines: {node: '>=12'}