Skip to content

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

feat: add checkUrl endpoint to replace searchBookmarks for URL existence checks

Mohamed Bassemb24927358fe3
diff --git a/apps/browser-extension/src/utils/badgeCache.ts b/apps/browser-extension/src/utils/badgeCache.ts
index caa4231d..dc91e962 100644
--- a/apps/browser-extension/src/utils/badgeCache.ts
+++ b/apps/browser-extension/src/utils/badgeCache.ts
@@ -1,9 +1,6 @@
// Badge count cache helpers
-import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";
-
import { getPluginSettings } from "./settings";
import { getApiClient, getQueryClient } from "./trpc";
-import { urlsMatchIgnoringAnchorAndTrailingSlash } from "./url";
/**
* Fetches the bookmark status for a given URL from the API.
@@ -18,28 +15,12 @@ async function fetchBadgeStatus(url: string): Promise<string | null> {
throw new Error("[badgeCache] API client not configured");
}
try {
- const data = await api.bookmarks.searchBookmarks.query({
- text: "url:" + url,
- });
- const bookmarks = data.bookmarks;
- const bookmarksLength = bookmarks.length;
- if (bookmarksLength === 0) {
- return null;
- }
-
- // First check the exact match (including anchor points)
- const exactMatch =
- bookmarks.find(
- (b) =>
- b.content.type === BookmarkTypes.LINK &&
- urlsMatchIgnoringAnchorAndTrailingSlash(url, b.content.url),
- ) || null;
-
- return exactMatch ? exactMatch.id : null;
+ const data = await api.bookmarks.checkUrl.query({ url });
+ return data.bookmarkId;
} catch (error) {
console.error(`[badgeCache] Failed to fetch status for ${url}:`, error);
// In case of API error, return a non-cacheable empty status
- // Propagate so cache treats this as a miss and doesn’t store
+ // Propagate so cache treats this as a miss and doesn't store
throw error;
}
}
diff --git a/packages/api/routes/bookmarks.ts b/packages/api/routes/bookmarks.ts
index 394c127c..e24d2ff8 100644
--- a/packages/api/routes/bookmarks.ts
+++ b/packages/api/routes/bookmarks.ts
@@ -90,6 +90,23 @@ const app = new Hono()
);
},
)
+
+ // GET /bookmarks/check-url
+ .get(
+ "/check-url",
+ zValidator(
+ "query",
+ z.object({
+ url: z.string(),
+ }),
+ ),
+ async (c) => {
+ const { url } = c.req.valid("query");
+ const result = await c.var.api.bookmarks.checkUrl({ url });
+ return c.json(result, 200);
+ },
+ )
+
.post(
"/singlefile",
zValidator(
diff --git a/packages/open-api/karakeep-openapi-spec.json b/packages/open-api/karakeep-openapi-spec.json
index a1b9dfc4..8cc3b7ff 100644
--- a/packages/open-api/karakeep-openapi-spec.json
+++ b/packages/open-api/karakeep-openapi-spec.json
@@ -983,6 +983,51 @@
}
}
},
+ "/bookmarks/check-url": {
+ "get": {
+ "description": "Check if a URL is already bookmarked. Uses substring matching to find candidates, then normalizes URLs (ignoring hash fragments and trailing slashes) for exact comparison.",
+ "summary": "Check if a URL exists in bookmarks",
+ "tags": [
+ "Bookmarks"
+ ],
+ "security": [
+ {
+ "bearerAuth": []
+ }
+ ],
+ "parameters": [
+ {
+ "schema": {
+ "type": "string"
+ },
+ "required": true,
+ "name": "url",
+ "in": "query"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "Object indicating whether the URL is bookmarked. bookmarkId is null if not found.",
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "object",
+ "properties": {
+ "bookmarkId": {
+ "type": "string",
+ "nullable": true
+ }
+ },
+ "required": [
+ "bookmarkId"
+ ]
+ }
+ }
+ }
+ }
+ }
+ }
+ },
"/bookmarks/{bookmarkId}": {
"get": {
"description": "Get bookmark by its id",
diff --git a/packages/open-api/lib/bookmarks.ts b/packages/open-api/lib/bookmarks.ts
index 0a3806b9..d52fe108 100644
--- a/packages/open-api/lib/bookmarks.ts
+++ b/packages/open-api/lib/bookmarks.ts
@@ -100,6 +100,34 @@ registry.registerPath({
},
});
+registry.registerPath({
+ method: "get",
+ path: "/bookmarks/check-url",
+ description:
+ "Check if a URL is already bookmarked. Uses substring matching to find candidates, then normalizes URLs (ignoring hash fragments and trailing slashes) for exact comparison.",
+ summary: "Check if a URL exists in bookmarks",
+ tags: ["Bookmarks"],
+ security: [{ [BearerAuth.name]: [] }],
+ request: {
+ query: z.object({
+ url: z.string(),
+ }),
+ },
+ responses: {
+ 200: {
+ description:
+ "Object indicating whether the URL is bookmarked. bookmarkId is null if not found.",
+ content: {
+ "application/json": {
+ schema: z.object({
+ bookmarkId: z.string().nullable(),
+ }),
+ },
+ },
+ },
+ },
+});
+
registry.registerPath({
method: "post",
path: "/bookmarks",
diff --git a/packages/trpc/routers/bookmarks.test.ts b/packages/trpc/routers/bookmarks.test.ts
index 3de51c6c..26fb17f7 100644
--- a/packages/trpc/routers/bookmarks.test.ts
+++ b/packages/trpc/routers/bookmarks.test.ts
@@ -1380,4 +1380,93 @@ describe("Bookmark Routes", () => {
);
});
});
+
+ describe("checkUrl", () => {
+ test<CustomTestContext>("returns null for non-existent URL", async ({
+ apiCallers,
+ }) => {
+ const api = apiCallers[0].bookmarks;
+ const result = await api.checkUrl({
+ url: "https://nonexistent.example.com",
+ });
+ expect(result.bookmarkId).toBeNull();
+ });
+
+ test<CustomTestContext>("returns bookmark id for exact URL match", async ({
+ apiCallers,
+ }) => {
+ const api = apiCallers[0].bookmarks;
+ const bookmark = await api.createBookmark({
+ url: "https://example.com/page",
+ type: BookmarkTypes.LINK,
+ });
+
+ const result = await api.checkUrl({
+ url: "https://example.com/page",
+ });
+ expect(result.bookmarkId).toEqual(bookmark.id);
+ });
+
+ test<CustomTestContext>("matches URL ignoring trailing slash", async ({
+ apiCallers,
+ }) => {
+ const api = apiCallers[0].bookmarks;
+ const bookmark = await api.createBookmark({
+ url: "https://example.com/page/",
+ type: BookmarkTypes.LINK,
+ });
+
+ const result = await api.checkUrl({
+ url: "https://example.com/page",
+ });
+ expect(result.bookmarkId).toEqual(bookmark.id);
+ });
+
+ test<CustomTestContext>("matches URL ignoring hash fragment", async ({
+ apiCallers,
+ }) => {
+ const api = apiCallers[0].bookmarks;
+ const bookmark = await api.createBookmark({
+ url: "https://example.com/page",
+ type: BookmarkTypes.LINK,
+ });
+
+ const result = await api.checkUrl({
+ url: "https://example.com/page#section",
+ });
+ expect(result.bookmarkId).toEqual(bookmark.id);
+ });
+
+ test<CustomTestContext>("does not match different URLs on same domain", async ({
+ apiCallers,
+ }) => {
+ const api = apiCallers[0].bookmarks;
+ await api.createBookmark({
+ url: "https://example.com/page-one",
+ type: BookmarkTypes.LINK,
+ });
+
+ const result = await api.checkUrl({
+ url: "https://example.com/page-two",
+ });
+ expect(result.bookmarkId).toBeNull();
+ });
+
+ test<CustomTestContext>("does not return bookmarks from other users", async ({
+ apiCallers,
+ }) => {
+ const api1 = apiCallers[0].bookmarks;
+ const api2 = apiCallers[1].bookmarks;
+
+ await api1.createBookmark({
+ url: "https://example.com/private",
+ type: BookmarkTypes.LINK,
+ });
+
+ const result = await api2.checkUrl({
+ url: "https://example.com/private",
+ });
+ expect(result.bookmarkId).toBeNull();
+ });
+ });
});
diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts
index 159e12c4..ed60e2df 100644
--- a/packages/trpc/routers/bookmarks.ts
+++ b/packages/trpc/routers/bookmarks.ts
@@ -1,5 +1,5 @@
import { experimental_trpcMiddleware, TRPCError } from "@trpc/server";
-import { and, eq, gt, inArray, lt, or } from "drizzle-orm";
+import { and, eq, gt, inArray, like, lt, or } from "drizzle-orm";
import { z } from "zod";
import type { ZBookmarkContent } from "@karakeep/shared/types/bookmarks";
@@ -749,6 +749,54 @@ export const bookmarksAppRouter = router({
},
};
}),
+ checkUrl: authedProcedure
+ .input(
+ z.object({
+ url: z.string(),
+ }),
+ )
+ .output(
+ z.object({
+ bookmarkId: z.string().nullable(),
+ }),
+ )
+ .query(async ({ input, ctx }) => {
+ // Normalize and compare URLs (ignoring hash fragment and trailing slash)
+ function normalizeUrl(url: string): string {
+ const u = new URL(url);
+ u.hash = "";
+ let pathname = u.pathname;
+ if (pathname.endsWith("/") && pathname !== "/") {
+ pathname = pathname.slice(0, -1);
+ }
+ u.pathname = pathname;
+ return u.toString();
+ }
+
+ // Strip hash before querying so the LIKE clause can match
+ const normalizedInput = normalizeUrl(input.url);
+
+ const results = await ctx.db
+ .select({ id: bookmarkLinks.id, url: bookmarkLinks.url })
+ .from(bookmarkLinks)
+ .leftJoin(bookmarks, eq(bookmarks.id, bookmarkLinks.id))
+ .where(
+ and(
+ eq(bookmarks.userId, ctx.user.id),
+ like(bookmarkLinks.url, `${normalizedInput}%`),
+ ),
+ );
+
+ if (results.length === 0) {
+ return { bookmarkId: null };
+ }
+
+ const exactMatch = results.find(
+ (r) => r.url && normalizeUrl(r.url) === normalizedInput,
+ );
+
+ return { bookmarkId: exactMatch?.id ?? null };
+ }),
getBookmarks: authedProcedure
.input(zGetBookmarksRequestSchema)
.output(zGetBookmarksResponseSchema)