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.

slack template

Kiran K9762ddfa67a4
diff --git a/apps/web/app/api/webhooks/callback/route.ts b/apps/web/app/api/webhooks/callback/route.ts
index 39920c3854..bd25a73587 100644
--- a/apps/web/app/api/webhooks/callback/route.ts
+++ b/apps/web/app/api/webhooks/callback/route.ts
@@ -1,13 +1,20 @@
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
import { prisma } from "@/lib/prisma";
import { recordWebhookEvent } from "@/lib/tinybird/record-webhook-event";
+import { WEBHOOK_TRIGGERS } from "@/lib/webhook/constants";
import {
handleWebhookFailure,
resetWebhookFailureCount,
} from "@/lib/webhook/failure";
-import { webhookPayloadSchema } from "@/lib/webhook/schemas";
import { webhookCallbackSchema } from "@/lib/zod/schemas/webhooks";
import { getSearchParams } from "@dub/utils";
+import { z } from "zod";
+
+const searchParamsSchema = z.object({
+ webhookId: z.string(),
+ eventId: z.string(),
+ event: z.enum(WEBHOOK_TRIGGERS),
+});
// POST /api/webhooks/callback – listen to webhooks status from QStash
export const POST = async (req: Request) => {
@@ -18,7 +25,9 @@ export const POST = async (req: Request) => {
const { url, status, body, sourceBody, sourceMessageId } =
webhookCallbackSchema.parse(rawBody);
- const { webhookId } = getSearchParams(req.url);
+ const { webhookId, eventId, event } = searchParamsSchema.parse(
+ getSearchParams(req.url),
+ );
const webhook = await prisma.webhook.findUnique({
where: { id: webhookId },
@@ -31,11 +40,6 @@ export const POST = async (req: Request) => {
const request = Buffer.from(sourceBody, "base64").toString("utf-8");
const response = Buffer.from(body, "base64").toString("utf-8");
-
- const { id: eventId, event } = webhookPayloadSchema.parse(
- JSON.parse(request),
- );
-
const isFailed = status >= 400;
await Promise.all([
diff --git a/apps/web/lib/webhook/qstash.ts b/apps/web/lib/webhook/qstash.ts
index a354b41b1e..da68756419 100644
--- a/apps/web/lib/webhook/qstash.ts
+++ b/apps/web/lib/webhook/qstash.ts
@@ -7,6 +7,7 @@ import z from "../zod";
import { createWebhookSignature } from "./signature";
import { prepareWebhookPayload } from "./transform";
import { EventDataProps } from "./types";
+import { identifyWebhookReceiver } from "./utils";
// Send webhooks to multiple webhooks
export const sendWebhooks = async ({
@@ -39,8 +40,13 @@ export const publishWebhookEventToQStash = async ({
webhook: Pick<Webhook, "id" | "url" | "secret">;
payload: z.infer<typeof webhookPayloadSchema>;
}) => {
- const callbackUrl = `${APP_DOMAIN_WITH_NGROK}/api/webhooks/callback?webhookId=${webhook.id}`;
+ const callbackUrl = new URL(`${APP_DOMAIN_WITH_NGROK}/api/webhooks/callback`);
+ callbackUrl.searchParams.append("webhookId", webhook.id);
+ callbackUrl.searchParams.append("eventId", payload.id);
+ callbackUrl.searchParams.append("event", payload.event);
+
const signature = await createWebhookSignature(webhook.secret, payload);
+ const receiver = identifyWebhookReceiver(webhook.url);
const response = await qstash.publishJSON({
url: webhook.url,
@@ -49,8 +55,8 @@ export const publishWebhookEventToQStash = async ({
"Dub-Signature": signature,
"Upstash-Hide-Headers": "true",
},
- callback: callbackUrl,
- failureCallback: callbackUrl,
+ callback: callbackUrl.href,
+ failureCallback: callbackUrl.href,
});
if (!response.messageId) {
@@ -59,3 +65,8 @@ export const publishWebhookEventToQStash = async ({
return response;
};
+
+// const finalPayload =
+// receiver === "slack"
+// ? generateLinkTemplate(payload.data, payload.event)
+// : payload;
diff --git a/apps/web/lib/webhook/slack-templates.ts b/apps/web/lib/webhook/slack-templates.ts
new file mode 100644
index 0000000000..87bd6a20f6
--- /dev/null
+++ b/apps/web/lib/webhook/slack-templates.ts
@@ -0,0 +1,40 @@
+import { LinkWebhookEvent } from "dub/models/components";
+import { WebhookTrigger } from "../types";
+
+export const generateLinkTemplate = (
+ data: LinkWebhookEvent["data"],
+ eventType: WebhookTrigger,
+) => {
+ const eventMessages = {
+ "link.created": "*New short link created*",
+ "link.updated": "*Short link updated*",
+ "link.deleted": "*Short link deleted*",
+ };
+
+ return {
+ blocks: [
+ {
+ type: "section",
+ text: {
+ type: "mrkdwn",
+ text: eventMessages[eventType],
+ },
+ },
+ {
+ type: "section",
+ fields: [
+ {
+ type: "mrkdwn",
+ text: `*Short link*\n<${data.shortLink}|${data.shortLink}>\n`,
+ },
+ {
+ type: "mrkdwn",
+ text: `*Destination URL*\n<${data.url}|${data.url}>\n`,
+ },
+ ],
+ },
+ ],
+ };
+};
+
+const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);