Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: add an authenticated metadata backfill for imported Loom videos

Richie McIlroyb23b87d5ea06
diff --git a/apps/web/__tests__/unit/loom-import-backfill.test.ts b/apps/web/__tests__/unit/loom-import-backfill.test.ts
new file mode 100644
index 000000000..f5be5ccbc
--- /dev/null
+++ b/apps/web/__tests__/unit/loom-import-backfill.test.ts
@@ -0,0 +1,228 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ selects: [] as unknown[][],
+ updates: [] as Array<Record<string, unknown>>,
+ inserts: [] as Array<Record<string, unknown>>,
+ member: vi.fn(),
+ provision: vi.fn(),
+ reassign: vi.fn(),
+}));
+
+vi.mock("server-only", () => ({}));
+vi.mock("@cap/database", () => {
+ const selection = {
+ from: () => selection,
+ innerJoin: () => selection,
+ where: () => selection,
+ limit: async () => mocks.selects.shift() ?? [],
+ };
+ return {
+ db: () => ({
+ 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);
+ },
+ }),
+ }),
+ };
+});
+vi.mock("@cap/database/helpers", () => ({ nanoId: () => "shareaaaaaaaaaa" }));
+vi.mock("@cap/database/schema", () => ({
+ folders: { id: "f.id", organizationId: "f.org", spaceId: "f.space" },
+ importedVideos: { id: "i.id", orgId: "i.org", source: "i.source" },
+ organizations: { id: "o.id", ownerId: "o.owner" },
+ sharedVideos: {
+ id: "s.id",
+ videoId: "s.video",
+ organizationId: "s.org",
+ folderId: "s.folder",
+ },
+ videos: {
+ id: "v.id",
+ ownerId: "v.owner",
+ orgId: "v.org",
+ folderId: "v.folder",
+ effectiveCreatedAt: "v.date",
+ metadata: "v.metadata",
+ },
+}));
+vi.mock("@/lib/loom-csv-import", () => ({
+ getOrganizationMemberByEmail: mocks.member,
+}));
+vi.mock("@/lib/organization-provisioning", () => ({
+ provisionOrganizationInvitee: mocks.provision,
+}));
+vi.mock("@/lib/loom-import-reassign", () => ({
+ reassignLoomImport: mocks.reassign,
+}));
+
+import { backfillLoomImport } from "@/lib/loom-import-backfill";
+
+const org = "orgaaaaaaaaaaaa";
+const video = (overrides: Record<string, unknown> = {}) => ({
+ ownerId: "lukeaaaaaaaaaaa",
+ folderId: "folderaaaaaaaaa",
+ effectiveCreatedAt: new Date("2026-09-23T10:00:00Z"),
+ orgOwnerId: "lukeaaaaaaaaaaa",
+ ...overrides,
+});
+
+beforeEach(() => {
+ mocks.selects = [];
+ mocks.updates = [];
+ mocks.inserts = [];
+ mocks.member.mockReset().mockResolvedValue({ userId: "mdaueraaaaaaaaa" });
+ mocks.provision.mockReset().mockResolvedValue({ userId: "newaaaaaaaaaaaa" });
+ mocks.reassign
+ .mockReset()
+ .mockResolvedValue({ status: "moved", copiedObjects: 3, cleanup: "done" });
+});
+
+describe("backfillLoomImport", () => {
+ it("moves the owner without touching sharing, keeping the sheet folder", async () => {
+ mocks.selects = [[video()], [{ id: "folderbbbbbbbbb" }]];
+ await expect(
+ backfillLoomImport(org, {
+ videoId: "videoaaaaaaaaaa",
+ ownerEmail: "[email protected]",
+ folderId: "folderbbbbbbbbb",
+ }),
+ ).resolves.toEqual({
+ videoId: "videoaaaaaaaaaa",
+ status: "updated",
+ changes: ["owner", "folder"],
+ });
+ expect(mocks.member).toHaveBeenCalledWith(
+ org,
+ );
+ expect(mocks.reassign).toHaveBeenCalledWith({
+ organizationId: org,
+ videoId: "videoaaaaaaaaaa",
+ fromUserId: "lukeaaaaaaaaaaa",
+ toUserId: "mdaueraaaaaaaaa",
+ options: { keepSharing: true, folderId: "folderbbbbbbbbb" },
+ });
+ expect(mocks.updates).toEqual([]);
+ });
+
+ it("adds an owner who is not in the workspace yet", async () => {
+ mocks.selects = [[video()]];
+ mocks.member.mockResolvedValue(null);
+ await backfillLoomImport(org, {
+ videoId: "videoaaaaaaaaaa",
+ ownerEmail: "[email protected]",
+ });
+ expect(mocks.provision).toHaveBeenCalledWith({
+ organizationId: org,
+ email: "[email protected]",
+ invitedByUserId: "lukeaaaaaaaaaaa",
+ role: "member",
+ });
+ expect(mocks.reassign).toHaveBeenCalledWith(
+ expect.objectContaining({ toUserId: "newaaaaaaaaaaaa" }),
+ );
+ });
+
+ it("leaves the owner alone when it already matches", async () => {
+ mocks.selects = [[video()]];
+ mocks.member.mockResolvedValue({ userId: "lukeaaaaaaaaaaa" });
+ await expect(
+ backfillLoomImport(org, {
+ videoId: "videoaaaaaaaaaa",
+ ownerEmail: "[email protected]",
+ }),
+ ).resolves.toMatchObject({ status: "unchanged" });
+ expect(mocks.reassign).not.toHaveBeenCalled();
+ });
+
+ it("sets the original date and the folder", async () => {
+ mocks.selects = [[video({ folderId: null })], [{ id: "folderbbbbbbbbb" }]];
+ await expect(
+ backfillLoomImport(org, {
+ videoId: "videoaaaaaaaaaa",
+ folderId: "folderbbbbbbbbb",
+ effectiveCreatedAt: "2023-09-14T16:11:20.773Z",
+ }),
+ ).resolves.toMatchObject({ changes: ["folder", "date"] });
+ expect(mocks.updates[0]).toEqual({ folderId: "folderbbbbbbbbb" });
+ expect(mocks.updates).toHaveLength(2);
+ });
+
+ it("skips a date that already matches", async () => {
+ mocks.selects = [[video()]];
+ await expect(
+ backfillLoomImport(org, {
+ videoId: "videoaaaaaaaaaa",
+ effectiveCreatedAt: "2026-09-23T10:00:00.000Z",
+ }),
+ ).resolves.toMatchObject({ status: "unchanged" });
+ });
+
+ it("shares into the team folder once", async () => {
+ mocks.selects = [[video()], [{ id: "teamfolderaaaaa" }], []];
+ await expect(
+ backfillLoomImport(org, {
+ videoId: "videoaaaaaaaaaa",
+ shareFolderId: "teamfolderaaaaa",
+ }),
+ ).resolves.toMatchObject({ changes: ["shared"] });
+ expect(mocks.inserts[0]).toMatchObject({
+ videoId: "videoaaaaaaaaaa",
+ organizationId: org,
+ folderId: "teamfolderaaaaa",
+ });
+ mocks.selects = [
+ [video()],
+ [{ id: "teamfolderaaaaa" }],
+ [{ id: "share", folderId: "teamfolderaaaaa" }],
+ ];
+ await expect(
+ backfillLoomImport(org, {
+ videoId: "videoaaaaaaaaaa",
+ shareFolderId: "teamfolderaaaaa",
+ }),
+ ).resolves.toMatchObject({ status: "unchanged" });
+ expect(mocks.inserts).toHaveLength(1);
+ });
+
+ it.each([
+ [[], "not a Loom import here"],
+ [[video(), []], "folder not in this workspace"],
+ ])("refuses unsafe changes: %#", async (selects, reason) => {
+ mocks.selects = (selects as unknown[]).map((row) =>
+ Array.isArray(row) ? row : [row],
+ );
+ await expect(
+ backfillLoomImport(org, {
+ videoId: "videoaaaaaaaaaa",
+ folderId: "folderbbbbbbbbb",
+ ownerEmail: "[email protected]",
+ }),
+ ).resolves.toMatchObject({ status: "skipped", reason });
+ expect(mocks.reassign).not.toHaveBeenCalled();
+ });
+
+ it("reports a move it could not make", async () => {
+ mocks.selects = [[video()]];
+ mocks.reassign.mockResolvedValue({
+ status: "skipped",
+ reason: "edited video",
+ });
+ await expect(
+ backfillLoomImport(org, {
+ videoId: "videoaaaaaaaaaa",
+ ownerEmail: "[email protected]",
+ }),
+ ).resolves.toMatchObject({ status: "skipped", reason: "edited video" });
+ });
+});
diff --git a/apps/web/__tests__/unit/loom-operator-tools.test.ts b/apps/web/__tests__/unit/loom-operator-tools.test.ts
index 32ea6b57c..98738ee1b 100644
--- a/apps/web/__tests__/unit/loom-operator-tools.test.ts
+++ b/apps/web/__tests__/unit/loom-operator-tools.test.ts
@@ -341,6 +341,20 @@ describe("reassignLoomImport", () => {
expect(mocks.claims).toHaveLength(2);
});
+ it("keeps sharing and visibility when asked", async () => {
+ mocks.selects = [[importedVideo()], [{ userId: ids.to }]];
+ await expect(
+ reassignLoomImport({
+ ...input,
+ options: { keepSharing: true, folderId: "folderaaaaaaaaa" },
+ }),
+ ).resolves.toMatchObject({ status: "moved" });
+ expect(mocks.updates).toEqual([
+ { ownerId: ids.to, folderId: "folderaaaaaaaaa" },
+ ]);
+ expect(mocks.deletes).toBe(0);
+ });
+
it("re-encrypts the word transcript for the new owner", async () => {
const key = getEditTranscriptObjectKey(ids.from, ids.video);
mocks.objects.push({ Key: key, Size: 50 });
diff --git a/apps/web/app/api/internal/loom-imports/backfill/route.ts b/apps/web/app/api/internal/loom-imports/backfill/route.ts
new file mode 100644
index 000000000..610e8a1fa
--- /dev/null
+++ b/apps/web/app/api/internal/loom-imports/backfill/route.ts
@@ -0,0 +1,107 @@
+import { createHash, timingSafeEqual } from "node:crypto";
+import { serverEnv } from "@cap/env";
+import {
+ HttpApi,
+ HttpApiBuilder,
+ HttpApiEndpoint,
+ HttpApiError,
+ HttpApiGroup,
+ HttpServerRequest,
+} from "@effect/platform";
+import { Effect, Layer, Schema } from "effect";
+import {
+ backfillLoomImport,
+ type LoomBackfillOutcome,
+} from "@/lib/loom-import-backfill";
+import { apiToHandler } from "@/lib/server";
+
+const MAX_ITEMS_PER_REQUEST = 25;
+const CapId = Schema.String.pipe(Schema.pattern(/^[A-Za-z0-9_-]{15}$/));
+
+class Api extends HttpApi.make("LoomImportBackfillApi").add(
+ HttpApiGroup.make("root").add(
+ HttpApiEndpoint.post("backfill")`/api/internal/loom-imports/backfill`
+ .setPayload(
+ Schema.Struct({
+ organizationId: CapId,
+ items: Schema.Array(
+ Schema.Struct({
+ videoId: CapId,
+ ownerEmail: Schema.optional(
+ Schema.String.pipe(Schema.maxLength(255)),
+ ),
+ folderId: Schema.optional(CapId),
+ effectiveCreatedAt: Schema.optional(
+ Schema.String.pipe(Schema.maxLength(64)),
+ ),
+ shareFolderId: Schema.optional(CapId),
+ }),
+ ).pipe(Schema.minItems(1), Schema.maxItems(MAX_ITEMS_PER_REQUEST)),
+ }),
+ )
+ .addSuccess(
+ Schema.Struct({
+ results: Schema.Array(
+ Schema.Struct({
+ videoId: Schema.String,
+ status: Schema.Literal(
+ "updated",
+ "unchanged",
+ "skipped",
+ "failed",
+ ),
+ changes: Schema.Array(Schema.String),
+ reason: Schema.optional(Schema.String),
+ }),
+ ),
+ }),
+ )
+ .addError(HttpApiError.Unauthorized),
+ ),
+) {}
+
+function digest(value: string) {
+ return createHash("sha256").update(value, "utf8").digest();
+}
+
+const ApiLive = HttpApiBuilder.api(Api).pipe(
+ Layer.provide(
+ HttpApiBuilder.group(Api, "root", (handlers) =>
+ handlers.handle("backfill", ({ payload }) =>
+ Effect.gen(function* () {
+ const request = yield* HttpServerRequest.HttpServerRequest;
+ const secret = serverEnv().MEDIA_SERVER_WEBHOOK_SECRET;
+ const header = request.headers["x-media-server-secret"];
+ if (
+ !secret ||
+ !header ||
+ !timingSafeEqual(digest(header), digest(secret))
+ )
+ return yield* new HttpApiError.Unauthorized();
+ const results = yield* Effect.forEach(
+ payload.items,
+ (item) =>
+ Effect.promise(
+ (): Promise<LoomBackfillOutcome> =>
+ backfillLoomImport(payload.organizationId, item).catch(
+ (error) => ({
+ videoId: item.videoId,
+ status: "failed",
+ changes: [],
+ reason:
+ error instanceof Error ? error.message : String(error),
+ }),
+ ),
+ ),
+ { concurrency: 1 },
+ );
+ return { results };
+ }),
+ ),
+ ),
+ ),
+);
+
+export const POST = apiToHandler(ApiLive);
+export const maxDuration = 300;
+export const dynamic = "force-dynamic";
diff --git a/apps/web/lib/loom-import-backfill.ts b/apps/web/lib/loom-import-backfill.ts
new file mode 100644
index 000000000..a7d22425c
--- /dev/null
+++ b/apps/web/lib/loom-import-backfill.ts
@@ -0,0 +1,217 @@
+import "server-only";
+
+import { db } from "@cap/database";
+import { nanoId } from "@cap/database/helpers";
+import {
+ folders,
+ importedVideos,
+ organizations,
+ sharedVideos,
+ videos,
+} 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 { reassignLoomImport } from "@/lib/loom-import-reassign";
+import { provisionOrganizationInvitee } from "@/lib/organization-provisioning";
+
+export type LoomBackfillItem = {
+ videoId: string;
+ ownerEmail?: string;
+ folderId?: string;
+ effectiveCreatedAt?: string;
+ shareFolderId?: string;
+};
+
+export type LoomBackfillOutcome = {
+ videoId: string;
+ status: "updated" | "unchanged" | "skipped" | "failed";
+ changes: string[];
+ reason?: string;
+};
+
+export async function backfillLoomImport(
+ organizationId: string,
+ item: LoomBackfillItem,
+): Promise<LoomBackfillOutcome> {
+ const orgId = Organisation.OrganisationId.make(organizationId);
+ const id = Video.VideoId.make(item.videoId);
+ const changes: string[] = [];
+ const [video] = await db()
+ .select({
+ ownerId: videos.ownerId,
+ folderId: videos.folderId,
+ effectiveCreatedAt: videos.effectiveCreatedAt,
+ orgOwnerId: organizations.ownerId,
+ })
+ .from(videos)
+ .innerJoin(
+ importedVideos,
+ and(
+ eq(importedVideos.id, videos.id),
+ eq(importedVideos.orgId, videos.orgId),
+ eq(importedVideos.source, "loom"),
+ ),
+ )
+ .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",
+ };
+
+ 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",
+ };
+ const sharedFolder = item.shareFolderId
+ ? await findFolder(orgId, item.shareFolderId, true)
+ : undefined;
+ if (item.shareFolderId && !sharedFolder)
+ return {
+ videoId: item.videoId,
+ status: "skipped",
+ changes,
+ 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);
+ const member =
+ existingMember ??
+ (await provisionOrganizationInvitee({
+ organizationId: orgId,
+ email,
+ invitedByUserId: video.orgOwnerId,
+ role: "member",
+ }).then((provisioned) => ({ userId: provisioned.userId, email })));
+ if (!member)
+ return {
+ videoId: item.videoId,
+ status: "failed",
+ changes,
+ 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");
+ }
+ }
+
+ if (
+ personalFolder &&
+ personalFolder.id !== video.folderId &&
+ !changes.includes("folder")
+ ) {
+ await db()
+ .update(videos)
+ .set({ folderId: personalFolder.id })
+ .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 (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()
+ .update(sharedVideos)
+ .set({ folderId: sharedFolder.id })
+ .where(eq(sharedVideos.id, share.id));
+ changes.push("shared");
+ }
+ }
+
+ return {
+ videoId: item.videoId,
+ status: changes.length > 0 ? "updated" : "unchanged",
+ changes,
+ };
+}
+
+async function findFolder(
+ organizationId: Organisation.OrganisationId,
+ folderId: string,
+ shared: boolean,
+) {
+ const [folder] = await db()
+ .select({ id: folders.id })
+ .from(folders)
+ .where(
+ and(
+ eq(folders.id, Folder.FolderId.make(folderId)),
+ eq(folders.organizationId, organizationId),
+ shared ? eq(folders.spaceId, organizationId) : isNull(folders.spaceId),
+ ),
+ )
+ .limit(1);
+ return folder;
+}
diff --git a/apps/web/lib/loom-import-reassign.ts b/apps/web/lib/loom-import-reassign.ts
index 270fdc733..4e5c9a7a2 100644
--- a/apps/web/lib/loom-import-reassign.ts
+++ b/apps/web/lib/loom-import-reassign.ts
@@ -11,7 +11,7 @@ import {
videoUploads,
} from "@cap/database/schema";
import { Storage } from "@cap/web-backend";
-import { Organisation, User, Video } from "@cap/web-domain";
+import { Folder, Organisation, User, Video } from "@cap/web-domain";
import { and, eq, sql } from "drizzle-orm";
import { Option } from "effect";
import { getEditTranscriptObjectKey } from "@/lib/edit-transcript";
@@ -21,6 +21,11 @@ import {
} from "@/lib/edit-transcript-storage";
import { runPromise } from "@/lib/server";
+export type LoomReassignOptions = {
+ keepSharing?: boolean;
+ folderId?: string | null;
+};
+
export type LoomReassignOutcome =
| {
videoId: string;
@@ -85,11 +90,13 @@ export async function reassignLoomImport({
videoId,
fromUserId,
toUserId,
+ options = {},
}: {
organizationId: string;
videoId: string;
fromUserId: string;
toUserId: string;
+ options?: LoomReassignOptions;
}): Promise<LoomReassignOutcome> {
const id = Video.VideoId.make(videoId);
const [video] = await db()
@@ -166,6 +173,7 @@ export async function reassignLoomImport({
fromUserId,
toUserId,
token,
+ options,
});
} finally {
await db()
@@ -189,6 +197,7 @@ async function moveClaimedVideo({
fromUserId,
toUserId,
token,
+ options,
}: {
id: Video.VideoId;
videoId: string;
@@ -196,6 +205,7 @@ async function moveClaimedVideo({
fromUserId: string;
toUserId: string;
token: string;
+ options: LoomReassignOptions;
}): Promise<LoomReassignOutcome> {
const source = await listObjects(videoId, fromUserId, organizationId);
if (source.objects.length === 0)
@@ -264,8 +274,10 @@ async function moveClaimedVideo({
.update(videos)
.set({
ownerId: User.UserId.make(toUserId),
- public: false,
- folderId: null,
+ folderId: options.folderId
+ ? Folder.FolderId.make(options.folderId)
+ : null,
+ ...(options.keepSharing ? {} : { public: false }),
})
.where(
and(
@@ -275,8 +287,10 @@ async function moveClaimedVideo({
),
);
if (getAffectedRows(result) !== 1) return false;
- await tx.delete(sharedVideos).where(eq(sharedVideos.videoId, id));
- await tx.delete(spaceVideos).where(eq(spaceVideos.videoId, id));
+ if (!options.keepSharing) {
+ await tx.delete(sharedVideos).where(eq(sharedVideos.videoId, id));
+ await tx.delete(spaceVideos).where(eq(spaceVideos.videoId, id));
+ }
return true;
});
if (!updated) {