CI / ci (push) Canceled after 0s
#179: every library-reflecting store write (draft save, session-born post, rename, move to project, archive/restore, trash, convert, project creation, import batch, pass advance) now rides one internal seam — performWrite pairs the write with exactly one rescan and owns the error strip (cleared BEFORE the write so importFiles' per-file failure messages survive the success tail; a nil-Home refusal writes nothing). The model owns the Home: watchedHome is private(set), set only by bind (construction + the Settings repoint); no public method takes a Home parameter — 14 view call sites and ~115 test call sites swept, dead home plumbing cut. The editor's debounced flush, the card closes, the pass substrate and the micropub publish stay off the rail by design (own error semantics, no library regroup). A converged review finding is closed with a guard: the pass advance re-resolves watchedHome after the engine's minutes-long turn, so a mid-pass Settings repoint now refuses the advance with an honest strip message instead of silently stamping pass state into the repointed library (wrong-piece write on a slug collision). createProject failures now surface in the sheet AND the strip — the rail-owns-the- strip posture supersedes the #108 sheet-only rule. Five rail pins (PostsModelWriteRailTests, suite members): write → one library refresh + strip cleared, failed write owns the strip, nil-Home refusal, import batch keeps per-file messages, mid-pass repoint refuses the advance — the last two flip-verified against their guards. ScriptedEngine gains endHeldPrompt (release a held turn as ended). Verification: suite 657/657 the evening this landed; the next-morning re-run could not start — both test runners hung before establishing connection (#134 testmanagerd wedge; an untouched control suite hangs identically — machine state, not this diff; reboot pending). Rail pins 5/5 isolated after the last test-only edit; swiftlint at baseline parity; swiftformat clean. Closes #179 [Generated by AI]
212 lines
6.5 KiB
Swift
212 lines
6.5 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 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
|
|
@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(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
|
|
}
|
|
}
|
|
}
|
|
}
|