oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "context"
5 "errors"
6 "sort"
7
8 "gorm.io/gorm"
9
10 "amadan.net/oficina/mapa/internal/mapa/notionmd"
11)
12
13// The write phase's working set (Notion import spec §8.1): every page
14// this run surveyed, with its staged tree, its coordinate and the title
15// planned for it; and every other row of the map, for links to pages
16// this run did not reach.
17
18// importEntry is one page this run surveyed.
19type importEntry struct {
20 row NotionPage
21 place coordinate
22 staged notionmd.Page
23 // title is the page's title for this run; planned what the plan
24 // first gave it, so the fix-up pass sees what moved (spec §13.6).
25 title, planned string
26 // live is a mapped page that still exists; current its record.
27 live bool
28 current Record
29 revision int64
30 result notionmd.Result
31 images *pageImages
32 // look is the icon and banner this run prepared, or nil when it
33 // prepared none (a fix-up after a Resume): the page keeps its own.
34 look *pageLook
35}
36
37// importSet is the run's pages in survey order, and the whole map.
38type importSet struct {
39 pages []*importEntry
40 byKey map[string]*importEntry
41 // others is every other row, by Notion key; titles caches the Mapa
42 // title of a mapped page this run did not survey.
43 others map[string]NotionPage
44 titles map[string]string
45 store *Store
46}
47
48// loadSet reads the map. Every row is opened: a few thousand seals.
49func (job *importJob) loadSet(ctx context.Context) (*importSet, error) {
50 var rows []NotionPage
51 err := job.store.database.WithContext(ctx).Order("id").
52 Find(&rows).Error
53 if err != nil {
54 return nil, err
55 }
56 set := &importSet{byKey: map[string]*importEntry{},
57 others: map[string]NotionPage{}, titles: map[string]string{},
58 store: job.store}
59 for _, row := range rows {
60 if err := set.add(ctx, job.lease.run, row); err != nil {
61 return nil, err
62 }
63 }
64 sort.SliceStable(set.pages, func(left, right int) bool {
65 return set.pages[left].staged.Seq < set.pages[right].staged.Seq
66 })
67 return set, nil
68}
69
70func (set *importSet) add(ctx context.Context, run int64,
71 row NotionPage) error {
72 place, err := set.store.openCoordinate(row)
73 if err != nil {
74 return err
75 }
76 key := notionKey(place.NotionID)
77 if row.SurveyedRun != run || len(row.SealedStaged) == 0 {
78 set.others[key] = row
79 return nil
80 }
81 if row.State == mapStaged {
82 place.Renamed = false // an earlier run's rename is not this one's
83 }
84 entry := &importEntry{row: row, place: place}
85 if entry.staged, err = set.store.openStaged(row); err != nil {
86 return err
87 }
88 if err := set.loadPage(ctx, entry); err != nil {
89 return err
90 }
91 set.pages = append(set.pages, entry)
92 set.byKey[key] = entry
93 return nil
94}
95
96// loadPage reads a mapped page's state and record.
97func (set *importSet) loadPage(ctx context.Context,
98 entry *importEntry) error {
99 if !entry.place.mapped(entry.row) {
100 return nil
101 }
102 page, found, err := set.store.anyPage(ctx, entry.place.PageID)
103 if err != nil || !found || page.Gone {
104 return err
105 }
106 entry.live, entry.revision = true, page.Revision
107 entry.current, err = set.store.OpenPage(page)
108 return err
109}
110
111// anyPage reads a page row by id, gone or not.
112func (store *Store) anyPage(ctx context.Context, id int64) (Page, bool,
113 error) {
114 var page Page
115 err := store.database.WithContext(ctx).Where("id = ?", id).
116 First(&page).Error
117 if errors.Is(err, gorm.ErrRecordNotFound) {
118 return page, false, nil
119 }
120 return page, err == nil, err
121}
122
123// Page is the mapper's resolver (spec §11.2): what a Notion id is to
124// this run, and its title when it is a live page.
125func (set *importSet) Page(id string) (string, notionmd.LinkState) {
126 key := notionKey(id)
127 if entry, ok := set.byKey[key]; ok {
128 if entry.row.State == mapFailed || entry.title == "" ||
129 (entry.place.mapped(entry.row) && !entry.live) {
130 return "", notionmd.Dead
131 }
132 return entry.title, notionmd.Live
133 }
134 row, ok := set.others[key]
135 if !ok {
136 return "", notionmd.Unshared
137 }
138 switch row.State {
139 case mapRow:
140 return "", notionmd.Row
141 case mapDatabase:
142 return "", notionmd.Database
143 }
144 return set.otherTitle(key, row)
145}
146
147// otherTitle is a mapped page this run did not survey: Live under its
148// current Mapa title if it still exists, else Dead.
149func (set *importSet) otherTitle(key string,
150 row NotionPage) (string, notionmd.LinkState) {
151 if title, ok := set.titles[key]; ok {
152 return title, liveIf(title)
153 }
154 place, err := set.store.openCoordinate(row)
155 title := ""
156 if err == nil && place.mapped(row) {
157 page, found, err := set.store.anyPage(context.Background(),
158 place.PageID)
159 if err == nil && found && !page.Gone {
160 record, _ := set.store.OpenPage(page)
161 title = record.Title
162 }
163 }
164 set.titles[key] = title
165 return title, liveIf(title)
166}
167
168func liveIf(title string) notionmd.LinkState {
169 if title == "" {
170 return notionmd.Dead
171 }
172 return notionmd.Live
173}
174