docs(examples): add the CMS proof-of-concept dogfooding artifact
A WordPress-inspired CMS whose entire backend is PiCloud Rhai scripts deployed with pic apply, plus a SvelteKit frontend. Built to dogfood the project tool, CLI, and SDK; exercises docs/kv/files/users, docs+queue+cron +pubsub triggers, the transactional-outbox notification chain, a docs before-interceptor, set_if CAS, SSE, per-app CORS, env overlays, and a durable Workflow (validate -> enrich || seo -> publish -> finalize) started with workflow::start and polled with workflow::run_status, visualized live with Svelte Flow. FINDINGS.md / SECURITY.md capture every gap, surprise, and security issue found (each tagged [PiCloud] vs [CMS]); the platform fixes for the actionable ones ship in the preceding commit. Also folds in the read-only `pic` allowlist entries added to .claude/settings.json during the session (fewer-permission-prompts). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
39
examples/cms-poc/scripts/posts/post_create.rhai
Normal file
39
examples/cms-poc/scripts/posts/post_create.rhai
Normal file
@@ -0,0 +1,39 @@
|
||||
// POST /cms/admin/posts — create a post (author+).
|
||||
import "auth" as auth;
|
||||
import "util" as util;
|
||||
let g = auth::guard(ctx, ["admin", "author"]);
|
||||
if !g.ok { return g.response; }
|
||||
let user = g.user;
|
||||
|
||||
let b = ctx.request.body;
|
||||
if b == () || b.title == () {
|
||||
return #{ statusCode: 400, body: #{ error: "title required" } };
|
||||
}
|
||||
|
||||
let posts = docs::collection("posts");
|
||||
let idx = kv::collection("slugs");
|
||||
let slug = if b.slug == () { util::slugify(b.title) } else { util::slugify(b.slug) };
|
||||
if idx.has("post:" + slug) {
|
||||
slug = slug + "-" + random::string(4).to_lower();
|
||||
}
|
||||
|
||||
let now = time::now();
|
||||
let status = if b.status == () { "draft" } else { b.status };
|
||||
let publish_at = ();
|
||||
if status == "published" { publish_at = now; }
|
||||
else if status == "scheduled" && b.publish_at != () { publish_at = b.publish_at; }
|
||||
|
||||
let data = #{
|
||||
title: b.title,
|
||||
slug: slug,
|
||||
body_md: if b.body_md == () { "" } else { b.body_md },
|
||||
excerpt: if b.excerpt == () { "" } else { b.excerpt },
|
||||
status: status,
|
||||
author_id: user.id,
|
||||
author_name: user.display_name,
|
||||
tags: if b.tags == () { [] } else { b.tags },
|
||||
publish_at: publish_at,
|
||||
};
|
||||
let id = posts.create(data);
|
||||
idx.set("post:" + slug, id);
|
||||
#{ statusCode: 201, body: #{ id: id, slug: slug, status: status } }
|
||||
13
examples/cms-poc/scripts/posts/post_delete.rhai
Normal file
13
examples/cms-poc/scripts/posts/post_delete.rhai
Normal file
@@ -0,0 +1,13 @@
|
||||
// DELETE /cms/admin/posts/:id — delete a post (admin only).
|
||||
import "auth" as auth;
|
||||
let g = auth::guard(ctx, ["admin"]);
|
||||
if !g.ok { return g.response; }
|
||||
|
||||
let id = ctx.request.params.id;
|
||||
let posts = docs::collection("posts");
|
||||
let doc = posts.get(id);
|
||||
if doc == () { return #{ statusCode: 404, body: #{ error: "not found" } }; }
|
||||
let slug = doc.data.slug;
|
||||
posts.delete(id);
|
||||
kv::collection("slugs").delete("post:" + slug);
|
||||
#{ statusCode: 200, body: #{ ok: true } }
|
||||
24
examples/cms-poc/scripts/posts/post_get.rhai
Normal file
24
examples/cms-poc/scripts/posts/post_get.rhai
Normal file
@@ -0,0 +1,24 @@
|
||||
// GET /cms/posts/:slug — public single published post (by slug).
|
||||
// Resolves slug -> id via the kv slug index, increments a CAS view counter.
|
||||
import "util" as util;
|
||||
let slug = ctx.request.params.slug;
|
||||
if slug == () { return #{ statusCode: 400, body: #{ error: "missing slug" } }; }
|
||||
|
||||
let idx = kv::collection("slugs");
|
||||
let id = idx.get("post:" + slug);
|
||||
if id == () { return #{ statusCode: 404, body: #{ error: "not found" } }; }
|
||||
|
||||
let posts = docs::collection("posts");
|
||||
let doc = posts.get(id);
|
||||
if doc == () { return #{ statusCode: 404, body: #{ error: "not found" } }; }
|
||||
|
||||
let p = util::shape_doc(doc);
|
||||
// A public reader may only see PUBLISHED posts — never a draft/scheduled one,
|
||||
// even if they guess the slug. (No platform per-row authz; enforced here.)
|
||||
if p.status != "published" {
|
||||
return #{ statusCode: 404, body: #{ error: "not found" } };
|
||||
}
|
||||
|
||||
let views = util::incr("views", "post:" + id);
|
||||
p.views = views;
|
||||
#{ statusCode: 200, body: p }
|
||||
44
examples/cms-poc/scripts/posts/post_update.rhai
Normal file
44
examples/cms-poc/scripts/posts/post_update.rhai
Normal file
@@ -0,0 +1,44 @@
|
||||
// PUT /cms/admin/posts/:id — update a post (author+ ; own-only unless admin).
|
||||
import "auth" as auth;
|
||||
import "util" as util;
|
||||
let g = auth::guard(ctx, ["admin", "author"]);
|
||||
if !g.ok { return g.response; }
|
||||
let user = g.user;
|
||||
|
||||
let id = ctx.request.params.id;
|
||||
let posts = docs::collection("posts");
|
||||
let doc = posts.get(id);
|
||||
if doc == () { return #{ statusCode: 404, body: #{ error: "not found" } }; }
|
||||
let cur = doc.data;
|
||||
|
||||
// IDOR guard: an author may edit only their OWN post; an admin edits any.
|
||||
let is_admin = users::has_role(user.id, "admin");
|
||||
if !is_admin && cur.author_id != user.id {
|
||||
return #{ statusCode: 403, body: #{ error: "not your post" } };
|
||||
}
|
||||
|
||||
let b = ctx.request.body;
|
||||
if b.title != () { cur.title = b.title; }
|
||||
if b.body_md != () { cur.body_md = b.body_md; }
|
||||
if b.excerpt != () { cur.excerpt = b.excerpt; }
|
||||
if b.tags != () { cur.tags = b.tags; }
|
||||
|
||||
let idx = kv::collection("slugs");
|
||||
if b.slug != () {
|
||||
let newslug = util::slugify(b.slug);
|
||||
if newslug != cur.slug {
|
||||
idx.delete("post:" + cur.slug);
|
||||
if idx.has("post:" + newslug) { newslug = newslug + "-" + random::string(4).to_lower(); }
|
||||
idx.set("post:" + newslug, id);
|
||||
cur.slug = newslug;
|
||||
}
|
||||
}
|
||||
|
||||
if b.status != () && b.status != cur.status {
|
||||
cur.status = b.status;
|
||||
if b.status == "published" && cur.publish_at == () { cur.publish_at = time::now(); }
|
||||
if b.status == "scheduled" && b.publish_at != () { cur.publish_at = b.publish_at; }
|
||||
}
|
||||
|
||||
posts.update(id, cur);
|
||||
#{ statusCode: 200, body: #{ id: id, slug: cur.slug, status: cur.status } }
|
||||
21
examples/cms-poc/scripts/posts/posts_admin_list.rhai
Normal file
21
examples/cms-poc/scripts/posts/posts_admin_list.rhai
Normal file
@@ -0,0 +1,21 @@
|
||||
// GET /cms/admin/posts — list posts for the dashboard (author+).
|
||||
// Admin sees all; an author sees only their own.
|
||||
import "auth" as auth;
|
||||
import "util" as util;
|
||||
let g = auth::guard(ctx, ["admin", "author"]);
|
||||
if !g.ok { return g.response; }
|
||||
let user = g.user;
|
||||
|
||||
let posts = docs::collection("posts");
|
||||
let is_admin = users::has_role(user.id, "admin");
|
||||
let filter = #{ "$sort": #{ updated_at: -1 }, "$limit": 100 };
|
||||
if !is_admin { filter.author_id = user.id; }
|
||||
|
||||
let rows = posts.find(filter);
|
||||
let out = [];
|
||||
for r in rows {
|
||||
let p = util::shape_doc(r);
|
||||
p.remove("body_md");
|
||||
out.push(p);
|
||||
}
|
||||
#{ statusCode: 200, body: #{ posts: out } }
|
||||
23
examples/cms-poc/scripts/posts/posts_list.rhai
Normal file
23
examples/cms-poc/scripts/posts/posts_list.rhai
Normal file
@@ -0,0 +1,23 @@
|
||||
// GET /cms/posts — public list of PUBLISHED posts, newest first.
|
||||
// Optional ?tag=<slug> filters by tag. Since the platform fix (F-015), the docs
|
||||
// filter DSL supports `$contains` (JSONB @>) for array-membership, so the tag
|
||||
// filter now runs IN THE DATABASE with real pagination — no over-fetch/in-script
|
||||
// scan needed.
|
||||
import "util" as util;
|
||||
let posts = docs::collection("posts");
|
||||
let per = util::to_int_or(vars::get("posts-per-page"), 10);
|
||||
let tag = ctx.request.query["tag"];
|
||||
|
||||
let filter = #{ status: "published", "$sort": #{ publish_at: -1 }, "$limit": per };
|
||||
if tag != () {
|
||||
filter.tags = #{ "$contains": tag };
|
||||
}
|
||||
|
||||
let rows = posts.find(filter);
|
||||
let out = [];
|
||||
for r in rows {
|
||||
let p = util::shape_doc(r);
|
||||
p.remove("body_md"); // list view: excerpt only
|
||||
out.push(p);
|
||||
}
|
||||
#{ statusCode: 200, body: #{ posts: out, tag: tag } }
|
||||
33
examples/cms-poc/scripts/posts/posts_on_publish.rhai
Normal file
33
examples/cms-poc/scripts/posts/posts_on_publish.rhai
Normal file
@@ -0,0 +1,33 @@
|
||||
// posts_on_publish — docs TRIGGER on the `posts` collection (create|update).
|
||||
// When a post transitions INTO "published", enqueue batched reader-notification
|
||||
// jobs onto the `post-notifications` queue (decoupling the burst from delivery,
|
||||
// and staying well under the per-execution emission ceiling via chunking).
|
||||
let ev = ctx.event.docs;
|
||||
if ev == () { return; }
|
||||
let data = ev.data;
|
||||
if data == () { return; }
|
||||
|
||||
let was = if ev.prev_data == () { () } else { ev.prev_data.status };
|
||||
let became_published = data.status == "published" && was != "published";
|
||||
if !became_published { return; }
|
||||
|
||||
// collect reader emails in chunks of 50
|
||||
let res = users::list(#{ "$limit": 500 });
|
||||
let batch = [];
|
||||
let total = 0;
|
||||
for u in res.users {
|
||||
if users::has_role(u.id, "reader") {
|
||||
batch.push(u.email);
|
||||
total += 1;
|
||||
if batch.len() >= 50 {
|
||||
queue::enqueue("post-notifications",
|
||||
#{ post_id: ev.id, title: data.title, slug: data.slug, emails: batch });
|
||||
batch = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
if batch.len() > 0 {
|
||||
queue::enqueue("post-notifications",
|
||||
#{ post_id: ev.id, title: data.title, slug: data.slug, emails: batch });
|
||||
}
|
||||
log::info("queued post notifications", #{ post: ev.id, title: data.title, readers: total });
|
||||
18
examples/cms-poc/scripts/posts/posts_scheduled_publish.rhai
Normal file
18
examples/cms-poc/scripts/posts/posts_scheduled_publish.rhai
Normal file
@@ -0,0 +1,18 @@
|
||||
// posts_scheduled_publish — cron TRIGGER (every minute). Flips due `scheduled`
|
||||
// posts to `published`, which itself fires posts_on_publish -> notifications
|
||||
// (a bounded trigger-fires-trigger chain).
|
||||
let posts = docs::collection("posts");
|
||||
let now = time::now();
|
||||
let due = posts.find(#{
|
||||
status: "scheduled",
|
||||
publish_at: #{ "$lte": now },
|
||||
"$limit": 100,
|
||||
});
|
||||
let n = 0;
|
||||
for r in due {
|
||||
let cur = r.data;
|
||||
cur.status = "published";
|
||||
posts.update(r.id, cur);
|
||||
n += 1;
|
||||
}
|
||||
if n > 0 { log::info("cron published scheduled posts", #{ count: n }); }
|
||||
Reference in New Issue
Block a user