feat(publish): Micropub draft → publish (#33)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
Two-step publish to Micro.blog over Micropub: Send as Draft (JSON h=entry create — name = the Title, markdown content verbatim, post-status draft), review via the preview link, then Publish (the action=update post-status flip). The piece remembers its draft URL as a frontmatter micropub_url stamp — in-place, body and foreign keys byte-identical — so a draft survives a reopen; Send Again re-drafts after edits. The app token lives in the Keychain (login keychain; entered once in the sheet, re-offered only on auth failure — ADR-0014's no-Keychain rule covers only provider keys hermes reads from .env). Frontmatter category: is permitted but never sent in v1 — pinned on the wire. The draft URL is trust-boundary data landing in frontmatter, so every URL passes a format gate (https + host, no line/control characters) at BOTH ends: the response decode (Location header → body url; the preview link is display-only, never the publish target) and Post.scan's read-back — imported frontmatter rides verbatim, and a newline--- rider would otherwise close the block and reparse as attacker markdown in the writer's piece. Twenty tests pin the wire contract, decode matrix, own-slot keychain round-trip, stamp/scan, and the model flow (flush-first, refusal guards, the category AC); all four audit guards are flip-verified. Full suite 529 passed / 0 failed. Live end-to-end on a real post (needs the owner's app token) remains open on the ticket.
This commit is contained in:
1 parent
3ee6756467
commit
414f1392ef
15 files changed
+1106
-7
No files matched your search
@@ -0,0 +1,212 @@
|
||||
//
|
||||
// MicropubPublishSheet.swift
|
||||
// Museion
|
||||
//
|
||||
// #33 (S6.1): the publish ask — draft first, then publish (G9:
|
||||
// publish is ask-before; each step is its own explicit click). The
|
||||
// sheet owns the whole two-step flow over the OPEN piece: Send as
|
||||
// Draft (h=entry, name, markdown content) → review on Micro.blog →
|
||||
// Publish (the post-status update against the piece's remembered
|
||||
// draft URL). The app token is entered here ONCE and lives in the
|
||||
// Keychain (G11; re-offered only on an auth failure). Refusals show
|
||||
// inline with the buttons kept (ApplyAskSheet posture).
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
/// #33 (S6.1): the two-step Micro.blog ask over the open piece.
|
||||
struct MicropubPublishSheet: View {
|
||||
let posts: PostsModel
|
||||
let home: URL
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
private enum Phase: Equatable {
|
||||
case idle
|
||||
case drafting
|
||||
case drafted
|
||||
case publishing
|
||||
case published
|
||||
}
|
||||
|
||||
@State private var phase: Phase = .idle
|
||||
/// The draft URL this sheet knows: fresh from a send, or the piece's
|
||||
/// remembered `micropub_url` (a draft sent in an earlier session).
|
||||
@State private var draftURL: String?
|
||||
@State private var previewURL: String?
|
||||
@State private var refusal: String?
|
||||
@State private var tokenField = ""
|
||||
@State private var reentry = false
|
||||
|
||||
/// Token entry shows when none is saved — or again after an auth
|
||||
/// failure (the entered-once token can be replaced, never a dead end).
|
||||
private var showsTokenField: Bool {
|
||||
reentry || MicropubTokenStore.read() == nil
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
Text("Publish to Micro.blog?")
|
||||
.font(.headline)
|
||||
Text(pieceTitle)
|
||||
.font(.callout.weight(.semibold))
|
||||
.lineLimit(2)
|
||||
Text("Museion sends it as a draft first — nothing goes out until you publish.")
|
||||
.font(.callout)
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
|
||||
if showsTokenField {
|
||||
SecureField("App token", text: $tokenField)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.font(.system(size: 12, design: DesignTokens.mono))
|
||||
Text("From micro.blog → Account → App Tokens. Stored in your Keychain.")
|
||||
.font(.system(size: 11, design: DesignTokens.mono))
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
}
|
||||
|
||||
if phase == .drafting || phase == .publishing {
|
||||
ProgressView()
|
||||
}
|
||||
|
||||
if let url = statusURL {
|
||||
// The draft lives on Micro.blog — the preview link goes
|
||||
// there for review; once published it's the post itself.
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(phase == .published ? "Published" : "Draft on Micro.blog")
|
||||
.font(.system(size: 11, design: DesignTokens.mono))
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
if let link = URL(string: phase == .published ? url : (previewURL ?? url)) {
|
||||
Link(destination: link) {
|
||||
Text(link.absoluteString)
|
||||
.font(.system(size: 11, design: DesignTokens.mono))
|
||||
.lineLimit(1)
|
||||
.truncationMode(.middle)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let refusal {
|
||||
Text(refusal)
|
||||
.font(.callout)
|
||||
.foregroundStyle(.orange)
|
||||
}
|
||||
|
||||
HStack(spacing: 8) {
|
||||
if phase == .published {
|
||||
Button("Done") { dismiss() }
|
||||
.buttonStyle(.borderedProminent)
|
||||
} else {
|
||||
Button("Publish") { publish() }
|
||||
.buttonStyle(.borderedProminent)
|
||||
.disabled(draftURL == nil || phase == .drafting || phase == .publishing)
|
||||
if draftURL != nil {
|
||||
// The piece changed since the draft — a fresh send
|
||||
// replaces it (and the remembered URL).
|
||||
Button("Send Again") { sendDraft() }
|
||||
.buttonStyle(.bordered)
|
||||
.disabled(phase == .drafting || phase == .publishing)
|
||||
} else {
|
||||
Button("Send as Draft") { sendDraft() }
|
||||
.buttonStyle(.bordered)
|
||||
.disabled(phase == .drafting || phase == .publishing)
|
||||
}
|
||||
Button("Cancel") { dismiss() }
|
||||
.buttonStyle(.bordered)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .trailing)
|
||||
}
|
||||
.padding(16)
|
||||
.frame(width: 340, alignment: .leading)
|
||||
.onAppear {
|
||||
// A draft sent in an earlier session is still publishable:
|
||||
// the piece remembers its URL.
|
||||
if draftURL == nil, let id = posts.session?.postID {
|
||||
if let remembered = posts.posts.first(where: { $0.id == id })?.micropubURL {
|
||||
draftURL = remembered
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Steps
|
||||
|
||||
/// The open piece's Title (the sheet is only reachable with a piece
|
||||
/// open; the id is the honest fallback).
|
||||
private var pieceTitle: String {
|
||||
posts.session?.postID.map { posts.title(of: $0) } ?? "This piece"
|
||||
}
|
||||
|
||||
private var statusURL: String? {
|
||||
phase == .drafted || phase == .published || phase == .publishing ? draftURL : nil
|
||||
}
|
||||
|
||||
/// Resolve the token: a freshly entered one is saved ONCE (rotation
|
||||
/// replaces); otherwise the Keychain's. Nil → the inline ask, never
|
||||
/// a silent send.
|
||||
private func resolveToken() -> String? {
|
||||
if !tokenField.trimmingCharacters(in: .whitespaces).isEmpty {
|
||||
let token = tokenField.trimmingCharacters(in: .whitespaces)
|
||||
MicropubTokenStore.save(token)
|
||||
reentry = false
|
||||
return token
|
||||
}
|
||||
guard let saved = MicropubTokenStore.read() else { return nil }
|
||||
return saved
|
||||
}
|
||||
|
||||
private func sendDraft() {
|
||||
guard let token = resolveToken() else {
|
||||
refusal = "Paste your Micro.blog app token first."
|
||||
return
|
||||
}
|
||||
refusal = nil
|
||||
phase = .drafting
|
||||
Task {
|
||||
switch await posts.micropubSendDraft(home: home, token: token) {
|
||||
case let .drafted(url, preview):
|
||||
draftURL = url
|
||||
previewURL = preview
|
||||
phase = .drafted
|
||||
case let .authFailed(detail):
|
||||
reentry = true // the entered-once token may be wrong
|
||||
refusal = "Micro.blog rejected the token: \(detail)"
|
||||
phase = draftURL == nil ? .idle : .drafted
|
||||
case let .refused(reason), let .unreachable(reason):
|
||||
refusal = reason
|
||||
phase = draftURL == nil ? .idle : .drafted
|
||||
case let .requestFailed(status, detail):
|
||||
refusal = "Micro.blog said \(status): \(detail)"
|
||||
phase = draftURL == nil ? .idle : .drafted
|
||||
case .published:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func publish() {
|
||||
guard let url = draftURL, let token = resolveToken() else {
|
||||
refusal = "Paste your Micro.blog app token first."
|
||||
return
|
||||
}
|
||||
refusal = nil
|
||||
phase = .publishing
|
||||
Task {
|
||||
switch await Micropub.publish(url: url, token: token) {
|
||||
case .published:
|
||||
phase = .published
|
||||
case let .authFailed(detail):
|
||||
reentry = true
|
||||
refusal = "Micro.blog rejected the token: \(detail)"
|
||||
phase = .drafted
|
||||
case let .refused(reason), let .unreachable(reason):
|
||||
refusal = reason
|
||||
phase = .drafted
|
||||
case let .requestFailed(status, detail):
|
||||
refusal = "Micro.blog said \(status): \(detail)"
|
||||
phase = .drafted
|
||||
case .drafted:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user