Repository navigation
feat(content): send Content Editor pages to the brand's webhook as content packages, and take back where they went live (Send to CMS #1331, part 1: server) - #1437
Conversation
…ntent packages, and take back where they went live (Send to CMS ansvisor#1331, part 1: server) Co-Authored-By: Claude Opus 5.5 <[email protected]>
…svisor#1331, part 1) guard_content_draft_server_columns (00247) now refuses a member's write of content_drafts.published_url and published_at, as of the other columns the server keeps: only the webhook callback (service role) sets where the page went live. Slug and meta stay editable. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Follow-up commit
Checked on the dev DB inside a rolled-back transaction, as the demo user with the
The web part is #1438. It should be deployed after this PR and after 00262 is applied on prod; the details are in its description. |
gkhngyk
left a comment
There was a problem hiding this comment.
A few fixes before merge:
-
Bulk sends have no pause between pages. The agreed decision on #1331 is "delivered one at a time in a background job with a short pause", to stay under the CMSs' per-minute caps.
deliverBatch(server/src/lib/content-delivery.js:273-295) sends the next page as soon as the last one answers, so a fast catch hook gets all 25 within a few seconds. Add a short wait between pages (for example aPAUSE_MSof 2 s, skipped after the last page) and cover it in the batch test with fake timers. -
The package and callback are narrower than #1331's phase 1 plan.
schemaVersion: 1is what workspaces will build their automations on, so either add these now or get the narrower v1 agreed on #1331 first:contentPackage(content-delivery.js:142-167) leaves outmethod,sourceUrl,pageType,language,jsonLd[]andbrand.id.methodandsourceUrlmatter most: without them, a workflow can't tell an optimized page from a new one, so it creates a second post instead of updating the live one. The server already reads the draft, somethod,source_urlandpage_typecan come from the row, not the browser.jsonLd[]can be read out of the cleaned HTML, since Webflow-style rich-text fields drop<script>.- The callback (
server/src/routes/webhook-content-draft-callback.js:28-38) only takes{ url }. The plan has{ status: "published" | "failed", externalId?, url? }. Zapier and Make answer 2xx before the CMS step runs, so a post the CMS rejects stays "sent" forever unless the workflow can reportfailed.external_idisn't stored on the delivery either (00262:35-59). - When the page moves to Published, the plan also keeps a
publishversion in History. 00215 reserves that origin for the server (content_draft_snapshot(p_draft_id, 'publish')asservice_role), and the callback doesn't call it.
-
Low: the callback checks the URL's length before normalizing it (
webhook-content-draft-callback.js:29,:71).new URL(...).hrefpercent-encodes non-ASCII, so a URL under 2,048 characters can come out over it. The column check then fails and the caller gets a 500 instead of a 400. Checkurl.length <= 2048after normalizing. -
Low: each send's sweep of rows still queued after 15 minutes (
00262:112-116) walks the organization's whole delivery history throughcontent_draft_deliveries_org_created, and the history grows by up to 200 rows a day. Since 00262 isn't applied yet, add a partial index:on public.content_draft_deliveries (organization_id) where status = 'queued'. -
Follow-up, separate PR: the other webhook senders (
dispatchBrandWebhook, the opportunity send and the webhook test inroutes/content.js) still post with plainfetch. Moving them ontopostJsongives them the same address, redirect, timeout and size guards.
Checked and fine:
- Callback auth. It only accepts the secret of an active webhook on the delivery's own brand that takes
content_draft.sent, compared timing-safe. Every failure gets the same 401, and it uses the existing rate limiters. - Send route. The 25-page and 200-a-day caps hold. The advisory lock, interrupt sweep and partial reservation behave as described: I ran a
pg_tempcopy in a rolled-back transaction. - RLS and grants on
content_draft_deliveries. Members can only read their own organization's rows, and onlyservice_rolecan executereserve_content_deliveries. guard_content_draft_server_columns. It matches prod's current body (00247) line for line, plus the two new columns on insert and update.- 00262. It applies cleanly on top of prod.
safe-post. On Cloud it refuses private addresses, both IP literals and resolved names. It follows no redirects, stops at 15 s and reads at most 8 KB. The new IPv6 ranges work.
…ck with the CMS's id
- a 2 s pause between a batch's pages, none before the first
- the callback is /webhooks/content-drafts/:deliveryId/status with
{ status: "published" | "failed", url?, externalId?, reason? }: unmapped
fields ("", null) read as left out, a numeric post id as text, an address
checked after the browser reads it, without sign-in details
- published keeps the page in History as a "publish" version; failed marks
the delivery failed unless it was published; a callback that lands before
the webhook answers keeps its word
- the package names what the draft is from its row: method, sourceUrl,
pageType, language (its Language rule, My rules for an untouched draft,
else the brand's) and jsonLd; the brand goes with its id
- 00262: external_id, a partial index on queued sends, and
content_draft_last_deliveries(uuid[]) for the Library's marks
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks. All items are in 13aee06.
One note on the 15-minute "interrupted" rule. A batch of 25 pages with three subscribed webhooks that all time out could run past 15 minutes, and a send in that window would close its later pages as interrupted. With one webhook, the worst case is about 7 minutes. I left it as it is. Say if you'd rather have a longer window. Server checks: format, lint and all 2,359 tests pass. The docs in #1441 are updated to match. |
…ansvisor#1437 review) - the callback is /status with { status, url?, externalId?, reason? } - the package's jsonLd, method, sourceUrl, pageType, language and brand.id - links to http, https, mailto, tel or relative addresses; the callback shares the opportunity callback's limits; a 2 s pause between pages Co-Authored-By: Claude Opus 5.5 <[email protected]>
reserve_content_deliveries closed a send left queued with sent_at = now(), so the Library dated it to the workspace's next send. It leaves sent_at empty now: the send reads at its created_at. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
One more small change in 00262 (5109eef), from the #1439/#1440 follow-ups. |
gkhngyk
left a comment
There was a problem hiding this comment.
One fix before merge. Everything else from the last review is done.
Last review:
- Pause between pages: done (
PAUSE_MS, with fake-timer tests). - Package and callback: done.
method,sourceUrl,pageType,languageandbrand.idcome from the database rows, andjsonLdcomes from the cleaned HTML.- The callback takes
status,url,externalIdandreason, storesexternal_id, and keeps apublishversion.
- URL length checked after normalizing: done.
- Partial index for the queued sweep: done.
- Other webhook senders moved onto
postJson: still a follow-up for a separate PR.
New:
- A re-sent page that goes live at the same address isn't kept in History.
- The "said again" check compares against the draft (
server/src/routes/webhook-content-draft-callback.js:162), not the delivery. - Here is what happens: a published page is edited and sent again, and the CMS updates the post in place. That is the update-the-post flow the package now points workflows to with
methodandsourceUrl. The new delivery's callback finds the draft alreadypublishedat that URL, so it skips both the draft write andcontent_draft_snapshot. - As a result, History keeps the first publish's text as the published version, and the page's
published_atstays at the first publish. The docs in #1441 say the page as published is kept in History. - Fix: make the repeat check per delivery.
- Read
published_urlalong with the delivery (line 93 already readsstatus). - Skip the writes only when this same delivery is already
publishedat the sameurl. - Otherwise, update the draft and call the snapshot.
content_draft_keep_versionalready skips a version that's identical to the latest one. - Skipping the delivery's own update in the repeat case also stops a retry from moving its
published_at.
- Read
- Test: send a published page a second time, change the page, call back published at the same address, and check that a second
publishversion is kept.
- The "said again" check compares against the draft (
On the 15-minute "interrupted" window: 30 minutes would cover a batch where three webhooks time out on every page (about 20 minutes). A server that really stopped would still be closed by the next send. Since 00262 isn't applied yet, the change costs nothing. This is optional; 15 minutes is fine if you prefer it.
Checked and fine:
- 00262 on prod. It applies cleanly (I ran it in a transaction and rolled it back). Inside that rolled-back transaction, signed in as members of two different workspaces:
content_draft_last_deliveriesreturns only the caller's own workspace's rows, one per page (the latest). Another workspace's row for the same page id doesn't replace the caller's own.anoncan't execute it.- Members can't insert deliveries or call
reserve_content_deliveries. - The sweep closes a queued row older than 15 minutes as interrupted, and leaves
sent_atempty. - A member's write of
published_urlis refused, and a slug write goes through.
external_id. Its check matches what the callback accepts: up to 200 characters, with a number stored as text.- Callback.
- Authentication is unchanged.
- Races between
publishedandfailedend as published in either order: thefailedwrite is conditional, thepublishedwrite isn't. - The snapshot call as
service_rolepassescontent_draft_snapshot's membership and origin checks.
deliver(). The.eq('status', 'queued')write leaves a callback's earlier result in place and returns it.- Package fields.
deliveryPagedrops anything else the browser sends. The server sets:method,sourceUrlandpageTypefrom the draft row;languagefrom the draft's rules, the workspace's My rules, or the brand;brand.idfrom the brand row;jsonLdfrom the HTML after it's cleaned.
|
One more low item, on your 15-minute question: a batch can still be going after 15 minutes.
|
…h 30 minutes before it reads as interrupted - the callback's "said again" check is the delivery's own: a new send of a page live at the same address (its post updated in place) moves the draft's published_at and keeps a "publish" version; the same delivery said twice changes nothing, its time included - 00262: a send left queued closes as interrupted after 30 minutes, not 15: a batch with webhooks timing out can run about 20 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…redoes a page write that failed The delivery row is marked published only once the draft and its "publish" version are written: a write that fails answers 500, and the workflow's retry, finding the delivery not yet published, does it all again. A retry of one already said keeps a CMS id it adds. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks. Both items are done in 429d829 and cffb209.
Checks: The other webhook senders moving onto |
gkhngyk
left a comment
There was a problem hiding this comment.
All items done. Looks good.
Last review:
- Re-sent page kept in History: done.
- The repeat check is per delivery now (
server/src/routes/webhook-content-draft-callback.js:144). Writes are skipped only when this same delivery is alreadypublishedat the sameurl, and then only anexternalIdthe retry adds is stored. - A new delivery that goes live at the same address updates the draft, moves its
published_atand callscontent_draft_snapshot. The new test covers it.
- The repeat check is per delivery now (
- 30-minute window: done.
- Moving the other webhook senders onto
postJsonstays a follow-up for a separate PR, as agreed.
New commits, checked and fine:
- Delivery written last (line 182).
- A draft write that fails answers 500 and leaves the delivery unpublished, so the workflow's retry does it all again. The retry test covers it.
- The snapshot stays best-effort (logged, not retried).
keep_versionalready skips a repeat, so a retry doesn't add a second version. - A
failedcallback that lands between the draft write and the delivery write is overwritten by thepublishedwrite, so published still wins in either order. deliver()'s.eq('status', 'queued')write still leaves the callback's result in place.
- 00262 on prod. The final version applies cleanly. I ran it in a transaction and rolled it back. Inside that transaction:
- The sweep closes a row queued 31 minutes ago as interrupted, with
sent_atleft empty. It leaves a row queued 20 minutes ago alone. - Signed in as members of two different workspaces,
content_draft_last_deliveriesreturned each caller only their own workspace's latest row for the same page id. - A member's write of
published_urlis refused, and a slug write goes through. A member can't insert a delivery. anoncan't execute the read function, andauthenticatedcan't executereserve_content_deliveries.- As
service_role, the callback's draft write andcontent_draft_snapshot(..., 'publish')both go through.
- The sweep closes a row queued 31 minutes ago as interrupted, with
…k (Send to CMS #1331, part 4) (#1441) * docs(content): document Send to CMS and the content_draft.sent webhook (Send to CMS #1331, part 4) - a Send to CMS guide: setting up the webhook, sending one page or up to 25 from the Library, the row icons, and building the workflow in n8n, Make or Zapier with the callback that reports the live address - the Webhooks reference: the content_draft.sent event (opt-in), its package, the X-Ansvisor-Signature HMAC, the published callback and the delivery rules (2xx, 15 s, no redirects, 25 a send, 200 a day) - the Content Editor guide points to it from the editor and the Library Co-Authored-By: Claude Opus 5.5 <[email protected]> * docs(content): match Send to CMS's status callback and package fields (#1437 review) - the callback is /status with { status, url?, externalId?, reason? } - the package's jsonLd, method, sourceUrl, pageType, language and brand.id - links to http, https, mailto, tel or relative addresses; the callback shares the opportunity callback's limits; a 2 s pause between pages Co-Authored-By: Claude Opus 5.5 <[email protected]> * docs(content): say a bulk send's later requests stop when its dialog closes (#1440) Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Cihan <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
…does (#1437 review) (#1568) Co-authored-by: Cihan <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
Summary
Send to CMS, phase 1 (webhook), part 1: the server (#1331). Admins and managers send Content Editor pages to the brand's webhook as content packages. The workspace's own automation (Zapier, Make, n8n, its CMS) turns them into posts and may call back with the address each went live at.
It follows the decisions on #1331:
The web parts (the editor's Send dialog, the Library's row and bulk send, the webhook's new event in Configure Webhook) come next.
Migration
00262_content_draft_deliveriescontent_draftsgetsslug,meta_title,meta_description,published_urlandpublished_at.content_draft_deliverieshas one row per page sent:queued→sent/failed→published);updated_at(as 00249).reserve_content_deliveriesqueues as many pages as the last 24 hours leave room for. It runs under a per-organization advisory lock, so sends at once can't pass the cap together (as 00243). It isSECURITY DEFINERand granted toservice_roleonly.published_urlon both tables takes http(s) only.webhook_configs_events_allowedaddscontent_draft.sent.supabase/schema.sqlis rebuilt.Sending (
lib/content-delivery.js)schemaVersion: 1:event: 'content_draft.sent';delivery { id, batchId, sentAt };page { draftId, title, slug, metaTitle, metaDescription, html, author, featuredImage };brand { name, domain, industry }, as the opportunity webhook sends it;callback_url,callback_method.on*handlers;data:image/…for an image'ssrc.java	script:is dropped.X-Webhook-Secretas the other webhooks send it, andX-Ansvisor-Signature: sha256=<HMAC of the raw body with the webhook's secret>, so a receiver can check the body.delivery.sentAtis inside the signed body, for a receiver that rejects old ones.callback_url, since the secret is what authenticates the callback.fieldthat is wrong (e.g.pages.2.slug). Slugs take letters, digits,-,_and single dots.no_webhook/webhook_not_subscribed(409),not_found(404),daily_limit(429).SSRF-safe POST (
lib/safe-post.js) works assafe-fetch.jsreads pages:http://n8n:5678/…on the workspace's own network), as the other webhooks reach it with plainfetch. Only the scheme and credentials are checked;Routes
POST /api/content-drafts/sendtakes{ brandId, pages: [...] }and checks the brand's access, then the member's role (admin or manager).POST /webhooks/content-drafts/:deliveryId/publishedtakes{ url }:/webhooks/content's rate limiters;content.jsnotes for the opportunity callback). The send's own result then doesn't overwritepublished;Deploy order: the migration, then the server. The web parts follow. Applied and checked on the dev DB; the reservation was tried inside a rolled-back transaction (a cap of 2 took 2 of 3 pages, then 0).
Also in
safe-fetch.js: the IPv4-compatible (::/96) and IPv4-translated (::ffff:0:0:0/96) IPv6 ranges are blocked, so[::127.0.0.1]no longer passes. Page reads (Optimize by URL) get this too.Pre-push review: one high finding, fixed: a callback that came before the webhook's answer was refused (409) and its URL lost. Also fixed:
animatepassing the second clean;published_urlhttp(s) only;@gkhngyk: please confirm self-hosted may post to private addresses, as its other webhooks already do. On Cloud it never does.
Related issue
Part of #1331 (phase 1). Doesn't close it.
Type of change
How to test
00262, then point a brand's webhook (Configure Webhook) at a request bin with thecontent_draft.sentevent. Until the web part lands, set the event on the row inwebhook_configs.POST /api/content-drafts/sendwith{ brandId, pages: [{ draftId, title, slug, html }] }:sent;webhook_not_subscribed.batchId, and the bin gets them one after another.POST /webhooks/content-drafts/<deliveryId>/publishedwithX-Webhook-Secretand{ "url": "https://…" }: the delivery and the page read published. With a wrong secret: 401.Checklist
server/:npm run format,npm run lint,npm test(194 files, 2,350 tests) passbash supabase/build-schema.sh:schema.sqlis in this PRsafe-post.test.js:checkWebhookUrl's refusals.content-delivery.test.js:content-drafts-send.test.js: the route, admins and managers only, its 400s and its codes.webhook-content-draft-callback.test.js:npm run version:checkfails onmainalready (compose images 0.2.0 vs 0.3.0); not touched here🤖 Generated with Claude Code