From 545cac1967f6882780021407a474690fea3f11ed Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Tue, 1 Jul 2025 22:51:59 +0000 Subject: feat: Add per user bookmark count quota --- packages/trpc/routers/bookmarks.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'packages/trpc/routers/bookmarks.ts') diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts index 2a02a0cd..f1fe10d7 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, count, eq, gt, inArray, lt, or } from "drizzle-orm"; import invariant from "tiny-invariant"; import { z } from "zod"; @@ -19,6 +19,7 @@ import { bookmarkTexts, customPrompts, tagsOnBookmarks, + users, } from "@karakeep/db/schema"; import { deleteAsset, @@ -267,6 +268,28 @@ export const bookmarksAppRouter = router({ return { ...alreadyExists, alreadyExists: true }; } } + + // Check user quota + const user = await ctx.db.query.users.findFirst({ + where: eq(users.id, ctx.user.id), + columns: { + bookmarkQuota: true, + }, + }); + + if (user?.bookmarkQuota !== null && user?.bookmarkQuota !== undefined) { + const currentBookmarkCount = await ctx.db + .select({ count: count() }) + .from(bookmarks) + .where(eq(bookmarks.userId, ctx.user.id)); + + if (currentBookmarkCount[0].count >= user.bookmarkQuota) { + throw new TRPCError({ + code: "FORBIDDEN", + message: `Bookmark quota exceeded. You can only have ${user.bookmarkQuota} bookmarks.`, + }); + } + } const bookmark = await ctx.db.transaction(async (tx) => { const bookmark = ( await tx -- cgit v1.3-1-g0d28