From cddaefd9420507318d71f56355ff5a6648dcd951 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sun, 19 Jan 2025 13:55:40 +0000 Subject: feat: Change webhooks to be configurable by users --- apps/workers/crawlerWorker.ts | 4 ++-- apps/workers/webhookWorker.ts | 21 ++++++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-) (limited to 'apps/workers') diff --git a/apps/workers/crawlerWorker.ts b/apps/workers/crawlerWorker.ts index 9666299d..6bb4f4ac 100644 --- a/apps/workers/crawlerWorker.ts +++ b/apps/workers/crawlerWorker.ts @@ -55,7 +55,7 @@ import { OpenAIQueue, triggerSearchReindex, triggerVideoWorker, - triggerWebhookWorker, + triggerWebhook, zCrawlLinkRequestSchema, } from "@hoarder/shared/queues"; import { BookmarkTypes } from "@hoarder/shared/types/bookmarks"; @@ -772,7 +772,7 @@ async function runCrawler(job: DequeuedJob) { await triggerVideoWorker(bookmarkId, url); // Trigger a webhook - await triggerWebhookWorker(bookmarkId, "crawled"); + await triggerWebhook(bookmarkId, "crawled"); // Do the archival as a separate last step as it has the potential for failure await archivalLogic(); diff --git a/apps/workers/webhookWorker.ts b/apps/workers/webhookWorker.ts index 5124f8a4..dec40570 100644 --- a/apps/workers/webhookWorker.ts +++ b/apps/workers/webhookWorker.ts @@ -54,20 +54,17 @@ async function fetchBookmark(linkId: string) { link: true, text: true, asset: true, + user: { + with: { + webhooks: true, + }, + }, }, }); } async function runWebhook(job: DequeuedJob) { const jobId = job.id; - const webhookUrls = serverConfig.webhook.urls; - if (!webhookUrls) { - logger.info( - `[webhook][${jobId}] No webhook urls configured. Skipping webhook job.`, - ); - return; - } - const webhookToken = serverConfig.webhook.token; const webhookTimeoutSec = serverConfig.webhook.timeoutSec; const { bookmarkId } = job.data; @@ -78,12 +75,18 @@ async function runWebhook(job: DequeuedJob) { ); } + if (!bookmark.user.webhooks) { + return; + } + logger.info( `[webhook][${jobId}] Starting a webhook job for bookmark with id "${bookmark.id}"`, ); await Promise.allSettled( - webhookUrls.map(async (url) => { + bookmark.user.webhooks.map(async (webhook) => { + const url = webhook.url; + const webhookToken = webhook.token; const maxRetries = serverConfig.webhook.retryTimes; let attempt = 0; let success = false; -- cgit v1.2.3-70-g09d2