Skip to content

Open source CapCut alternative.

TypeScript
15.0 KB
Raw Blame
// Synced notes: one CutNote row per note, written by the iOS app and the
// desktop Notes tab. Both sides merge by last-writer-wins on the client's
// `updatedAt` stamp, and deletes keep tombstone rows so a removal made
// offline on one device still lands on the other.
//
// Notes file into folders — CutFolder rows with scope "note", the same table
// the library and the projects home keep their folders in. Folder ids are
// client-generated like note ids, so a phone can make one offline and push it
// under the id it already filed notes against. Folders file into folders the
// same way, through `parentId`; the top level is null.
import { noteLibraryFoldersSchema, noteLocationSchema } from "@/cut/lib/noteReference";
import { Prisma } from "@/generated/prisma/client";
import { resolveParent, subtreeOf } from "@/cut/lib/folderTree";
import { NOTE_LABELS_MAX } from "@/cut/lib/types";
import { prisma } from "@/lib/prisma";
import { UserError } from "@/cut/lib/userError";
import { caught, err } from "./util";

export interface NoteView {
  libraryLocation: unknown;
  id: string;
  title: string;
  body: string;
  colorIndex: number;
  folderId: string | null;
  labelIds: string[];
  updatedAt: number;
  deletedAt: number | null;
  createdAt: number;
}

export interface NoteFolderView {
  id: string;
  name: string;
  parentId: string | null;
  updatedAt: number;
  createdAt: number;
}

export interface NoteLabelView {
  id: string;
  name: string;
  updatedAt: number;
  createdAt: number;
}

const view = (row: {
  libraryLocation: unknown;
  id: string;
  title: string;
  body: string;
  colorIndex: number;
  folderId: string | null;
  labelIds: string[];
  updatedAt: Date;
  deletedAt: Date | null;
  createdAt: Date;
}): NoteView => ({
  libraryLocation: row.libraryLocation,
  id: row.id,
  title: row.title,
  body: row.body,
  colorIndex: row.colorIndex,
  folderId: row.folderId,
  labelIds: row.labelIds,
  updatedAt: row.updatedAt.getTime(),
  deletedAt: row.deletedAt?.getTime() ?? null,
  createdAt: row.createdAt.getTime(),
});

const folderView = (row: {
  id: string;
  name: string;
  parentId: string | null;
  updatedAt: Date;
  createdAt: Date;
}): NoteFolderView => ({
  id: row.id,
  name: row.name,
  parentId: row.parentId,
  updatedAt: row.updatedAt.getTime(),
  createdAt: row.createdAt.getTime(),
});

const labelView = (row: {
  id: string;
  name: string;
  updatedAt: Date;
  createdAt: Date;
}): NoteLabelView => ({
  id: row.id,
  name: row.name,
  updatedAt: row.updatedAt.getTime(),
  createdAt: row.createdAt.getTime(),
});

const NOTE_ID = /^[\w-]{1,64}$/;
const TEXT_MAX = 20_000;

/** The folder a note may be filed in: one of this account's note folders, or
 * the top level. An id naming nothing files the note at the top level rather
 * than failing the write — the folder may have been deleted on the other
 * device while this one was offline. */
async function resolveFolder(db: typeof prisma, userId: string, folderId: unknown): Promise<string | null> {
  if (typeof folderId !== "string" || !folderId) return null;
  const row = await db.cutFolder.findFirst({
    where: { id: folderId, userId, scope: "note" },
    select: { id: true },
  });
  return row?.id ?? null;
}

/** The folder a note folder may be filed in, by the rules every folder table
 * shares (folderTree.ts), against this account's note folders. */
async function resolveNoteParent(db: typeof prisma, userId: string, id: string, parentId: unknown): Promise<string | null> {
  if (typeof parentId !== "string" || !parentId) return null;
  const folders = await db.cutFolder.findMany({
    where: { userId, scope: "note" },
    select: { id: true, parentId: true },
  });
  return resolveParent(folders, id, parentId);
}

/** The labels a note may carry: the ids among `labelIds` that name one of
 * this account's labels. An id naming nothing is dropped rather than failing
 * the write — the label may have been deleted on the other device while this
 * one was offline. `undefined` means the write said nothing about labels — an
 * older client — and the note keeps the labels it has. */
async function resolveLabels(db: typeof prisma, userId: string, labelIds: unknown): Promise<string[] | undefined> {
  if (!Array.isArray(labelIds)) return undefined;
  const asked = [...new Set(labelIds.filter((id): id is string => typeof id === "string"))];
  if (asked.length === 0) return [];
  const rows = await db.cutNoteLabel.findMany({
    where: { id: { in: asked }, userId },
    select: { id: true },
  });
  const known = new Set(rows.map((r) => r.id));
  return asked.filter((id) => known.has(id));
}

