A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
fix: only merge the caller's own bookmarks into the target list (#3261)
Mohamed Bassemf6c6ffd29a64
diff --git a/packages/trpc/models/lists.ts b/packages/trpc/models/lists.tsindex 555cc757..beba035e 100644--- a/packages/trpc/models/lists.ts+++ b/packages/trpc/models/lists.ts@@ -1269,18 +1269,31 @@ export class ManualList extends List {});}- const bookmarkIds = await this.getBookmarkIds();+ // Only move the caller's own bookmarks, like addToList does. Bookmarks+ // added by collaborators stay tied to their membership of the source list.+ const ownedBookmarks = await this.ctx.db+ .select({ id: bookmarksInLists.bookmarkId })+ .from(bookmarksInLists)+ .innerJoin(bookmarks, eq(bookmarks.id, bookmarksInLists.bookmarkId))+ .where(+ and(+ eq(bookmarksInLists.listId, this.list.id),+ eq(bookmarks.userId, this.ctx.user.id),+ ),+ );await this.ctx.db.transaction((tx) => {- tx.insert(bookmarksInLists)- .values(- bookmarkIds.map((id) => ({- bookmarkId: id,- listId: targetList.id,- })),- )- .onConflictDoNothing()- .run();+ if (ownedBookmarks.length > 0) {+ tx.insert(bookmarksInLists)+ .values(+ ownedBookmarks.map(({ id }) => ({+ bookmarkId: id,+ listId: targetList.id,+ })),+ )+ .onConflictDoNothing()+ .run();+ }if (deleteSourceAfterMerge) {tx.delete(bookmarkLists)diff --git a/packages/trpc/routers/lists.test.ts b/packages/trpc/routers/lists.test.tsindex be3a970a..20a3766f 100644--- a/packages/trpc/routers/lists.test.ts+++ b/packages/trpc/routers/lists.test.ts@@ -155,6 +155,83 @@ describe("Lists Routes", () => {).rejects.toThrow(/List not found/);});+ test<CustomTestContext>("merge doesn't move collaborators' bookmarks", async ({+ apiCallers,+ }) => {+ const [ownerApi, editorApi, viewerApi] = apiCallers;++ const sourceList = await ownerApi.lists.create({+ name: "Source List",+ type: "manual",+ icon: "π",+ });+ const targetList = await ownerApi.lists.create({+ name: "Target List",+ type: "manual",+ icon: "π",+ });+ await shareList(ownerApi, editorApi, sourceList.id, "editor");+ await shareList(ownerApi, viewerApi, targetList.id, "viewer");++ const ownerBookmarkId = await createTestBookmark(ownerApi);+ const editorBookmarkId = await createTestBookmark(editorApi);+ await ownerApi.lists.addToList({+ listId: sourceList.id,+ bookmarkId: ownerBookmarkId,+ });+ await editorApi.lists.addToList({+ listId: sourceList.id,+ bookmarkId: editorBookmarkId,+ });++ await ownerApi.lists.merge({+ sourceId: sourceList.id,+ targetId: targetList.id,+ deleteSourceAfterMerge: true,+ });++ const { bookmarks } = await viewerApi.bookmarks.getBookmarks({+ listId: targetList.id,+ });+ expect(bookmarks.map((b) => b.id)).toEqual([ownerBookmarkId]);+ await expect(() =>+ viewerApi.bookmarks.getBookmark({ bookmarkId: editorBookmarkId }),+ ).rejects.toThrow(/Bookmark not found/);+ });++ test<CustomTestContext>("merge a list with only collaborators' bookmarks", async ({+ apiCallers,+ }) => {+ const [ownerApi, editorApi] = apiCallers;++ const sourceList = await ownerApi.lists.create({+ name: "Source List",+ type: "manual",+ icon: "π",+ });+ const targetList = await ownerApi.lists.create({+ name: "Target List",+ type: "manual",+ icon: "π",+ });+ await shareList(ownerApi, editorApi, sourceList.id, "editor");+ await editorApi.lists.addToList({+ listId: sourceList.id,+ bookmarkId: await createTestBookmark(editorApi),+ });++ await ownerApi.lists.merge({+ sourceId: sourceList.id,+ targetId: targetList.id,+ deleteSourceAfterMerge: false,+ });++ const { bookmarks } = await ownerApi.bookmarks.getBookmarks({+ listId: targetList.id,+ });+ expect(bookmarks).toHaveLength(0);+ });+test<CustomTestContext>("delete list", async ({ apiCallers }) => {const api = apiCallers[0].lists;