From bbbf33536c6c1a73fdfe76c42ff19cd08aabcc6f Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Mon, 13 Jan 2025 00:00:30 +0000 Subject: fix: Fix url matcher not matching sourceUrl. Fixes #874 --- packages/trpc/lib/__tests__/search.test.ts | 3 ++- packages/trpc/lib/search.ts | 27 +++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) (limited to 'packages/trpc/lib') diff --git a/packages/trpc/lib/__tests__/search.test.ts b/packages/trpc/lib/__tests__/search.test.ts index 8a6f1949..7f573b4f 100644 --- a/packages/trpc/lib/__tests__/search.test.ts +++ b/packages/trpc/lib/__tests__/search.test.ts @@ -111,6 +111,7 @@ beforeEach(async () => { assetType: "image", fileName: "test.png", assetId: "asset-id", + sourceUrl: "https://example.com/image.png", }, ]); @@ -236,7 +237,7 @@ describe("getBookmarkIdsFromMatcher", () => { inverse: false, }; const result = await getBookmarkIdsFromMatcher(mockCtx, matcher); - expect(result).toEqual(["b1", "b4"]); + expect(result).toEqual(["b1", "b4", "b6"]); }); it("should handle url matcher with inverse=true", async () => { diff --git a/packages/trpc/lib/search.ts b/packages/trpc/lib/search.ts index eac323df..de76748b 100644 --- a/packages/trpc/lib/search.ts +++ b/packages/trpc/lib/search.ts @@ -4,6 +4,7 @@ import { exists, gt, gte, + isNotNull, like, lt, lte, @@ -13,6 +14,7 @@ import { } from "drizzle-orm"; import { + bookmarkAssets, bookmarkLinks, bookmarkLists, bookmarks, @@ -197,6 +199,20 @@ async function getIds( eq(bookmarks.userId, userId), comp(bookmarkLinks.url, `%${matcher.url}%`), ), + ) + .union( + db + .select({ id: bookmarkAssets.id }) + .from(bookmarkAssets) + .leftJoin(bookmarks, eq(bookmarks.id, bookmarkAssets.id)) + .where( + and( + eq(bookmarks.userId, userId), + // When a user is asking for a link, the inverse matcher should match only assets with URLs. + isNotNull(bookmarkAssets.sourceUrl), + comp(bookmarkAssets.sourceUrl, `%${matcher.url}%`), + ), + ), ); } case "favourited": { @@ -235,13 +251,16 @@ async function getIds( ); } case "type": { - const comp = matcher.inverse - ? ne(bookmarks.type, matcher.typeName) - : eq(bookmarks.type, matcher.typeName); + const comp = matcher.inverse ? ne : eq; return db .select({ id: bookmarks.id }) .from(bookmarks) - .where(and(eq(bookmarks.userId, userId), comp)); + .where( + and( + eq(bookmarks.userId, userId), + comp(bookmarks.type, matcher.typeName), + ), + ); } case "and": { const vals = await Promise.all( -- cgit v1.2.3-70-g09d2