feat(microblog): set and create categories
A piece's `category:` frontmatter line now travels to Micro.blog: a first send carries the list beside title and content, and a re-send replaces the post's categories with exactly the piece's list — only when the piece has the line (opt-in by key; `[]` clears; no line leaves the blog's). Naming a category Micro.blog doesn't know creates it there, so there is no separate create step. The publish sheet gains a Categories field: a comma list with the system completion menu over the blog's own categories. The system replaces the whole field text with a completion, so the completion is the merge — a pick adds, a half-typed last word is replaced. The edit lands on the piece at the send (a failed write refuses with nothing on the wire); Publish with a pending edit applies it first and flips the post that send touched. The one-line codec lives in PostFrontmatter (line-bounded; backslashes and quotes escaped; a Set de-duplicates). Import keeps an imported `category:` line — the writer's classification, unlike the micropub bookkeeping keys. Reviewed twice (two-axis during the build, finish-audit after): the stale pending baseline after a failed step, the backslash round-trip, and the publish targeting the caller's possibly-stale URL were found and fixed before this commit. 39 of the 40 new or changed pins were watched fail on their own guard. Rail 791/791; SwiftFormat and SwiftLint clean. Docs: AGENTS.md bullets and learnings, the GLOSSARY term, CHANGELOG [Unreleased], PRD S6.1, the devlog, and the swift wiki (text completion is a whole-text contract). Closes #207
This commit is contained in:
1 parent
6aa31a419a
commit
6db4cf8cf1
18 files changed
+1629
-67
No files matched your search
+147
-42
@@ -6,15 +6,19 @@
|
||||
// Two-step publish (G2/G11): create with `post-status: draft`, review,
|
||||
// then `action=update` flips the draft to published. Bearer app-token
|
||||
// auth — no OAuth in v1; the token lives in the Keychain, entered
|
||||
// once. Frontmatter `category:` is permitted but inert (v1 never
|
||||
// sends it — the request carries name + content only).
|
||||
// once. #207: the piece's `category:` list TRAVELS — a create carries
|
||||
// it when non-empty (naming a category Micro.blog doesn't know
|
||||
// creates it there), a re-send carries it in `replace` only when the
|
||||
// piece has the line (`[]` clears; no line leaves the blog's). The
|
||||
// blog's own list for the sheet's suggestions: MicropubCategories.swift.
|
||||
// #34 (S6.2): uploaded illustrations rewrite their asset refs IN
|
||||
// PLACE with the uploaded URLs (position is the writer's) — the
|
||||
// media surface (parse/upload/config/rewrite) lives in
|
||||
// MicropubMedia.swift, the orchestration in micropubSend below.
|
||||
// #206: a piece that remembers its post re-sends as an IN-PLACE
|
||||
// update (action=update + replace name/content — never a second
|
||||
// post), Publish is remembered on the piece (`micropub_published`),
|
||||
// post; #207 adds `category` when the piece carries the line),
|
||||
// Publish is remembered on the piece (`micropub_published`),
|
||||
// and a 404 forgets the link. Wire facts live-verified 2026-12-04
|
||||
// (microblog.dev/api/posting): `replace` sets name and content;
|
||||
// updates answer 200 with an empty body, 404 when the URL is gone.
|
||||
@@ -78,7 +82,7 @@ nonisolated enum Micropub {
|
||||
return request
|
||||
}
|
||||
|
||||
static func makeDraftRequest(title: String, body: String, token: String) -> URLRequest {
|
||||
static func makeDraftRequest(title: String, body: String, token: String, categories: [String] = []) -> URLRequest {
|
||||
// h=entry with markdown content and name for the title; the
|
||||
// Title publishes as the entry's name, the body ships verbatim
|
||||
// (GLOSSARY: never stripped) — the one carve-out is #34's asset
|
||||
@@ -86,15 +90,17 @@ nonisolated enum Micropub {
|
||||
// the WIRE (position and alt are the writer's; the piece on
|
||||
// disk never changes). No photo property: it would render
|
||||
// wherever the theme decides, not where placed (2026-11-01).
|
||||
// category: never sent in v1.
|
||||
jsonPost([
|
||||
"type": ["h-entry"],
|
||||
"properties": [
|
||||
"name": [title],
|
||||
"content": [body],
|
||||
"post-status": ["draft"],
|
||||
] as [String: Any],
|
||||
], token: token)
|
||||
// #207: the categories travel — naming one Micro.blog doesn't
|
||||
// know creates it there; no list, no key.
|
||||
var properties: [String: Any] = [
|
||||
"name": [title],
|
||||
"content": [body],
|
||||
"post-status": ["draft"],
|
||||
]
|
||||
if !categories.isEmpty {
|
||||
properties["category"] = categories
|
||||
}
|
||||
return jsonPost(["type": ["h-entry"], "properties": properties], token: token)
|
||||
}
|
||||
|
||||
static func makePublishRequest(url: String, token: String) -> URLRequest {
|
||||
@@ -109,14 +115,21 @@ nonisolated enum Micropub {
|
||||
/// replaced IN PLACE over the remembered URL (wire-verified
|
||||
/// 2026-12-04: `replace` sets name and content, JSON-only). Never
|
||||
/// a status flip: a draft stays a draft, a published post stays live.
|
||||
static func makeUpdateRequest(url: String, title: String, body: String, token: String) -> URLRequest {
|
||||
jsonPost([
|
||||
/// #207: `categories` opts the post's categories into `replace` —
|
||||
/// a list (even empty: it clears them) is sent as written; nil
|
||||
/// leaves whatever the post has.
|
||||
static func makeUpdateRequest(url: String, title: String, body: String, token: String, categories: [String]? = nil) -> URLRequest {
|
||||
var replace: [String: Any] = [
|
||||
"name": [title],
|
||||
"content": [body],
|
||||
]
|
||||
if let categories {
|
||||
replace["category"] = categories
|
||||
}
|
||||
return jsonPost([
|
||||
"action": "update",
|
||||
"url": url,
|
||||
"replace": [
|
||||
"name": [title],
|
||||
"content": [body],
|
||||
],
|
||||
"replace": replace,
|
||||
], token: token)
|
||||
}
|
||||
|
||||
@@ -124,14 +137,17 @@ nonisolated enum Micropub {
|
||||
|
||||
/// Step 1: send the piece as a draft. The piece's Title is the
|
||||
/// entry's name; the markdown body is the content; #34's uploaded
|
||||
/// illustrations already rewrote their refs in the body.
|
||||
/// illustrations already rewrote their refs in the body. #207: the
|
||||
/// categories ride as the entry's `category` (non-empty only).
|
||||
static func sendDraft(
|
||||
title: String,
|
||||
body: String,
|
||||
token: String,
|
||||
categories: [String] = [],
|
||||
transport: Transport = urlSessionTransport,
|
||||
) async -> Outcome {
|
||||
await run(makeDraftRequest(title: title, body: body, token: token), transport: transport, decodeDraftURL: true)
|
||||
let request = makeDraftRequest(title: title, body: body, token: token, categories: categories)
|
||||
return await run(request, transport: transport, decodeDraftURL: true)
|
||||
}
|
||||
|
||||
/// Step 2: publish the draft — flip its post-status. Needs the
|
||||
@@ -147,15 +163,18 @@ nonisolated enum Micropub {
|
||||
|
||||
/// #206: step 1 again, in place — the piece's current title and
|
||||
/// body replace the remembered post's (a draft refreshed for
|
||||
/// review; a live post edited). Needs the remembered URL.
|
||||
/// review; a live post edited). Needs the remembered URL. #207:
|
||||
/// `categories` non-nil also replaces the post's categories.
|
||||
static func update(
|
||||
url: String,
|
||||
title: String,
|
||||
body: String,
|
||||
token: String,
|
||||
categories: [String]? = nil,
|
||||
transport: Transport = urlSessionTransport,
|
||||
) async -> Outcome {
|
||||
let outcome = await run(makeUpdateRequest(url: url, title: title, body: body, token: token), transport: transport, decodeDraftURL: false)
|
||||
let request = makeUpdateRequest(url: url, title: title, body: body, token: token, categories: categories)
|
||||
let outcome = await run(request, transport: transport, decodeDraftURL: false)
|
||||
// `run` has one success word for the URL-less steps; the update's
|
||||
// carries the URL it touched.
|
||||
if outcome == .published {
|
||||
@@ -326,20 +345,42 @@ extension PostsModel {
|
||||
return (id, home)
|
||||
}
|
||||
|
||||
/// #207: the open piece's categories as they are on DISK — what the
|
||||
/// publish sheet's field syncs to (never the scan snapshot: the list
|
||||
/// can lag the open piece). No open saved piece → none.
|
||||
func openPieceCategories() -> [String] {
|
||||
guard let piece = openMicropubPiece,
|
||||
let file = PostStore.folder(of: piece.id, home: piece.home)?.appendingPathComponent("post.md"),
|
||||
let raw = try? String(contentsOf: file, encoding: .utf8)
|
||||
else { return [] }
|
||||
return PostFrontmatter.categories(in: PostFrontmatter.parse(raw))
|
||||
}
|
||||
|
||||
/// Step 1 of the two-step publish: send the OPEN piece to Micro.blog
|
||||
/// (name = Title, markdown content — category frontmatter never
|
||||
/// travels). runPass's guard set minus the engine gates (no engine
|
||||
/// involved); flush-first so the wire carries the editor's latest.
|
||||
/// (name = Title, markdown content). runPass's guard set minus
|
||||
/// the engine gates (no engine involved); flush-first so the wire
|
||||
/// carries the editor's latest. #207: the piece's `category:` list
|
||||
/// travels — a create carries it when non-empty; a re-send carries
|
||||
/// `category` in `replace` only when the piece HAS the line
|
||||
/// (opt-in by key — `[]` clears the post's categories, no line
|
||||
/// leaves the blog's).
|
||||
/// #206: a piece that remembers no post is CREATED as a draft
|
||||
/// (h=entry, post-status draft) and remembers its URL
|
||||
/// (`micropub_url` stamp); a piece that remembers one UPDATES that
|
||||
/// post in place — same URL, no second post — so a draft refreshes
|
||||
/// for review and a published piece edits live. A stamp failure
|
||||
/// never un-sends the draft.
|
||||
/// #207: `categories` (the sheet's edit) — nil = no edit, the
|
||||
/// piece's line is used as written; non-nil = that list is written
|
||||
/// to the piece FIRST (`[]` renders `category: []`), after the
|
||||
/// flush and before the ONE disk read that feeds the wire, so disk
|
||||
/// and blog never disagree. A category write that cannot land
|
||||
/// REFUSES the send — nothing goes out.
|
||||
/// #179: outside the write rail by design — publish is not a library
|
||||
/// verb, and a stamp failure must never un-send the draft.
|
||||
func micropubSend(
|
||||
token: String,
|
||||
categories: [String]? = nil,
|
||||
transport: Micropub.Transport = Micropub.urlSessionTransport,
|
||||
) async -> Micropub.Outcome {
|
||||
guard let piece = openMicropubPiece else {
|
||||
@@ -348,12 +389,15 @@ extension PostsModel {
|
||||
guard flushOrAbort(piece.id) else {
|
||||
return .refused(lastError ?? "Couldn't save the piece before sending.")
|
||||
}
|
||||
if let refused = stampCategories(categories, piece: piece) {
|
||||
return refused
|
||||
}
|
||||
// Disk is the truth post-flush — ONE read serves the wire fields:
|
||||
// Title (heading markers stripped — it publishes as the entry's
|
||||
// name), the frontmatter-stripped body (the stamp is
|
||||
// bookkeeping, never prose; the body itself is never stripped,
|
||||
// GLOSSARY), and the remembered URL. No scan dependency: the
|
||||
// list can lag the open piece.
|
||||
// GLOSSARY), the remembered URL, and the categories. No scan
|
||||
// dependency: the list can lag the open piece.
|
||||
guard let dir = PostStore.folder(of: piece.id, home: piece.home),
|
||||
let raw = try? String(contentsOf: dir.appendingPathComponent("post.md"), encoding: .utf8)
|
||||
else {
|
||||
@@ -375,13 +419,61 @@ extension PostsModel {
|
||||
return outcome
|
||||
}
|
||||
}
|
||||
return await updateRememberedOrCreate(
|
||||
raw: raw,
|
||||
wireBody: wireBody,
|
||||
piece: piece,
|
||||
token: token,
|
||||
transport: transport,
|
||||
)
|
||||
}
|
||||
|
||||
/// #207: the sheet's category edit lands on the piece HERE — after
|
||||
/// the flush, before the ONE read that feeds the wire, so disk and
|
||||
/// blog never disagree. nil is no edit. The refusal when the line
|
||||
/// cannot land: the send stops here and nothing goes out.
|
||||
private func stampCategories(_ categories: [String]?, piece: (id: String, home: URL)) -> Micropub.Outcome? {
|
||||
guard let categories else { return nil }
|
||||
do {
|
||||
try PostStore.stampMicropub(piece.id, home: piece.home, [("category", PostFrontmatter.categoryValue(categories))])
|
||||
} catch {
|
||||
return .refused("Couldn't save this piece's categories — nothing was sent. (\(error.localizedDescription))")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/// The wire step over the disk's text (`raw` — the Title and the
|
||||
/// frontmatter come from it) and the wire body. #206: a piece that
|
||||
/// remembers its post UPDATES it in place (the remembered URL never
|
||||
/// moves, nothing new to stamp; a 404 means the post is gone on
|
||||
/// Micro.blog — the piece forgets it rather than fail forever
|
||||
/// against a dead URL); a piece that remembers none is CREATED as
|
||||
/// a draft and remembers its URL. A stamp failure can't un-send
|
||||
/// the draft; the sheet still holds the URL for this session — the
|
||||
/// piece just loses the memory across a reopen (it would create a
|
||||
/// second draft then). Kept quiet: the same disk accepted the
|
||||
/// flush moments before.
|
||||
/// #207: a create carries the categories when non-empty (never
|
||||
/// `[]`); an update carries `category` only when the piece HAS the
|
||||
/// line — opt-in by key, `[]` clears, no line leaves the blog's.
|
||||
private func updateRememberedOrCreate(
|
||||
raw: String,
|
||||
wireBody: String,
|
||||
piece: (id: String, home: URL),
|
||||
token: String,
|
||||
transport: Micropub.Transport,
|
||||
) async -> Micropub.Outcome {
|
||||
let matter = PostFrontmatter.parse(raw)
|
||||
let title = Post.title(of: raw)
|
||||
// #206: the piece remembers its post → update THAT post in
|
||||
// place (the remembered URL never moves; nothing new to stamp).
|
||||
// A 404 means the post is gone on Micro.blog — the piece forgets
|
||||
// it rather than fail forever against a dead URL.
|
||||
if let remembered = Micropub.rememberedURL(PostFrontmatter.parse(raw)) {
|
||||
let outcome = await Micropub.update(url: remembered, title: title, body: wireBody, token: token, transport: transport)
|
||||
if let remembered = Micropub.rememberedURL(matter) {
|
||||
let outcome = await Micropub.update(
|
||||
url: remembered,
|
||||
title: title,
|
||||
body: wireBody,
|
||||
token: token,
|
||||
categories: matter["category"] != nil ? PostFrontmatter.categories(in: matter) : nil,
|
||||
transport: transport,
|
||||
)
|
||||
if case .requestFailed(404, _) = outcome {
|
||||
return micropubPostGone(id: piece.id, home: piece.home)
|
||||
}
|
||||
@@ -391,15 +483,10 @@ extension PostsModel {
|
||||
title: title,
|
||||
body: wireBody,
|
||||
token: token,
|
||||
categories: PostFrontmatter.categories(in: matter),
|
||||
transport: transport,
|
||||
)
|
||||
if case let .drafted(url, _) = outcome {
|
||||
// Remember the draft's URL for the publish step and every
|
||||
// later in-place update. A stamp failure can't un-send the
|
||||
// draft; the sheet still holds the URL for this session —
|
||||
// the piece just loses the memory across a reopen (it
|
||||
// would create a second draft then). Kept quiet: the same
|
||||
// disk accepted the flush moments before.
|
||||
try? PostStore.stampMicropub(piece.id, home: piece.home, [("micropub_url", url)])
|
||||
}
|
||||
return outcome
|
||||
@@ -415,9 +502,16 @@ extension PostsModel {
|
||||
/// content travels, and the editor never writes frontmatter. The URL
|
||||
/// is trust-gated HERE — it lands in frontmatter (the heal), and a
|
||||
/// caller's value is external until proven.
|
||||
/// #207: `categories` non-nil = pending edits — the piece is sent
|
||||
/// FIRST (`micropubSend`, the edit landing on disk), then the post
|
||||
/// that send leaves is published: the remembered one, or the fresh
|
||||
/// draft a create returned — never the caller's URL, which the
|
||||
/// sheet may hold stale against the disk. A failed send returns its
|
||||
/// outcome with NO publish — disk and blog never silently disagree.
|
||||
func micropubPublish(
|
||||
url: String,
|
||||
token: String,
|
||||
categories: [String]? = nil,
|
||||
transport: Micropub.Transport = Micropub.urlSessionTransport,
|
||||
) async -> Micropub.Outcome {
|
||||
guard let piece = openMicropubPiece else {
|
||||
@@ -426,11 +520,22 @@ extension PostsModel {
|
||||
guard Micropub.trustworthyURL(url) else {
|
||||
return .refused("That post address isn't a clean https URL — nothing was sent or remembered.")
|
||||
}
|
||||
let outcome = await Micropub.publish(url: url, token: token, transport: transport)
|
||||
var target = url
|
||||
if let categories {
|
||||
switch await micropubSend(token: token, categories: categories, transport: transport) {
|
||||
case let .updated(touched):
|
||||
target = touched
|
||||
case let .drafted(fresh, _):
|
||||
target = fresh
|
||||
case let other:
|
||||
return other
|
||||
}
|
||||
}
|
||||
let outcome = await Micropub.publish(url: target, token: token, transport: transport)
|
||||
switch outcome {
|
||||
case .published:
|
||||
try? PostStore.stampMicropub(piece.id, home: piece.home, [
|
||||
("micropub_url", url),
|
||||
("micropub_url", target),
|
||||
("micropub_published", FixCardStore.formatted(Date(), PostFrontmatter.stampFormat)),
|
||||
])
|
||||
case .requestFailed(404, _):
|
||||
|
||||
Reference in new issue
Block a user