| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | |
| 8 | "gorm.io/gorm" |
| 9 | |
| 10 | "amadan.net/oficina/mapa/internal/mapa/notionmd" |
| 11 | "amadan.net/oficina/mapa/internal/mapa/seal" |
| 12 | ) |
| 13 | |
| 14 | // The map (Notion import spec §13.1, §15): one notion_pages row per |
| 15 | // Notion id the import has seen. Its plaintext is a print and a state; |
| 16 | // everything that joins it to a Mapa page is in the sealed coordinate. |
| 17 | |
| 18 | // Map row states. |
| 19 | const ( |
| 20 | // A page's: staged by Survey, then what Write did with it. |
| 21 | mapStaged = "staged" |
| 22 | mapCreated = "created" |
| 23 | mapUpdated = "updated" |
| 24 | mapUnchanged = "unchanged" |
| 25 | mapKept = "kept" |
| 26 | mapGone = "gone" |
| 27 | mapFailed = "failed" |
| 28 | // A page in the trash in Notion: skipped (spec §8.1). |
| 29 | mapTrashed = "trashed" |
| 30 | // Not pages: a database row shared directly, a database, and the |
| 31 | // workspace the map came from (spec §8.3, one workspace per map). |
| 32 | mapRow = "row" |
| 33 | mapDatabase = "database" |
| 34 | mapWorkspace = "workspace" |
| 35 | // A page of an import that was finished: kept for phase 2, and |
| 36 | // unmapped for any new import (spec §8.6). |
| 37 | mapFinished = "finished" |
| 38 | ) |
| 39 | |
| 40 | // workspaceKey is the workspace row's print input: no Notion id has it. |
| 41 | const workspaceKey = "workspace" |
| 42 | |
| 43 | // coordinate is a map row's sealed record. |
| 44 | type coordinate struct { |
| 45 | NotionID string `json:"id"` |
| 46 | // Title is the page's title as Notion last gave it (spec §9.2: |
| 47 | // titles already assigned are kept until Notion's changes). |
| 48 | Title string `json:"title,omitempty"` |
| 49 | PageID int64 `json:"page,omitempty"` |
| 50 | // Revision is the revision the import last wrote (spec §13.2). |
| 51 | Revision int64 `json:"revision,omitempty"` |
| 52 | Parent string `json:"parent,omitempty"` |
| 53 | // Fitted is set when the Mapa title differs from Notion's (§9.2); |
| 54 | // Renamed when this run's update renamed the page (§13.3). |
| 55 | Fitted bool `json:"fitted,omitempty"` |
| 56 | // UnderRow is a page inside a database row, imported without the |
| 57 | // row it sits in, and listed (spec §8.1). |
| 58 | UnderRow bool `json:"underRow,omitempty"` |
| 59 | Renamed bool `json:"renamed,omitempty"` |
| 60 | // Pin is set once the page, a root of the import, was offered for |
| 61 | // the runner's sidebar (columns and icons spec §6.3): Run is the run |
| 62 | // that offered it, and Pinned whether a slot was free. |
| 63 | Pin *pinMark `json:"pin,omitempty"` |
| 64 | // Render is the last render's counts, ids and losses (§8.5). |
| 65 | Render *notionmd.Result `json:"render,omitempty"` |
| 66 | // Kept says why a page was kept: edited, or only links updated. |
| 67 | Kept string `json:"kept,omitempty"` |
| 68 | // Workspace is the workspace row's identity; Name a database's. |
| 69 | Workspace string `json:"workspace,omitempty"` |
| 70 | Name string `json:"name,omitempty"` |
| 71 | // Images is how many of the page's images were imported, left as |
| 72 | // links, and failed by reason. ImageLines is the line written for |
| 73 | // each hosted image block, so the fix-up pass after a Resume can |
| 74 | // render the page without fetching (Fable L6). Reuse is |
| 75 | // notion_images' (import_images_place.go). |
| 76 | Images *imageCounts `json:"images,omitempty"` |
| 77 | ImageLines map[string]string `json:"imageLines,omitempty"` |
| 78 | } |
| 79 | |
| 80 | // mapped is a page row the import wrote, and whose page it still |
| 81 | // tracks: a finished import's rows are not, for a new import. |
| 82 | func (place coordinate) mapped(row NotionPage) bool { |
| 83 | return place.PageID != 0 && row.State != mapFinished |
| 84 | } |
| 85 | |
| 86 | // notionKey is the one spelling of a Notion id: lower case, no dashes. |
| 87 | // A page id in a URL and in an object differ by dashes alone. |
| 88 | func notionKey(id string) string { |
| 89 | return strings.ToLower(strings.ReplaceAll(id, "-", "")) |
| 90 | } |
| 91 | |
| 92 | // notionPrint is a Notion id's handle in the map. |
| 93 | func (store *Store) notionPrint(id string) (string, error) { |
| 94 | return store.sealer.Print(seal.PrintNotionPage, []byte(notionKey(id))) |
| 95 | } |
| 96 | |
| 97 | // mapRowOf reads the row for a Notion id, if there is one. |
| 98 | func (store *Store) mapRowOf(database *gorm.DB, id string) (NotionPage, |
| 99 | bool, error) { |
| 100 | fingerprint, err := store.notionPrint(id) |
| 101 | if err != nil { |
| 102 | return NotionPage{}, false, err |
| 103 | } |
| 104 | var row NotionPage |
| 105 | err = database.Where("notion_print = ?", fingerprint).First(&row).Error |
| 106 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 107 | return NotionPage{NotionPrint: fingerprint}, false, nil |
| 108 | } |
| 109 | return row, err == nil, err |
| 110 | } |
| 111 | |
| 112 | func (store *Store) sealCoordinate(place coordinate) ([]byte, error) { |
| 113 | plain, err := json.Marshal(place) |
| 114 | if err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | return store.sealer.Seal(seal.PurposeNotionCoordinate, plain) |
| 118 | } |
| 119 | |
| 120 | func (store *Store) openCoordinate(row NotionPage) (coordinate, error) { |
| 121 | var place coordinate |
| 122 | if len(row.SealedCoordinate) == 0 { |
| 123 | return place, nil |
| 124 | } |
| 125 | plain, err := store.sealer.Open(seal.PurposeNotionCoordinate, |
| 126 | row.SealedCoordinate) |
| 127 | if err != nil { |
| 128 | return place, err |
| 129 | } |
| 130 | return place, json.Unmarshal(plain, &place) |
| 131 | } |
| 132 | |
| 133 | func (store *Store) sealStaged(page notionmd.Page) ([]byte, error) { |
| 134 | plain, err := json.Marshal(page) |
| 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | return store.sealer.Seal(seal.PurposeNotionStaged, plain) |
| 139 | } |
| 140 | |
| 141 | func (store *Store) openStaged(row NotionPage) (notionmd.Page, error) { |
| 142 | var page notionmd.Page |
| 143 | plain, err := store.sealer.Open(seal.PurposeNotionStaged, |
| 144 | row.SealedStaged) |
| 145 | if err != nil { |
| 146 | return page, err |
| 147 | } |
| 148 | return page, json.Unmarshal(plain, &page) |
| 149 | } |
| 150 | |
| 151 | // saveRow writes a map row: created when new, else its state, code, |
| 152 | // runs and sealed parts as given. |
| 153 | func saveRow(database *gorm.DB, row *NotionPage) error { |
| 154 | if row.ID == 0 { |
| 155 | return database.Create(row).Error |
| 156 | } |
| 157 | return database.Model(row).Select("State", "ErrorCode", "SurveyedRun", |
| 158 | "SeenRun", "SealedCoordinate", "SealedStaged").Updates(row).Error |
| 159 | } |
| 160 | |