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.

added logs for when someone adds a link

Steven Tey82b4978363ca
diff --git a/lib/constants/blacklist.ts b/lib/constants/blacklist.ts
index 0a0fd472df..826e04b9c8 100644
--- a/lib/constants/blacklist.ts
+++ b/lib/constants/blacklist.ts
@@ -1,5 +1,7 @@
/*
+ ************************************************
* Crowdsourced list of blacklisted domains
+ ************************************************
* How to contribute:
* 1. Find a domain that you think should be blacklisted (contain phishing links, spam links, pornographic content, etc.)
* 2. Do a "CMD/CTRL + F" search for the domain in this file
diff --git a/lib/cron/domains.tsx b/lib/cron/domains.tsx
index bb6d24c858..7535610b55 100644
--- a/lib/cron/domains.tsx
+++ b/lib/cron/domains.tsx
@@ -1,7 +1,7 @@
import sendMail from "emails";
import InvalidDomain from "emails/InvalidDomain";
import ProjectDeleted from "emails/ProjectDeleted";
-import { log } from "@/lib/cron/utils";
+import { log } from "@/lib/utils";
import { removeDomain } from "@/lib/domains";
import prisma from "@/lib/prisma";
import { deleteProject, redis } from "@/lib/upstash";
@@ -15,7 +15,7 @@ export const handleDomainUpdates = async (
sentEmails: string[],
) => {
if (changed) {
- await log(`Domain *${domain}* changed status to *${verified}*`);
+ await log(`Domain *${domain}* changed status to *${verified}*`, "cron");
}
if (verified) return;
@@ -53,7 +53,10 @@ export const handleDomainUpdates = async (
}),
removeDomain(domain),
deleteProject(domain),
- log(`Domain *${domain}* has been invalid for > 30 days, deleting.`),
+ log(
+ `Domain *${domain}* has been invalid for > 30 days, deleting.`,
+ "cron",
+ ),
sendMail({
subject: `Your project ${projectSlug} has been deleted`,
to: ownerEmail,
@@ -65,6 +68,7 @@ export const handleDomainUpdates = async (
} else {
return await log(
`Domain *${domain}* has been invalid for > 30 days but has links, not deleting.`,
+ "cron",
);
}
}
@@ -79,7 +83,10 @@ const sendDomainInvalidEmail = async (
) => {
const ownerEmail = await getProjectOwnerEmail(projectSlug);
return await Promise.all([
- log(`Domain *${domain}* is invalid for ${invalidDays} days, email sent.`),
+ log(
+ `Domain *${domain}* is invalid for ${invalidDays} days, email sent.`,
+ "cron",
+ ),
sendMail({
subject: `Your domain ${domain} needs to be configured`,
to: ownerEmail,
diff --git a/lib/cron/usage.tsx b/lib/cron/usage.tsx
index 472be561ef..334d331c10 100644
--- a/lib/cron/usage.tsx
+++ b/lib/cron/usage.tsx
@@ -3,7 +3,7 @@ import UsageExceeded from "emails/UsageExceeded";
import prisma from "@/lib/prisma";
import { redis } from "@/lib/upstash";
import { getFirstAndLastDay } from "@/lib/utils";
-import { log } from "./utils";
+import { log } from "@/lib/utils";
export const updateUsage = async () => {
const users = await prisma.user.findMany({
@@ -75,6 +75,7 @@ export const updateUsage = async () => {
if (ownerExceededUsage) {
await log(
`${email} is over usage limit. Usage: ${totalUsage}, Limit: ${usageLimit}`,
+ "cron",
);
const sentFirstUsageLimitEmail = sentEmails.some(
(email) => email.type === "firstUsageLimitEmail",
diff --git a/lib/cron/utils.ts b/lib/cron/utils.ts
deleted file mode 100644
index 94b7c10b65..0000000000
--- a/lib/cron/utils.ts
+++ /dev/null
@@ -1,25 +0,0 @@
-export const log = async (message: string) => {
- /* Log a message to the console */
- if (!process.env.DUB_SLACK_HOOK) return;
- try {
- return await fetch(process.env.DUB_SLACK_HOOK, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- blocks: [
- {
- type: "section",
- text: {
- type: "mrkdwn",
- text: message,
- },
- },
- ],
- }),
- });
- } catch (e) {
- console.log(`Failed to log to Vercel Slack. Error: ${e}`);
- }
-};
diff --git a/lib/utils.ts b/lib/utils.ts
index 0bd723c013..396c9add8d 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -286,3 +286,35 @@ export const truncate = (str: string, length: number) => {
if (str.length <= length) return str;
return `${str.slice(0, length)}...`;
};
+
+const logTypeToEnv = {
+ cron: process.env.DUB_SLACK_HOOK_CRON,
+ links: process.env.DUB_SLACK_HOOK_LINKS,
+};
+
+export const log = async (message: string, type: "cron" | "links") => {
+ /* Log a message to the console */
+ const HOOK = logTypeToEnv[type];
+ if (!HOOK) return;
+ try {
+ return await fetch(HOOK, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ blocks: [
+ {
+ type: "section",
+ text: {
+ type: "mrkdwn",
+ text: message,
+ },
+ },
+ ],
+ }),
+ });
+ } catch (e) {
+ console.log(`Failed to log to Vercel Slack. Error: ${e}`);
+ }
+};
diff --git a/pages/api/links/index.ts b/pages/api/links/index.ts
index 2b4340f4b4..0b1b5e94fd 100644
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@ -3,6 +3,7 @@ import { addLink, getLinksForProject } from "@/lib/api/links";
import { Session, withUserAuth } from "@/lib/auth";
import { BLACKLIST } from "@/lib/constants";
import { getDomainWithoutWWW } from "@/lib/utils";
+import { log } from "@/lib/utils";
// This is a special route for retrieving and creating custom dub.sh links.
@@ -40,6 +41,7 @@ export default withUserAuth(
if (response === null) {
return res.status(403).json({ error: "Key already exists" });
}
+ await log(`${session.user.email} created a new link for ${url}`, "links");
return res.status(200).json(response);
} else {
res.setHeader("Allow", ["GET", "POST"]);