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.

stashed changes

Steven Tey27aeedbf2986
diff --git a/apps/web/app/api/callback/expire/route.ts b/apps/web/app/api/callback/expire/route.ts
deleted file mode 100644
index 018c84f0ce..0000000000
--- a/apps/web/app/api/callback/expire/route.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-export async function POST() {
- return new Response("ok");
-}
diff --git a/apps/web/app/api/cron/links/delete/route.ts b/apps/web/app/api/cron/links/delete/route.ts
new file mode 100644
index 0000000000..258393a1d2
--- /dev/null
+++ b/apps/web/app/api/cron/links/delete/route.ts
@@ -0,0 +1,39 @@
+import { deleteLink } from "@/lib/api/links";
+import { receiver } from "@/lib/cron";
+import prisma from "@/lib/prisma";
+
+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;
+
+ const link = await prisma.link.findUnique({
+ where: {
+ id: linkId,
+ },
+ });
+
+ if (!link) {
+ return new Response("Link not found. Skipping...", { status: 200 });
+ }
+
+ if (link.userId) {
+ return new Response("Link claimed. Skipping...", { status: 200 });
+ }
+
+ await deleteLink({
+ domain: link.domain,
+ key: link.key,
+ });
+
+ return new Response("Link deleted.", { status: 200 });
+}
diff --git a/apps/web/app/api/cron/links/verify/route.ts b/apps/web/app/api/cron/links/verify/route.ts
new file mode 100644
index 0000000000..ba185e997d
--- /dev/null
+++ b/apps/web/app/api/cron/links/verify/route.ts
@@ -0,0 +1,42 @@
+import { receiver } from "@/lib/cron";
+import prisma from "@/lib/prisma";
+import { GOOGLE_FAVICON_URL, getApexDomain, log } from "@dub/utils";
+
+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;
+
+ const link = await prisma.link.findUnique({
+ where: {
+ id: linkId,
+ },
+ });
+
+ if (!link) {
+ return new Response("Link not found", { status: 200 });
+ }
+
+ const invalidFavicon = await fetch(
+ `${GOOGLE_FAVICON_URL}${getApexDomain(link.url)}`,
+ ).then((res) => !res.ok);
+
+ if (invalidFavicon) {
+ await log({
+ message: `Suspicious link detected: ${link.domain}/${link.key} → ${link.url}`,
+ type: "links",
+ mention: true,
+ });
+ }
+
+ return new Response("OK", { status: 200 });
+}
diff --git a/apps/web/app/api/cron/verify/route.ts b/apps/web/app/api/cron/verify/route.ts
index ba185e997d..27ae0cafdb 100644
--- a/apps/web/app/api/cron/verify/route.ts
+++ b/apps/web/app/api/cron/verify/route.ts
@@ -1,42 +1 @@
-import { receiver } from "@/lib/cron";
-import prisma from "@/lib/prisma";
-import { GOOGLE_FAVICON_URL, getApexDomain, log } from "@dub/utils";
-
-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;
-
- const link = await prisma.link.findUnique({
- where: {
- id: linkId,
- },
- });
-
- if (!link) {
- return new Response("Link not found", { status: 200 });
- }
-
- const invalidFavicon = await fetch(
- `${GOOGLE_FAVICON_URL}${getApexDomain(link.url)}`,
- ).then((res) => !res.ok);
-
- if (invalidFavicon) {
- await log({
- message: `Suspicious link detected: ${link.domain}/${link.key} → ${link.url}`,
- type: "links",
- mention: true,
- });
- }
-
- return new Response("OK", { status: 200 });
-}
+export { POST } from "../links/verify/route";
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 934db9abff..ebb3758b70 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -67,7 +67,7 @@ export const POST = withAuth(
if (!success) {
return new Response(
- "Rate limited – you can only create up to 10 links per day without an account.",
+ "Rate limited – you can only create up to 10 links per day without an account.",
{ status: 429 },
);
}
@@ -82,14 +82,25 @@ export const POST = withAuth(
});
}
- if (link.domain === "dub.sh") {
- await qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/verify`,
- body: {
- linkId: response.id,
- },
- });
- }
+ await Promise.all([
+ link.domain === "dub.sh" &&
+ qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/verify`,
+ body: {
+ linkId: response.id,
+ },
+ }),
+ // for public links, delete after 30 mins (if not claimed)
+ !link.userId &&
+ qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/delete`,
+ // delete after 30 mins
+ delay: 30 * 60 * 1000,
+ body: {
+ linkId: response.id,
+ },
+ }),
+ ]);
return NextResponse.json(response, { headers });
},
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 8cf4fac86c..4a2bfbc118 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -222,7 +222,18 @@ export async function processLink({
session?: Session;
bulk?: boolean;
}) {
- let { domain, key, url, image, rewrite, expiresAt, geo } = payload;
+ let {
+ domain,
+ key,
+ url,
+ image,
+ proxy,
+ rewrite,
+ expiresAt,
+ ios,
+ android,
+ geo,
+ } = payload;
// url checks
if (!url) {
@@ -250,36 +261,6 @@ export async function processLink({
};
}
- // custom social media image checks
- const uploadedImage = image && image.startsWith("data:image") ? true : false;
- if (uploadedImage && !process.env.CLOUDINARY_URL) {
- return {
- link: payload,
- error: "Missing Cloudinary environment variable.",
- status: 400,
- };
- }
-
- // expire date checks
- if (expiresAt) {
- const date = new Date(expiresAt);
- if (isNaN(date.getTime())) {
- return {
- link: payload,
- error: "Invalid expiry date. Expiry date must be in ISO-8601 format.",
- status: 422,
- };
- }
- // check if expiresAt is in the future
- if (new Date(expiresAt) < new Date()) {
- return {
- link: payload,
- error: "Expiry date must be in the future.",
- status: 422,
- };
- }
- }
-
if (project) {
if (!project.domains?.find((d) => d.slug === domain)) {
return {
@@ -316,13 +297,6 @@ export async function processLink({
status: 422,
};
}
- if (rewrite) {
- return {
- link: payload,
- error: "You can only use link cloaking on a custom domain.",
- status: 403,
- };
- }
if (domain === "dub.sh") {
const keyBlacklisted = await isBlacklistedKey(key);
if (keyBlacklisted) {
@@ -380,10 +354,11 @@ export async function processLink({
// free plan restrictions
if (!project || project.plan === "free") {
- if (geo) {
+ if (proxy || rewrite || expiresAt || ios || android || geo) {
return {
link: payload,
- error: "You can only use geo targeting on a Pro plan and above.",
+ error:
+ "You can only use link cloaking, custom social media cards, link expiration, device and geo targeting on a Pro plan and above. Upgrade to Pro to use these features.",
status: 403,
};
}
@@ -418,6 +393,36 @@ export async function processLink({
}
}
+ // custom social media image checks
+ const uploadedImage = image && image.startsWith("data:image") ? true : false;
+ if (uploadedImage && !process.env.CLOUDINARY_URL) {
+ return {
+ link: payload,
+ error: "Missing Cloudinary environment variable.",
+ status: 400,
+ };
+ }
+
+ // expire date checks
+ if (expiresAt) {
+ const date = new Date(expiresAt);
+ if (isNaN(date.getTime())) {
+ return {
+ link: payload,
+ error: "Invalid expiry date. Expiry date must be in ISO-8601 format.",
+ status: 422,
+ };
+ }
+ // check if expiresAt is in the future
+ if (new Date(expiresAt) < new Date()) {
+ return {
+ link: payload,
+ error: "Expiry date must be in the future.",
+ status: 422,
+ };
+ }
+ }
+
return {
link: {
...payload,
@@ -706,10 +711,6 @@ export async function deleteLink({
cloudinary.v2.uploader.destroy(`${domain}/${key}`, {
invalidate: true,
}),
- // deleteClickData({
- // domain,
- // key,
- // }),
redis.del(`${domain}:${key}`),
]);
}
diff --git a/apps/web/scripts/get-active-projects.ts b/apps/web/scripts/get-active-projects.ts
index 6033351c58..769289a836 100644
--- a/apps/web/scripts/get-active-projects.ts
+++ b/apps/web/scripts/get-active-projects.ts
@@ -25,16 +25,22 @@ async function main() {
take: 100,
});
console.table(
- projects.map((project) => ({
- ...project,
- domains: `${
- project.domains
- .slice(0, 2)
- .map((domain) => domain.slug)
- .join(", ") +
- (project.domains.length > 2 ? `... ${project._count.domains - 2}+` : "")
- }`,
- })),
+ // sort by links count
+ projects
+ // .sort((a, b) => b._count.links - a._count.links)
+ .map((project) => ({
+ ...project,
+ domains: `${
+ project.domains
+ .slice(0, 2)
+ .map((domain) => domain.slug)
+ .join(", ") +
+ (project.domains.length > 2
+ ? `... ${project._count.domains - 2}+`
+ : "")
+ }`,
+ })),
+ ["slug", "plan", "usage", "_count"],
);
}
diff --git a/apps/web/scripts/get-active-users.ts b/apps/web/scripts/get-active-users.ts
new file mode 100644
index 0000000000..d65158afeb
--- /dev/null
+++ b/apps/web/scripts/get-active-users.ts
@@ -0,0 +1,46 @@
+import "dotenv-flow/config";
+import prisma from "@/lib/prisma";
+
+async function main() {
+ const users = await prisma.link.groupBy({
+ by: ["userId"],
+ _count: {
+ id: true,
+ },
+ where: {
+ domain: {
+ in: ["dub.sh", "chatg.pt", "spti.fi", "amzn.id"],
+ },
+ },
+ orderBy: {
+ _count: {
+ id: "desc",
+ },
+ },
+ take: 100,
+ });
+ const usersWithEmails = await Promise.all(
+ users
+ .filter(({ userId }) => userId !== null)
+ .map(async (user) => {
+ const u = await prisma.user.findUnique({
+ where: {
+ id: user.userId!,
+ },
+ select: {
+ email: true,
+ },
+ });
+ if (!u) {
+ return user;
+ }
+ return {
+ email: u.email,
+ links: user._count.id,
+ };
+ }),
+ );
+ console.table(usersWithEmails);
+}
+
+main();
diff --git a/apps/web/ui/modals/add-edit-link-modal/android-section.tsx b/apps/web/ui/modals/add-edit-link-modal/android-section.tsx
index ff06b9559f..45d69ab1e4 100644
--- a/apps/web/ui/modals/add-edit-link-modal/android-section.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/android-section.tsx
@@ -1,8 +1,21 @@
-import { InfoTooltip, SimpleTooltipContent, Switch } from "@dub/ui";
+import useProject from "@/lib/swr/use-project";
+import { ModalContext } from "@/ui/modals/provider";
+import {
+ InfoTooltip,
+ SimpleTooltipContent,
+ Switch,
+ TooltipContent,
+} from "@dub/ui";
import { FADE_IN_ANIMATION_SETTINGS, HOME_DOMAIN } from "@dub/utils";
import { type Link as LinkProps } from "@prisma/client";
import { motion } from "framer-motion";
-import { Dispatch, SetStateAction, useEffect, useState } from "react";
+import {
+ Dispatch,
+ SetStateAction,
+ useContext,
+ useEffect,
+ useState,
+} from "react";
export default function AndroidSection({
props,
@@ -13,6 +26,8 @@ export default function AndroidSection({
data: LinkProps;
setData: Dispatch<SetStateAction<LinkProps>>;
}) {
+ const { plan } = useProject();
+ const { setShowUpgradePlanModal } = useContext(ModalContext);
const { android } = data;
const [enabled, setEnabled] = useState(!!android);
useEffect(() => {
@@ -45,7 +60,28 @@ export default function AndroidSection({
}
/>
</div>
- <Switch fn={() => setEnabled(!enabled)} checked={enabled} />
+ <Switch
+ fn={() => setEnabled(!enabled)}
+ checked={enabled}
+ // Android targeting is only available on Dub's Pro plan
+ {...(!plan || plan === "free"
+ ? {
+ disabledTooltip: (
+ <TooltipContent
+ title={`Android targeting is only available on ${process.env.NEXT_PUBLIC_APP_NAME}'s Pro plan. Upgrade to Pro to use this feature.`}
+ cta="Upgrade to Pro"
+ {...(plan === "free"
+ ? {
+ onClick: () => setShowUpgradePlanModal(true),
+ }
+ : {
+ href: `${HOME_DOMAIN}/pricing`,
+ })}
+ />
+ ),
+ }
+ : {})}
+ />
</div>
{enabled && (
<motion.div
diff --git a/apps/web/ui/modals/add-edit-link-modal/ios-section.tsx b/apps/web/ui/modals/add-edit-link-modal/ios-section.tsx
index 1b048f3e8e..d8adc492d3 100644
--- a/apps/web/ui/modals/add-edit-link-modal/ios-section.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/ios-section.tsx
@@ -1,8 +1,21 @@
-import { InfoTooltip, SimpleTooltipContent, Switch } from "@dub/ui";
+import useProject from "@/lib/swr/use-project";
+import { ModalContext } from "@/ui/modals/provider";
+import {
+ InfoTooltip,
+ SimpleTooltipContent,
+ Switch,
+ TooltipContent,
+} from "@dub/ui";
import { FADE_IN_ANIMATION_SETTINGS, HOME_DOMAIN } from "@dub/utils";
import { type Link as LinkProps } from "@prisma/client";
import { motion } from "framer-motion";
-import { Dispatch, SetStateAction, useEffect, useState } from "react";
+import {
+ Dispatch,
+ SetStateAction,
+ useContext,
+ useEffect,
+ useState,
+} from "react";
export default function IOSSection({
props,
@@ -13,6 +26,8 @@ export default function IOSSection({
data: LinkProps;
setData: Dispatch<SetStateAction<LinkProps>>;
}) {
+ const { plan } = useProject();
+ const { setShowUpgradePlanModal } = useContext(ModalContext);
const { ios } = data;
const [enabled, setEnabled] = useState(!!ios);
useEffect(() => {
@@ -43,7 +58,28 @@ export default function IOSSection({
}
/>
</div>
- <Switch fn={() => setEnabled(!enabled)} checked={enabled} />
+ <Switch
+ fn={() => setEnabled(!enabled)}
+ checked={enabled}
+ // iOS targeting is only available on Dub's Pro plan
+ {...(!plan || plan === "free"
+ ? {
+ disabledTooltip: (
+ <TooltipContent
+ title={`iOS targeting is only available on ${process.env.NEXT_PUBLIC_APP_NAME}'s Pro plan. Upgrade to Pro to use this feature.`}
+ cta="Upgrade to Pro"
+ {...(plan === "free"
+ ? {
+ onClick: () => setShowUpgradePlanModal(true),
+ }
+ : {
+ href: `${HOME_DOMAIN}/pricing`,
+ })}
+ />
+ ),
+ }
+ : {})}
+ />
</div>
{enabled && (
<motion.div
diff --git a/apps/web/ui/modals/add-edit-link-modal/rewrite-section.tsx b/apps/web/ui/modals/add-edit-link-modal/rewrite-section.tsx
index 1fc1ca4a78..394cce703d 100644
--- a/apps/web/ui/modals/add-edit-link-modal/rewrite-section.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/rewrite-section.tsx
@@ -1,8 +1,20 @@
-import { InfoTooltip, SimpleTooltipContent, Switch } from "@dub/ui";
+import useProject from "@/lib/swr/use-project";
+import { ModalContext } from "@/ui/modals/provider";
+import {
+ InfoTooltip,
+ SimpleTooltipContent,
+ Switch,
+ TooltipContent,
+} from "@dub/ui";
import { HOME_DOMAIN } from "@dub/utils";
import { type Link as LinkProps } from "@prisma/client";
-import { useParams } from "next/navigation";
-import { Dispatch, SetStateAction, useEffect, useState } from "react";
+import {
+ Dispatch,
+ SetStateAction,
+ useContext,
+ useEffect,
+ useState,
+} from "react";
export default function RewriteSection({
data,
@@ -11,7 +23,8 @@ export default function RewriteSection({
data: LinkProps;
setData: Dispatch<SetStateAction<LinkProps>>;
}) {
- const { slug } = useParams() as { slug: string };
+ const { plan } = useProject();
+ const { setShowUpgradePlanModal } = useContext(ModalContext);
const { rewrite } = data;
const [enabled, setEnabled] = useState(rewrite);
@@ -46,18 +59,24 @@ export default function RewriteSection({
<Switch
fn={() => setEnabled(!enabled)}
checked={enabled}
- // link cloaking is only available on custom domains
- {...(slug
- ? {}
- : {
+ // link cloaking is only available on Dub's Pro plan
+ {...(!plan || plan === "free"
+ ? {
disabledTooltip: (
- <SimpleTooltipContent
- title="You can only use link cloaking on a project with a custom domain."
- cta="Learn more."
- href={`${HOME_DOMAIN}/help/article/how-to-create-link#dubsh-links-vs-custom-domain-links`}
+ <TooltipContent
+ title={`Link cloaking is only available on ${process.env.NEXT_PUBLIC_APP_NAME}'s Pro plan. Upgrade to Pro to use this feature.`}
+ cta="Upgrade to Pro"
+ {...(plan === "free"
+ ? {
+ onClick: () => setShowUpgradePlanModal(true),
+ }
+ : {
+ href: `${HOME_DOMAIN}/pricing`,
+ })}
/>
),
- })}
+ }
+ : {})}
/>
</div>
</div>