import { logger } from "../../logger"; import { prisma } from "../db"; import { extractUploadIdsFromUnknown } from "./extractUploadIds"; import { browserHoldsUploadGrant } from "./uploadGrantCookie"; type UploadClaimRow = { id: string; userId: string | null; ruleId: string | null; draftId: string | null; }; async function loadUploadRows(ids: string[]): Promise { if (ids.length === 0) return []; return prisma.upload.findMany({ where: { id: { in: ids } }, select: { id: true, userId: true, ruleId: true, draftId: true }, }); } /** * A save may take a file only when it is unlinked (or already on this Rule) * and was made by this account or browser. */ async function idsClaimableForRule( rows: UploadClaimRow[], opts: { ruleId: string; userId: string | null }, ): Promise { const out: string[] = []; for (const row of rows) { if (row.ruleId != null && row.ruleId !== opts.ruleId) continue; if (row.ruleId === opts.ruleId) { out.push(row.id); continue; } if (opts.userId != null && row.userId === opts.userId) { out.push(row.id); continue; } if (row.userId == null && (await browserHoldsUploadGrant(row.id))) { out.push(row.id); } } return out; } async function idsClaimableForDraft( rows: UploadClaimRow[], opts: { draftId: string; userId: string }, ): Promise { const out: string[] = []; for (const row of rows) { if (row.ruleId != null) continue; if (row.draftId != null && row.draftId !== opts.draftId) continue; if (row.userId === opts.userId) { out.push(row.id); continue; } if (row.userId == null && (await browserHoldsUploadGrant(row.id))) { out.push(row.id); } } return out; } export async function linkUploadsToRule(opts: { ruleId: string; document: unknown; userId: string | null; }): Promise { const ids = extractUploadIdsFromUnknown(opts.document); if (ids.length === 0) return; try { const rows = await loadUploadRows(ids); const claimable = await idsClaimableForRule(rows, opts); if (claimable.length === 0) return; await prisma.upload.updateMany({ where: { id: { in: claimable }, AND: [ { OR: [{ ruleId: null }, { ruleId: opts.ruleId }] }, opts.userId ? { OR: [{ userId: opts.userId }, { userId: null }] } : { userId: null }, ], }, data: { ruleId: opts.ruleId, draftId: null, ...(opts.userId ? { userId: opts.userId } : {}), }, }); } catch (err) { logger.warn("[uploads] failed to link uploads to rule", opts.ruleId, err); } } export async function linkUploadsToDraft(opts: { draftId: string; payload: unknown; userId: string; }): Promise { const ids = extractUploadIdsFromUnknown(opts.payload); if (ids.length === 0) return; try { const rows = await loadUploadRows(ids); const claimable = await idsClaimableForDraft(rows, opts); if (claimable.length === 0) return; await prisma.upload.updateMany({ where: { id: { in: claimable }, ruleId: null, AND: [ { OR: [{ draftId: null }, { draftId: opts.draftId }] }, { OR: [{ userId: opts.userId }, { userId: null }] }, ], }, data: { draftId: opts.draftId, userId: opts.userId, }, }); } catch (err) { logger.warn("[uploads] failed to link uploads to draft", opts.draftId, err); } }