oficina / mapa Public

Pages inside (WIP): the import pins its roots once, marked in the sealed coordinate; the report says how many; I23

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev f5ed75bd1e14666ad91d202388f3d6738c8b0b0a parent 487b4fd
7 files changed, +217 −2
  • internal/mapa/import_ledger.go +4 −0
  • internal/mapa/import_pins.go +100 −0
  • internal/mapa/import_pins_test.go +89 −0
  • internal/mapa/import_report.go +14 −0
  • internal/mapa/import_write.go +6 −0
  • internal/mapa/inside_test.go +2 −2
  • internal/mapa/templates/import.html +2 −0
diff --git a/internal/mapa/import_ledger.go b/internal/mapa/import_ledger.go
index b75eb3b..ebcdab4 100644
--- a/internal/mapa/import_ledger.go
+++ b/internal/mapa/import_ledger.go
@@ -57,6 +57,10 @@ type coordinate struct {
// row it sits in, and listed (spec §8.1).
UnderRow bool `json:"underRow,omitempty"`
Renamed bool `json:"renamed,omitempty"`
+ // Pin is set once the page, a root of the import, was offered for
+ // the runner's sidebar (columns and icons spec §6.3): Run is the run
+ // that offered it, and Pinned whether a slot was free.
+ Pin *pinMark `json:"pin,omitempty"`
// Render is the last render's counts, ids and losses (§8.5).
Render *notionmd.Result `json:"render,omitempty"`
// Kept says why a page was kept: edited, or only links updated.
diff --git a/internal/mapa/import_pins.go b/internal/mapa/import_pins.go
new file mode 100644
index 0000000..5f23fd7
--- /dev/null
+++ b/internal/mapa/import_pins.go
@@ -0,0 +1,100 @@
+package mapa
+
+import (
+ "context"
+)
+
+// The import's pins (columns and icons spec §6.3): the roots of the
+// import, pages with no imported parent and so no "In:" line, are pinned
+// to the runner's own sidebar, once each, so Pinned resembles the top of
+// their Notion sidebar. Pins are per person and sealed (level-up Q1):
+// nobody else's sidebar changes.
+
+// pinMark is a root's sealed record of its offer.
+type pinMark struct {
+ Run int64 `json:"run"`
+ Pinned bool `json:"pinned"`
+}
+
+// autoPin pins the roots never offered before, in survey order, after
+// the runner's own pins, up to the slots free, in one write that marks
+// each offered root and logs one pins event by the runner. A root with
+// no slot is marked too, and listed. Nothing to offer writes nothing.
+func (job *importJob) autoPin(ctx context.Context, set *importSet) error {
+ roots := job.unofferedRoots(set)
+ if len(roots) == 0 {
+ return nil
+ }
+ return job.store.write(ctx, func(tx *Tx) error {
+ if err := holdLease(tx.db, job.lease); err != nil {
+ return err
+ }
+ memberID, err := tx.member(job.who)
+ if err != nil {
+ return err
+ }
+ var member Member
+ if err := tx.db.First(&member, memberID).Error; err != nil {
+ return err
+ }
+ ids, err := tx.store.openPins(member.SealedPins)
+ if err != nil {
+ return err
+ }
+ for _, entry := range roots {
+ pinned := len(ids) < maxPins && !hasPin(ids, entry.place.PageID)
+ if pinned {
+ ids = append(ids, entry.place.PageID)
+ }
+ entry.place.Pin = &pinMark{Run: job.lease.run, Pinned: pinned}
+ if err := tx.markPlace(entry); err != nil {
+ return err
+ }
+ }
+ return tx.savePins(memberID, ids)
+ })
+}
+
+// unofferedRoots is the run's live roots that carry no pin mark, in
+// survey order.
+func (job *importJob) unofferedRoots(set *importSet) []*importEntry {
+ var roots []*importEntry
+ for _, entry := range set.pages {
+ if entry.place.Pin != nil || entry.place.PageID == 0 ||
+ set.parentTitle(entry) != "" || !entry.live {
+ continue
+ }
+ roots = append(roots, entry)
+ }
+ return roots
+}
+
+func hasPin(ids []int64, pageID int64) bool {
+ for _, id := range ids {
+ if id == pageID {
+ return true
+ }
+ }
+ return false
+}
+
+// markPlace marks an entry's stored coordinate with its pin mark: read
+// again inside the write, since the page's own transaction wrote it
+// last, and the run's copy of it is not that one.
+func (tx *Tx) markPlace(entry *importEntry) error {
+ var row NotionPage
+ if err := tx.db.First(&row, entry.row.ID).Error; err != nil {
+ return err
+ }
+ place, err := tx.store.openCoordinate(row)
+ if err != nil {
+ return err
+ }
+ place.Pin = entry.place.Pin
+ sealed, err := tx.store.sealCoordinate(place)
+ if err != nil {
+ return err
+ }
+ return tx.db.Model(&NotionPage{}).Where("id = ?", row.ID).
+ Update("sealed_coordinate", sealed).Error
+}
diff --git a/internal/mapa/import_pins_test.go b/internal/mapa/import_pins_test.go
new file mode 100644
index 0000000..4c1c13d
--- /dev/null
+++ b/internal/mapa/import_pins_test.go
@@ -0,0 +1,89 @@
+package mapa
+
+import (
+ "strings"
+ "testing"
+
+ "amadan.net/oficina/notion.git/notiontest"
+)
+
+// I23 (columns and icons spec §6.3, §9): the import pins its roots to
+// the runner's sidebar, after their own pins, once; a re-run pins
+// nothing; a root the person unpinned stays unpinned; nobody else's
+// pins change.
+
+func pinTitles(t *testing.T, world *importWorld, who string) []string {
+ t.Helper()
+ viewer, err := world.store.Viewer(ctx(), who, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ ids, err := world.store.Pins(ctx(), viewer)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var titles []string
+ for _, id := range ids {
+ var page Page
+ if err := world.store.database.First(&page, id).Error; err != nil {
+ t.Fatal(err)
+ }
+ record, err := world.store.OpenPage(page)
+ if err != nil {
+ t.Fatal(err)
+ }
+ titles = append(titles, record.Title)
+ }
+ return titles
+}
+
+func TestI23TheImportPinsItsRootsOnce(t *testing.T) {
+ doc := notiontest.NewDocumented()
+ world := newImportWorld(t, doc.Workspace)
+ mine := mustCreate(t, world.store, "Mine", "Text.")
+ viewer, _ := world.store.Viewer(ctx(), ana, nil)
+ if err := world.store.SetPin(ctx(), viewer, mine.ID, true); err != nil {
+ t.Fatal(err)
+ }
+ world.connect(ana)
+ run := world.importAs(ana)
+ if run.State != runDone {
+ t.Fatalf("first run: %+v", run)
+ }
+ pins := pinTitles(t, world, ana)
+ report, err := world.store.report(ctx(), run)
+ if err != nil || report.Pinned != len(pins)-1 ||
+ report.Offered != report.Pinned {
+ t.Fatalf("the report's pins: %+v %v", report, err)
+ }
+ if len(pins) < 2 || pins[0] != "Mine" {
+ t.Fatalf("Ana's pins: %v", pins)
+ }
+ for _, title := range pins[1:] {
+ _, record, ok := world.pageTitled(title)
+ if !ok || strings.HasPrefix(record.Body, "In: ") {
+ t.Errorf("pinned %q, which is not a root", title)
+ }
+ }
+ if got := pinTitles(t, world, bo); len(got) != 0 {
+ t.Fatalf("Bo's pins changed: %v", got)
+ }
+ root, _, _ := world.pageTitled(pins[1])
+ viewer, _ = world.store.Viewer(ctx(), ana, nil)
+ if err := world.store.SetPin(ctx(), viewer, root.ID, false); err != nil {
+ t.Fatal(err)
+ }
+ events := world.count("events", "verb = ?", VerbPinsSet)
+ world.server.Reset()
+ if run := world.importAs(ana); run.State != runDone {
+ t.Fatalf("second run: %+v", run)
+ }
+ if world.count("events", "verb = ?", VerbPinsSet) != events {
+ t.Fatal("a re-run wrote the pins again")
+ }
+ for _, title := range pinTitles(t, world, ana) {
+ if title == pins[1] {
+ t.Fatal("a root Ana unpinned was pinned again")
+ }
+ }
+}
diff --git a/internal/mapa/import_report.go b/internal/mapa/import_report.go
index 24ed578..fa680ad 100644
--- a/internal/mapa/import_report.go
+++ b/internal/mapa/import_report.go
@@ -28,6 +28,11 @@ type reportView struct {
Fallbacks, Neutralised, Unshared, Notion int
Fitted, Kept, FailedPages, NotFoundPages []reportPage
Placeholders, UnderRow []reportPage
+ // Pinned and Unpinned are the roots this run offered to the
+ // runner's sidebar, read from their sealed pin marks (columns and
+ // icons spec §6.3): pinned, and those that found no free slot.
+ Pinned, Offered int
+ Unpinned []reportPage
}
type countView struct {
@@ -106,6 +111,15 @@ func (tally *reportTally) add(entry reportRow) {
if row.SurveyedRun == tally.run {
tally.outcome(entry)
}
+ if mark := place.Pin; mark != nil && mark.Run == tally.run {
+ tally.view.Offered++
+ if mark.Pinned {
+ tally.view.Pinned++
+ } else {
+ tally.view.Unpinned = append(tally.view.Unpinned,
+ tally.page(place))
+ }
+ }
}
// outcome counts what this run did with a page.
diff --git a/internal/mapa/import_write.go b/internal/mapa/import_write.go
index 5317157..e9d6aff 100644
--- a/internal/mapa/import_write.go
+++ b/internal/mapa/import_write.go
@@ -38,6 +38,12 @@ func (job *importJob) write(ctx context.Context) error {
if err := crashPoint("write:fixed"); err != nil {
return err
}
+ if err := job.autoPin(ctx, set); err != nil {
+ return err
+ }
+ if err := crashPoint("write:pinned"); err != nil {
+ return err
+ }
return job.done(ctx)
}
diff --git a/internal/mapa/inside_test.go b/internal/mapa/inside_test.go
index 3d7538a..9841273 100644
--- a/internal/mapa/inside_test.go
+++ b/internal/mapa/inside_test.go
@@ -39,7 +39,8 @@ func TestIN1OnlyAnExactFirstInLineIsInside(t *testing.T) {
} {
mustCreate(t, store, title, body)
}
- if _, err := store.Retitle(ctx(), ana, parent.ID, "Old projects"); err != nil {
+ _, err := store.Retitle(ctx(), ana, parent.ID, "Old projects")
+ if err != nil {
t.Fatal(err)
}
if _, err := store.Retitle(ctx(), ana, parent.ID, "Projects"); err != nil {
@@ -49,7 +50,6 @@ func TestIN1OnlyAnExactFirstInLineIsInside(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- _ = scratch
viewer, _ := store.Viewer(ctx(), ana, nil)
got := insideTitles(t, index, ScopeOf(viewer), parent.ID)
if got != "Beta, Alpha, Gamma, Old title" {
diff --git a/internal/mapa/templates/import.html b/internal/mapa/templates/import.html
index 1f4f1cd..5ecaf27 100644
--- a/internal/mapa/templates/import.html
+++ b/internal/mapa/templates/import.html
@@ -56,6 +56,8 @@
<h2 id="import-report">Report</h2>
{{range .ImageFailures}}{{if eq .Label "images aren't set up in Mapa yet"}}<p mapa-notice role="status"><strong>No images were imported: images aren't set up in Mapa yet.</strong> Each one is a line in its page. Run the import again once images are set up.</p>{{end}}{{if hasSuffix .Label "allowance is full"}}<p mapa-notice role="status"><strong>Images stopped: {{.Label}}.</strong> Raise that allowance, or free some, and run the import again: the pages waiting for images are filled in.</p>{{end}}{{end}}
{{with .Roots}}<p>Started from: {{range $i, $r := .}}{{if $i}}, {{end}}“{{$r}}”{{end}}.</p>{{end}}
+{{if or .Pinned .Unpinned}}<p mapa-import-pins>{{if .Unpinned}}Pinned {{number L .Pinned}} of your {{number L .Offered}} top-level pages. Your sidebar holds {{number L 20}} pins.{{else}}Pinned your {{number L .Pinned}} top-level pages to your sidebar.{{end}}</p>
+{{with .Unpinned}}<ul mapa-list>{{range .}}<li>{{if .Href}}<a href="{{.Href}}">{{.Title}}</a>{{else}}{{.Title}}{{end}}</li>{{end}}</ul>{{end}}{{end}}
<dl rst-detail>
<dt>Pages seen</dt><dd>{{number L .Seen}}</dd>
<dt>Created</dt><dd>{{number L .Created}}</dd>