Skip to content

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

feat: add a way to purge bookmarks of user over quota

Mohamed Bassem9232defcac64
diff --git a/apps/cli/src/commands/admin.ts b/apps/cli/src/commands/admin.ts
index f63ed1a4..621f10a3 100644
--- a/apps/cli/src/commands/admin.ts
+++ b/apps/cli/src/commands/admin.ts
@@ -1,3 +1,5 @@
+import { stdin as input, stdout as output } from "node:process";
+import readline from "node:readline/promises";
import { getGlobalOptions } from "@/lib/globals";
import {
printErrorMessageWithReason,
@@ -131,6 +133,72 @@ usersCmd
}
});
+usersCmd
+ .command("purge-over-quota")
+ .description(
+ "delete a user's oldest bookmarks until they are within their bookmark quota",
+ )
+ .argument("<userId>", "the id of the user to purge bookmarks for")
+ .option("--dry-run", "only show how many bookmarks would be deleted")
+ .option("-y, --yes", "skip confirmation prompt")
+ .action(async (userId, opts) => {
+ const api = getAPIClient();
+
+ try {
+ const plan = await api.admin.purgeBookmarksOverQuota.mutate({
+ userId,
+ dryRun: true,
+ });
+
+ if (plan.numBookmarksToDelete === 0 || opts.dryRun) {
+ if (getGlobalOptions().json) {
+ printObject(plan);
+ } else {
+ printStatusMessage(
+ true,
+ `User has ${plan.numBookmarks} bookmarks with a quota of ${plan.bookmarkQuota}. ${plan.numBookmarksToDelete} bookmarks would be deleted.`,
+ );
+ }
+ return;
+ }
+
+ if (!opts.yes) {
+ const rl = readline.createInterface({ input, output });
+ const answer = (
+ await rl.question(
+ `User has ${plan.numBookmarks} bookmarks with a quota of ${plan.bookmarkQuota}. This will permanently delete their ${plan.numBookmarksToDelete} oldest bookmarks. Proceed? (yes/no): `,
+ )
+ )
+ .trim()
+ .toLowerCase();
+ rl.close();
+ if (answer !== "y" && answer !== "yes") {
+ printStatusMessage(false, "Purge aborted by user");
+ return;
+ }
+ }
+
+ const result = await api.admin.purgeBookmarksOverQuota.mutate({
+ userId,
+ });
+ if (getGlobalOptions().json) {
+ printObject(result);
+ } else {
+ printStatusMessage(
+ true,
+ result.enqueued
+ ? `Purge of ${result.numBookmarksToDelete} bookmarks queued`
+ : "User is already within their bookmark quota",
+ );
+ }
+ } catch (error) {
+ printErrorMessageWithReason(
+ "Failed to purge bookmarks over quota",
+ error as object,
+ );
+ }
+ });
+
adminCmd.addCommand(usersCmd);
// --- Bookmarks subcommand ---
diff --git a/apps/workers/workers/adminMaintenance/tasks/purgeBookmarksOverQuota.test.ts b/apps/workers/workers/adminMaintenance/tasks/purgeBookmarksOverQuota.test.ts
new file mode 100644
index 00000000..7e4a7979
--- /dev/null
+++ b/apps/workers/workers/adminMaintenance/tasks/purgeBookmarksOverQuota.test.ts
@@ -0,0 +1,194 @@
+import { asc, eq } from "drizzle-orm";
+import { beforeEach, describe, expect, test, vi } from "vitest";
+
+import type { ZAdminMaintenanceTask } from "@karakeep/shared-server";
+import type { DequeuedJob } from "@karakeep/shared/queueing";
+import { bookmarks, users } from "@karakeep/db/schema";
+import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";
+
+import { runPurgeBookmarksOverQuotaTask } from "./purgeBookmarksOverQuota";
+
+const mocks = await vi.hoisted(async () => {
+ const { getInMemoryDB } = await import("@karakeep/db/drizzle");
+ return {
+ db: getInMemoryDB(true),
+ adminMaintenanceEnqueue: vi.fn(),
+ searchIndexingEnqueue: vi.fn(),
+ embeddingsEnqueue: vi.fn(),
+ };
+});
+
+vi.mock("@karakeep/db", async (original) => ({
+ ...(await original<typeof import("@karakeep/db")>()),
+ db: mocks.db,
+}));
+
+vi.mock("@karakeep/shared-server", async (original) => ({
+ ...(await original<typeof import("@karakeep/shared-server")>()),
+ AdminMaintenanceQueue: { enqueue: mocks.adminMaintenanceEnqueue },
+ SearchIndexingQueue: { enqueue: mocks.searchIndexingEnqueue },
+ EmbeddingsQueue: { enqueue: mocks.embeddingsEnqueue },
+}));
+
+const db = mocks.db;
+
+function buildJob(
+ task: ZAdminMaintenanceTask,
+ abortSignal = new AbortController().signal,
+): DequeuedJob<ZAdminMaintenanceTask> {
+ return { id: "job", data: task, priority: 0, runNumber: 0, abortSignal };
+}
+
+async function createUserWithBookmarks(
+ bookmarkQuota: number | null,
+ numBookmarks: number,
+) {
+ const [user] = await db
+ .insert(users)
+ .values({
+ name: "Test",
+ email: `${crypto.randomUUID()}@test.com`,
+ bookmarkQuota,
+ })
+ .returning();
+ const base = new Date("2026-01-01T00:00:00.000Z").getTime();
+ const created =
+ numBookmarks > 0
+ ? await db
+ .insert(bookmarks)
+ .values(
+ Array.from(
+ { length: numBookmarks },
+ (_, i): typeof bookmarks.$inferInsert => ({
+ userId: user.id,
+ type: BookmarkTypes.TEXT,
+ title: `bookmark-${i}`,
+ createdAt: new Date(base + i * 1000),
+ }),
+ ),
+ )
+ .returning()
+ : [];
+ return { user, bookmarkIds: created.map((b) => b.id) };
+}
+
+async function remainingBookmarkIds(userId: string) {
+ const rows = await db
+ .select({ id: bookmarks.id })
+ .from(bookmarks)
+ .where(eq(bookmarks.userId, userId))
+ .orderBy(asc(bookmarks.createdAt));
+ return rows.map((r) => r.id);
+}
+
+const fastOpts = {
+ batchSize: 2,
+ interBatchPauseMs: 0,
+ runTimeBudgetMs: 60_000,
+};
+
+describe("purge_bookmarks_over_quota", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ test("deletes the oldest bookmarks until the user is within quota", async () => {
+ const { user, bookmarkIds } = await createUserWithBookmarks(3, 8);
+ const task = {
+ type: "purge_bookmarks_over_quota" as const,
+ args: { userId: user.id },
+ };
+
+ await runPurgeBookmarksOverQuotaTask(buildJob(task), task, fastOpts);
+
+ expect(await remainingBookmarkIds(user.id)).toEqual(bookmarkIds.slice(5));
+ expect(mocks.searchIndexingEnqueue).toHaveBeenCalledTimes(5);
+ expect(mocks.embeddingsEnqueue).toHaveBeenCalledTimes(5);
+ expect(mocks.adminMaintenanceEnqueue).not.toHaveBeenCalled();
+ });
+
+ test("doesn't touch other users' bookmarks", async () => {
+ const { user } = await createUserWithBookmarks(1, 3);
+ const other = await createUserWithBookmarks(1, 3);
+ const task = {
+ type: "purge_bookmarks_over_quota" as const,
+ args: { userId: user.id },
+ };
+
+ await runPurgeBookmarksOverQuotaTask(buildJob(task), task, fastOpts);
+
+ expect(await remainingBookmarkIds(user.id)).toHaveLength(1);
+ expect(await remainingBookmarkIds(other.user.id)).toEqual(
+ other.bookmarkIds,
+ );
+ });
+
+ test("is a no-op for users without a quota or within quota", async () => {
+ const unlimited = await createUserWithBookmarks(null, 3);
+ const within = await createUserWithBookmarks(5, 3);
+
+ for (const { user } of [unlimited, within]) {
+ const task = {
+ type: "purge_bookmarks_over_quota" as const,
+ args: { userId: user.id },
+ };
+ await runPurgeBookmarksOverQuotaTask(buildJob(task), task, fastOpts);
+ expect(await remainingBookmarkIds(user.id)).toHaveLength(3);
+ }
+ expect(mocks.searchIndexingEnqueue).not.toHaveBeenCalled();
+ });
+
+ test("is a no-op for a missing user", async () => {
+ const task = {
+ type: "purge_bookmarks_over_quota" as const,
+ args: { userId: "does-not-exist" },
+ };
+ await runPurgeBookmarksOverQuotaTask(buildJob(task), task, fastOpts);
+ expect(mocks.adminMaintenanceEnqueue).not.toHaveBeenCalled();
+ });
+
+ test("yields by enqueueing a continuation once the time budget is spent", async () => {
+ const { user, bookmarkIds } = await createUserWithBookmarks(1, 6);
+ const task = {
+ type: "purge_bookmarks_over_quota" as const,
+ args: { userId: user.id },
+ };
+
+ // With a zero budget, every run deletes exactly one batch then yields.
+ const opts = { ...fastOpts, runTimeBudgetMs: 0 };
+
+ await runPurgeBookmarksOverQuotaTask(buildJob(task), task, opts);
+ expect(await remainingBookmarkIds(user.id)).toEqual(bookmarkIds.slice(2));
+ expect(mocks.adminMaintenanceEnqueue).toHaveBeenCalledTimes(1);
+ expect(mocks.adminMaintenanceEnqueue).toHaveBeenLastCalledWith(task);
+
+ await runPurgeBookmarksOverQuotaTask(buildJob(task), task, opts);
+ expect(await remainingBookmarkIds(user.id)).toEqual(bookmarkIds.slice(4));
+ expect(mocks.adminMaintenanceEnqueue).toHaveBeenCalledTimes(2);
+
+ // The last bookmark over quota fits in the final batch, so no more
+ // continuations are enqueued.
+ await runPurgeBookmarksOverQuotaTask(buildJob(task), task, opts);
+ expect(await remainingBookmarkIds(user.id)).toEqual(bookmarkIds.slice(5));
+ expect(mocks.adminMaintenanceEnqueue).toHaveBeenCalledTimes(2);
+ });
+
+ test("stops when the job is aborted", async () => {
+ const { user } = await createUserWithBookmarks(1, 6);
+ const task = {
+ type: "purge_bookmarks_over_quota" as const,
+ args: { userId: user.id },
+ };
+ const controller = new AbortController();
+ controller.abort();
+
+ await runPurgeBookmarksOverQuotaTask(
+ buildJob(task, controller.signal),
+ task,
+ fastOpts,
+ );
+
+ expect(await remainingBookmarkIds(user.id)).toHaveLength(6);
+ expect(mocks.adminMaintenanceEnqueue).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/workers/workers/adminMaintenance/tasks/purgeBookmarksOverQuota.ts b/apps/workers/workers/adminMaintenance/tasks/purgeBookmarksOverQuota.ts
new file mode 100644
index 00000000..b0877ac4
--- /dev/null
+++ b/apps/workers/workers/adminMaintenance/tasks/purgeBookmarksOverQuota.ts
@@ -0,0 +1,132 @@
+import { TRPCError } from "@trpc/server";
+import { asc, count, eq } from "drizzle-orm";
+
+import type {
+ ZAdminMaintenancePurgeBookmarksOverQuotaTask,
+ ZAdminMaintenanceTask,
+} from "@karakeep/shared-server";
+import type { DequeuedJob } from "@karakeep/shared/queueing";
+import { db } from "@karakeep/db";
+import { bookmarks, users } from "@karakeep/db/schema";
+import { AdminMaintenanceQueue } from "@karakeep/shared-server";
+import logger from "@karakeep/shared/logger";
+import { buildImpersonatingAuthedContext } from "@karakeep/trpc/lib/impersonate";
+import { Bookmark } from "@karakeep/trpc/models/bookmarks";
+
+const DEFAULT_OPTS = {
+ // Bookmarks deleted between re-checks of the user's excess.
+ batchSize: 50,
+ // Pause between batches to let other writers grab the SQLite write lock
+ // and to let the downstream queues (search, embeddings, webhooks) catch up.
+ interBatchPauseMs: 250,
+ // How long a single run is allowed to keep deleting before it yields by
+ // enqueueing a continuation job. Must stay well below the worker's timeout.
+ // Yielding keeps a huge purge from monopolizing the (concurrency 1) admin
+ // maintenance worker and from being killed mid-way by the job timeout.
+ runTimeBudgetMs: 2 * 60 * 1000,
+};
+
+function sleep(ms: number): Promise<void> {
+ return new Promise((resolve) => setTimeout(resolve, ms));
+}
+
+/**
+ * Returns how many bookmarks the user has above their bookmark quota, or
+ * null if the user doesn't exist.
+ */
+async function getNumBookmarksOverQuota(
+ userId: string,
+): Promise<number | null> {
+ const user = await db.query.users.findFirst({
+ where: eq(users.id, userId),
+ columns: { bookmarkQuota: true },
+ });
+ if (!user) {
+ return null;
+ }
+ if (user.bookmarkQuota === null) {
+ return 0;
+ }
+ const [{ numBookmarks }] = await db
+ .select({ numBookmarks: count() })
+ .from(bookmarks)
+ .where(eq(bookmarks.userId, userId));
+ return Math.max(0, numBookmarks - user.bookmarkQuota);
+}
+
+/**
+ * Deletes a user's oldest bookmarks until they are back within their bookmark
+ * quota.
+ *
+ * The excess is recomputed from the live state before every batch, so the
+ * task carries no cursor: a re-run, retry, duplicate or continuation job
+ * simply picks up whatever is still over quota (and quota changes made while
+ * the purge is running are respected).
+ */
+export async function runPurgeBookmarksOverQuotaTask(
+ job: DequeuedJob<ZAdminMaintenanceTask>,
+ task: ZAdminMaintenancePurgeBookmarksOverQuotaTask,
+ opts = DEFAULT_OPTS,
+): Promise<void> {
+ const jobId = job.id;
+ const { userId } = task.args;
+ const logPrefix = `[adminMaintenance:purge_bookmarks_over_quota][${jobId}]`;
+ const startedAt = Date.now();
+ let deletedCount = 0;
+
+ while (true) {
+ if (job.abortSignal.aborted) {
+ logger.warn(
+ `${logPrefix} Aborted after deleting ${deletedCount} bookmarks for user ${userId}`,
+ );
+ return;
+ }
+
+ const numOverQuota = await getNumBookmarksOverQuota(userId);
+ if (numOverQuota === null) {
+ logger.warn(`${logPrefix} User ${userId} not found, nothing to purge`);
+ return;
+ }
+ if (numOverQuota === 0) {
+ break;
+ }
+
+ // Only yield after making progress, so that continuations can't loop
+ // forever without deleting anything.
+ if (deletedCount > 0 && Date.now() - startedAt >= opts.runTimeBudgetMs) {
+ await AdminMaintenanceQueue.enqueue(task);
+ logger.info(
+ `${logPrefix} Yielding after deleting ${deletedCount} bookmarks for user ${userId}. ${numOverQuota} bookmarks still over quota, enqueued a continuation job.`,
+ );
+ return;
+ }
+
+ const batch = await db
+ .select({ id: bookmarks.id })
+ .from(bookmarks)
+ .where(eq(bookmarks.userId, userId))
+ .orderBy(asc(bookmarks.createdAt), asc(bookmarks.id))
+ .limit(Math.min(opts.batchSize, numOverQuota));
+
+ const ctx = await buildImpersonatingAuthedContext(userId);
+ for (const { id } of batch) {
+ try {
+ const bookmark = await Bookmark.fromId(ctx, id, false);
+ await bookmark.delete();
+ deletedCount++;
+ } catch (e) {
+ // The user might have deleted it themselves in the meantime.
+ if (e instanceof TRPCError && e.code === "NOT_FOUND") {
+ continue;
+ }
+ throw e;
+ }
+ }
+
+ await sleep(opts.interBatchPauseMs);
+ }
+
+ logger.info(
+ `${logPrefix} User ${userId} is within their bookmark quota. Deleted ${deletedCount} bookmarks in this run.`,
+ );
+}
diff --git a/apps/workers/workers/adminMaintenanceWorker.ts b/apps/workers/workers/adminMaintenanceWorker.ts
index 92d52a22..2b43ef2b 100644
--- a/apps/workers/workers/adminMaintenanceWorker.ts
+++ b/apps/workers/workers/adminMaintenanceWorker.ts
@@ -12,6 +12,7 @@ import logger from "@karakeep/shared/logger";
import { DequeuedJob, getQueueClient } from "@karakeep/shared/queueing";
import { runMigrateLargeLinkHtmlTask } from "./adminMaintenance/tasks/migrateLinkHtmlContent";
+import { runPurgeBookmarksOverQuotaTask } from "./adminMaintenance/tasks/purgeBookmarksOverQuota";
import { runTidyAssetsTask } from "./adminMaintenance/tasks/tidyAssets";
export class AdminMaintenanceWorker {
@@ -84,6 +85,8 @@ async function runAdminMaintenance(job: DequeuedJob<ZAdminMaintenanceTask>) {
return runMigrateLargeLinkHtmlTask(
job as DequeuedJob<ZAdminMaintenanceMigrateLargeLinkHtmlTask>,
);
+ case "purge_bookmarks_over_quota":
+ return runPurgeBookmarksOverQuotaTask(job, task);
default: {
const exhaustiveCheck: never = task;
throw new Error(
diff --git a/packages/shared-server/src/queues.ts b/packages/shared-server/src/queues.ts
index 84882bb4..f4078e34 100644
--- a/packages/shared-server/src/queues.ts
+++ b/packages/shared-server/src/queues.ts
@@ -197,14 +197,34 @@ export const zTidyAssetsRequestSchema = z.object({
});
export type ZTidyAssetsRequest = z.infer<typeof zTidyAssetsRequestSchema>;
-export const zAdminMaintenanceTaskSchema = z.discriminatedUnion("type", [
- z.object({
- type: z.literal("tidy_assets"),
- args: zTidyAssetsRequestSchema,
- }),
- z.object({
- type: z.literal("migrate_large_link_html"),
+const zTidyAssetsTaskSchema = z.object({
+ type: z.literal("tidy_assets"),
+ args: zTidyAssetsRequestSchema,
+});
+
+const zMigrateLargeLinkHtmlTaskSchema = z.object({
+ type: z.literal("migrate_large_link_html"),
+});
+
+const zPurgeBookmarksOverQuotaTaskSchema = z.object({
+ type: z.literal("purge_bookmarks_over_quota"),
+ args: z.object({
+ userId: z.string(),
}),
+});
+
+// System-wide tasks that can be triggered through the generic
+// `runAdminMaintenanceTask` endpoint. User-scoped (and destructive) tasks
+// have their own dedicated endpoints.
+export const zSystemAdminMaintenanceTaskSchema = z.discriminatedUnion("type", [
+ zTidyAssetsTaskSchema,
+ zMigrateLargeLinkHtmlTaskSchema,
+]);
+
+export const zAdminMaintenanceTaskSchema = z.discriminatedUnion("type", [
+ zTidyAssetsTaskSchema,
+ zMigrateLargeLinkHtmlTaskSchema,
+ zPurgeBookmarksOverQuotaTaskSchema,
]);
export type ZAdminMaintenanceTask = z.infer<typeof zAdminMaintenanceTaskSchema>;
@@ -217,6 +237,10 @@ export type ZAdminMaintenanceMigrateLargeLinkHtmlTask = Extract<
ZAdminMaintenanceTask,
{ type: "migrate_large_link_html" }
>;
+export type ZAdminMaintenancePurgeBookmarksOverQuotaTask = Extract<
+ ZAdminMaintenanceTask,
+ { type: "purge_bookmarks_over_quota" }
+>;
export const AdminMaintenanceQueue = createDeferredQueue<ZAdminMaintenanceTask>(
"admin_maintenance_queue",
diff --git a/packages/trpc/routers/admin.test.ts b/packages/trpc/routers/admin.test.ts
index ee397305..301b4115 100644
--- a/packages/trpc/routers/admin.test.ts
+++ b/packages/trpc/routers/admin.test.ts
@@ -657,4 +657,133 @@ describe("Admin Routes", () => {
);
});
});
+
+ describe("purgeBookmarksOverQuota", () => {
+ async function setup(
+ db: CustomTestContext["db"],
+ bookmarkQuota: number | null,
+ numBookmarks: number,
+ ) {
+ const [adminUser] = await db
+ .insert(users)
+ .values({
+ name: "Admin User",
+ email: "[email protected]",
+ role: "admin",
+ })
+ .returning();
+ const [user] = await db
+ .insert(users)
+ .values({
+ name: "Over Quota User",
+ email: "[email protected]",
+ bookmarkQuota,
+ })
+ .returning();
+ if (numBookmarks > 0) {
+ await db.insert(bookmarks).values(
+ Array.from(
+ { length: numBookmarks },
+ (): typeof bookmarks.$inferInsert => ({
+ userId: user.id,
+ type: BookmarkTypes.TEXT,
+ }),
+ ),
+ );
+ }
+ return {
+ adminApi: getApiCaller(db, adminUser.id, adminUser.email, "admin")
+ .admin,
+ user,
+ };
+ }
+
+ test<CustomTestContext>("enqueues a purge for users over quota", async ({
+ db,
+ }) => {
+ const { adminApi, user } = await setup(db, 2, 5);
+
+ const res = await adminApi.purgeBookmarksOverQuota({ userId: user.id });
+
+ expect(res).toEqual({
+ numBookmarks: 5,
+ bookmarkQuota: 2,
+ numBookmarksToDelete: 3,
+ enqueued: true,
+ });
+ expect(testQueueMocks.adminMaintenanceEnqueue).toHaveBeenCalledTimes(1);
+ expect(testQueueMocks.adminMaintenanceEnqueue).toHaveBeenCalledWith({
+ type: "purge_bookmarks_over_quota",
+ args: { userId: user.id },
+ });
+ });
+
+ test<CustomTestContext>("dry run doesn't enqueue anything", async ({
+ db,
+ }) => {
+ const { adminApi, user } = await setup(db, 2, 5);
+
+ const res = await adminApi.purgeBookmarksOverQuota({
+ userId: user.id,
+ dryRun: true,
+ });
+
+ expect(res.numBookmarksToDelete).toEqual(3);
+ expect(res.enqueued).toEqual(false);
+ expect(testQueueMocks.adminMaintenanceEnqueue).not.toHaveBeenCalled();
+ });
+
+ test<CustomTestContext>("doesn't enqueue for users within quota", async ({
+ db,
+ }) => {
+ const { adminApi, user } = await setup(db, 5, 5);
+
+ const res = await adminApi.purgeBookmarksOverQuota({ userId: user.id });
+
+ expect(res.numBookmarksToDelete).toEqual(0);
+ expect(res.enqueued).toEqual(false);
+ expect(testQueueMocks.adminMaintenanceEnqueue).not.toHaveBeenCalled();
+ });
+
+ test<CustomTestContext>("rejects users without a quota and unknown users", async ({
+ db,
+ }) => {
+ const { adminApi, user } = await setup(db, null, 5);
+
+ await expect(() =>
+ adminApi.purgeBookmarksOverQuota({ userId: user.id }),
+ ).rejects.toThrow(/bookmark quota/);
+ await expect(() =>
+ adminApi.purgeBookmarksOverQuota({ userId: "does-not-exist" }),
+ ).rejects.toThrow(/User not found/);
+ expect(testQueueMocks.adminMaintenanceEnqueue).not.toHaveBeenCalled();
+ });
+
+ test<CustomTestContext>("non-admins can't purge", async ({
+ apiCallers,
+ db,
+ }) => {
+ const { user } = await setup(db, 2, 5);
+
+ await expect(() =>
+ apiCallers[0].admin.purgeBookmarksOverQuota({ userId: user.id }),
+ ).rejects.toThrow(/FORBIDDEN/);
+ });
+
+ test<CustomTestContext>("can't be triggered through the generic maintenance endpoint", async ({
+ db,
+ }) => {
+ const { adminApi, user } = await setup(db, 2, 5);
+
+ const input: unknown = {
+ type: "purge_bookmarks_over_quota",
+ args: { userId: user.id },
+ };
+ await expect(() =>
+ // @ts-expect-error purge isn't a system maintenance task
+ adminApi.runAdminMaintenanceTask(input),
+ ).rejects.toThrow();
+ expect(testQueueMocks.adminMaintenanceEnqueue).not.toHaveBeenCalled();
+ });
+ });
});
diff --git a/packages/trpc/routers/admin.ts b/packages/trpc/routers/admin.ts
index 479b983b..4e96d880 100644
--- a/packages/trpc/routers/admin.ts
+++ b/packages/trpc/routers/admin.ts
@@ -25,7 +25,7 @@ import {
triggerSearchReindex,
VideoWorkerQueue,
WebhookQueue,
- zAdminMaintenanceTaskSchema,
+ zSystemAdminMaintenanceTaskSchema,
} from "@karakeep/shared-server";
import serverConfig from "@karakeep/shared/config";
import logger from "@karakeep/shared/logger";
@@ -464,7 +464,7 @@ export const adminAppRouter = router({
);
}),
runAdminMaintenanceTask: adminJobsProcedure
- .input(zAdminMaintenanceTaskSchema)
+ .input(zSystemAdminMaintenanceTaskSchema)
.mutation(async ({ input }) => {
await AdminMaintenanceQueue.enqueue(input);
}),
@@ -593,6 +593,61 @@ export const adminAppRouter = router({
});
}
}),
+ purgeBookmarksOverQuota: adminUsersProcedure
+ .input(
+ z.object({
+ userId: z.string(),
+ dryRun: z.boolean().optional().default(false),
+ }),
+ )
+ .output(
+ z.object({
+ numBookmarks: z.number(),
+ bookmarkQuota: z.number(),
+ numBookmarksToDelete: z.number(),
+ enqueued: z.boolean(),
+ }),
+ )
+ .mutation(async ({ input, ctx }) => {
+ const user = await ctx.db.query.users.findFirst({
+ where: eq(users.id, input.userId),
+ columns: { bookmarkQuota: true },
+ });
+ if (!user) {
+ throw new TRPCError({
+ code: "NOT_FOUND",
+ message: "User not found",
+ });
+ }
+ if (user.bookmarkQuota === null) {
+ throw new TRPCError({
+ code: "BAD_REQUEST",
+ message: "User doesn't have a bookmark quota",
+ });
+ }
+ const [{ numBookmarks }] = await ctx.db
+ .select({ numBookmarks: count() })
+ .from(bookmarks)
+ .where(eq(bookmarks.userId, input.userId));
+ const numBookmarksToDelete = Math.max(
+ 0,
+ numBookmarks - user.bookmarkQuota,
+ );
+
+ const enqueued = !input.dryRun && numBookmarksToDelete > 0;
+ if (enqueued) {
+ await AdminMaintenanceQueue.enqueue({
+ type: "purge_bookmarks_over_quota",
+ args: { userId: input.userId },
+ });
+ }
+ return {
+ numBookmarks,
+ bookmarkQuota: user.bookmarkQuota,
+ numBookmarksToDelete,
+ enqueued,
+ };
+ }),
getAdminNoticies: adminSystemProcedure
.output(
z.object({
diff --git a/packages/trpc/testUtils.ts b/packages/trpc/testUtils.ts
index 3a038c71..15dcb230 100644
--- a/packages/trpc/testUtils.ts
+++ b/packages/trpc/testUtils.ts
@@ -8,6 +8,7 @@ import { createCallerFactory } from "./index";
import { appRouter } from "./routers/_app";
const testQueueMocks = vi.hoisted(() => ({
+ adminMaintenanceEnqueue: vi.fn(),
assetPreprocessingEnqueue: vi.fn(),
embeddingsEnqueue: vi.fn(),
linkCrawlerEnqueue: vi.fn(),
@@ -120,6 +121,9 @@ export function defaultBeforeEach(seedDB = true) {
(await original()) as typeof import("@karakeep/shared-server");
return {
...mod,
+ AdminMaintenanceQueue: {
+ enqueue: testQueueMocks.adminMaintenanceEnqueue,
+ },
AssetPreprocessingQueue: {
enqueue: testQueueMocks.assetPreprocessingEnqueue,
},