feat(microblog): show Micro.blog publish status
The publish sheet flipped a draft live and remembered nothing, so nothing on disk said a piece was out and the library could not show it. A successful Publish now stamps `micropub_published` in the piece's frontmatter; the row and the sheet read `published <date>` or `draft on Micro.blog`, and a published piece's menu opens the live post. Sending a piece again no longer creates a second post: a piece that remembers its post updates it in place (name and content only, so categories set on Micro.blog survive). An image whose bytes are unchanged rides its earlier upload through a per-piece memo. When Micro.blog answers 404 the piece forgets both keys and the sheet offers a fresh draft; a clear that cannot land says so instead of claiming the link is gone. Museion's micropub keys are dropped at the import boundary. An imported `micropub_url` passes the format gate and would aim the in-place update at one of the writer's own posts under their token, so the keys are treated like `origin`: ours to set, never a foreign file's to carry in. Publish is model-owned and trust-gates its URL before anything is stamped. Tests read recorded requests through an optional accessor: an out-of-range subscript after a failed count expectation trapped the hosted runner and reported every queued test as crashed. +16 tests net (17 new pins in MicropubStatusTests, one retired whose premise the in-place design voids); every new pin was watched fail on its own guard. Rail 752/752; SwiftFormat and SwiftLint clean. Reviews: standards + spec during the build, then a three-reviewer finish audit. Docs: AGENTS.md bullets and learnings, GLOSSARY terms, CHANGELOG [Unreleased], README, and the devlog. Closes #206
This commit is contained in:
1 parent
6938826101
commit
6aa31a419a
21 files changed
+1151
-225
No files matched your search
@@ -11,6 +11,10 @@
|
||||
// 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).
|
||||
// #206: the sheet opens on the piece's publish record. A published
|
||||
// piece says so (and links its live post) and offers Update Live
|
||||
// Post; a draft offers Publish / Update Draft. Both re-sends update
|
||||
// the remembered post IN PLACE — never a second post.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
@@ -21,17 +25,26 @@ struct MicropubPublishSheet: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
private enum Phase: Equatable {
|
||||
/// Never sent — nothing on Micro.blog yet.
|
||||
case idle
|
||||
case drafting
|
||||
/// A draft is on Micro.blog, not published (fresh or remembered).
|
||||
case drafted
|
||||
case updating
|
||||
case updated
|
||||
case publishing
|
||||
/// The publish record — this session's Publish, or the piece's stamp.
|
||||
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?
|
||||
/// The post URL this sheet knows: fresh from a send, or the piece's
|
||||
/// remembered `micropub_url` (sent in an earlier session) — a draft's
|
||||
/// until it is published, the live post's after.
|
||||
@State private var postURL: String?
|
||||
/// #206: when the piece was published — its stamp, or this session's
|
||||
/// Publish. Nil while it is a draft.
|
||||
@State private var publishedAt: Date?
|
||||
@State private var previewURL: String?
|
||||
@State private var refusal: String?
|
||||
@State private var tokenField = ""
|
||||
@@ -43,16 +56,22 @@ struct MicropubPublishSheet: View {
|
||||
reentry || MicropubTokenStore.read() == nil
|
||||
}
|
||||
|
||||
private var busy: Bool {
|
||||
phase == .drafting || phase == .updating || phase == .publishing
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
Text("Publish to Micro.blog?")
|
||||
Text(publishedAt == nil ? "Publish to Micro.blog?" : "On 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 phase == .idle {
|
||||
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)
|
||||
@@ -69,18 +88,18 @@ struct MicropubPublishSheet: View {
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
}
|
||||
|
||||
if phase == .drafting || phase == .publishing {
|
||||
if busy {
|
||||
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.
|
||||
if let label = statusLabel {
|
||||
// A published piece's link is the post itself; a draft's
|
||||
// is its review page.
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(phase == .published ? "Published" : "Draft on Micro.blog")
|
||||
Text(label)
|
||||
.font(.system(size: 11, design: DesignTokens.mono))
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
if let link = URL(string: phase == .published ? url : (previewURL ?? url)) {
|
||||
if let link = statusLink {
|
||||
Link(destination: link) {
|
||||
Text(link.absoluteString)
|
||||
.font(.system(size: 11, design: DesignTokens.mono))
|
||||
@@ -97,24 +116,25 @@ struct MicropubPublishSheet: View {
|
||||
}
|
||||
|
||||
HStack(spacing: 8) {
|
||||
if phase == .published {
|
||||
if publishedAt != nil {
|
||||
// The piece is out: the only send left edits the live
|
||||
// post, immediately — the label says so. A stamp with no
|
||||
// remembered link (a hand-added key) has no post to
|
||||
// update: that send is a fresh draft, and says so.
|
||||
Button(postURL == nil ? "Send as Draft" : "Update Live Post") { send() }
|
||||
.buttonStyle(.bordered)
|
||||
.disabled(busy)
|
||||
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)
|
||||
}
|
||||
.disabled(postURL == nil || busy)
|
||||
// The piece changed since the draft — a re-send
|
||||
// updates that same draft (never a second post).
|
||||
Button(postURL == nil ? "Send as Draft" : "Update Draft") { send() }
|
||||
.buttonStyle(.bordered)
|
||||
.disabled(busy)
|
||||
Button("Cancel") { dismiss() }
|
||||
.buttonStyle(.bordered)
|
||||
}
|
||||
@@ -124,17 +144,18 @@ struct MicropubPublishSheet: View {
|
||||
.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
|
||||
}
|
||||
}
|
||||
// A piece sent in an earlier session still has its record:
|
||||
// the remembered URL, and (#206) its publish stamp.
|
||||
guard postURL == nil, let id = posts.session?.postID,
|
||||
let post = posts.posts.first(where: { $0.id == id })
|
||||
else { return }
|
||||
postURL = post.micropubURL
|
||||
publishedAt = post.micropubPublished
|
||||
phase = settledPhase
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Steps
|
||||
// MARK: Status
|
||||
|
||||
/// The open piece's Title (the sheet is only reachable with a piece
|
||||
/// open; the id is the honest fallback).
|
||||
@@ -142,10 +163,49 @@ struct MicropubPublishSheet: View {
|
||||
posts.session?.postID.map { posts.title(of: $0) } ?? "This piece"
|
||||
}
|
||||
|
||||
private var statusURL: String? {
|
||||
phase == .drafted || phase == .published || phase == .publishing ? draftURL : nil
|
||||
private var statusLabel: String? {
|
||||
switch phase {
|
||||
case .idle:
|
||||
nil
|
||||
case .drafting:
|
||||
"Sending draft…"
|
||||
case .drafted:
|
||||
"Draft on Micro.blog — not published yet"
|
||||
case .updating:
|
||||
publishedAt == nil ? "Updating draft…" : "Updating live post…"
|
||||
case .updated:
|
||||
publishedAt.map { "Updated live — published \(Micropub.shortDate($0))" } ?? "Draft updated — not published yet"
|
||||
case .publishing:
|
||||
"Publishing…"
|
||||
case .published:
|
||||
publishedAt.map { "Published \(Micropub.shortDate($0))" }
|
||||
}
|
||||
}
|
||||
|
||||
/// A published piece links its live post; a draft links the review
|
||||
/// page — only the send that created it carries one, so a draft
|
||||
/// remembered from an earlier session shows no (dead) link.
|
||||
private var statusLink: URL? {
|
||||
if publishedAt != nil {
|
||||
return postURL.flatMap { URL(string: $0) }
|
||||
}
|
||||
return previewURL.flatMap { URL(string: $0) }
|
||||
}
|
||||
|
||||
/// Where the sheet rests on the piece's record: published, draft,
|
||||
/// or never sent.
|
||||
private var settledPhase: Phase {
|
||||
if publishedAt != nil {
|
||||
return .published
|
||||
}
|
||||
if postURL != nil {
|
||||
return .drafted
|
||||
}
|
||||
return .idle
|
||||
}
|
||||
|
||||
// MARK: Steps
|
||||
|
||||
/// Resolve the token: a freshly entered one is saved ONCE (rotation
|
||||
/// replaces); otherwise the Keychain's. Nil → the inline ask, never
|
||||
/// a silent send.
|
||||
@@ -160,29 +220,44 @@ struct MicropubPublishSheet: View {
|
||||
return saved
|
||||
}
|
||||
|
||||
private func sendDraft() {
|
||||
/// A failed step: the message shows inline with the buttons kept, and
|
||||
/// the sheet rests on the piece's record (a rejected token re-opens
|
||||
/// entry — the entered-once token may be wrong).
|
||||
private func fail(_ message: String, tokenRejected: Bool = false) {
|
||||
if tokenRejected {
|
||||
reentry = true
|
||||
}
|
||||
refusal = message
|
||||
phase = settledPhase
|
||||
}
|
||||
|
||||
/// Step 1 — the model decides create-vs-update by the piece's
|
||||
/// remembered URL: a first send creates the draft, every later one
|
||||
/// updates that post in place.
|
||||
private func send() {
|
||||
guard let token = resolveToken() else {
|
||||
refusal = "Paste your Micro.blog app token first."
|
||||
return
|
||||
}
|
||||
refusal = nil
|
||||
phase = .drafting
|
||||
phase = postURL == nil ? .drafting : .updating
|
||||
Task {
|
||||
switch await posts.micropubSendDraft(token: token) {
|
||||
switch await posts.micropubSend(token: token) {
|
||||
case let .drafted(url, preview):
|
||||
draftURL = url
|
||||
postURL = url
|
||||
previewURL = preview
|
||||
phase = .drafted
|
||||
case let .updated(url):
|
||||
postURL = url
|
||||
phase = .updated
|
||||
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
|
||||
fail("Micro.blog rejected the token: \(detail)", tokenRejected: true)
|
||||
case let .refused(reason), let .unreachable(reason):
|
||||
refusal = reason
|
||||
phase = draftURL == nil ? .idle : .drafted
|
||||
fail(reason)
|
||||
case let .requestFailed(status, detail):
|
||||
refusal = "Micro.blog said \(status): \(detail)"
|
||||
phase = draftURL == nil ? .idle : .drafted
|
||||
fail("Micro.blog said \(status): \(detail)")
|
||||
case let .forgotten(reason):
|
||||
forgot(reason)
|
||||
case .published:
|
||||
break
|
||||
}
|
||||
@@ -190,29 +265,39 @@ struct MicropubPublishSheet: View {
|
||||
}
|
||||
|
||||
private func publish() {
|
||||
guard let url = draftURL, let token = resolveToken() else {
|
||||
guard let url = postURL, 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) {
|
||||
switch await posts.micropubPublish(url: url, token: token) {
|
||||
case .published:
|
||||
publishedAt = Date()
|
||||
phase = .published
|
||||
case let .authFailed(detail):
|
||||
reentry = true
|
||||
refusal = "Micro.blog rejected the token: \(detail)"
|
||||
phase = .drafted
|
||||
fail("Micro.blog rejected the token: \(detail)", tokenRejected: true)
|
||||
case let .refused(reason), let .unreachable(reason):
|
||||
refusal = reason
|
||||
phase = .drafted
|
||||
fail(reason)
|
||||
case let .requestFailed(status, detail):
|
||||
refusal = "Micro.blog said \(status): \(detail)"
|
||||
phase = .drafted
|
||||
case .drafted:
|
||||
fail("Micro.blog said \(status): \(detail)")
|
||||
case let .forgotten(reason):
|
||||
forgot(reason)
|
||||
case .drafted, .updated:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// #206: Micro.blog lost the post and the piece forgot it — the
|
||||
/// sheet is back on a never-sent piece (a fresh draft is one click
|
||||
/// away).
|
||||
private func forgot(_ reason: String) {
|
||||
refusal = reason
|
||||
postURL = nil
|
||||
publishedAt = nil
|
||||
previewURL = nil
|
||||
phase = .idle
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user