forked from dubinc/dub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Offload link creation and editing side effects to qstash
- Loading branch information
1 parent
92e6c05
commit 5445e54
Showing
7 changed files
with
116 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { qstash, receiver } from "@/lib/cron"; | ||
import prisma from "@/lib/prisma"; | ||
import { | ||
APP_DOMAIN_WITH_NGROK, | ||
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, type } = body as { | ||
linkId: string; | ||
type: "create" | "edit"; | ||
}; | ||
|
||
const link = await prisma.link.findUnique({ | ||
where: { | ||
id: linkId, | ||
}, | ||
}); | ||
|
||
if (!link) { | ||
return new Response("Link not found", { status: 200 }); | ||
} | ||
|
||
console.log(`Received ${type} event for link ${linkId}`); | ||
|
||
// if the link is a dub.sh link, do some checks | ||
if (link.domain === "dub.sh") { | ||
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, | ||
}); | ||
} | ||
} | ||
|
||
// for public links, delete after 30 mins (if not claimed) | ||
if (!link.userId) { | ||
await qstash.publishJSON({ | ||
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/delete`, | ||
// delete after 30 mins | ||
delay: 30 * 60, | ||
body: { | ||
linkId, | ||
}, | ||
}); | ||
} | ||
|
||
// increment links usage | ||
if (type === "create" && link.projectId) { | ||
const updatedProject = await prisma.project.update({ | ||
where: { | ||
id: link.projectId, | ||
}, | ||
data: { | ||
linksUsage: { | ||
increment: 1, | ||
}, | ||
}, | ||
}); | ||
console.log({ updatedProject }); | ||
if (updatedProject.linksUsage > updatedProject.linksLimit) { | ||
// send email | ||
} | ||
} | ||
|
||
return new Response("OK", { status: 200 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "../event/route"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { POST } from "../links/verify/route"; | ||
export { POST } from "../links/event/route"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters