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 admin experience

Steven Tey4870105d5785
diff --git a/apps/web/app/admin.dub.co/actions.ts b/apps/web/app/admin.dub.co/actions.ts
index f79c92653d..23967df5fb 100644
--- a/apps/web/app/admin.dub.co/actions.ts
+++ b/apps/web/app/admin.dub.co/actions.ts
@@ -3,15 +3,10 @@
import { deleteProject } from "@/lib/api/projects";
import { getSession, hashToken } from "@/lib/auth";
import prisma from "@/lib/prisma";
-import {
- DUB_PROJECT_ID,
- LEGAL_USER_ID,
- SHORT_DOMAIN,
- getDomainWithoutWWW,
-} from "@dub/utils";
-import { DUB_DOMAINS, LEGAL_PROJECT_ID } from "@dub/utils/dist/constants";
-import { get } from "@vercel/edge-config";
+import { DUB_PROJECT_ID, getDomainWithoutWWW } from "@dub/utils";
import { randomBytes } from "crypto";
+import cloudinary from "cloudinary";
+import { get } from "@vercel/edge-config";
export async function isAdmin() {
const session = await getSession();
@@ -202,7 +197,6 @@ export async function getUserByKey(data: FormData) {
export async function banUser(data: FormData) {
const email = data.get("email") as string;
- const hostnames = data.getAll("hostname") as string[];
if (!(await isAdmin())) {
return {
@@ -216,6 +210,7 @@ export async function banUser(data: FormData) {
},
select: {
id: true,
+ email: true,
projects: {
where: {
role: "owner",
@@ -240,47 +235,10 @@ export async function banUser(data: FormData) {
};
}
- const blacklistedDomains = (await get("domains")) as string[];
const blacklistedEmails = (await get("emails")) as string[];
- const ban = await Promise.allSettled([
- prisma.link.updateMany({
- where: {
- userId: user.id,
- domain: {
- in: DUB_DOMAINS.map((domain) => domain.slug),
- },
- },
- data: {
- userId: LEGAL_USER_ID,
- projectId: LEGAL_PROJECT_ID,
- },
- }),
- fetch(
- `https://api.vercel.com/v1/edge-config/${process.env.EDGE_CONFIG_ID}/items?teamId=${process.env.TEAM_ID_VERCEL}`,
- {
- method: "PATCH",
- headers: {
- Authorization: `Bearer ${process.env.AUTH_BEARER_TOKEN}`,
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- items: [
- {
- operation: "update",
- key: "domains",
- value: [...blacklistedDomains, ...hostnames],
- },
- {
- operation: "update",
- key: "emails",
- value: [...blacklistedEmails, email],
- },
- ],
- }),
- },
- ).then((res) => res.json()),
- ...user.projects.map(({ project }) =>
+ await Promise.allSettled(
+ user.projects.map(({ project }) =>
deleteProject({
id: project.id,
slug: project.slug,
@@ -288,88 +246,27 @@ export async function banUser(data: FormData) {
logo: project.logo || undefined,
}),
),
- ]);
-
- const response = await prisma.user.delete({
- where: {
- id: user.id,
- },
- });
-
- console.log(
- JSON.stringify(
- {
- ban,
- response,
- },
- null,
- 2,
- ),
);
- return true;
-}
-
-export async function getLinkByKey(data: FormData) {
- const key = data.get("key") as string;
-
- if (!(await isAdmin())) {
- return {
- error: "Unauthorized",
- };
- }
-
- const link = await prisma.link.findUnique({
- where: {
- domain_key: {
- domain: "dub.sh",
- key,
- },
- },
- select: {
- key: true,
- url: true,
- },
- });
- if (!link) {
- return {
- error: "No link found",
- };
- }
-
- return {
- key: link?.key as string,
- url: link?.url as string,
- domain: getDomainWithoutWWW(link?.url as string),
- };
-}
-
-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())) {
- return {
- error: "Unauthorized",
- };
- }
-
- const blacklistedDomains = (await get("domains")) as string[];
-
- const response = await Promise.allSettled([
- prisma.link.update({
+ await Promise.allSettled([
+ prisma.user.delete({
where: {
- domain_key: {
- domain: SHORT_DOMAIN,
- key,
- },
- },
- data: {
- userId: LEGAL_USER_ID,
- projectId: LEGAL_PROJECT_ID,
+ id: user.id,
},
}),
+ cloudinary.v2.uploader.destroy(`avatars/${user.id}`, {
+ invalidate: true,
+ }),
+ fetch(
+ `https://api.resend.com/audiences/${process.env.RESEND_AUDIENCE_ID}/contacts/${user.email}`,
+ {
+ method: "DELETE",
+ headers: {
+ Authorization: `Bearer ${process.env.RESEND_API_KEY}`,
+ "Content-Type": "application/json",
+ },
+ },
+ ),
fetch(
`https://api.vercel.com/v1/edge-config/${process.env.EDGE_CONFIG_ID}/items?teamId=${process.env.TEAM_ID_VERCEL}`,
{
@@ -382,8 +279,8 @@ export async function deleteAndBlacklistLink(data: FormData) {
items: [
{
operation: "update",
- key: "domains",
- value: [...blacklistedDomains, ...hostnames],
+ key: "emails",
+ value: [...blacklistedEmails, email],
},
],
}),
@@ -391,15 +288,5 @@ export async function deleteAndBlacklistLink(data: FormData) {
),
]);
- console.log(
- JSON.stringify(
- {
- response,
- },
- null,
- 2,
- ),
- );
-
return true;
}
diff --git a/apps/web/app/admin.dub.co/page.tsx b/apps/web/app/admin.dub.co/page.tsx
index 096fd3aa0c..c0c6dbb1e9 100644
--- a/apps/web/app/admin.dub.co/page.tsx
+++ b/apps/web/app/admin.dub.co/page.tsx
@@ -31,13 +31,6 @@ export default function AdminPage() {
</p>
<BanUser />
</div>
- <div className="flex flex-col space-y-4 px-5 py-10">
- <h2 className="text-xl font-semibold">Delete Anonymous Links</h2>
- <p className="text-sm text-gray-500">
- Delete anonymous dub.sh links and blacklists the domain.
- </p>
- <DeleteLink />
- </div>
</div>
);
}
diff --git a/apps/web/app/api/admin/links/[linkId]/ban/route.ts b/apps/web/app/api/admin/links/[linkId]/ban/route.ts
new file mode 100644
index 0000000000..333130091a
--- /dev/null
+++ b/apps/web/app/api/admin/links/[linkId]/ban/route.ts
@@ -0,0 +1,66 @@
+import { withAdmin } from "@/lib/auth";
+import { NextResponse } from "next/server";
+import prisma from "@/lib/prisma";
+import {
+ LEGAL_PROJECT_ID,
+ LEGAL_USER_ID,
+ getDomainWithoutWWW,
+} from "@dub/utils";
+import { formatRedisLink, redis } from "@/lib/upstash";
+import { get } from "@vercel/edge-config";
+
+// DELETE /api/admin/links/[linkId]/ban – ban a link
+export const DELETE = withAdmin(async ({ params }) => {
+ const { linkId } = params as { linkId: string };
+
+ const link = await prisma.link.findUnique({
+ where: {
+ id: linkId,
+ },
+ });
+
+ if (!link) {
+ return NextResponse.next();
+ }
+
+ const blacklistedDomains = (await get("domains")) as string[];
+
+ const response = await Promise.all([
+ prisma.link.update({
+ where: {
+ id: linkId,
+ },
+ data: {
+ userId: LEGAL_USER_ID,
+ projectId: LEGAL_PROJECT_ID,
+ },
+ }),
+ redis.hset(link.domain, {
+ [link.key.toLowerCase()]: {
+ ...(await formatRedisLink(link)),
+ projectId: LEGAL_PROJECT_ID,
+ },
+ }),
+ fetch(
+ `https://api.vercel.com/v1/edge-config/${process.env.EDGE_CONFIG_ID}/items?teamId=${process.env.TEAM_ID_VERCEL}`,
+ {
+ method: "PATCH",
+ headers: {
+ Authorization: `Bearer ${process.env.AUTH_BEARER_TOKEN}`,
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ items: [
+ {
+ operation: "update",
+ key: "domains",
+ value: [...blacklistedDomains, getDomainWithoutWWW(link.url)],
+ },
+ ],
+ }),
+ },
+ ),
+ ]);
+
+ return NextResponse.json(response);
+});
diff --git a/apps/web/app/api/admin/links/route.ts b/apps/web/app/api/admin/links/route.ts
index 20341b931e..9d9612433d 100644
--- a/apps/web/app/api/admin/links/route.ts
+++ b/apps/web/app/api/admin/links/route.ts
@@ -39,9 +39,16 @@ export const GET = withAdmin(async ({ searchParams }) => {
},
],
}),
- userId: {
- not: LEGAL_USER_ID,
- },
+ OR: [
+ {
+ userId: {
+ not: LEGAL_USER_ID,
+ },
+ },
+ {
+ userId: null,
+ },
+ ],
},
include: {
user: true,
diff --git a/apps/web/ui/links/link-card.tsx b/apps/web/ui/links/link-card.tsx
index a96f6e17d6..fc30d4e063 100644
--- a/apps/web/ui/links/link-card.tsx
+++ b/apps/web/ui/links/link-card.tsx
@@ -46,7 +46,8 @@ import { useParams } from "next/navigation";
import Linkify from "linkify-react";
import punycode from "punycode/";
import { useEffect, useMemo, useRef, useState } from "react";
-import useSWR from "swr";
+import useSWR, { mutate } from "swr";
+import { toast } from "sonner";
export default function LinkCard({
props,
@@ -56,6 +57,7 @@ export default function LinkCard({
};
}) {
const {
+ id,
key,
domain,
url,
@@ -482,6 +484,43 @@ export default function LinkCard({
X
</kbd>
</button>
+ {!slug && ( // this is only shown in admin mode (where there's no slug)
+ <button
+ onClick={() => {
+ window.confirm(
+ "Are you sure you want to ban this link? It will blacklist the domain and prevent any links from that domain from being created.",
+ ) &&
+ (setOpenPopover(false),
+ toast.promise(
+ fetch(`/api/admin/links/${id}/ban`, {
+ method: "DELETE",
+ }).then(async () => {
+ await mutate(
+ (key) =>
+ typeof key === "string" &&
+ key.startsWith("/api/admin/links"),
+ undefined,
+ { revalidate: true },
+ );
+ }),
+ {
+ loading: "Banning link...",
+ success: "Link banned!",
+ error: "Error banning link.",
+ },
+ ));
+ }}
+ className="group flex w-full items-center justify-between rounded-md p-2 text-left text-sm font-medium text-red-600 transition-all duration-75 hover:bg-red-600 hover:text-white"
+ >
+ <IconMenu
+ text="Ban"
+ icon={<Delete className="h-4 w-4" />}
+ />
+ <kbd className="hidden rounded bg-red-100 px-2 py-0.5 text-xs font-light text-red-600 transition-all duration-75 group-hover:bg-red-500 group-hover:text-white sm:inline-block">
+ B
+ </kbd>
+ </button>
+ )}
</div>
}
align="end"