| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | ) |
| 7 | |
| 8 | // The import writes pages only through these two methods (Notion import |
| 9 | // spec §14.1, D15), each one Store.write: the lease check, the page and |
| 10 | // its map row land together or not at all, with their events. Neither |
| 11 | // merges, and only the run calls them (gate I17). |
| 12 | // |
| 13 | // When Mapa has images, both commit with attaching on, except |
| 14 | // ImportUpdate's renamed revision, which holds the previous body |
| 15 | // (images spec §3.2, §7). |
| 16 | |
| 17 | // ErrImportKept is ImportUpdate finding the page moved since the run |
| 18 | // decided: edited, renamed or deleted in Mapa. Nothing was written. |
| 19 | var ErrImportKept = errors.New("mapa: the page changed in Mapa; kept") |
| 20 | |
| 21 | // importWrite is what one import write needs. |
| 22 | type importWrite struct { |
| 23 | lease runLease |
| 24 | who string |
| 25 | row NotionPage |
| 26 | place coordinate |
| 27 | title string |
| 28 | body string |
| 29 | // icon and banner are the page's look (columns and icons spec |
| 30 | // §6.1): written by ImportCreate, and by ImportUpdate's styled |
| 31 | // revision when they changed. |
| 32 | icon *Icon |
| 33 | banner *Banner |
| 34 | // origin is Notion's created and edited times for the page. |
| 35 | origin Origin |
| 36 | // pageID and expect are ImportUpdate's: the page, and the revision |
| 37 | // the map says the import last wrote. |
| 38 | pageID, expect int64 |
| 39 | // images is where the page's images went, kept with the page. |
| 40 | images []placedImage |
| 41 | // state is the map row's state after ImportUpdate: updated, or, for |
| 42 | // a page the fix-up pass writes again, what the run made it (Fable |
| 43 | // L6: a page created this run stays "created"). |
| 44 | state string |
| 45 | } |
| 46 | |
| 47 | // ImportCreate makes a team page for a Notion page, as the runner, |
| 48 | // claiming its title through the namespace code CreateTeamPage uses, so |
| 49 | // a taken or date-shaped title is refused exactly as it is there. |
| 50 | func (store *Store) ImportCreate(ctx context.Context, |
| 51 | write importWrite) (Page, error) { |
| 52 | title, err := CleanTitle(write.title) |
| 53 | if err != nil { |
| 54 | return Page{}, err |
| 55 | } |
| 56 | var page Page |
| 57 | err = store.write(ctx, func(tx *Tx) error { |
| 58 | tx.importing = true |
| 59 | if err := holdLease(tx.db, write.lease); err != nil { |
| 60 | return err |
| 61 | } |
| 62 | actor, err := tx.member(write.who) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | origin := write.origin |
| 67 | page, err = tx.create(KindPage, RungTeam, actor, Record{Title: title, |
| 68 | Body: cleanBody(write.body), Origin: &origin, Imported: true, |
| 69 | Icon: write.icon, Banner: write.banner}, |
| 70 | actor, VerbCreated, attachOwn(0)) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | if err := crashPoint("create:page"); err != nil { |
| 75 | return err |
| 76 | } |
| 77 | if err := tx.claimTitle(page.ID, title); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | if err := crashPoint("create:title"); err != nil { |
| 81 | return err |
| 82 | } |
| 83 | return tx.placeRow(write, page, mapCreated) |
| 84 | }) |
| 85 | return page, err |
| 86 | } |
| 87 | |
| 88 | // ImportUpdate brings an untouched page up to Notion's version, as the |
| 89 | // runner. Inside its own write it re-reads the page: if its revision is |
| 90 | // not the one the map holds, or it is gone, it returns ErrImportKept and |
| 91 | // writes nothing (spec §13.2). A new title is its own revision first, |
| 92 | // logged renamed, holding exactly the previous body, with drafts on the |
| 93 | // old revision carried to it; a new look is the next, logged styled, |
| 94 | // holding the same body (columns and icons spec §6.4); a new body is |
| 95 | // the last, logged edited. |
| 96 | func (store *Store) ImportUpdate(ctx context.Context, |
| 97 | write importWrite) (Page, error) { |
| 98 | title, err := CleanTitle(write.title) |
| 99 | if err != nil { |
| 100 | return Page{}, err |
| 101 | } |
| 102 | var page Page |
| 103 | err = store.write(ctx, func(tx *Tx) error { |
| 104 | tx.importing = true |
| 105 | if err := holdLease(tx.db, write.lease); err != nil { |
| 106 | return err |
| 107 | } |
| 108 | actor, err := tx.member(write.who) |
| 109 | if err != nil { |
| 110 | return err |
| 111 | } |
| 112 | var record Record |
| 113 | if page, record, err = tx.importable(write); err != nil { |
| 114 | return err |
| 115 | } |
| 116 | if err := tx.importRename(&page, &record, actor, title); err != nil { |
| 117 | return err |
| 118 | } |
| 119 | if err := crashPoint("update:renamed"); err != nil { |
| 120 | return err |
| 121 | } |
| 122 | if err := tx.importStyle(&page, &record, actor, write); err != nil { |
| 123 | return err |
| 124 | } |
| 125 | if err := crashPoint("update:styled"); err != nil { |
| 126 | return err |
| 127 | } |
| 128 | if err := tx.importEdit(&page, &record, actor, write); err != nil { |
| 129 | return err |
| 130 | } |
| 131 | if err := crashPoint("update:edited"); err != nil { |
| 132 | return err |
| 133 | } |
| 134 | return tx.placeRow(write, page, firstOf(write.state, mapUpdated)) |
| 135 | }) |
| 136 | return page, err |
| 137 | } |
| 138 | |
| 139 | // importable is the page, if it is still the import's to update. |
| 140 | func (tx *Tx) importable(write importWrite) (Page, Record, error) { |
| 141 | var page Page |
| 142 | err := tx.db.Where("id = ?", write.pageID).First(&page).Error |
| 143 | if err != nil || page.Gone || page.Kind != KindPage || |
| 144 | page.Revision != write.expect { |
| 145 | return page, Record{}, ErrImportKept |
| 146 | } |
| 147 | record, err := tx.store.OpenPage(page) |
| 148 | return page, record, err |
| 149 | } |
| 150 | |
| 151 | func (tx *Tx) importRename(page *Page, record *Record, actor int64, |
| 152 | title string) error { |
| 153 | if record.Title == title { |
| 154 | return nil |
| 155 | } |
| 156 | if Fold(title) != Fold(record.Title) { |
| 157 | if err := tx.retitle(page.ID, record.Title, title); err != nil { |
| 158 | return err |
| 159 | } |
| 160 | } |
| 161 | from := page.Revision |
| 162 | record.Title, record.Imported = title, true |
| 163 | // The renamed revision holds the previous body: it attaches nothing |
| 164 | // (images spec §3.2). |
| 165 | if err := tx.commit(page, *record, actor, VerbRenamed, |
| 166 | attachNothing); err != nil { |
| 167 | return err |
| 168 | } |
| 169 | return tx.carryDrafts(page.ID, from, page.Revision) |
| 170 | } |
| 171 | |
| 172 | // importStyle is the styled revision (columns and icons spec §6.4): |
| 173 | // the new icon and banner over the body as it stands, attaching the |
| 174 | // run's own images, with drafts carried as a rename's are. |
| 175 | func (tx *Tx) importStyle(page *Page, record *Record, actor int64, |
| 176 | write importWrite) error { |
| 177 | next := *record |
| 178 | next.Icon, next.Banner, next.Imported = write.icon, write.banner, true |
| 179 | if sameLook(*record, next) { |
| 180 | return nil |
| 181 | } |
| 182 | from := page.Revision |
| 183 | if err := tx.commit(page, next, actor, VerbStyled, |
| 184 | attachOwn(0)); err != nil { |
| 185 | return err |
| 186 | } |
| 187 | *record = next |
| 188 | return tx.carryDrafts(page.ID, from, page.Revision) |
| 189 | } |
| 190 | |
| 191 | func (tx *Tx) importEdit(page *Page, record *Record, actor int64, |
| 192 | write importWrite) error { |
| 193 | origin := write.origin |
| 194 | body := cleanBody(write.body) |
| 195 | if record.Body == body { |
| 196 | return nil |
| 197 | } |
| 198 | record.Body, record.Origin, record.Imported = body, &origin, true |
| 199 | return tx.commit(page, *record, actor, VerbEdited, attachOwn(0)) |
| 200 | } |
| 201 | |
| 202 | // placeRow writes the map row with the page: its coordinate names the |
| 203 | // page and the last revision the import wrote. |
| 204 | func (tx *Tx) placeRow(write importWrite, page Page, state string) error { |
| 205 | place := write.place |
| 206 | place.PageID, place.Revision = page.ID, page.Revision |
| 207 | sealed, err := tx.store.sealCoordinate(place) |
| 208 | if err != nil { |
| 209 | return err |
| 210 | } |
| 211 | row := write.row |
| 212 | row.State, row.ErrorCode, row.SealedCoordinate = state, "", sealed |
| 213 | err = tx.db.Model(&row).Select("State", "ErrorCode", |
| 214 | "SealedCoordinate").Updates(&row).Error |
| 215 | if err != nil { |
| 216 | return err |
| 217 | } |
| 218 | return keepPlaces(tx.db, tx.store, page.ID, write.images) |
| 219 | } |
| 220 | |