@kidecms/core 0.19.1 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/admin/components/AssetsGrid.tsx +1 -1
- package/admin/components/CollaborationReviewBar.tsx +245 -61
- package/admin/layouts/AdminLayout.astro +41 -2
- package/admin/lib/collaboration.ts +91 -0
- package/admin/views/EditView.astro +1 -1
- package/admin/views/NeedsYouView.astro +100 -0
- package/admin/views/edit/EditScripts.astro +90 -15
- package/core/admin.ts +2 -0
- package/core/api.ts +31 -2
- package/core/collaboration.ts +73 -0
- package/core/generator.ts +19 -2
- package/core/index.ts +8 -2
- package/middleware/auth.ts +7 -1
- package/package.json +1 -1
- package/routes/admin/[...path].astro +19 -6
- package/routes/admin/setup.astro +3 -2
- package/routes/api/[collection]/[...path].ts +12 -5
- package/routes/api/auth/setup.ts +7 -5
- package/routes/api/collaboration/index.ts +15 -0
- package/admin/components/Untitled +0 -1
|
@@ -3,7 +3,7 @@ import type { APIRoute } from "astro";
|
|
|
3
3
|
import config from "virtual:kide/config";
|
|
4
4
|
import { cms } from "virtual:kide/api";
|
|
5
5
|
import { collaboration } from "virtual:kide/runtime";
|
|
6
|
-
import { formatPagePath, getPageHref, isApprover, resolveCollaboration } from "../../../core";
|
|
6
|
+
import { FieldError, formatPagePath, getPageHref, isApprover, resolveCollaboration } from "../../../core";
|
|
7
7
|
import { loadSharedSectionUsageCounts } from "../../../admin/lib/edit-data";
|
|
8
8
|
|
|
9
9
|
export const prerender = false;
|
|
@@ -32,15 +32,19 @@ const isFormRequest = (request: Request) => {
|
|
|
32
32
|
};
|
|
33
33
|
|
|
34
34
|
// Append _toast and _msg params so the layout can render a server-side toast
|
|
35
|
-
const redirect = (location: string, toast?: { status: "success" | "error"; msg: string }) => {
|
|
35
|
+
const redirect = (location: string, toast?: { status: "success" | "error"; msg: string; field?: string }) => {
|
|
36
36
|
let target = location;
|
|
37
37
|
if (toast) {
|
|
38
38
|
const sep = target.includes("?") ? "&" : "?";
|
|
39
39
|
target += `${sep}_toast=${toast.status}&_msg=${encodeURIComponent(toast.msg)}`;
|
|
40
|
+
// The admin uses this to focus and mark the offending input.
|
|
41
|
+
if (toast.field) target += `&_field=${encodeURIComponent(toast.field)}`;
|
|
40
42
|
}
|
|
41
43
|
return new Response(null, { status: 303, headers: { Location: target } });
|
|
42
44
|
};
|
|
43
45
|
|
|
46
|
+
const camelCaseColumn = (column: string) => column.replace(/_([a-z0-9])/g, (_, c: string) => c.toUpperCase());
|
|
47
|
+
|
|
44
48
|
const stripToastParams = (url: string) => {
|
|
45
49
|
const [path, query] = url.split("?");
|
|
46
50
|
if (!query) return url;
|
|
@@ -206,15 +210,18 @@ const handleHtmlMutation = async (
|
|
|
206
210
|
return redirect(redirectTo);
|
|
207
211
|
} catch (error) {
|
|
208
212
|
let msg = error instanceof Error ? error.message : `Failed to ${action}`;
|
|
213
|
+
// Structured where we raise it ourselves; parsed only where SQLite is the source.
|
|
214
|
+
let field = error instanceof FieldError ? error.field : undefined;
|
|
209
215
|
if (msg.toLowerCase().includes("unique constraint failed")) {
|
|
210
216
|
const match = msg.match(/unique constraint failed:\s*\S+\.(\w+)/i);
|
|
211
|
-
const
|
|
212
|
-
|
|
217
|
+
const column = match ? match[1] : null;
|
|
218
|
+
field = column ? camelCaseColumn(column) : undefined;
|
|
219
|
+
msg = `A document with this ${column ?? "field"} already exists`;
|
|
213
220
|
} else if (msg.startsWith("Failed query:")) {
|
|
214
221
|
msg = `Failed to ${action} ${collection.labels.singular.toLowerCase()}`;
|
|
215
222
|
}
|
|
216
223
|
const fallback = documentId ? redirectTo : redirectTo || `/admin/${collectionSlug}/new`;
|
|
217
|
-
return redirect(fallback, { status: "error", msg });
|
|
224
|
+
return redirect(fallback, { status: "error", msg, field });
|
|
218
225
|
}
|
|
219
226
|
};
|
|
220
227
|
|
package/routes/api/auth/setup.ts
CHANGED
|
@@ -60,8 +60,10 @@ export const POST: APIRoute = async ({ request, clientAddress }) => {
|
|
|
60
60
|
return Response.json({ error: "Users collection not configured." }, { status: 500 });
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
// Prevent setup if
|
|
64
|
-
|
|
63
|
+
// Prevent setup if an admin already exists (fast path / friendly redirect).
|
|
64
|
+
// Deliberately admin-scoped: seeded demo editors, or an invite that created an
|
|
65
|
+
// editor first, must not strand a project with no way to make its first admin.
|
|
66
|
+
const existing = await db.select().from(tables.users.main).where(eq(tables.users.main.role, "admin")).limit(1);
|
|
65
67
|
if (existing.length > 0) {
|
|
66
68
|
return new Response(null, {
|
|
67
69
|
status: 303,
|
|
@@ -74,12 +76,12 @@ export const POST: APIRoute = async ({ request, clientAddress }) => {
|
|
|
74
76
|
const hashedPassword = await hashPassword(password);
|
|
75
77
|
|
|
76
78
|
// Atomic winner election that leaves no orphan state: a single conditional insert creates
|
|
77
|
-
// the first admin only if
|
|
78
|
-
//
|
|
79
|
+
// the first admin only if none exists yet. Self-recovering — if anything fails there's no
|
|
80
|
+
// marker to strand setup, and a retry just runs the same guarded insert.
|
|
79
81
|
await db.run(sql`
|
|
80
82
|
INSERT INTO cms_users (_id, name, email, role, password, _created_at, _updated_at)
|
|
81
83
|
SELECT ${id}, ${name}, ${email}, 'admin', ${hashedPassword}, ${now}, ${now}
|
|
82
|
-
WHERE NOT EXISTS (SELECT 1 FROM cms_users)
|
|
84
|
+
WHERE NOT EXISTS (SELECT 1 FROM cms_users WHERE role = 'admin')
|
|
83
85
|
`);
|
|
84
86
|
|
|
85
87
|
// Confirm we won the race (our row exists). If not, another concurrent setup created the
|
|
@@ -45,6 +45,15 @@ export const GET: APIRoute = async ({ url }) => {
|
|
|
45
45
|
return Response.json({ state, comments, activity });
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
+
// Editing is author-only, deliberately narrower than resolve/delete: removing a
|
|
49
|
+
// comment is moderation, but rewriting the words under someone else's name and
|
|
50
|
+
// avatar is not something an approver should be able to do.
|
|
51
|
+
const authorizeCommentEdit = async (commentId: string, actor: { id: string }) => {
|
|
52
|
+
const comment = await collaboration.getComment(commentId);
|
|
53
|
+
if (!comment || !isEnabled(comment.collection)) return null;
|
|
54
|
+
return comment.authorId === actor.id ? comment : null;
|
|
55
|
+
};
|
|
56
|
+
|
|
48
57
|
// POST /api/cms/collaboration { collection, id, action, ...payload }
|
|
49
58
|
export const POST: APIRoute = async ({ request, locals }) => {
|
|
50
59
|
const actor = getActor(locals);
|
|
@@ -85,6 +94,12 @@ export const POST: APIRoute = async ({ request, locals }) => {
|
|
|
85
94
|
);
|
|
86
95
|
return Response.json({ comment });
|
|
87
96
|
}
|
|
97
|
+
case "updateComment": {
|
|
98
|
+
const comment = await authorizeCommentEdit(String((body as any).commentId), actor);
|
|
99
|
+
if (!comment) return forbidden();
|
|
100
|
+
const updated = await collaboration.updateComment(comment._id, String((body as any).body ?? ""), actor);
|
|
101
|
+
return Response.json({ comment: updated });
|
|
102
|
+
}
|
|
88
103
|
case "resolveComment": {
|
|
89
104
|
const comment = await authorizeComment(String((body as any).commentId), actor);
|
|
90
105
|
if (!comment) return forbidden();
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
deleting
|