Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): retry temporary Loom failures by error code and keep CSV values on re-import

Richie McIlroyf6699d48fc4d
diff --git a/apps/web/__tests__/unit/loom-csv.test.ts b/apps/web/__tests__/unit/loom-csv.test.ts
index b1433c2a4..7435b47c6 100644
--- a/apps/web/__tests__/unit/loom-csv.test.ts
+++ b/apps/web/__tests__/unit/loom-csv.test.ts
@@ -1,5 +1,10 @@
import { describe, expect, it } from "vitest";
-import { formatLoomCsvCell, parseConciergeLoomCsv } from "@/lib/loom-csv";
+import {
+ formatLoomCsvCell,
+ parseConciergeLoomCsv,
+ parseLoomCsvRecords,
+ readLoomCsvCell,
+} from "@/lib/loom-csv";
describe("concierge Loom CSV", () => {
it("keeps quoted workspace names and source row numbers", () => {
@@ -54,3 +59,18 @@ describe("Loom CSV results cells", () => {
expect(formatLoomCsvCell("\r=1")).toBe(`"'\r=1"`);
});
});
+
+describe("Loom CSV results round trip", () => {
+ it("restores neutralized values when a results CSV is imported again", () => {
+ const values = ["@Sales", "[email protected]", "-Ops, EU", "=1"];
+ const [cells] = parseLoomCsvRecords(
+ `${values.map(formatLoomCsvCell).join(",")}\n`,
+ );
+ expect(cells?.map(readLoomCsvCell)).toEqual(values);
+ });
+
+ it("leaves other leading apostrophes alone", () => {
+ expect(readLoomCsvCell("'Sales")).toBe("'Sales");
+ expect(readLoomCsvCell("O'Brien")).toBe("O'Brien");
+ });
+});
diff --git a/apps/web/__tests__/unit/loom-import.test.ts b/apps/web/__tests__/unit/loom-import.test.ts
index 650581202..e26258bd6 100644
--- a/apps/web/__tests__/unit/loom-import.test.ts
+++ b/apps/web/__tests__/unit/loom-import.test.ts
@@ -1207,6 +1207,59 @@ describe("importFromLoom", () => {
expect(startMock).toHaveBeenCalledTimes(1);
});
+ it("marks CSV rows that hit a temporary Loom failure as retryable", async () => {
+ whereMock.mockImplementation((conditions: unknown) => {
+ if (JSON.stringify(conditions).includes("sourceId")) {
+ return Promise.resolve([]);
+ }
+
+ return withLimit([{ userId: "member-123", email: "[email protected]" }]);
+ });
+
+ const fetchMock = vi.mocked(fetch);
+ fetchMock.mockImplementation(async (input) => {
+ const url = typeof input === "string" ? input : input.toString();
+
+ if (url.includes("/transcoded-url")) {
+ return {
+ ok: false,
+ status: 429,
+ headers: new Headers({ "retry-after": "30" }),
+ body: null,
+ } as Response;
+ }
+
+ throw new Error(`Unexpected fetch: ${url}`);
+ });
+
+ const { importFromLoomCsv } = await import("@/actions/loom");
+
+ const result = await importFromLoomCsv({
+ orgId: "org-1" as never,
+ rows: [
+ {
+ rowNumber: 2,
+ loomUrl: "https://www.loom.com/share/loom-ratelimited1",
+ userEmail: "[email protected]",
+ },
+ ],
+ });
+
+ expect(result.results).toStrictEqual([
+ {
+ rowNumber: 2,
+ userEmail: "[email protected]",
+ spaceName: undefined,
+ success: false,
+ videoId: undefined,
+ error:
+ "Loom is temporarily unavailable. Please retry the import later.",
+ errorCode: "loom_temporarily_unavailable",
+ },
+ ]);
+ expect(startMock).not.toHaveBeenCalled();
+ });
+
it("limits CSV imports to 500 rows", async () => {
const fetchMock = vi.mocked(fetch);
const { importFromLoomCsv } = await import("@/actions/loom");
diff --git a/apps/web/actions/loom.ts b/apps/web/actions/loom.ts
index b16184855..2fd4d4890 100644
--- a/apps/web/actions/loom.ts
+++ b/apps/web/actions/loom.ts
@@ -90,10 +90,13 @@ interface LoomDownloadResult {
error?: string;
}
+export type LoomImportErrorCode = "loom_temporarily_unavailable";
+
export interface LoomImportResult {
success: boolean;
videoId?: Video.VideoId;
error?: string;
+ errorCode?: LoomImportErrorCode;
}
export interface LoomCsvImportRow {
@@ -110,6 +113,7 @@ export interface LoomCsvImportRowResult {
success: boolean;
videoId?: Video.VideoId;
error?: string;
+ errorCode?: LoomImportErrorCode;
}
export interface LoomCsvImportResult {
@@ -494,7 +498,11 @@ async function importLoomVideoForOwner({
downloadUrl = await getLoomDownloadUrl(loomVideoId);
} catch (error) {
if (error instanceof LoomDownloadTemporaryError) {
- return { success: false, error: error.message };
+ return {
+ success: false,
+ error: error.message,
+ errorCode: "loom_temporarily_unavailable",
+ };
}
throw error;
}
@@ -1109,6 +1117,7 @@ async function processLoomCsvRows({
success: result.success,
videoId: result.videoId,
error: result.error ?? spaceError,
+ ...(result.errorCode ? { errorCode: result.errorCode } : {}),
});
} catch {
results.push({
diff --git a/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx b/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
index 53ffddf7e..cc719df2e 100644
--- a/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
+++ b/apps/web/app/(org)/dashboard/import/loom/ImportLoomPage.tsx
@@ -56,7 +56,11 @@ import {
} from "@/actions/loom";
import { useDashboardContext } from "@/app/(org)/dashboard/Contexts";
import { UpgradeModal } from "@/components/UpgradeModal";
-import { formatLoomCsvCell, parseLoomCsvRecords } from "@/lib/loom-csv";
+import {
+ formatLoomCsvCell,
+ parseLoomCsvRecords,
+ readLoomCsvCell,
+} from "@/lib/loom-csv";
import {
type LoomImportDestination,
loomImportDestinationHref,
@@ -100,7 +104,7 @@ const MAX_SPACE_NAME_LENGTH = 255;
const MAX_LOOM_CSV_IMPORT_ROWS = 500;
const LOOM_CSV_BATCH_SIZE = 10;
const LOOM_CSV_BATCH_DELAY_MS = 1500;
-const LOOM_CSV_RATE_LIMIT_RETRY_DELAY_MS = 30000;
+const LOOM_CSV_RETRY_DELAY_MS = 30000;
const LOOM_CSV_NOT_ATTEMPTED_MESSAGE = "Not attempted.";
const LOOM_CSV_LIMIT_MESSAGE =
"CSV imports are limited to 500 videos at a time. Contact support to raise this limit.";
@@ -166,22 +170,19 @@ function withNotAttemptedRows(
].sort((a, b) => a.rowNumber - b.rowNumber);
}
-function isRateLimitError(message: string | undefined) {
- if (!message) return false;
- const normalized = message.toLowerCase();
- return normalized.includes("rate limit") || normalized.includes("too many");
-}
-
-function rateLimitedBatchRows(
+function retryableBatchRows(
batch: MappedRow[],
results: LoomCsvImportRowResult[],
) {
- const limited = new Set(
+ const retryable = new Set(
results
- .filter((row) => !row.success && isRateLimitError(row.error))
+ .filter(
+ (row) =>
+ !row.success && row.errorCode === "loom_temporarily_unavailable",
+ )
.map((row) => row.rowNumber),
);
- return batch.filter((row) => limited.has(row.rowNumber));
+ return batch.filter((row) => retryable.has(row.rowNumber));
}
function importErrorMessage(error: unknown) {
@@ -218,7 +219,8 @@ function parseCsv(text: string, fileName: string): CsvData {
const headers = records[0]?.map((header) => header.trim()) ?? [];
const rows = records
.slice(1)
- .filter((row) => row.some((cell) => cell.trim().length > 0));
+ .filter((row) => row.some((cell) => cell.trim().length > 0))
+ .map((row) => row.map(readLoomCsvCell));
if (headers.length === 0) {
throw new Error("No CSV headers found.");
@@ -593,15 +595,14 @@ export const ImportLoomPage = ({
}
batchResults = importResult.results;
- retryRows = rateLimitedBatchRows(batch, batchResults);
+ retryRows = retryableBatchRows(batch, batchResults);
} catch (error) {
const message = importErrorMessage(error);
batchResults = batch.map((row) => buildRowFailure(row, message));
- if (isRateLimitError(message)) retryRows = batch;
}
if (retryRows.length > 0) {
- await delay(LOOM_CSV_RATE_LIMIT_RETRY_DELAY_MS);
+ await delay(LOOM_CSV_RETRY_DELAY_MS);
try {
const retryResult = await importFromLoomCsv({
diff --git a/apps/web/lib/loom-csv.ts b/apps/web/lib/loom-csv.ts
index 25c5cf24b..407c93456 100644
--- a/apps/web/lib/loom-csv.ts
+++ b/apps/web/lib/loom-csv.ts
@@ -51,6 +51,7 @@ export function parseLoomCsvRecords(text: string) {
}
const SPREADSHEET_FORMULA_PREFIX = /^[=+\-@\t\r]/;
+const NEUTRALIZED_FORMULA_PREFIX = /^'(?=[=+\-@\t\r])/;
export function formatLoomCsvCell(value: string) {
const literal = SPREADSHEET_FORMULA_PREFIX.test(value) ? `'${value}` : value;
@@ -60,6 +61,10 @@ export function formatLoomCsvCell(value: string) {
return literal;
}
+export function readLoomCsvCell(value: string) {
+ return value.replace(NEUTRALIZED_FORMULA_PREFIX, "");
+}
+
export function parseConciergeLoomCsv(text: string): LoomCsvImportRow[] {
const records = parseLoomCsvRecords(text);
const headers = records[0]?.map((value) => value.toLowerCase()) ?? [];