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.

simplify expired links

Steven Tey14a0bc5a6742
diff --git a/apps/web/app/api/callback/expire/route.ts b/apps/web/app/api/callback/expire/route.ts
index 302c80c297..018c84f0ce 100644
--- a/apps/web/app/api/callback/expire/route.ts
+++ b/apps/web/app/api/callback/expire/route.ts
@@ -1,58 +1,3 @@
-import { receiver } from "@/lib/cron";
-import prisma from "@/lib/prisma";
-import { redis } from "@/lib/upstash";
-
-export async function POST(req: Request) {
- const body = await req.json();
- if (process.env.VERCEL === "1") {
- const isValid = await receiver.verify({
- signature: req.headers.get("Upstash-Signature") || "",
- body: JSON.stringify(body),
- });
- if (!isValid) {
- return new Response("Unauthorized", { status: 401 });
- }
- }
-
- const { linkId } = body;
- if (!linkId) {
- return new Response("Missing link ID", { status: 200 });
- }
-
- const link = await prisma.link.findUnique({
- where: {
- id: linkId,
- },
- select: {
- domain: true,
- key: true,
- expiresAt: true,
- },
- });
-
- if (!link) {
- return new Response("Link not found", { status: 200 });
- }
-
- if (!link.expiresAt || link.expiresAt > new Date()) {
- return new Response(
- "Link is not expired or does not have an expiration date",
- { status: 200 },
- );
- }
-
- const { domain, key } = link;
-
- const redisLink = await redis.get(`${domain}:${key}`);
-
- if (!redisLink) {
- return new Response("Link not found", { status: 404 });
- }
-
- await redis.set(`${domain}:${key}`, {
- ...redisLink,
- expired: true,
- });
-
- return new Response("Successfully expired link", { status: 200 });
+export async function POST() {
+ return new Response("ok");
}
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 4a08da43bb..f187bb7682 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -11,6 +11,7 @@ import {
} from "next/server";
import { isBlacklistedReferrer } from "../edge-config";
import { getLinkViaEdge } from "../planetscale";
+import { RedisLinkProps } from "../types";
export default async function LinkMiddleware(
req: NextRequest,
@@ -42,18 +43,7 @@ export default async function LinkMiddleware(
const inspectMode = key.endsWith("+");
- const response = await redis.get<{
- url: string;
- password?: boolean;
- proxy?: boolean;
- rewrite?: boolean;
- iframeable?: boolean;
- expired?: boolean;
- ios?: string;
- android?: string;
- geo?: object;
- banned?: boolean;
- }>(
+ const response = await redis.get<RedisLinkProps>(
// if inspect mode is enabled, remove the trailing `+` from the key
`${domain}:${inspectMode ? key.slice(0, -1) : key}`,
);
@@ -64,7 +54,7 @@ export default async function LinkMiddleware(
proxy,
rewrite,
iframeable,
- expired,
+ expiresAt,
ios,
android,
geo,
@@ -97,15 +87,17 @@ export default async function LinkMiddleware(
}
}
- // if the link has expired or is banned
- if (expired || banned) {
+ // if the link is banned
+ if (banned) {
return NextResponse.rewrite(
- new URL(
- `/${banned ? "banned" : "expired"}/${domain}/${encodeURIComponent(
- key,
- )}`,
- req.url,
- ),
+ new URL(`/banned/${domain}/${encodeURIComponent(key)}`, req.url),
+ );
+ }
+
+ // if the link has expired
+ if (expiresAt && new Date(expiresAt) < new Date()) {
+ return NextResponse.rewrite(
+ new URL(`/expired/${domain}/${encodeURIComponent(key)}`, req.url),
);
}
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 241d53d6df..ecf8664dfd 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -6,6 +6,19 @@ export interface SimpleLinkProps {
key: string;
url: string;
}
+export interface RedisLinkProps {
+ url: string;
+ password?: boolean;
+ proxy?: boolean;
+ rewrite?: boolean;
+ iframeable?: boolean;
+ expiresAt?: string;
+ ios?: string;
+ android?: string;
+ geo?: object;
+ banned?: boolean;
+}
+
export interface TagProps {
id: string;
name: string;