Files
museion/Museion/MicropubCategories.swift
T
AlexKucera 6db4cf8cf1 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
2026-12-05 08:09:57 +01:00

49 lines
2.1 KiB
Swift

//
// MicropubCategories.swift
// Museion
//
// #207: the blog's own categories — the list the publish sheet
// offers as suggestions for the piece's `category:` line. One
// query, no writes: naming a category Micro.blog doesn't know
// CREATES it when the post is sent (the create/update carry the
// list — Micropub.swift and the codec in PostFrontmatter.swift),
// so there is no separate create call here.
//
// Wire facts (microblog.dev/api/posting, book.micro.blog/micropub):
// `GET /micropub?q=category` answers the blog's categories as a
// root `categories` string array; some answers carry the richer
// `microblog-categories` objects (`name` + `url`) instead. A 2xx
// with neither is a blog without categories — no suggestions is
// not an error, and neither is any failure: the sheet stays silent
// (a typed new name still creates the category at the send, and a
// rejected token surfaces on the send itself).
//
import Foundation
nonisolated extension Micropub {
/// `GET <endpoint>?q=category` — the config query's shape.
static func makeCategoryListRequest(token: String) -> URLRequest {
queryRequest("category", token: token)
}
/// The blog's categories for the sheet's suggestions — trimmed,
/// empties dropped; `[]` on any failure or non-2xx (there is no
/// error to surface — see the header).
static func blogCategories(token: String, transport: Transport = urlSessionTransport) async -> [String] {
guard let (data, response) = try? await transport(makeCategoryListRequest(token: token)),
(200 ..< 300).contains(response.statusCode) else { return [] }
return categoryNames(in: data)
}
/// The root `categories` strings, else the `microblog-categories`
/// objects' `name` fields.
private static func categoryNames(in data: Data) -> [String] {
let root = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] ?? [:]
let names = (root["categories"] as? [String])
?? (root["microblog-categories"] as? [[String: Any]])?.compactMap { $0["name"] as? String }
?? []
return names.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty }
}
}