aboutsummaryrefslogtreecommitdiffstats
path: root/packages
diff options
context:
space:
mode:
authorMohamed Bassem <me@mbassem.com>2025-12-30 13:29:03 +0200
committerGitHub <noreply@github.com>2025-12-30 11:29:03 +0000
commitb20ba9cfccd22159bf8263165cca76aab3147d9c (patch)
tree166f5c477d1c368a5017aae9f726b692d655fc0e /packages
parenta0b4a26ad398137e13c35f3fe0dad99154537d91 (diff)
downloadkarakeep-b20ba9cfccd22159bf8263165cca76aab3147d9c.tar.zst
feat: add "URL Does Not Contain" condition to rule engine (#2280)
* feat: add "URL Does Not Contain" condition to rule engine Add a new condition type `urlDoesNotContain` that allows users to create rules based on URLs that do NOT contain specific strings. This enables more flexible rule configurations, such as: - Automatically adding bookmarks to a "Read Later" list if the URL does not contain "reddit.com" or "youtube.com" Changes: - Added `urlDoesNotContain` condition type to Zod schema - Implemented evaluation logic in RuleEngine - Added UI support in ConditionBuilder component - Added translation key for new condition type - Added test coverage for the new condition Fixes #2259 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Mohamed Bassem <MohamedBassem@users.noreply.github.com> * fix type link --------- Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: Mohamed Bassem <MohamedBassem@users.noreply.github.com>
Diffstat (limited to 'packages')
-rw-r--r--packages/shared/types/rules.ts8
-rw-r--r--packages/trpc/lib/__tests__/ruleEngine.test.ts16
-rw-r--r--packages/trpc/lib/ruleEngine.ts7
3 files changed, 31 insertions, 0 deletions
diff --git a/packages/shared/types/rules.ts b/packages/shared/types/rules.ts
index 92300b3c..0daec524 100644
--- a/packages/shared/types/rules.ts
+++ b/packages/shared/types/rules.ts
@@ -54,6 +54,11 @@ const zUrlContainsCondition = z.object({
str: z.string(),
});
+const zUrlDoesNotContainCondition = z.object({
+ type: z.literal("urlDoesNotContain"),
+ str: z.string(),
+});
+
const zImportedFromFeedCondition = z.object({
type: z.literal("importedFromFeed"),
feedId: z.string(),
@@ -80,6 +85,7 @@ const zIsArchivedCondition = z.object({
const nonRecursiveCondition = z.discriminatedUnion("type", [
zAlwaysTrueCondition,
zUrlContainsCondition,
+ zUrlDoesNotContainCondition,
zImportedFromFeedCondition,
zBookmarkTypeIsCondition,
zHasTagCondition,
@@ -98,6 +104,7 @@ export const zRuleEngineConditionSchema: z.ZodType<RuleEngineCondition> =
z.discriminatedUnion("type", [
zAlwaysTrueCondition,
zUrlContainsCondition,
+ zUrlDoesNotContainCondition,
zImportedFromFeedCondition,
zBookmarkTypeIsCondition,
zHasTagCondition,
@@ -227,6 +234,7 @@ const ruleValidaitorFn = (
case "isArchived":
return true;
case "urlContains":
+ case "urlDoesNotContain":
if (condition.str.length == 0) {
ctx.addIssue({
code: "custom",
diff --git a/packages/trpc/lib/__tests__/ruleEngine.test.ts b/packages/trpc/lib/__tests__/ruleEngine.test.ts
index ede22ec6..600d8aa9 100644
--- a/packages/trpc/lib/__tests__/ruleEngine.test.ts
+++ b/packages/trpc/lib/__tests__/ruleEngine.test.ts
@@ -219,6 +219,22 @@ describe("RuleEngine", () => {
expect(engine.doesBookmarkMatchConditions(condition)).toBe(false);
});
+ it("should return false for urlDoesNotContain condition when URL contains string", () => {
+ const condition: RuleEngineCondition = {
+ type: "urlDoesNotContain",
+ str: "example.com",
+ };
+ expect(engine.doesBookmarkMatchConditions(condition)).toBe(false);
+ });
+
+ it("should return true for urlDoesNotContain condition when URL does not contain string", () => {
+ const condition: RuleEngineCondition = {
+ type: "urlDoesNotContain",
+ str: "nonexistent",
+ };
+ expect(engine.doesBookmarkMatchConditions(condition)).toBe(true);
+ });
+
it("should return true for importedFromFeed condition", () => {
const condition: RuleEngineCondition = {
type: "importedFromFeed",
diff --git a/packages/trpc/lib/ruleEngine.ts b/packages/trpc/lib/ruleEngine.ts
index c191619b..6b5f8fdf 100644
--- a/packages/trpc/lib/ruleEngine.ts
+++ b/packages/trpc/lib/ruleEngine.ts
@@ -3,6 +3,7 @@ import { and, eq } from "drizzle-orm";
import { bookmarks, tagsOnBookmarks } from "@karakeep/db/schema";
import { LinkCrawlerQueue } from "@karakeep/shared-server";
+import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";
import {
RuleEngineAction,
RuleEngineCondition,
@@ -83,6 +84,12 @@ export class RuleEngine {
case "urlContains": {
return (this.bookmark.link?.url ?? "").includes(condition.str);
}
+ case "urlDoesNotContain": {
+ return (
+ this.bookmark.type == BookmarkTypes.LINK &&
+ !(this.bookmark.link?.url ?? "").includes(condition.str)
+ );
+ }
case "importedFromFeed": {
return this.bookmark.rssFeeds.some(
(f) => f.rssFeedId === condition.feedId,