feat(library): projects & archive substrate — S1 (#107)
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
Pieces re-root from posts/<id>/ to projects/<name>/<id>/ with an archive/ twin root (ADR-0012): scan walks both levels, New Piece and Import land in the selected project else the default posts, the slug namespace is global across projects and archive (an archived slug is never reused), and every piece carries type: post frontmatter — stamped by New/Import, read leniently (absent or unknown reads as post), the file stays post.md for every content type. Write paths resolve the folder from the id alone (folder(of:)); sessions and sidecars never thread (project, id). Finish-audit hardening: duplicate ids on disk (Finder copy) dedupe in scan to the same first-match folder(of:) writes to; folder(of:) skips hidden/symlinked projects like scan does. Dev posts moved by hand to projects/posts/ — no migration machinery, the app never reads the old posts/ root (#103). Full suite green (128 unit + UI), swiftlint clean on changed files. Closes #107
This commit is contained in:
1 parent
50eecc6dcb
commit
3ca9d4e601
17 files changed
+608
-197
No files matched your search
+41
-24
@@ -2,9 +2,10 @@
|
||||
// PostsModel.swift
|
||||
// Museion
|
||||
//
|
||||
// #18 (S1.1): the Posts pane's write side — FSEvents watcher on posts/ with
|
||||
// a ~0.5 s debounced rescan (#61), selection, and the New Post draft
|
||||
// lifecycle (#62). Own ops route through the same rescan: one path.
|
||||
// #18 (S1.1): the Posts pane's write side — FSEvents watcher on the piece
|
||||
// roots (projects/ + archive/, #107) with a ~0.5 s debounced rescan (#61),
|
||||
// selection, and the New Post draft lifecycle (#62). Own ops route through
|
||||
// the same rescan: one path.
|
||||
//
|
||||
|
||||
import CoreServices
|
||||
@@ -12,20 +13,23 @@ import Foundation
|
||||
import Observation
|
||||
|
||||
/// The New Post draft: body text only, no folder until first save (#62).
|
||||
/// #107: remembers the project the piece will land in when first saved.
|
||||
struct PostDraft {
|
||||
var text = "# Untitled\n\n"
|
||||
let project: String
|
||||
}
|
||||
|
||||
/// Recursive FSEvents stream on one directory (#61): file-level events, so
|
||||
/// nested `posts/<id>/post.md` edits from Finder fire too — a plain dir-FD
|
||||
/// dispatch source only sees direct children and misses them.
|
||||
/// Recursive FSEvents stream on one or more directories (#61): file-level
|
||||
/// events, so nested `projects/<name>/<id>/post.md` edits from Finder fire
|
||||
/// too — a plain dir-FD dispatch source only sees direct children and misses
|
||||
/// them.
|
||||
final nonisolated class PostsWatcher: @unchecked Sendable {
|
||||
/// Fired on the main actor when anything under the watched path changes.
|
||||
/// Fired on the main actor when anything under a watched path changes.
|
||||
var onEvent: (@MainActor @Sendable () -> Void)?
|
||||
|
||||
private var stream: FSEventStreamRef?
|
||||
|
||||
func start(url: URL) {
|
||||
func start(urls: [URL]) {
|
||||
stop()
|
||||
var context = FSEventStreamContext(
|
||||
version: 0,
|
||||
@@ -43,7 +47,7 @@ final nonisolated class PostsWatcher: @unchecked Sendable {
|
||||
kCFAllocatorDefault,
|
||||
callback,
|
||||
&context,
|
||||
[url.path] as CFArray,
|
||||
urls.map(\.path) as CFArray,
|
||||
FSEventStreamEventId(kFSEventStreamEventIdSinceNow),
|
||||
0.5, // seconds; FSEvents coalesces within this latency (the #61 window)
|
||||
FSEventStreamCreateFlags(kFSEventStreamCreateFlagFileEvents),
|
||||
@@ -69,7 +73,8 @@ final nonisolated class PostsWatcher: @unchecked Sendable {
|
||||
@MainActor
|
||||
@Observable
|
||||
final class PostsModel {
|
||||
/// The scanned, title-sorted list — mirrors `posts/` at all times.
|
||||
/// The scanned, title-sorted list — mirrors `projects/` + `archive/` at
|
||||
/// all times (#107).
|
||||
private(set) var posts: [Post] = []
|
||||
|
||||
/// The selected Post's id; nil = nothing selected (empty editor).
|
||||
@@ -101,9 +106,9 @@ final class PostsModel {
|
||||
/// check reused verbatim for foreign files.
|
||||
private var externalWatcher = PostsWatcher()
|
||||
|
||||
/// Bind a Home: ensure `posts/` exists (FSEvents on a missing path is
|
||||
/// unreliable; the AC's lazy rule is about post folders, not this root),
|
||||
/// initial scan + start (or restart) the watcher. Called from
|
||||
/// Bind a Home: ensure both container roots exist (FSEvents on a missing
|
||||
/// path is unreliable; archive/ is a watcher root now, #107), initial
|
||||
/// scan + start (or restart) the watcher. Called from
|
||||
/// `.task(id: home)` — re-fires on Home repoint.
|
||||
func bind(home: URL) {
|
||||
startWatching(home: home)
|
||||
@@ -115,12 +120,12 @@ final class PostsModel {
|
||||
private func startWatching(home: URL) {
|
||||
guard watchedHome != home else { return }
|
||||
watchedHome = home
|
||||
try? FileManager.default.createDirectory(
|
||||
at: home.appendingPathComponent("posts", isDirectory: true),
|
||||
withIntermediateDirectories: true,
|
||||
)
|
||||
let roots = Post.containerRoots.map { home.appendingPathComponent($0, isDirectory: true) }
|
||||
for root in roots {
|
||||
try? FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
|
||||
}
|
||||
watcher.onEvent = { [weak self] in Task { await self?.rescan() } }
|
||||
watcher.start(url: home.appendingPathComponent("posts", isDirectory: true))
|
||||
watcher.start(urls: roots)
|
||||
}
|
||||
|
||||
/// Off-main scan; keeps selection valid across the fresh refresh.
|
||||
@@ -158,6 +163,13 @@ final class PostsModel {
|
||||
// writes and same-volume moves; if a big Home ever stalls the UI, wrap
|
||||
// them in Task.detached like rescan.
|
||||
|
||||
/// #107: the project a NEW piece lands in — the selected non-archived
|
||||
/// post's project, else the default. One home; newPost/importFiles must
|
||||
/// not drift.
|
||||
private var targetProject: String {
|
||||
posts.first(where: { $0.id == selectedPostID && !$0.isArchived })?.project ?? Post.defaultProject
|
||||
}
|
||||
|
||||
/// Tear the watcher down on the main actor (its scheduling thread) —
|
||||
/// deinit of a @MainActor-held object may run elsewhere. #64: app quit
|
||||
/// flushes the open session first (sidecar rides along). #96: quit also
|
||||
@@ -170,11 +182,14 @@ final class PostsModel {
|
||||
}
|
||||
|
||||
/// New Post (#62): fresh in-memory draft, no folder, editor takes it.
|
||||
/// #107: the draft targets the selected post's project (archived posts
|
||||
/// target the default — new pieces are never born archived).
|
||||
func newPost() {
|
||||
let target = targetProject
|
||||
dismissExternal() // #92: opening a draft dismisses the guest
|
||||
session?.teardown(flushFirst: true) // #64: selection change flushes
|
||||
session = nil
|
||||
draft = PostDraft()
|
||||
draft = PostDraft(project: target)
|
||||
selectedPostID = nil
|
||||
}
|
||||
|
||||
@@ -194,7 +209,7 @@ final class PostsModel {
|
||||
func saveDraft(home: URL) {
|
||||
guard let draft else { return }
|
||||
do {
|
||||
let post = try PostStore.writeNew(body: draft.text, home: home)
|
||||
let post = try PostStore.writeNew(body: draft.text, project: draft.project, home: home)
|
||||
self.draft = nil
|
||||
selectedPostID = post.id
|
||||
session = EditorSession(postID: post.id, home: home)
|
||||
@@ -302,11 +317,11 @@ final class PostsModel {
|
||||
/// main actor already; no re-hop needed.
|
||||
private func startExternalWatch(url: URL) {
|
||||
externalWatcher.onEvent = { [weak self] in
|
||||
guard let self, let session = self.externalSession else { return }
|
||||
guard let self, let session = externalSession else { return }
|
||||
guard let disk = try? String(contentsOf: url, encoding: .utf8) else { return }
|
||||
session.diskDidChange(diskText: disk)
|
||||
}
|
||||
externalWatcher.start(url: url.deletingLastPathComponent())
|
||||
externalWatcher.start(urls: [url.deletingLastPathComponent()])
|
||||
}
|
||||
|
||||
/// #94: stage a multi-file import — the ONE Origin ask presents; the
|
||||
@@ -325,15 +340,17 @@ final class PostsModel {
|
||||
/// a Post; a per-file failure continues the batch into lastError (stated
|
||||
/// default, reversible; never an alert, never an abort). The last
|
||||
/// imported post opens; a failure anywhere keeps its message up even
|
||||
/// when other files landed.
|
||||
/// when other files landed. #107: the batch lands in the selected
|
||||
/// post's project (newPost's target rule).
|
||||
func importFiles(origin: Post.Origin, home: URL) {
|
||||
guard let urls = pendingImportURLs else { return }
|
||||
pendingImportURLs = nil
|
||||
let project = targetProject
|
||||
var last: Post?
|
||||
var failures = 0
|
||||
for url in urls {
|
||||
do {
|
||||
last = try PostStore.importFile(at: url, origin: origin, home: home)
|
||||
last = try PostStore.importFile(at: url, origin: origin, project: project, home: home)
|
||||
} catch {
|
||||
failures += 1
|
||||
lastError = "Import failed — \(url.lastPathComponent): \(error.localizedDescription)"
|
||||
|
||||
Reference in new issue
Block a user