Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): avoid duplicate shares when adding Caps to folders

Aditya Garudbd7648a5f6cd
diff --git a/apps/web/__tests__/unit/add-folder-videos.test.ts b/apps/web/__tests__/unit/add-folder-videos.test.ts
new file mode 100644
index 000000000..04cd3a0fe
--- /dev/null
+++ b/apps/web/__tests__/unit/add-folder-videos.test.ts
@@ -0,0 +1,139 @@
+import { Folder, Organisation, Space, Video } from "@cap/web-domain";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { addVideosToFolder } from "@/actions/folders/add-videos";
+
+const mocks = vi.hoisted(() => ({ query: vi.fn(), user: vi.fn() }));
+
+vi.mock("@cap/database", async () => {
+ const { drizzle } = await import("drizzle-orm/mysql-proxy");
+ return { db: () => drizzle(mocks.query) };
+});
+vi.mock("@cap/database/auth/session", () => ({ getCurrentUser: mocks.user }));
+vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
+
+const folderId = Folder.FolderId.make("folder-1");
+const orgId = Organisation.OrganisationId.make("org-1");
+const existingId = Video.VideoId.make("existing-video");
+const newId = Video.VideoId.make("new-video");
+
+beforeEach(() => {
+ mocks.user.mockResolvedValue({ id: "owner", activeOrganizationId: orgId });
+});
+
+describe.each([
+ {
+ name: "space",
+ spaceId: Space.SpaceId.make("space-1"),
+ table: "space_videos",
+ },
+ { name: "organization", spaceId: orgId, table: "shared_videos" },
+])("adding videos to a $name folder", ({ spaceId, table }) => {
+ function setup(ownedIds: Video.VideoId[], sharedIds: Video.VideoId[]) {
+ mocks.query.mockImplementation(async (query: string) => {
+ if (query.startsWith("select")) {
+ if (query.includes("from `folders`")) {
+ return { rows: [[folderId, spaceId]] };
+ }
+ if (query.includes("from `videos`")) {
+ return { rows: ownedIds.map((id) => [id]) };
+ }
+ return { rows: sharedIds.map((id) => [id]) };
+ }
+ return { rows: [{ insertId: 1, affectedRows: 1 }] };
+ });
+ }
+
+ it("moves an existing share without inserting another sharing row", async () => {
+ setup([existingId], [existingId]);
+
+ await expect(
+ addVideosToFolder(folderId, [existingId], spaceId),
+ ).resolves.toMatchObject({ success: true, addedCount: 1 });
+
+ const writes = mocks.query.mock.calls.filter(([query]) =>
+ String(query).startsWith("insert"),
+ );
+ expect(writes).toHaveLength(0);
+ const update = mocks.query.mock.calls.find(([query]) =>
+ String(query).startsWith(`update \`${table}\``),
+ );
+ expect(update?.[1]).toEqual([folderId, spaceId, existingId]);
+ });
+
+ it("inserts only missing shares when moving a mixed selection", async () => {
+ setup([existingId, newId], [existingId]);
+
+ await expect(
+ addVideosToFolder(folderId, [existingId, newId], spaceId),
+ ).resolves.toMatchObject({ success: true, addedCount: 2 });
+
+ const insert = mocks.query.mock.calls.find(([query]) =>
+ String(query).startsWith(`insert into \`${table}\``),
+ );
+ expect(insert?.[1]).toContain(newId);
+ expect(insert?.[1]).not.toContain(existingId);
+ const existingQuery = mocks.query.mock.calls.find(([query]) =>
+ String(query).includes(`from \`${table}\``),
+ );
+ expect(existingQuery?.[1]).toEqual([spaceId, existingId, newId]);
+ expect(existingQuery?.[0]).not.toContain("folderId");
+ const update = mocks.query.mock.calls.find(([query]) =>
+ String(query).startsWith(`update \`${table}\``),
+ );
+ expect(update?.[1]).toEqual([folderId, spaceId, existingId, newId]);
+ });
+
+ it("creates the share when the video is not in this collection", async () => {
+ setup([newId], []);
+
+ await expect(
+ addVideosToFolder(folderId, [newId], spaceId),
+ ).resolves.toMatchObject({ success: true, addedCount: 1 });
+
+ const insert = mocks.query.mock.calls.find(([query]) =>
+ String(query).startsWith(`insert into \`${table}\``),
+ );
+ expect(insert?.[1]).toEqual([
+ expect.any(String),
+ ...(table === "shared_videos"
+ ? [newId, folderId, spaceId, "owner"]
+ : [spaceId, folderId, newId, "owner"]),
+ ]);
+ });
+
+ it("does not share requested videos the caller does not own", async () => {
+ setup([newId], []);
+
+ await expect(
+ addVideosToFolder(folderId, [existingId, newId], spaceId),
+ ).resolves.toMatchObject({ success: true, addedCount: 1 });
+
+ const writes = mocks.query.mock.calls.filter(
+ ([query]) => !String(query).startsWith("select"),
+ );
+ expect(writes).toHaveLength(2);
+ for (const [, params] of writes) {
+ expect(params).toContain(newId);
+ expect(params).not.toContain(existingId);
+ }
+ });
+
+ it("does not write when the sharing lookup fails", async () => {
+ setup([existingId], [existingId]);
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ mocks.query
+ .mockResolvedValueOnce({ rows: [[folderId, spaceId]] })
+ .mockResolvedValueOnce({ rows: [[existingId]] })
+ .mockRejectedValueOnce(new Error("Database unavailable"));
+
+ await expect(
+ addVideosToFolder(folderId, [existingId], spaceId),
+ ).resolves.toMatchObject({ success: false });
+
+ expect(
+ mocks.query.mock.calls.every(([query]) =>
+ String(query).startsWith("select"),
+ ),
+ ).toBe(true);
+ });
+});
diff --git a/apps/web/actions/folders/add-videos.ts b/apps/web/actions/folders/add-videos.ts
index 817da6191..fee5a5e74 100644
--- a/apps/web/actions/folders/add-videos.ts
+++ b/apps/web/actions/folders/add-videos.ts
@@ -50,13 +50,29 @@ export async function addVideosToFolder(
}
const isAllSpacesEntry = spaceId === user.activeOrganizationId;
+ const sharesWithOrganization = isAllSpacesEntry && !!folder.spaceId;
+ const sharingTable = sharesWithOrganization ? sharedVideos : spaceVideos;
+ const existingShares = await db()
+ .select({ videoId: sharingTable.videoId })
+ .from(sharingTable)
+ .where(
+ and(
+ sharesWithOrganization
+ ? eq(sharedVideos.organizationId, user.activeOrganizationId)
+ : eq(spaceVideos.spaceId, spaceId),
+ inArray(sharingTable.videoId, validVideoIds),
+ ),
+ );
+ const existingVideoIds = new Set(
+ existingShares.map((share) => share.videoId),
+ );
+ const newVideoIds = validVideoIds.filter((id) => !existingVideoIds.has(id));
- //if we're adding videos to a folder from Caps page, then insert the videos into the folder
- if (isAllSpacesEntry && folder.spaceId) {
+ if (newVideoIds.length > 0 && sharesWithOrganization) {
await db()
.insert(sharedVideos)
.values(
- validVideoIds.map((videoId) => {
+ newVideoIds.map((videoId) => {
const id = nanoId();
return {
id,
@@ -67,11 +83,11 @@ export async function addVideosToFolder(
};
}),
);
- } else {
+ } else if (newVideoIds.length > 0) {
await db()
.insert(spaceVideos)
.values(
- validVideoIds.map((videoId) => {
+ newVideoIds.map((videoId) => {
const id = nanoId();
return {
id,