| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | |
| 7 | "gorm.io/gorm" |
| 8 | |
| 9 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 10 | ) |
| 11 | |
| 12 | // Daily notes (§3.3, §6): one per person per day, private to that |
| 13 | // person, shown as its date. The (owner, date) print enforces "one" |
| 14 | // without a plaintext date. |
| 15 | |
| 16 | // dayHolder is the page holding (owner, date)'s daily note, or 0. |
| 17 | func (tx *Tx) dayHolder(ownerID int64, date string) (int64, error) { |
| 18 | fingerprint, err := tx.store.dailyPrint(ownerID, date) |
| 19 | if err != nil { |
| 20 | return 0, err |
| 21 | } |
| 22 | var held DailyPrint |
| 23 | err = tx.db.Where("print = ?", fingerprint).First(&held).Error |
| 24 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 25 | return 0, nil |
| 26 | } |
| 27 | return held.PageID, err |
| 28 | } |
| 29 | |
| 30 | // claimDay reserves (owner, date) for page. |
| 31 | func (tx *Tx) claimDay(pageID, ownerID int64, date string) error { |
| 32 | fingerprint, err := tx.store.dailyPrint(ownerID, date) |
| 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | return tx.db.Omit("Page").Create(&DailyPrint{Print: fingerprint, |
| 37 | PageID: pageID}).Error |
| 38 | } |
| 39 | |
| 40 | // restoreDaily brings a deleted daily note back as that day's note, or, |
| 41 | // when the person has written a new note for the day since, as a |
| 42 | // scratch page headed with the date (spec §15, build note). |
| 43 | func (tx *Tx) restoreDaily(old Page, record Record, |
| 44 | actor int64) (Page, error) { |
| 45 | holder, err := tx.dayHolder(old.OwnerID, record.Date) |
| 46 | if err != nil { |
| 47 | return Page{}, err |
| 48 | } |
| 49 | if holder != 0 { |
| 50 | scratch := Record{Body: "# " + record.Date + "\n\n" + record.Body} |
| 51 | return tx.create(KindScratch, RungPrivate, old.OwnerID, scratch, |
| 52 | actor, VerbRestored, attachNothing) |
| 53 | } |
| 54 | page, err := tx.create(KindDaily, RungPrivate, old.OwnerID, record, |
| 55 | actor, VerbRestored, attachNothing) |
| 56 | if err != nil { |
| 57 | return page, err |
| 58 | } |
| 59 | return page, tx.claimDay(page.ID, old.OwnerID, record.Date) |
| 60 | } |
| 61 | |
| 62 | // ErrNoDate is a daily note asked for by something that is not a date. |
| 63 | var ErrNoDate = errors.New("mapa: not a date") |
| 64 | |
| 65 | // ErrEmpty is a first save with nothing in it. A day nothing was written |
| 66 | // on leaves no trace (gate 10), so an empty first save writes nothing. |
| 67 | var ErrEmpty = errors.New("Write something first.") |
| 68 | |
| 69 | // SaveDaily commits who's note for date. The first save that holds any |
| 70 | // text creates the note (§6): opening the day, or saving it empty, never |
| 71 | // does. baseRevision is 0 for a note that did not exist when the editor |
| 72 | // opened; a note that appeared meanwhile is then stale. |
| 73 | func (store *Store) SaveDaily(ctx context.Context, who, date string, |
| 74 | baseRevision int64, body string) (Page, error) { |
| 75 | return store.SaveDailyFrom(ctx, who, date, baseRevision, 0, body) |
| 76 | } |
| 77 | |
| 78 | // SaveDailyFrom is SaveDaily from a tab that held the day's draft at |
| 79 | // version held (0 for none). Creating the note clears that draft, and |
| 80 | // never a newer one another tab wrote. |
| 81 | func (store *Store) SaveDailyFrom(ctx context.Context, who, date string, |
| 82 | baseRevision, held int64, body string) (Page, error) { |
| 83 | if !markup.IsDate(date) { |
| 84 | return Page{}, ErrNoDate |
| 85 | } |
| 86 | body = cleanBody(body) |
| 87 | var page Page |
| 88 | err := store.write(ctx, func(tx *Tx) error { |
| 89 | actor, err := tx.member(who) |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | holder, err := tx.dayHolder(actor, date) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | if holder == 0 { |
| 98 | page, err = tx.newDaily(actor, date, baseRevision, held, body) |
| 99 | return err |
| 100 | } |
| 101 | if page, err = tx.visiblePage(actor, holder); err != nil { |
| 102 | return err |
| 103 | } |
| 104 | if page.Revision != baseRevision { |
| 105 | return ErrStale |
| 106 | } |
| 107 | return tx.commitDay(&page, actor, date, body) |
| 108 | }) |
| 109 | if errors.Is(err, errNoChange) { |
| 110 | return page, nil |
| 111 | } |
| 112 | return page, err |
| 113 | } |
| 114 | |
| 115 | // commitDay commits a written day's note, unless the body is already |
| 116 | // what it says: a save that changes nothing writes nothing (level-up |
| 117 | // §6.4, step 7). |
| 118 | func (tx *Tx) commitDay(page *Page, actor int64, date, body string) error { |
| 119 | current, err := tx.store.OpenPage(*page) |
| 120 | if err != nil { |
| 121 | return err |
| 122 | } |
| 123 | if current.Body == body { |
| 124 | return errNoChange |
| 125 | } |
| 126 | return tx.commit(page, Record{Date: date, Body: body}, actor, |
| 127 | VerbEdited, attachOwn(0)) |
| 128 | } |
| 129 | |
| 130 | func (tx *Tx) newDaily(actor int64, date string, baseRevision, held int64, |
| 131 | body string) (Page, error) { |
| 132 | if baseRevision != 0 { |
| 133 | // The note was deleted while the editor was open. |
| 134 | return Page{}, ErrStale |
| 135 | } |
| 136 | if body == "" { |
| 137 | return Page{}, ErrEmpty |
| 138 | } |
| 139 | page, err := tx.create(KindDaily, RungPrivate, actor, |
| 140 | Record{Date: date, Body: body}, actor, VerbCreated, attachOwn(0)) |
| 141 | if err != nil { |
| 142 | return page, err |
| 143 | } |
| 144 | fingerprint, err := tx.store.dailyPrint(actor, date) |
| 145 | if err != nil { |
| 146 | return page, err |
| 147 | } |
| 148 | // The unwritten day's draft is this text now, if this tab held it. |
| 149 | err = tx.db.Where("person_id = ? AND daily_print = ? AND version = ?", |
| 150 | actor, fingerprint, held).Delete(&Draft{}).Error |
| 151 | if err != nil { |
| 152 | return page, err |
| 153 | } |
| 154 | if err := tx.rekeyDay(actor, fingerprint, page.ID); err != nil { |
| 155 | return page, err |
| 156 | } |
| 157 | return page, tx.claimDay(page.ID, actor, date) |
| 158 | } |
| 159 | |
| 160 | // rekeyDay moves a newer draft for the day, one another tab wrote, onto |
| 161 | // the note that now exists (review N1). Keyed by the daily print it |
| 162 | // would be stranded: once the note exists, the day's draft is looked up |
| 163 | // by the note. Its base becomes 0, which no revision has, so committing |
| 164 | // it shows the two texts side by side and the person chooses. A draft |
| 165 | // write; no event. |
| 166 | func (tx *Tx) rekeyDay(actor int64, fingerprint string, pageID int64) error { |
| 167 | return tx.db.Model(&Draft{}).Where("person_id = ? AND daily_print = ?", |
| 168 | actor, fingerprint).Updates(map[string]any{"page_id": pageID, |
| 169 | "daily_print": nil, "base_revision": 0}).Error |
| 170 | } |
| 171 | |
| 172 | // CreateScratch makes a private, untitled page (§3.3). Like a daily |
| 173 | // note, it exists only once it holds something. |
| 174 | func (store *Store) CreateScratch(ctx context.Context, who, |
| 175 | body string) (Page, error) { |
| 176 | body = cleanBody(body) |
| 177 | if body == "" { |
| 178 | return Page{}, ErrEmpty |
| 179 | } |
| 180 | var page Page |
| 181 | err := store.write(ctx, func(tx *Tx) error { |
| 182 | actor, err := tx.member(who) |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | page, err = tx.create(KindScratch, RungPrivate, actor, |
| 187 | Record{Body: body}, actor, VerbCreated, attachOwn(0)) |
| 188 | return err |
| 189 | }) |
| 190 | return page, err |
| 191 | } |
| 192 | |
| 193 | // SaveDay is the day editor's Save: it creates the note with the first |
| 194 | // text, or commits over the existing note through the block merge. |
| 195 | func (store *Store) SaveDay(ctx context.Context, viewer Viewer, date string, |
| 196 | base, held int64, body string) (Outcome, int64, error) { |
| 197 | holder, found, err := store.dayPage(ctx, viewer, date) |
| 198 | if err != nil { |
| 199 | return 0, 0, err |
| 200 | } |
| 201 | if found { |
| 202 | outcome, err := store.CommitFrom(ctx, viewer.Address, holder, base, |
| 203 | held, body) |
| 204 | return outcome, holder, err |
| 205 | } |
| 206 | page, err := store.SaveDailyFrom(ctx, viewer.Address, date, base, held, |
| 207 | body) |
| 208 | if errors.Is(err, ErrStale) { |
| 209 | // The note was deleted while the editor was open; its text is |
| 210 | // kept as a new note for the day. |
| 211 | page, err = store.SaveDailyFrom(ctx, viewer.Address, date, 0, held, |
| 212 | body) |
| 213 | } |
| 214 | return Saved, page.ID, err |
| 215 | } |
| 216 | |