Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix: only trigger webhook job if user have webhooks configured

Mohamed Bassem49d57dfdad2d
diff --git a/apps/workers/workers/crawlerWorker.ts b/apps/workers/workers/crawlerWorker.ts
index 85d83265..d437986d 100644
--- a/apps/workers/workers/crawlerWorker.ts
+++ b/apps/workers/workers/crawlerWorker.ts
@@ -51,7 +51,6 @@ import {
QuotaService,
setSpanAttributes,
triggerSearchReindex,
- triggerWebhook,
VideoWorkerQueue,
withSpan,
zCrawlLinkRequestSchema,
@@ -80,6 +79,7 @@ import {
import { getRateLimitClient } from "@karakeep/shared/ratelimiting";
import { tryCatch } from "@karakeep/shared/tryCatch";
import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";
+import { WebhooksService } from "@karakeep/trpc/models/webhooks.service";
import type {
ParseSubprocessError,
@@ -2161,7 +2161,15 @@ async function runCrawler(
}
// Trigger a webhook
- await triggerWebhook(bookmarkId, "crawled", undefined, enqueueOpts);
+ {
+ const webhookService = new WebhooksService(db);
+ await webhookService.triggerWebhook(
+ bookmarkId,
+ "crawled",
+ userId,
+ enqueueOpts,
+ );
+ }
// Do the archival as a separate last step as it has the potential for failure
await archivalLogic();
diff --git a/apps/workers/workers/inference/tagging.ts b/apps/workers/workers/inference/tagging.ts
index 3750b474..d53f4118 100644
--- a/apps/workers/workers/inference/tagging.ts
+++ b/apps/workers/workers/inference/tagging.ts
@@ -21,7 +21,6 @@ import {
setSpanAttributes,
triggerRuleEngineOnEvent,
triggerSearchReindex,
- triggerWebhook,
} from "@karakeep/shared-server";
import { ASSET_TYPES, readAsset } from "@karakeep/shared/assetdb";
import serverConfig from "@karakeep/shared/config";
@@ -30,6 +29,7 @@ import { buildImagePrompt } from "@karakeep/shared/prompts";
import { buildTextPrompt } from "@karakeep/shared/prompts.server";
import { DequeuedJob, EnqueueOptions } from "@karakeep/shared/queueing";
import { Bookmark } from "@karakeep/trpc/models/bookmarks";
+import { WebhooksService } from "@karakeep/trpc/models/webhooks.service";
const openAIResponseSchema = z.object({
tags: z.array(z.string()),
@@ -582,7 +582,15 @@ export async function runTagging(
};
// Trigger a webhook
- await triggerWebhook(bookmarkId, "ai tagged", undefined, enqueueOpts);
+ {
+ const webhookService = new WebhooksService(db);
+ await webhookService.triggerWebhook(
+ bookmarkId,
+ "ai tagged",
+ bookmark.userId,
+ enqueueOpts,
+ );
+ }
// Update the search index
await triggerSearchReindex(bookmarkId, enqueueOpts);
diff --git a/apps/workers/workers/webhookWorker.ts b/apps/workers/workers/webhookWorker.ts
index 875a0ac6..e45e6b1d 100644
--- a/apps/workers/workers/webhookWorker.ts
+++ b/apps/workers/workers/webhookWorker.ts
@@ -73,12 +73,24 @@ async function fetchUserWebhooks(userId: string) {
});
}
+function canDeliverWebhookWithoutBookmark(
+ operation: ZWebhookRequest["operation"],
+) {
+ return operation === "deleted";
+}
+
async function runWebhook(job: DequeuedJob<ZWebhookRequest>) {
const jobId = job.id;
const webhookTimeoutSec = serverConfig.webhook.timeoutSec;
- const { bookmarkId } = job.data;
+ const { bookmarkId, operation } = job.data;
const bookmark = await fetchBookmark(bookmarkId);
+ if (!bookmark && !canDeliverWebhookWithoutBookmark(operation)) {
+ logger.info(
+ `[webhook][${jobId}] Bookmark with id ${bookmarkId} not found for "${operation}" webhook. Skipping webhook`,
+ );
+ return;
+ }
const userId = job.data.userId ?? bookmark?.userId;
if (!userId) {
diff --git a/packages/shared-server/src/queues.ts b/packages/shared-server/src/queues.ts
index 4d4a61d6..d1b02823 100644
--- a/packages/shared-server/src/queues.ts
+++ b/packages/shared-server/src/queues.ts
@@ -257,22 +257,6 @@ export const WebhookQueue = createDeferredQueue<ZWebhookRequest>(
},
);
-export async function triggerWebhook(
- bookmarkId: string,
- operation: ZWebhookRequest["operation"],
- userId?: string,
- opts?: EnqueueOptions,
-) {
- await WebhookQueue.enqueue(
- {
- bookmarkId,
- userId,
- operation,
- },
- opts,
- );
-}
-
// RuleEngine worker
export const zRuleEngineRequestSchema = z.object({
bookmarkId: z.string(),
diff --git a/packages/trpc/models/bookmarks.ts b/packages/trpc/models/bookmarks.ts
index c8cd1f00..e2813343 100644
--- a/packages/trpc/models/bookmarks.ts
+++ b/packages/trpc/models/bookmarks.ts
@@ -29,7 +29,9 @@ import {
rssFeedImportsTable,
tagsOnBookmarks,
} from "@karakeep/db/schema";
-import { SearchIndexingQueue, triggerWebhook } from "@karakeep/shared-server";
+import { SearchIndexingQueue } from "@karakeep/shared-server";
+
+import { WebhooksService } from "./webhooks.service";
import { deleteAsset, readAsset } from "@karakeep/shared/assetdb";
import { getAlignedExpiry } from "@karakeep/shared/signedTokens";
import {
@@ -91,6 +93,10 @@ export class BareBookmark {
return this.bareBookmark.createdAt;
}
+ get userId() {
+ return this.bareBookmark.userId;
+ }
+
static async bareFromId(ctx: AuthedContext, bookmarkId: string) {
const bookmark = await ctx.db.query.bookmarks.findFirst({
where: eq(bookmarks.id, bookmarkId),
@@ -935,9 +941,15 @@ export class Bookmark extends BareBookmark {
},
);
- await triggerWebhook(this.bookmark.id, "deleted", this.ctx.user.id, {
- groupId: this.ctx.user.id,
- });
+ const webhookService = new WebhooksService(this.ctx.db);
+ await webhookService.triggerWebhook(
+ this.bookmark.id,
+ "deleted",
+ this.bookmark.userId,
+ {
+ groupId: this.ctx.user.id,
+ },
+ );
if (deleted.changes > 0) {
await this.cleanupAssets();
}
diff --git a/packages/trpc/models/webhooks.service.ts b/packages/trpc/models/webhooks.service.ts
index b1b5e327..197e36ab 100644
--- a/packages/trpc/models/webhooks.service.ts
+++ b/packages/trpc/models/webhooks.service.ts
@@ -12,6 +12,8 @@ import {
import type { Actor, Authorized } from "../lib/actor";
import { actorUserId, assertOwnership, authorize } from "../lib/actor";
import { WebhooksRepo } from "./webhooks.repo";
+import { WebhookQueue, ZWebhookRequest } from "@karakeep/shared-server";
+import { EnqueueOptions } from "@karakeep/shared/queueing";
type Webhook = typeof webhooksTable.$inferSelect;
@@ -71,4 +73,24 @@ export class WebhooksService {
throw new TRPCError({ code: "NOT_FOUND" });
}
}
+
+ async triggerWebhook(
+ bookmarkId: string,
+ operation: ZWebhookRequest["operation"],
+ userId: string,
+ opts?: EnqueueOptions,
+ ) {
+ const count = await this.repo.countByUser(userId);
+ if (count === 0) {
+ return;
+ }
+ await WebhookQueue.enqueue(
+ {
+ bookmarkId,
+ userId,
+ operation,
+ },
+ opts,
+ );
+ }
}
diff --git a/packages/trpc/routers/bookmarks.test.ts b/packages/trpc/routers/bookmarks.test.ts
index 26fb17f7..33d3d81b 100644
--- a/packages/trpc/routers/bookmarks.test.ts
+++ b/packages/trpc/routers/bookmarks.test.ts
@@ -29,7 +29,6 @@ vi.mock("@karakeep/shared-server", async (original) => {
},
triggerRuleEngineOnEvent: vi.fn(),
triggerSearchReindex: vi.fn(),
- triggerWebhook: vi.fn(),
};
});
diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts
index 680bc31f..e45faaca 100644
--- a/packages/trpc/routers/bookmarks.ts
+++ b/packages/trpc/routers/bookmarks.ts
@@ -26,7 +26,6 @@ import {
QuotaService,
triggerRuleEngineOnEvent,
triggerSearchReindex,
- triggerWebhook,
} from "@karakeep/shared-server";
import { SUPPORTED_BOOKMARK_ASSET_TYPES } from "@karakeep/shared/assetdb";
import serverConfig from "@karakeep/shared/config";
@@ -56,6 +55,7 @@ import { authedProcedure, createRateLimitMiddleware, router } from "../index";
import { getBookmarkIdsFromMatcher } from "../lib/search";
import { Asset } from "../models/assets";
import { BareBookmark, Bookmark } from "../models/bookmarks";
+import { WebhooksService } from "../models/webhooks.service";
export const ensureBookmarkOwnership = experimental_trpcMiddleware<{
ctx: AuthedContext;
@@ -373,10 +373,10 @@ export const bookmarksAppRouter = router({
enqueueOpts,
),
triggerSearchReindex(bookmark.id, enqueueOpts),
- triggerWebhook(
+ new WebhooksService(ctx.db).triggerWebhook(
bookmark.id,
"created",
- /* userId */ undefined,
+ bookmark.userId,
enqueueOpts,
),
]);
@@ -538,9 +538,14 @@ export const bookmarksAppRouter = router({
triggerSearchReindex(input.bookmarkId, {
groupId: ctx.user.id,
}),
- triggerWebhook(input.bookmarkId, "edited", ctx.user.id, {
- groupId: ctx.user.id,
- }),
+ new WebhooksService(ctx.db).triggerWebhook(
+ input.bookmarkId,
+ "edited",
+ updatedBookmark.userId,
+ {
+ groupId: ctx.user.id,
+ },
+ ),
]);
return updatedBookmark;
@@ -584,9 +589,14 @@ export const bookmarksAppRouter = router({
triggerSearchReindex(input.bookmarkId, {
groupId: ctx.user.id,
}),
- triggerWebhook(input.bookmarkId, "edited", ctx.user.id, {
- groupId: ctx.user.id,
- }),
+ new WebhooksService(ctx.db).triggerWebhook(
+ input.bookmarkId,
+ "edited",
+ ctx.bookmark.userId,
+ {
+ groupId: ctx.user.id,
+ },
+ ),
]);
}),
@@ -1032,9 +1042,14 @@ export const bookmarksAppRouter = router({
triggerSearchReindex(input.bookmarkId, {
groupId: ctx.user.id,
}),
- triggerWebhook(input.bookmarkId, "edited", ctx.user.id, {
- groupId: ctx.user.id,
- }),
+ new WebhooksService(ctx.db).triggerWebhook(
+ input.bookmarkId,
+ "edited",
+ ctx.bookmark.userId,
+ {
+ groupId: ctx.user.id,
+ },
+ ),
]);
}
return res;
@@ -1182,9 +1197,14 @@ Author: ${bookmark.author ?? ""}
triggerSearchReindex(input.bookmarkId, {
groupId: ctx.user.id,
}),
- triggerWebhook(input.bookmarkId, "edited", ctx.user.id, {
- groupId: ctx.user.id,
- }),
+ new WebhooksService(ctx.db).triggerWebhook(
+ input.bookmarkId,
+ "edited",
+ ctx.bookmark.userId,
+ {
+ groupId: ctx.user.id,
+ },
+ ),
]);
return {
diff --git a/packages/trpc/testUtils.ts b/packages/trpc/testUtils.ts
index 744b85af..eb51d871 100644
--- a/packages/trpc/testUtils.ts
+++ b/packages/trpc/testUtils.ts
@@ -97,7 +97,6 @@ export function defaultBeforeEach(seedDB = true) {
},
triggerRuleEngineOnEvent: vi.fn(),
triggerSearchReindex: vi.fn(),
- triggerWebhook: vi.fn(),
};
});
Object.assign(context, await buildTestContext(seedDB));