export function createNotesCloud(db: typeof prisma = prisma) {
  /** A deleted Library folder takes the notes filed in it, the way it takes
   * its files: each stays as a tombstone so the phone drops it too. */
  async function deleteLibraryFolderNotes(userId: string, residency: string, folderIds: string[]) {
    if (!folderIds.length) return;
    const now = new Date();
    await db.cutNote.updateMany({
      where: { userId, deletedAt: null, AND: [
        { libraryLocation: { path: ["residency"], equals: residency } },
        { OR: folderIds.map((id) => ({ libraryLocation: { path: ["folderId"], equals: id } })) },
      ] },
      data: { updatedAt: now, deletedAt: now },
    });
  }
  return {
  deleteLibraryFolderNotes,
  async deleteInLibraryFolders(userId: string, req: Request) {
    const parsed = noteLibraryFoldersSchema.safeParse(await req.json());
    if (!parsed.success) return err("Invalid Library folders.", 400);
    await deleteLibraryFolderNotes(userId, parsed.data.residency, parsed.data.folderIds);
    return Response.json({ ok: true });
  },
  async get(userId: string, id: string) {
    const row = await db.cutNote.findFirst({ where: { id, userId, deletedAt: null } });
    return row ? Response.json(view(row)) : err(new UserError("note_not_found", "Note not found."), 404);
  },
  /** Every note including tombstones, newest first, and the folders they file
   * into. Notes are small and the set is one person's, so the merge reads the
   * whole list. */
  async list(userId: string) {
    const [rows, folders, labels] = await Promise.all([
      db.cutNote.findMany({ where: { userId }, orderBy: { updatedAt: "desc" } }),
      db.cutFolder.findMany({
        where: { userId, scope: "note" },
        orderBy: { createdAt: "asc" },
      }),
      db.cutNoteLabel.findMany({ where: { userId }, orderBy: { createdAt: "asc" } }),
    ]);
    return Response.json({
      notes: rows.map(view),
      folders: folders.map(folderView),
      labels: labels.map(labelView),
    });
  },

  /** Upsert one note. The write applies only when its stamp is not older than
   * the stored row's — last writer wins — and a live write revives a
   * tombstone. The response is whichever version won. */
  async put(userId: string, id: string, req: Request) {
    try {
      if (!NOTE_ID.test(id)) return err("Invalid note id.", 400);
      const body = (await req.json()) as {
        libraryLocation?: unknown;
        title?: string;
        body?: string;
        colorIndex?: number;
        folderId?: string | null;
        labelIds?: string[];
        updatedAt?: number;
      };
      if (typeof body.updatedAt !== "number" || !Number.isFinite(body.updatedAt)) {
        return err("updatedAt is required.", 400);
      }
      const stamp = new Date(body.updatedAt);
      const parsedLocation = body.libraryLocation == null ? null : noteLocationSchema.safeParse(body.libraryLocation);
      if (parsedLocation && !parsedLocation.success) return err("Invalid Library location.", 400);
      const location = body.libraryLocation === undefined ? undefined
        : parsedLocation?.success ? parsedLocation.data : null;
      if (location?.residency === "cloud" && location.folderId !== null && !await db.cutFolder.findFirst({ where: { id: location.folderId, userId, scope: "library" }, select: { id: true } })) {
        return err(new UserError("library_folder_not_found", "Library folder not found."), 404);
      }
      // Over the cap the write is refused outright: truncating here would
      // answer 200 with a note wearing fewer labels than the person gave it,
      // and the merge would carry that loss back to their other device.
      if (Array.isArray(body.labelIds) && new Set(body.labelIds).size > NOTE_LABELS_MAX) {
        return err(
          new UserError("note_labels_too_many", `A note can carry at most ${NOTE_LABELS_MAX} labels.`, {
            max: NOTE_LABELS_MAX,
          }),
          400
        );
      }
      const labelIds = await resolveLabels(db, userId, body.labelIds);
      const data = {
        ...(location === undefined ? {} : { libraryLocation: location === null ? Prisma.DbNull : location }),
        title: String(body.title ?? "").slice(0, 200),
        body: String(body.body ?? "").slice(0, TEXT_MAX),
        colorIndex: Number.isInteger(body.colorIndex) ? (body.colorIndex as number) : 0,
        folderId: await resolveFolder(db, userId, body.folderId),
        updatedAt: stamp,
        deletedAt: null,
      };
      const existing = await db.cutNote.findFirst({ where: { id, userId } });
      if (!existing) {
        const row = await db.cutNote.create({
          data: { id, userId, ...data, labelIds: labelIds ?? [] },
        });
        return Response.json(view(row));
      }
      if (existing.updatedAt.getTime() > stamp.getTime()) {
        return Response.json(view(existing));
      }
      const row = await db.cutNote.update({
        where: { id },
        // A write that said nothing about labels keeps the ones stored.
        data: labelIds === undefined ? data : { ...data, labelIds },
      });
      return Response.json(view(row));
    } catch (e) {
      return caught(e, new UserError("note_save_failed", "Could not save the note."));
    }
  },

  /** Delete one note: the row stays as a tombstone carrying the delete's own
   * stamp, so both clients drop their copies on the next merge. */
  async remove(userId: string, id: string) {
    try {
      const existing = await db.cutNote.findFirst({ where: { id, userId } });
      if (!existing) return Response.json({ ok: true });
      await db.cutNote.update({
        where: { id },
        data: { updatedAt: new Date(), deletedAt: new Date() },
      });
      return Response.json({ ok: true });
    } catch (e) {
      return caught(e, new UserError("note_delete_failed", "Could not delete the note."));
    }
  },

  // --- Folders (scope "note") ---

  /** Create, rename or move one folder under a client-chosen id. One call
   * serves all three, so a phone that made the folder offline pushes it with
   * the request a rename uses. A write that says nothing about `parentId`
   * keeps the parent the folder has; the response is the folder as stored,
   * which is where a parent the server could not honor shows up. */
  async putFolder(userId: string, id: string, req: Request) {
    try {
      if (!NOTE_ID.test(id)) return err("Invalid folder id.", 400);
      const body = (await req.json()) as { name?: string; parentId?: string | null };
      const name = String(body.name ?? "").trim().slice(0, 120);
      if (!name) return err(new UserError("folder_name_required", "Folder name required."), 400);
      const existing = await db.cutFolder.findFirst({
        where: { id, userId, scope: "note" },
        select: { id: true, parentId: true },
      });
      const parentId =
        body.parentId === undefined
          ? (existing?.parentId ?? null)
          : await resolveNoteParent(db, userId, id, body.parentId);
      if (!existing) {
        const row = await db.cutFolder.create({
          data: { id, userId, name, parentId, scope: "note" },
        });
        return Response.json(folderView(row));
      }
      const row = await db.cutFolder.update({ where: { id }, data: { name, parentId } });
      return Response.json(folderView(row));
    } catch (e) {
      return caught(e, new UserError("note_folder_save_failed", "Could not save the folder."));
    }
  },

  /** Delete one folder and everything under it: the folders filed inside,
   * however deep, and every note they hold, which stay as tombstones so the
   * phone drops its copies on the next merge. */
  async removeFolder(userId: string, id: string) {
    try {
      await db.$transaction(async (tx) => {
        const folders = await tx.cutFolder.findMany({
          where: { userId, scope: "note" },
          select: { id: true, parentId: true },
        });
        if (!folders.some((f) => f.id === id)) return;
        const tree = subtreeOf(folders, id);
        const now = new Date();
        await tx.cutNote.updateMany({
          where: { userId, folderId: { in: tree }, deletedAt: null },
          data: { updatedAt: now, deletedAt: now },
        });
        await tx.cutFolder.deleteMany({ where: { userId, scope: "note", id: { in: tree } } });
      });
      return Response.json({ ok: true });
    } catch (e) {
      return caught(e, new UserError("note_folder_delete_failed", "Could not delete the folder."));
    }
  },

  // --- Labels ---

  /** Create or rename one label under a client-chosen id, the way folders
   * work: one call serves both, so a phone that made the label offline pushes
   * it with the request a rename uses. */
  async putLabel(userId: string, id: string, req: Request) {
    try {
      if (!NOTE_ID.test(id)) return err("Invalid label id.", 400);
      const body = (await req.json()) as { name?: string };
      const name = String(body.name ?? "").trim().slice(0, 60);
      if (!name) return err(new UserError("label_name_required", "Label name required."), 400);
      const existing = await db.cutNoteLabel.findFirst({
        where: { id, userId },
        select: { id: true },
      });
      if (!existing) {
        const row = await db.cutNoteLabel.create({ data: { id, userId, name } });
        return Response.json(labelView(row));
      }
      const row = await db.cutNoteLabel.update({ where: { id }, data: { name } });
      return Response.json(labelView(row));
    } catch (e) {
      return caught(e, new UserError("label_save_failed", "Could not save the label."));
    }
  },

  /** Delete one label and take it off every note that carries it. The notes'
   * own stamps stay put — dropping a dead id is not an edit that should win a
   * merge — and clients drop unknown ids on their own. */
  async removeLabel(userId: string, id: string) {
    try {
      await db.$transaction(async (tx) => {
        const { count } = await tx.cutNoteLabel.deleteMany({ where: { id, userId } });
        if (!count) return;
        const notes = await tx.cutNote.findMany({
          where: { userId, labelIds: { has: id } },
          select: { id: true, labelIds: true },
        });
        for (const n of notes) {
          await tx.cutNote.update({
            where: { id: n.id },
            data: { labelIds: n.labelIds.filter((l) => l !== id) },
          });
        }
      });
      return Response.json({ ok: true });
    } catch (e) {
      return caught(e, new UserError("label_delete_failed", "Could not delete the label."));
    }
  },
};
}

export const notesCloud = createNotesCloud();