A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
fix(trpc): show collaborator bookmarks in public lists (#3019)
Chandan P6f908b244cf2
diff --git a/packages/trpc/lib/impersonate.ts b/packages/trpc/lib/impersonate.tsindex f44a2c70..993c66ba 100644--- a/packages/trpc/lib/impersonate.ts+++ b/packages/trpc/lib/impersonate.ts@@ -1,12 +1,13 @@import { eq } from "drizzle-orm";-import { db } from "@karakeep/db";+import { db as globalDb } from "@karakeep/db";import { users } from "@karakeep/db/schema";import { AuthedContext } from "..";export async function buildImpersonatingAuthedContext(userId: string,+ db: AuthedContext["db"] = globalDb,): Promise<AuthedContext> {const user = await db.query.users.findFirst({where: eq(users.id, userId),diff --git a/packages/trpc/models/lists.ts b/packages/trpc/models/lists.tsindex 6015555b..8f565df6 100644--- a/packages/trpc/models/lists.ts+++ b/packages/trpc/models/lists.ts@@ -218,7 +218,10 @@ export abstract class List {// The token here acts as an authed context, so we can create// an impersonating context for the list owner as long as// we don't leak the context.- const authedCtx = await buildImpersonatingAuthedContext(listdb.userId);+ const authedCtx = await buildImpersonatingAuthedContext(+ listdb.userId,+ ctx.db,+ );const listObj = List.fromData(authedCtx,{@@ -228,11 +231,13 @@ export abstract class List {},null,);- const bookmarkIds = await listObj.getBookmarkIds();+ const numItems = await listObj.getSize();const list = listObj.asZBookmarkList();+ // Filtering by listId instead of by ids, because the ids path is scoped to+ // the list owner and would hide the bookmarks added by collaborators.const bookmarks = await Bookmark.loadMulti(authedCtx, {- ids: bookmarkIds,+ listId: listObj.id,includeContent: false,limit: pagination.limit,sortOrder: pagination.order,@@ -245,7 +250,7 @@ export abstract class List {name: list.name,description: list.description,ownerName: listdb.user.name,- numItems: bookmarkIds.length,+ numItems,},bookmarks: bookmarks.bookmarks.map((b) => b.asPublicBookmark()),nextCursor: bookmarks.nextCursor,diff --git a/packages/trpc/routers/sharedLists.test.ts b/packages/trpc/routers/sharedLists.test.tsindex 3440fae4..da336666 100644--- a/packages/trpc/routers/sharedLists.test.ts+++ b/packages/trpc/routers/sharedLists.test.ts@@ -868,6 +868,66 @@ describe("Shared Lists", () => {});});+ describe("Public Shared Lists", () => {+ test<CustomTestContext>("should show collaborator bookmarks in a public list", async ({+ apiCallers,+ unauthedAPICaller,+ }) => {+ const ownerApi = apiCallers[0];+ const collaboratorApi = apiCallers[1];++ const list = await ownerApi.lists.create({+ name: "Public Shared List",+ icon: "📚",+ type: "manual",+ });++ // Owner adds a bookmark+ const ownerBookmark = await ownerApi.bookmarks.createBookmark({+ type: BookmarkTypes.TEXT,+ text: "Owner's bookmark",+ });++ await ownerApi.lists.addToList({+ listId: list.id,+ bookmarkId: ownerBookmark.id,+ });++ await addAndAcceptCollaborator(+ ownerApi,+ collaboratorApi,+ list.id,+ "editor",+ );++ // Collaborator adds their own bookmark+ const collabBookmark = await collaboratorApi.bookmarks.createBookmark({+ type: BookmarkTypes.TEXT,+ text: "Collaborator's bookmark",+ });++ await collaboratorApi.lists.addToList({+ listId: list.id,+ bookmarkId: collabBookmark.id,+ });++ await ownerApi.lists.edit({+ listId: list.id,+ public: true,+ });++ const publicView =+ await unauthedAPICaller.publicBookmarks.getPublicBookmarksInList({+ listId: list.id,+ });++ expect(publicView.list.numItems).toBe(2);+ expect(publicView.bookmarks.map((b) => b.id).sort()).toEqual(+ [ownerBookmark.id, collabBookmark.id].sort(),+ );+ });+ });+describe("Bookmark Editing Permissions", () => {test<CustomTestContext>("should not allow viewer to add bookmarks to list", async ({apiCallers,