| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | |
| 7 | "gorm.io/gorm" |
| 8 | |
| 9 | "amadan.net/oficina/mapa/internal/mapa/notionmd" |
| 10 | "amadan.net/oficina/mapa/internal/mapa/seal" |
| 11 | "amadan.net/oficina/notion.git/notion" |
| 12 | ) |
| 13 | |
| 14 | // notion_images (Notion import spec §12, §15): where each Notion-hosted |
| 15 | // image block went on each page, sealed. A place is keyed by the block |
| 16 | // and the Notion page it sits on, which the map pins to one Mapa page, |
| 17 | // so it is known before a new page exists; the block alone finds it on |
| 18 | // another page, for a copy. |
| 19 | |
| 20 | // placedImage is one block's outcome on one page, before it is kept. |
| 21 | type placedImage struct { |
| 22 | page, block, key string |
| 23 | publicID, reason string |
| 24 | permanent bool |
| 25 | // look marks an icon's or a cover's place (columns and icons spec |
| 26 | // §6.1), printed in its own domain (lookPrints). |
| 27 | look bool |
| 28 | } |
| 29 | |
| 30 | // imagePlace is a notion_images row's sealed record. Key is the file's |
| 31 | // notion.FileKey when the place was made. A row written before keys |
| 32 | // carries "edited" instead, the block's minute-rounded edit time, which |
| 33 | // nothing reads now: its empty Key matches nothing, so its file is |
| 34 | // fetched once more and the row rewritten with a key. |
| 35 | type imagePlace struct { |
| 36 | Block string `json:"block"` |
| 37 | Key string `json:"key,omitempty"` |
| 38 | Page int64 `json:"page,omitempty"` |
| 39 | PublicID string `json:"image,omitempty"` |
| 40 | Reason string `json:"reason,omitempty"` |
| 41 | } |
| 42 | |
| 43 | // Image place states. |
| 44 | const ( |
| 45 | imageImported = "imported" |
| 46 | imageRefused = "refused" |
| 47 | ) |
| 48 | |
| 49 | // imagePrints are a block's two handles: on a page, and alone. |
| 50 | func (store *Store) imagePrints(page, block string) (string, string, |
| 51 | error) { |
| 52 | place, err := store.sealer.Print(seal.PrintNotionImage, |
| 53 | []byte("place\x00"+notionKey(page)+"\x00"+notionKey(block))) |
| 54 | if err != nil { |
| 55 | return "", "", err |
| 56 | } |
| 57 | alone, err := store.sealer.Print(seal.PrintNotionImage, |
| 58 | []byte("block\x00"+notionKey(block))) |
| 59 | return place, alone, err |
| 60 | } |
| 61 | |
| 62 | // lookPrints are an icon's or a cover's handles: one print, in a domain |
| 63 | // of its own, for both, so a look row never shares a block's place and |
| 64 | // its block print finds no other row (Fable on phase 4, L4). |
| 65 | func (store *Store) lookPrints(page, pseudo string) (string, string, |
| 66 | error) { |
| 67 | place, err := store.sealer.Print(seal.PrintNotionImage, |
| 68 | []byte("look\x00"+notionKey(page)+"\x00"+pseudo)) |
| 69 | return place, place, err |
| 70 | } |
| 71 | |
| 72 | // printsOf is a placed image's handles. |
| 73 | func (store *Store) printsOf(placed placedImage) (string, string, error) { |
| 74 | if placed.look { |
| 75 | return store.lookPrints(placed.page, placed.block) |
| 76 | } |
| 77 | return store.imagePrints(placed.page, placed.block) |
| 78 | } |
| 79 | |
| 80 | func (store *Store) openPlace(row NotionImage) (imagePlace, error) { |
| 81 | var place imagePlace |
| 82 | plain, err := store.sealer.Open(seal.PurposeNotionImage, |
| 83 | row.SealedRecord) |
| 84 | if err != nil { |
| 85 | return place, err |
| 86 | } |
| 87 | return place, json.Unmarshal(plain, &place) |
| 88 | } |
| 89 | |
| 90 | // priorLook is where a page's icon or cover went last time. |
| 91 | func (job *importJob) priorLook(ctx context.Context, page, |
| 92 | pseudo string) (imagePlace, bool, error) { |
| 93 | place, _, err := job.store.lookPrints(page, pseudo) |
| 94 | if err != nil { |
| 95 | return imagePlace{}, false, err |
| 96 | } |
| 97 | return job.placeAt(ctx, place) |
| 98 | } |
| 99 | |
| 100 | // priorPlace is where the block went on this page last time. |
| 101 | func (job *importJob) priorPlace(ctx context.Context, page, |
| 102 | block string) (imagePlace, bool, error) { |
| 103 | place, _, err := job.store.imagePrints(page, block) |
| 104 | if err != nil { |
| 105 | return imagePlace{}, false, err |
| 106 | } |
| 107 | return job.placeAt(ctx, place) |
| 108 | } |
| 109 | |
| 110 | // placeAt is the notion_images row at a place print, opened. |
| 111 | func (job *importJob) placeAt(ctx context.Context, |
| 112 | place string) (imagePlace, bool, error) { |
| 113 | row, found := notionImageOf(job.store.database.WithContext(ctx), place) |
| 114 | if !found { |
| 115 | return imagePlace{}, false, nil |
| 116 | } |
| 117 | prior, err := job.store.openPlace(row) |
| 118 | return prior, err == nil, err |
| 119 | } |
| 120 | |
| 121 | // copyImage copies the block's image from another page that still holds |
| 122 | // it, where the runner may read it (spec §12: one image, one place), |
| 123 | // and only from a place made of the file the block shows now: a file |
| 124 | // replaced since is downloaded afresh, once, and the other page copies |
| 125 | // that (Fable round 3, M2). The file's key decides, not the block's |
| 126 | // minute-rounded edit time (§25 note 17). |
| 127 | func (job *importJob) copyImage(ctx context.Context, entry *importEntry, |
| 128 | block notionmd.Block) (AddedImage, bool) { |
| 129 | _, alone, err := job.store.imagePrints(entry.staged.ID, block.ID) |
| 130 | if err != nil { |
| 131 | return nil, false |
| 132 | } |
| 133 | key := notion.FileKey(block.File.URL) |
| 134 | var rows []NotionImage |
| 135 | job.store.database.WithContext(ctx).Where("block_print = ? AND "+ |
| 136 | "state = ?", alone, imageImported).Find(&rows) |
| 137 | for _, row := range rows { |
| 138 | other, err := job.store.openPlace(row) |
| 139 | if err != nil || other.Page == 0 || !sameFile(other.Key, key) || |
| 140 | other.Page == entry.place.PageID || |
| 141 | !job.images.Held(ctx, other.PublicID, other.Page) { |
| 142 | continue |
| 143 | } |
| 144 | if added, err := job.images.Copy(ctx, job.who, |
| 145 | other.PublicID); err == nil { |
| 146 | return added, true |
| 147 | } |
| 148 | } |
| 149 | return nil, false |
| 150 | } |
| 151 | |
| 152 | // recordImages keeps where an unchanged page's images went, as |
| 153 | // bookkeeping: nothing of the page is written. A page that is written |
| 154 | // keeps its images' places in its own transaction (placeRow), so a crash |
| 155 | // between the two cannot lose them (Fable round 3, L1). |
| 156 | func (job *importJob) recordImages(ctx context.Context, entry *importEntry, |
| 157 | prepared *pageImages) error { |
| 158 | return job.store.bookkeep(ctx, job.lease, func(db *gorm.DB) error { |
| 159 | return keepPlaces(db, job.store, entry.place.PageID, |
| 160 | prepared.placed) |
| 161 | }) |
| 162 | } |
| 163 | |
| 164 | // keepPlaces keeps where each image went. A transient refusal keeps |
| 165 | // nothing, so the next run tries again; a permanent one is kept with the |
| 166 | // file's key, and not tried again until the block shows another file |
| 167 | // (§12). |
| 168 | func keepPlaces(db *gorm.DB, store *Store, pageID int64, |
| 169 | placed []placedImage) error { |
| 170 | for _, each := range placed { |
| 171 | if err := keepPlace(db, store, pageID, each); err != nil { |
| 172 | return err |
| 173 | } |
| 174 | } |
| 175 | return nil |
| 176 | } |
| 177 | |
| 178 | func keepPlace(db *gorm.DB, store *Store, pageID int64, |
| 179 | placed placedImage) error { |
| 180 | place, alone, err := store.printsOf(placed) |
| 181 | if err != nil { |
| 182 | return err |
| 183 | } |
| 184 | row, found := notionImageOf(db, place) |
| 185 | record := imagePlace{Block: placed.block, Key: placed.key} |
| 186 | switch { |
| 187 | case placed.publicID != "": |
| 188 | row.State = imageImported |
| 189 | record.Page, record.PublicID = pageID, placed.publicID |
| 190 | case placed.permanent: |
| 191 | row.State, record.Reason = imageRefused, placed.reason |
| 192 | case found: |
| 193 | return db.Delete(&row).Error |
| 194 | default: |
| 195 | return nil |
| 196 | } |
| 197 | plain, _ := json.Marshal(record) |
| 198 | if row.SealedRecord, err = store.sealer.Seal( |
| 199 | seal.PurposeNotionImage, plain); err != nil { |
| 200 | return err |
| 201 | } |
| 202 | row.PlacePrint, row.BlockPrint = place, alone |
| 203 | return db.Save(&row).Error |
| 204 | } |
| 205 | |