2026-01-10 10:35:21 +08:00
|
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
|
import { createSupabaseRouteClient } from "@/lib/supabase/server";
|
|
|
|
|
|
import { applyMindmapOps, type MindmapOp } from "@/lib/mindmap/mindmapOps";
|
|
|
|
|
|
import { readMindmapLocal, writeMindmapLocal } from "@/lib/mindmap/mindmapLocalStore";
|
2026-01-17 10:12:53 +08:00
|
|
|
|
import { isConvexEnabled } from "@/lib/convex/enabled";
|
|
|
|
|
|
import { getAuthedConvexClient } from "@/lib/convex/route";
|
|
|
|
|
|
import { api } from "@/lib/convex/api";
|
2026-01-10 10:35:21 +08:00
|
|
|
|
|
|
|
|
|
|
const defaultMindmapData = {
|
|
|
|
|
|
data: { text: "中心主题" },
|
|
|
|
|
|
children: [],
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
type RequestPayload = {
|
|
|
|
|
|
ops: MindmapOp[];
|
|
|
|
|
|
actor?: { kind?: string; provider?: string; model?: string };
|
|
|
|
|
|
reason?: string;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export async function POST(
|
|
|
|
|
|
request: Request,
|
|
|
|
|
|
{ params }: { params: Promise<{ docId: string; mindmapId: string }> },
|
|
|
|
|
|
) {
|
|
|
|
|
|
const { docId, mindmapId } = await params;
|
2026-01-17 10:12:53 +08:00
|
|
|
|
|
|
|
|
|
|
if (isConvexEnabled()) {
|
|
|
|
|
|
const { auth, client } = getAuthedConvexClient();
|
|
|
|
|
|
|
|
|
|
|
|
const payload = (await request.json().catch(() => null)) as RequestPayload | null;
|
|
|
|
|
|
const ops = Array.isArray(payload?.ops) ? payload!.ops : [];
|
|
|
|
|
|
if (!ops.length) {
|
|
|
|
|
|
return NextResponse.json({ error: "缺少 ops" }, { status: 400 });
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ops.length > 80) {
|
|
|
|
|
|
return NextResponse.json({ error: "ops 过多(最多 80)" }, { status: 400 });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const current = await client.query(api.mindmaps.get, {
|
|
|
|
|
|
userId: auth.userId,
|
|
|
|
|
|
docId,
|
|
|
|
|
|
mindmapId,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const baseData = current?.data ?? defaultMindmapData;
|
|
|
|
|
|
const { data: nextData, applied, errors } = applyMindmapOps(baseData, ops);
|
|
|
|
|
|
|
|
|
|
|
|
await client.mutation(api.mindmaps.put, {
|
|
|
|
|
|
userId: auth.userId,
|
|
|
|
|
|
docId,
|
|
|
|
|
|
mindmapId,
|
|
|
|
|
|
data: nextData,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({
|
|
|
|
|
|
ok: true,
|
|
|
|
|
|
applied,
|
|
|
|
|
|
errors,
|
|
|
|
|
|
data: nextData,
|
|
|
|
|
|
meta: {
|
|
|
|
|
|
documentId: docId,
|
|
|
|
|
|
mindmapId,
|
|
|
|
|
|
actor: payload?.actor ?? null,
|
|
|
|
|
|
reason: payload?.reason ?? null,
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return NextResponse.json({ error: (error as Error).message }, { status: 400 });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-10 10:35:21 +08:00
|
|
|
|
const supabase = await createSupabaseRouteClient();
|
|
|
|
|
|
const {
|
|
|
|
|
|
data: { session },
|
|
|
|
|
|
} = await supabase.auth.getSession();
|
|
|
|
|
|
if (!session) {
|
|
|
|
|
|
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 校验页面归属
|
|
|
|
|
|
const { data: doc, error: docError } = await supabase
|
|
|
|
|
|
.from("documents")
|
|
|
|
|
|
.select("id,title,mindmap_data")
|
|
|
|
|
|
.eq("id", docId)
|
|
|
|
|
|
.eq("user_id", session.user.id)
|
|
|
|
|
|
.maybeSingle();
|
|
|
|
|
|
if (docError) {
|
|
|
|
|
|
return NextResponse.json({ error: docError.message }, { status: 400 });
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!doc) {
|
|
|
|
|
|
return NextResponse.json({ error: "页面不存在" }, { status: 404 });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const payload = (await request.json().catch(() => null)) as RequestPayload | null;
|
|
|
|
|
|
const ops = Array.isArray(payload?.ops) ? payload!.ops : [];
|
|
|
|
|
|
if (!ops.length) {
|
|
|
|
|
|
return NextResponse.json({ error: "缺少 ops" }, { status: 400 });
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ops.length > 80) {
|
|
|
|
|
|
return NextResponse.json({ error: "ops 过多(最多 80)" }, { status: 400 });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const local = await readMindmapLocal(docId, mindmapId);
|
|
|
|
|
|
const baseData = local.ok ? local.data : (doc.mindmap_data ?? defaultMindmapData);
|
|
|
|
|
|
|
|
|
|
|
|
const { data: nextData, applied, errors } = applyMindmapOps(baseData, ops);
|
|
|
|
|
|
|
|
|
|
|
|
await writeMindmapLocal(docId, mindmapId, nextData, doc.title ?? "无标题");
|
|
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({
|
|
|
|
|
|
ok: true,
|
|
|
|
|
|
applied,
|
|
|
|
|
|
errors,
|
|
|
|
|
|
data: nextData,
|
|
|
|
|
|
meta: {
|
|
|
|
|
|
documentId: docId,
|
|
|
|
|
|
mindmapId,
|
|
|
|
|
|
actor: payload?.actor ?? null,
|
|
|
|
|
|
reason: payload?.reason ?? null,
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|