Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

4.0 KB
Raw Blame
import type { InvkBoard } from './board';
import type { RestoredMediaLedger } from './restoreProjectMedia';
import type { InvkBoardIssue } from './transfer';

import { createStagingBoard, getBoardProjectId, isRequestCancellation, placeBoardInProject } from './assetTransport';

/**
 * A project's boards travel as a set, but only the inbox rides the project create: the server claims it in the same
 * transaction that writes the document. So every board is staged alike — a private Library board, made in source order
 * so creation order survives — media lands on it before the commit point, and the others are moved into the project
 * once it exists. An inbox is always staged, even for a source that lists no boards, so the create knows the board id
 * the creator's workspace opens on and carries both in one request.
 */

export interface StagedBoard {
  board: InvkBoard;
  /** The Library board standing in for `board` until the project exists. */
  stagingBoardId: string;
}

export interface StagedBoards {
  boards: StagedBoard[];
  /** The staging board the project create claims as its inbox. */
  inboxStagingBoardId: string;
}

export interface MemberBoardPlacementDeps {
  getBoardProjectId?: typeof getBoardProjectId;
  /** After each board settles, placed or not. */
  onProgress?: (completed: number, total: number) => void;
  placeBoardInProject?: typeof placeBoardInProject;
  signal?: AbortSignal;
}

/**
 * One staging board per board, in `boards` order, each on the ledger the moment it exists so a restore abandoned
 * part-way can delete it. The inbox's is named after the project, the others after themselves. A source without an
 * inbox (an archive with no board entries, say) gets an empty one staged first.
 */
export const createStagingBoards = async (
  boards: readonly InvkBoard[],
  projectName: string,
  ledger: RestoredMediaLedger,
  signal?: AbortSignal,
  create: typeof createStagingBoard = createStagingBoard
): Promise<StagedBoards> => {
  const staged: StagedBoard[] = [];
  const withInbox = boards.some((board) => board.isInbox)
    ? boards
    : [{ archived: false, isInbox: true, items: [], name: projectName }, ...boards];

  for (const board of withInbox) {
    const stagingBoardId = await create(board.isInbox ? projectName : board.name, signal);

    ledger.boardIds.push(stagingBoardId);
    staged.push({ board, stagingBoardId });
  }

  return { boards: staged, inboxStagingBoardId: staged.find(({ board }) => board.isInbox)!.stagingBoardId };
};

/**
 * Move every board but the inbox into the created project, archived as the source had it. Each settles on its own,
 * because a project with most of its boards beats none: a board that could not move is reported, and stays in the
 * Library with everything on it. Cancellation and account expiry still abort.
 */
export const placeMemberBoards = async (
  staged: readonly StagedBoard[],
  projectId: string,
  deps: MemberBoardPlacementDeps = {}
): Promise<InvkBoardIssue[]> => {
  const place = deps.placeBoardInProject ?? placeBoardInProject;
  const readProjectId = deps.getBoardProjectId ?? getBoardProjectId;
  const issues: InvkBoardIssue[] = [];
  /** A move with no usable answer may still have been made; the board says where it is. */
  const wasPlaced = async (stagingBoardId: string): Promise<boolean> => {
    try {
      return (await readProjectId(stagingBoardId, deps.signal)) === projectId;
    } catch (error) {
      if (isRequestCancellation(error)) {
        throw error;
      }

      return false;
    }
  };
  const members = staged.filter(({ board }) => !board.isInbox);

  for (const [index, { board, stagingBoardId }] of members.entries()) {
    try {
      await place(stagingBoardId, projectId, board.archived, deps.signal);
    } catch (error) {
      if (isRequestCancellation(error)) {
        throw error;
      }

      if (!(await wasPlaced(stagingBoardId))) {
        issues.push({ name: board.name });
      }
    }

    deps.onProgress?.(index + 1, members.length);
  }

  return issues;
};