Files
museion/Museion/MicropubPublishSheet.swift
T
AlexKucera 6938826101 feat(microblog): add app token settings page and honest 5xx wording
The app token could only be entered in the publish sheet, and that
field came back only after a 401/403, so a saved token had no standing
way to be replaced or removed. The new Settings → Micro.blog page
writes the same `MicropubTokenStore` slot the sheet reads, with a
status row read back from the Keychain after every write so a locked
keychain fails visibly instead of claiming "stored". The sheet now
names the stored token a send carries when its field is hidden.

Micro.blog's errors arrive as HTML pages, never Micropub JSON, so
`decodeFailure` fell through to a bare `HTTP 503` that said nothing
about whose side it was on. Any 5xx without a JSON description now
reads "unavailable on their side — retry in a few minutes"; a
described 5xx keeps its description and a 4xx keeps the bare status.
One decode covers create, publish, q=config, and media uploads.

Docs: AGENTS.md bullets, CHANGELOG [Unreleased] Added/Fixed, and the
devlog for this task plus the 0.1.2 release log.
2026-12-04 15:06:00 +01:00

219 lines
6.8 KiB
Swift

//
// 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 when none is saved and
// lives in the Keychain (G11; re-offered only on an auth failure —
// Settings → Micro.blog is the standing manager). 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
@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)
} else {
// A stored token is in use — the field's absence alone
// never said which token a send carries.
Text("Sends with the app token in your Keychain — Settings → Micro.blog manages it.")
.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(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
}
}
}
}