Skip to content

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

feat: support bookmark source condition in rule engine (#2526) (#2547)

冰水泡枸杞b5d628ccd48c
diff --git a/apps/web/components/dashboard/rules/RuleEngineConditionBuilder.tsx b/apps/web/components/dashboard/rules/RuleEngineConditionBuilder.tsx
index 28bf690d..47a2cee7 100644
--- a/apps/web/components/dashboard/rules/RuleEngineConditionBuilder.tsx
+++ b/apps/web/components/dashboard/rules/RuleEngineConditionBuilder.tsx
@@ -33,6 +33,7 @@ import type {
RuleEngineCondition,
RuleEngineEvent,
} from "@karakeep/shared/types/rules";
+import { zBookmarkSourceSchema } from "@karakeep/shared/types/bookmarks";
import { FeedSelector } from "../feeds/FeedSelector";
import { TagAutocomplete } from "../tags/TagAutocomplete";
@@ -75,6 +76,9 @@ export function ConditionBuilder({
case "bookmarkTypeIs":
onChange({ type: "bookmarkTypeIs", bookmarkType: "link" });
break;
+ case "bookmarkSourceIs":
+ onChange({ type: "bookmarkSourceIs", source: "rss" });
+ break;
case "hasTag":
onChange({ type: "hasTag", tagId: "" });
break;
@@ -112,6 +116,8 @@ export function ConditionBuilder({
return <Rss className="h-4 w-4" />;
case "bookmarkTypeIs":
return <FileType className="h-4 w-4" />;
+ case "bookmarkSourceIs":
+ return <Rss className="h-4 w-4" />;
case "hasTag":
return <Tag className="h-4 w-4" />;
case "isFavourited":
@@ -214,6 +220,33 @@ export function ConditionBuilder({
</div>
);
+ case "bookmarkSourceIs":
+ return (
+ <div className="mt-2">
+ <Select
+ value={value.source}
+ onValueChange={(source) =>
+ onChange({
+ ...value,
+ source:
+ source as (typeof zBookmarkSourceSchema.options)[number],
+ })
+ }
+ >
+ <SelectTrigger>
+ <SelectValue placeholder="Select bookmark source" />
+ </SelectTrigger>
+ <SelectContent>
+ {zBookmarkSourceSchema.options.map((source) => (
+ <SelectItem key={source} value={source}>
+ {source}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+ );
+
case "hasTag":
return (
<div className="mt-2">
@@ -314,6 +347,9 @@ export function ConditionBuilder({
<SelectItem value="bookmarkTypeIs">
{t("settings.rules.conditions_types.bookmark_type_is")}
</SelectItem>
+ <SelectItem value="bookmarkSourceIs">
+ {t("settings.rules.conditions_types.bookmark_source_is")}
+ </SelectItem>
<SelectItem value="hasTag">
{t("settings.rules.conditions_types.has_tag")}
</SelectItem>
diff --git a/apps/web/lib/i18n/locales/en/translation.json b/apps/web/lib/i18n/locales/en/translation.json
index 03206c82..59c24cc9 100644
--- a/apps/web/lib/i18n/locales/en/translation.json
+++ b/apps/web/lib/i18n/locales/en/translation.json
@@ -367,6 +367,7 @@
"title_does_not_contain": "Title Does Not Contain",
"imported_from_feed": "Imported From Feed",
"bookmark_type_is": "Bookmark Type Is",
+ "bookmark_source_is": "Bookmark Source Is",
"has_tag": "Has Tag",
"is_favourited": "Is Favourited",
"is_archived": "Is Archived",
diff --git a/packages/shared/types/rules.ts b/packages/shared/types/rules.ts
index fd99c266..adf44823 100644
--- a/packages/shared/types/rules.ts
+++ b/packages/shared/types/rules.ts
@@ -1,5 +1,7 @@
import { RefinementCtx, z } from "zod";
+import { zBookmarkSourceSchema } from "./bookmarks";
+
// Events
const zBookmarkAddedEvent = z.object({
type: z.literal("bookmarkAdded"),
@@ -79,6 +81,11 @@ const zBookmarkTypeIsCondition = z.object({
bookmarkType: z.enum(["link", "text", "asset"]),
});
+const zBookmarkSourceIsCondition = z.object({
+ type: z.literal("bookmarkSourceIs"),
+ source: zBookmarkSourceSchema,
+});
+
const zHasTagCondition = z.object({
type: z.literal("hasTag"),
tagId: z.string(),
@@ -100,6 +107,7 @@ const nonRecursiveCondition = z.discriminatedUnion("type", [
zTitleDoesNotContainCondition,
zImportedFromFeedCondition,
zBookmarkTypeIsCondition,
+ zBookmarkSourceIsCondition,
zHasTagCondition,
zIsFavouritedCondition,
zIsArchivedCondition,
@@ -121,6 +129,7 @@ export const zRuleEngineConditionSchema: z.ZodType<RuleEngineCondition> =
zTitleDoesNotContainCondition,
zImportedFromFeedCondition,
zBookmarkTypeIsCondition,
+ zBookmarkSourceIsCondition,
zHasTagCondition,
zIsFavouritedCondition,
zIsArchivedCondition,
@@ -244,6 +253,7 @@ const ruleValidaitorFn = (
switch (condition.type) {
case "alwaysTrue":
case "bookmarkTypeIs":
+ case "bookmarkSourceIs":
case "isFavourited":
case "isArchived":
return true;
diff --git a/packages/trpc/lib/__tests__/ruleEngine.test.ts b/packages/trpc/lib/__tests__/ruleEngine.test.ts
index b737e3a5..aa556759 100644
--- a/packages/trpc/lib/__tests__/ruleEngine.test.ts
+++ b/packages/trpc/lib/__tests__/ruleEngine.test.ts
@@ -129,6 +129,7 @@ describe("RuleEngine", () => {
title: "Example Bookmark Title",
favourited: false,
archived: false,
+ source: "rss",
})
.returning({ id: bookmarks.id })
).map((b) => b.id);
@@ -299,6 +300,22 @@ describe("RuleEngine", () => {
expect(engine.doesBookmarkMatchConditions(condition)).toBe(false);
});
+ it("should return true for bookmarkSourceIs condition", () => {
+ const condition: RuleEngineCondition = {
+ type: "bookmarkSourceIs",
+ source: "rss",
+ };
+ expect(engine.doesBookmarkMatchConditions(condition)).toBe(true);
+ });
+
+ it("should return false for bookmarkSourceIs condition mismatch", () => {
+ const condition: RuleEngineCondition = {
+ type: "bookmarkSourceIs",
+ source: "web",
+ };
+ expect(engine.doesBookmarkMatchConditions(condition)).toBe(false);
+ });
+
it("should return true for hasTag condition", () => {
const condition: RuleEngineCondition = { type: "hasTag", tagId: tagId1 };
expect(engine.doesBookmarkMatchConditions(condition)).toBe(true);
diff --git a/packages/trpc/lib/ruleEngine.ts b/packages/trpc/lib/ruleEngine.ts
index acfd747e..66b84508 100644
--- a/packages/trpc/lib/ruleEngine.ts
+++ b/packages/trpc/lib/ruleEngine.ts
@@ -118,6 +118,9 @@ export class RuleEngine {
case "bookmarkTypeIs": {
return this.bookmark.type === condition.bookmarkType;
}
+ case "bookmarkSourceIs": {
+ return this.bookmark.source === condition.source;
+ }
case "hasTag": {
return this.bookmark.tagsOnBookmarks.some(
(t) => t.tagId === condition.tagId,