From 136f126296af65f50da598d084d1485c0e40437a Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sun, 27 Apr 2025 00:02:20 +0100 Subject: feat: Implement generic rule engine (#1318) * Add schema for the new rule engine * Add rule engine backend logic * Implement the worker logic and event firing * Implement the UI changesfor the rule engine * Ensure that when a referenced list or tag are deleted, the corresponding event/action is * Dont show smart lists in rule engine events * Add privacy validations for attached tag and list ids * Move the rules logic into a models --- packages/trpc/models/lists.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'packages/trpc/models/lists.ts') diff --git a/packages/trpc/models/lists.ts b/packages/trpc/models/lists.ts index 8072060f..4da127d2 100644 --- a/packages/trpc/models/lists.ts +++ b/packages/trpc/models/lists.ts @@ -5,6 +5,7 @@ import { z } from "zod"; import { SqliteError } from "@karakeep/db"; import { bookmarkLists, bookmarksInLists } from "@karakeep/db/schema"; +import { triggerRuleEngineOnEvent } from "@karakeep/shared/queues"; import { parseSearchQuery } from "@karakeep/shared/searchQueryParser"; import { ZBookmarkList, @@ -117,7 +118,9 @@ export abstract class List implements PrivacyAware { } } - async update(input: z.infer) { + async update( + input: z.infer, + ): Promise { const result = await this.ctx.db .update(bookmarkLists) .set({ @@ -137,7 +140,7 @@ export abstract class List implements PrivacyAware { if (result.length == 0) { throw new TRPCError({ code: "NOT_FOUND" }); } - return result[0]; + this.list = result[0]; } abstract get type(): "manual" | "smart"; @@ -248,6 +251,12 @@ export class ManualList extends List { listId: this.list.id, bookmarkId, }); + await triggerRuleEngineOnEvent(bookmarkId, [ + { + type: "addedToList", + listId: this.list.id, + }, + ]); } catch (e) { if (e instanceof SqliteError) { if (e.code == "SQLITE_CONSTRAINT_PRIMARYKEY") { @@ -279,6 +288,12 @@ export class ManualList extends List { message: `Bookmark ${bookmarkId} is already not in list ${this.list.id}`, }); } + await triggerRuleEngineOnEvent(bookmarkId, [ + { + type: "removedFromList", + listId: this.list.id, + }, + ]); } async update(input: z.infer) { -- cgit v1.2.3-70-g09d2