| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | ) |
| 6 | |
| 7 | // The import's pins (columns and icons spec §6.3): the roots of the |
| 8 | // import, pages with no imported parent and so no "In:" line, are pinned |
| 9 | // to the runner's own sidebar, once each, so Pinned resembles the top of |
| 10 | // their Notion sidebar. Pins are per person and sealed (level-up Q1): |
| 11 | // nobody else's sidebar changes. |
| 12 | |
| 13 | // pinMark is a root's sealed record of its offer. |
| 14 | type pinMark struct { |
| 15 | Run int64 `json:"run"` |
| 16 | Pinned bool `json:"pinned"` |
| 17 | } |
| 18 | |
| 19 | // autoPin pins the roots never offered before, in survey order, after |
| 20 | // the runner's own pins, up to the slots free, in one write that marks |
| 21 | // each offered root and logs one pins event by the runner. A root with |
| 22 | // no slot is marked too, and listed. Nothing to offer writes nothing. |
| 23 | func (job *importJob) autoPin(ctx context.Context, set *importSet) error { |
| 24 | roots := job.unofferedRoots(set) |
| 25 | if len(roots) == 0 { |
| 26 | return nil |
| 27 | } |
| 28 | return job.store.write(ctx, func(tx *Tx) error { |
| 29 | if err := holdLease(tx.db, job.lease); err != nil { |
| 30 | return err |
| 31 | } |
| 32 | memberID, err := tx.member(job.who) |
| 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | var member Member |
| 37 | if err := tx.db.First(&member, memberID).Error; err != nil { |
| 38 | return err |
| 39 | } |
| 40 | ids, err := tx.store.openPins(member.SealedPins) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | for _, entry := range roots { |
| 45 | // A root the runner pinned already counts as pinned. |
| 46 | pinned := hasPin(ids, entry.place.PageID) |
| 47 | if !pinned && len(ids) < maxPins { |
| 48 | ids, pinned = append(ids, entry.place.PageID), true |
| 49 | } |
| 50 | entry.place.Pin = &pinMark{Run: job.lease.run, Pinned: pinned} |
| 51 | if err := tx.markPlace(entry); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | } |
| 55 | if err := crashPoint("pin:marked"); err != nil { |
| 56 | return err |
| 57 | } |
| 58 | return tx.savePins(memberID, ids) |
| 59 | }) |
| 60 | } |
| 61 | |
| 62 | // unofferedRoots is the run's live roots that carry no pin mark, in |
| 63 | // survey order. A root is a page surveyed with no imported parent |
| 64 | // (staged.Parent is ""): a page whose parent failed or was deleted in |
| 65 | // Mapa this run has no In: line, but it is not top-level in Notion, so |
| 66 | // it is not offered, and not marked (Fable on the build, M6). |
| 67 | func (job *importJob) unofferedRoots(set *importSet) []*importEntry { |
| 68 | var roots []*importEntry |
| 69 | for _, entry := range set.pages { |
| 70 | if entry.place.Pin != nil || entry.place.PageID == 0 || |
| 71 | entry.staged.Parent != "" || !entry.live { |
| 72 | continue |
| 73 | } |
| 74 | roots = append(roots, entry) |
| 75 | } |
| 76 | return roots |
| 77 | } |
| 78 | |
| 79 | func hasPin(ids []int64, pageID int64) bool { |
| 80 | for _, id := range ids { |
| 81 | if id == pageID { |
| 82 | return true |
| 83 | } |
| 84 | } |
| 85 | return false |
| 86 | } |
| 87 | |
| 88 | // markPlace marks an entry's stored coordinate with its pin mark: read |
| 89 | // again inside the write, since the page's own transaction wrote it |
| 90 | // last, and the run's copy of it is not that one. |
| 91 | func (tx *Tx) markPlace(entry *importEntry) error { |
| 92 | var row NotionPage |
| 93 | if err := tx.db.First(&row, entry.row.ID).Error; err != nil { |
| 94 | return err |
| 95 | } |
| 96 | place, err := tx.store.openCoordinate(row) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | place.Pin = entry.place.Pin |
| 101 | sealed, err := tx.store.sealCoordinate(place) |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | return tx.db.Model(&NotionPage{}).Where("id = ?", row.ID). |
| 106 | Update("sealed_coordinate", sealed).Error |
| 107 | } |
| 108 | |