Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: validate backfill owners, folders and shares
Richie McIlroyf54bee4cd4c5
diff --git a/apps/web/__tests__/unit/loom-import-backfill.test.ts b/apps/web/__tests__/unit/loom-import-backfill.test.tsindex f5be5ccbc..d8ec6f3aa 100644--- a/apps/web/__tests__/unit/loom-import-backfill.test.ts+++ b/apps/web/__tests__/unit/loom-import-backfill.test.ts@@ -4,6 +4,8 @@ const mocks = vi.hoisted(() => ({selects: [] as unknown[][],updates: [] as Array<Record<string, unknown>>,inserts: [] as Array<Record<string, unknown>>,+ shares: [] as unknown[][],+ deletes: 0,member: vi.fn(),provision: vi.fn(),reassign: vi.fn(),@@ -16,9 +18,35 @@ vi.mock("@cap/database", () => {innerJoin: () => selection,where: () => selection,limit: async () => mocks.selects.shift() ?? [],+ for: async () => [],+ then: (resolve: (rows: unknown[]) => void) =>+ resolve(mocks.shares.shift() ?? []),+ };+ const handle = {+ select: () => selection,+ update: () => ({+ set: (values: Record<string, unknown>) => ({+ where: async () => {+ mocks.updates.push(values);+ },+ }),+ }),+ insert: () => ({+ values: async (values: Record<string, unknown>) => {+ mocks.inserts.push(values);+ },+ }),+ delete: () => ({+ where: async () => {+ mocks.deletes++;+ },+ }),};return {db: () => ({+ ...handle,+ transaction: async (run: (tx: typeof handle) => Promise<unknown>) =>+ run(handle),select: () => selection,update: () => ({set: (values: Record<string, unknown>) => ({@@ -57,6 +85,9 @@ vi.mock("@cap/database/schema", () => ({}));vi.mock("@/lib/loom-csv-import", () => ({getOrganizationMemberByEmail: mocks.member,+ normalizeImportEmail: (email: string) => email.trim().toLowerCase(),+ isValidImportEmail: (email: string) =>+ /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email),}));vi.mock("@/lib/organization-provisioning", () => ({provisionOrganizationInvitee: mocks.provision,@@ -80,6 +111,8 @@ beforeEach(() => {mocks.selects = [];mocks.updates = [];mocks.inserts = [];+ mocks.shares = [];+ mocks.deletes = 0;mocks.member.mockReset().mockResolvedValue({ userId: "mdaueraaaaaaaaa" });mocks.provision.mockReset().mockResolvedValue({ userId: "newaaaaaaaaaaaa" });mocks.reassign@@ -87,9 +120,18 @@ beforeEach(() => {.mockResolvedValue({ status: "moved", copiedObjects: 3, cleanup: "done" });});+const lukeFolder = (id = "folderbbbbbbbbb") => ({+ id,+ createdById: "lukeaaaaaaaaaaa",+});+describe("backfillLoomImport", () => {- it("moves the owner without touching sharing, keeping the sheet folder", async () => {- mocks.selects = [[video()], [{ id: "folderbbbbbbbbb" }]];+ it("moves the owner without touching sharing, into the sheet folder", async () => {+ mocks.selects = [+ [video()],+ [lukeFolder()],+ [lukeFolder("folderaaaaaaaaa")],+ ];await expect(backfillLoomImport(org, {videoId: "videoaaaaaaaaaa",@@ -115,8 +157,24 @@ describe("backfillLoomImport", () => {expect(mocks.updates).toEqual([]);});+ it("clears an inherited folder the new owner could not open", async () => {+ mocks.selects = [+ [video({ orgOwnerId: "owneraaaaaaaaaa" })],+ [{ id: "folderaaaaaaaaa", createdById: "lukeaaaaaaaaaaa" }],+ ];+ await backfillLoomImport(org, {+ videoId: "videoaaaaaaaaaa",+ ownerEmail: "[email protected]",+ });+ expect(mocks.reassign).toHaveBeenCalledWith(+ expect.objectContaining({+ options: { keepSharing: true, folderId: null },+ }),+ );+ });+it("adds an owner who is not in the workspace yet", async () => {- mocks.selects = [[video()]];+ mocks.selects = [[video()], [lukeFolder("folderaaaaaaaaa")]];mocks.member.mockResolvedValue(null);await backfillLoomImport(org, {videoId: "videoaaaaaaaaaa",@@ -133,8 +191,23 @@ describe("backfillLoomImport", () => {);});- it("leaves the owner alone when it already matches", async () => {+ it("never creates accounts for invalid owner emails", async () => {mocks.selects = [[video()]];+ await expect(+ backfillLoomImport(org, {+ videoId: "videoaaaaaaaaaa",+ ownerEmail: "unknown",+ }),+ ).resolves.toMatchObject({+ status: "skipped",+ reason: "invalid owner email",+ });+ expect(mocks.member).not.toHaveBeenCalled();+ expect(mocks.provision).not.toHaveBeenCalled();+ });++ it("leaves the owner alone when it already matches", async () => {+ mocks.selects = [[video()], [lukeFolder("folderaaaaaaaaa")]];mocks.member.mockResolvedValue({ userId: "lukeaaaaaaaaaaa" });await expect(backfillLoomImport(org, {@@ -146,7 +219,7 @@ describe("backfillLoomImport", () => {});it("sets the original date and the folder", async () => {- mocks.selects = [[video({ folderId: null })], [{ id: "folderbbbbbbbbb" }]];+ mocks.selects = [[video({ folderId: null })], [lukeFolder()]];await expect(backfillLoomImport(org, {videoId: "videoaaaaaaaaaa",@@ -159,7 +232,7 @@ describe("backfillLoomImport", () => {});it("skips a date that already matches", async () => {- mocks.selects = [[video()]];+ mocks.selects = [[video()], [lukeFolder("folderaaaaaaaaa")]];await expect(backfillLoomImport(org, {videoId: "videoaaaaaaaaaa",@@ -168,8 +241,13 @@ describe("backfillLoomImport", () => {).resolves.toMatchObject({ status: "unchanged" });});- it("shares into the team folder once", async () => {- mocks.selects = [[video()], [{ id: "teamfolderaaaaa" }], []];+ it("shares into the team folder once and folds duplicate shares", async () => {+ mocks.selects = [+ [video()],+ [lukeFolder("teamfolderaaaaa")],+ [lukeFolder("folderaaaaaaaaa")],+ ];+ mocks.shares = [[]];await expect(backfillLoomImport(org, {videoId: "videoaaaaaaaaaa",@@ -183,21 +261,32 @@ describe("backfillLoomImport", () => {});mocks.selects = [[video()],- [{ id: "teamfolderaaaaa" }],- [{ id: "share", folderId: "teamfolderaaaaa" }],+ [lukeFolder("teamfolderaaaaa")],+ [lukeFolder("folderaaaaaaaaa")],+ ];+ mocks.shares = [+ [+ { id: "share1", folderId: "teamfolderaaaaa" },+ { id: "share2", folderId: "otherfolderaaaa" },+ ],];await expect(backfillLoomImport(org, {videoId: "videoaaaaaaaaaa",shareFolderId: "teamfolderaaaaa",}),- ).resolves.toMatchObject({ status: "unchanged" });+ ).resolves.toMatchObject({ changes: ["shared"] });+ expect(mocks.deletes).toBe(1);expect(mocks.inserts).toHaveLength(1);});it.each([[[], "not a Loom import here"],[[video(), []], "folder not in this workspace"],+ [+ [video(), [{ id: "folderbbbbbbbbb", createdById: "someoneaaaaaaaa" }]],+ "folder belongs to someone other than the owner",+ ],])("refuses unsafe changes: %#", async (selects, reason) => {mocks.selects = (selects as unknown[]).map((row) =>Array.isArray(row) ? row : [row],@@ -212,8 +301,8 @@ describe("backfillLoomImport", () => {expect(mocks.reassign).not.toHaveBeenCalled();});- it("reports a move it could not make", async () => {- mocks.selects = [[video()]];+ it("reports moves it could not make or finish cleaning up", async () => {+ mocks.selects = [[video()], [lukeFolder("folderaaaaaaaaa")]];mocks.reassign.mockResolvedValue({status: "skipped",reason: "edited video",@@ -224,5 +313,20 @@ describe("backfillLoomImport", () => {ownerEmail: "[email protected]",}),).resolves.toMatchObject({ status: "skipped", reason: "edited video" });+ mocks.selects = [[video()], [lukeFolder("folderaaaaaaaaa")]];+ mocks.reassign.mockResolvedValue({+ status: "moved",+ copiedObjects: 3,+ cleanup: "failed",+ });+ await expect(+ backfillLoomImport(org, {+ videoId: "videoaaaaaaaaaa",+ ownerEmail: "[email protected]",+ }),+ ).resolves.toMatchObject({+ status: "updated",+ warning: "old owner's files could not be removed",+ });});});diff --git a/apps/web/app/api/internal/loom-imports/backfill/route.ts b/apps/web/app/api/internal/loom-imports/backfill/route.tsindex 610e8a1fa..ecefa50b1 100644--- a/apps/web/app/api/internal/loom-imports/backfill/route.ts+++ b/apps/web/app/api/internal/loom-imports/backfill/route.ts@@ -52,6 +52,7 @@ class Api extends HttpApi.make("LoomImportBackfillApi").add(),changes: Schema.Array(Schema.String),reason: Schema.optional(Schema.String),+ warning: Schema.optional(Schema.String),}),),}),diff --git a/apps/web/lib/loom-import-backfill.ts b/apps/web/lib/loom-import-backfill.tsindex a7d22425c..f7bee4584 100644--- a/apps/web/lib/loom-import-backfill.ts+++ b/apps/web/lib/loom-import-backfill.ts@@ -11,7 +11,11 @@ import {} from "@cap/database/schema";import { Folder, Organisation, User, Video } from "@cap/web-domain";import { and, eq, isNull, sql } from "drizzle-orm";-import { getOrganizationMemberByEmail } from "@/lib/loom-csv-import";+import {+ getOrganizationMemberByEmail,+ isValidImportEmail,+ normalizeImportEmail,+} from "@/lib/loom-csv-import";import { reassignLoomImport } from "@/lib/loom-import-reassign";import { provisionOrganizationInvitee } from "@/lib/organization-provisioning";@@ -28,8 +32,11 @@ export type LoomBackfillOutcome = {status: "updated" | "unchanged" | "skipped" | "failed";changes: string[];reason?: string;+ warning?: string;};+type FolderRow = { id: Folder.FolderId; createdById: User.UserId };+export async function backfillLoomImport(organizationId: string,item: LoomBackfillItem,@@ -37,6 +44,15 @@ export async function backfillLoomImport(const orgId = Organisation.OrganisationId.make(organizationId);const id = Video.VideoId.make(item.videoId);const changes: string[] = [];+ const result = (+ status: LoomBackfillOutcome["status"],+ extra: Partial<LoomBackfillOutcome> = {},+ ): LoomBackfillOutcome => ({+ videoId: item.videoId,+ status,+ changes,+ ...extra,+ });const [video] = await db().select({ownerId: videos.ownerId,@@ -56,41 +72,38 @@ export async function backfillLoomImport(.innerJoin(organizations, eq(organizations.id, videos.orgId)).where(and(eq(videos.id, id), eq(videos.orgId, orgId))).limit(1);- if (!video)- return {- videoId: item.videoId,- status: "skipped",- changes,- reason: "not a Loom import here",- };+ if (!video) return result("skipped", { reason: "not a Loom import here" });++ const date = item.effectiveCreatedAt+ ? new Date(item.effectiveCreatedAt)+ : undefined;+ if (date && Number.isNaN(date.getTime()))+ return result("failed", { reason: "invalid date" });++ const email =+ item.ownerEmail !== undefined+ ? normalizeImportEmail(item.ownerEmail)+ : undefined;+ if (email !== undefined && !isValidImportEmail(email))+ return result("skipped", { reason: "invalid owner email" });const personalFolder = item.folderId? await findFolder(orgId, item.folderId, false): undefined;if (item.folderId && !personalFolder)- return {- videoId: item.videoId,- status: "skipped",- changes,- reason: "folder not in this workspace",- };+ return result("skipped", { reason: "folder not in this workspace" });const sharedFolder = item.shareFolderId? await findFolder(orgId, item.shareFolderId, true): undefined;if (item.shareFolderId && !sharedFolder)- return {- videoId: item.videoId,- status: "skipped",- changes,+ return result("skipped", {reason: "shared folder not in this workspace",- };+ });let ownerId = video.ownerId;- if (item.ownerEmail) {- const email = item.ownerEmail.trim().toLowerCase();- const existingMember = await getOrganizationMemberByEmail(orgId, email);+ if (email) {const member =- existingMember ??+ (await getOrganizationMemberByEmail(orgId, email)) ??(await provisionOrganizationInvitee({organizationId: orgId,email,@@ -98,112 +111,108 @@ export async function backfillLoomImport(role: "member",}).then((provisioned) => ({ userId: provisioned.userId, email })));if (!member)- return {- videoId: item.videoId,- status: "failed",- changes,+ return result("failed", {reason: "could not add the owner to the workspace",- };- if (member.userId !== video.ownerId) {- const moved = await reassignLoomImport({- organizationId,- videoId: item.videoId,- fromUserId: video.ownerId,- toUserId: member.userId,- options: {- keepSharing: true,- folderId: personalFolder?.id ?? video.folderId,- },});- if (moved.status !== "moved")- return {- videoId: item.videoId,- status: moved.status,- changes,- reason: moved.reason,- };- ownerId = User.UserId.make(member.userId);- changes.push("owner");- if (personalFolder && personalFolder.id !== video.folderId)- changes.push("folder");- }+ ownerId = User.UserId.make(member.userId);}- if (- personalFolder &&- personalFolder.id !== video.folderId &&- !changes.includes("folder")- ) {+ const canUseFolder = (folder: FolderRow) =>+ folder.createdById === ownerId || folder.createdById === video.orgOwnerId;+ if (personalFolder && !canUseFolder(personalFolder))+ return result("skipped", {+ reason: "folder belongs to someone other than the owner",+ });+ const currentFolder = video.folderId+ ? await findFolder(orgId, video.folderId, false)+ : undefined;+ const targetFolderId =+ personalFolder?.id ??+ (currentFolder && canUseFolder(currentFolder) ? currentFolder.id : null);++ let warning: string | undefined;+ if (ownerId !== video.ownerId) {+ const moved = await reassignLoomImport({+ organizationId,+ videoId: item.videoId,+ fromUserId: video.ownerId,+ toUserId: ownerId,+ options: { keepSharing: true, folderId: targetFolderId },+ });+ if (moved.status !== "moved")+ return result(moved.status, { reason: moved.reason });+ if (moved.cleanup === "failed")+ warning = "old owner's files could not be removed";+ changes.push("owner");+ if (targetFolderId !== video.folderId) changes.push("folder");+ } else if (targetFolderId !== video.folderId) {await db().update(videos)- .set({ folderId: personalFolder.id })+ .set({ folderId: targetFolderId }).where(and(eq(videos.id, id), eq(videos.ownerId, ownerId)));changes.push("folder");}- if (item.effectiveCreatedAt) {- const date = new Date(item.effectiveCreatedAt);- if (Number.isNaN(date.getTime()))- return {- videoId: item.videoId,- status: "failed",- changes,- reason: "invalid date",- };- if (video.effectiveCreatedAt?.getTime() !== date.getTime()) {- await db()- .update(videos)- .set({- metadata: sql`JSON_SET(COALESCE(${videos.metadata}, JSON_OBJECT()), '$.customCreatedAt', ${date.toISOString()})`,- })- .where(eq(videos.id, id));- changes.push("date");- }+ if (date && video.effectiveCreatedAt?.getTime() !== date.getTime()) {+ await db()+ .update(videos)+ .set({+ metadata: sql`JSON_SET(COALESCE(${videos.metadata}, JSON_OBJECT()), '$.customCreatedAt', ${date.toISOString()})`,+ })+ .where(eq(videos.id, id));+ changes.push("date");}if (sharedFolder) {- const [share] = await db()- .select({ id: sharedVideos.id, folderId: sharedVideos.folderId })- .from(sharedVideos)- .where(- and(- eq(sharedVideos.videoId, id),- eq(sharedVideos.organizationId, orgId),- ),- )- .limit(1);- if (!share) {- await db().insert(sharedVideos).values({- id: nanoId(),- videoId: id,- organizationId: orgId,- folderId: sharedFolder.id,- sharedByUserId: ownerId,- });- changes.push("shared");- } else if (share.folderId !== sharedFolder.id) {- await db()+ const shared = await db().transaction(async (tx) => {+ await tx+ .select({ id: videos.id })+ .from(videos)+ .where(eq(videos.id, id))+ .for("update");+ const shares = await tx+ .select({ id: sharedVideos.id, folderId: sharedVideos.folderId })+ .from(sharedVideos)+ .where(+ and(+ eq(sharedVideos.videoId, id),+ eq(sharedVideos.organizationId, orgId),+ ),+ );+ const [keep, ...duplicates] = shares;+ for (const duplicate of duplicates)+ await tx.delete(sharedVideos).where(eq(sharedVideos.id, duplicate.id));+ if (!keep) {+ await tx.insert(sharedVideos).values({+ id: nanoId(),+ videoId: id,+ organizationId: orgId,+ folderId: sharedFolder.id,+ sharedByUserId: ownerId,+ });+ return true;+ }+ if (keep.folderId === sharedFolder.id && duplicates.length === 0)+ return false;+ await tx.update(sharedVideos).set({ folderId: sharedFolder.id })- .where(eq(sharedVideos.id, share.id));- changes.push("shared");- }+ .where(eq(sharedVideos.id, keep.id));+ return true;+ });+ if (shared) changes.push("shared");}- return {- videoId: item.videoId,- status: changes.length > 0 ? "updated" : "unchanged",- changes,- };+ return result(changes.length > 0 ? "updated" : "unchanged", { warning });}async function findFolder(organizationId: Organisation.OrganisationId,folderId: string,shared: boolean,-) {+): Promise<FolderRow | undefined> {const [folder] = await db()- .select({ id: folders.id })+ .select({ id: folders.id, createdById: folders.createdById }).from(folders).where(and(