| 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 | "amadan.net/oficina/mapa/internal/mapa/seal" |
| 11 | ) |
| 12 | |
| 13 | // Outcome is what a revision-checked save did. |
| 14 | type Outcome int |
| 15 | |
| 16 | // The four outcomes of a save (§5, §6). |
| 17 | const ( |
| 18 | // Saved: the base was current, and the body is the new revision. |
| 19 | Saved Outcome = iota + 1 |
| 20 | // Merged: a teammate's revision landed in between, the block merge |
| 21 | // was clean, and the merged body is the new revision. |
| 22 | Merged |
| 23 | // Conflicted: two people edited the same block. Nothing committed; |
| 24 | // the text stays the person's draft until they choose. |
| 25 | Conflicted |
| 26 | // NoBase: the base revision is past the history window, so there is |
| 27 | // no merge base. Nothing committed; the person chooses side by side. |
| 28 | NoBase |
| 29 | // Kept: saved or merged, from a tab that had not seen the person's |
| 30 | // newer draft. The draft is kept, its base unchanged, so committing |
| 31 | // it later merges against this save; it is offered as Save or |
| 32 | // Discard. |
| 33 | Kept |
| 34 | ) |
| 35 | |
| 36 | // errUnmerged ends a commit that must not happen, so that the draft can |
| 37 | // be kept outside the transaction. |
| 38 | var errUnmerged = errors.New("mapa: the save did not merge") |
| 39 | |
| 40 | // Commit is CommitFrom for a save that held no draft. |
| 41 | func (store *Store) Commit(ctx context.Context, who string, pageID, |
| 42 | base int64, body string) (Outcome, error) { |
| 43 | return store.CommitFrom(ctx, who, pageID, base, 0, body) |
| 44 | } |
| 45 | |
| 46 | // CommitFrom is the revision-checked save (§5): the scriptless Save, and |
| 47 | // every draft commit. held is the version of the person's draft the tab |
| 48 | // had, 0 for none. A save over the current revision commits; over an |
| 49 | // older one, the block merge runs; if it cannot settle, nothing is |
| 50 | // committed and the text is kept as the person's draft. |
| 51 | // |
| 52 | // A tab that had not seen the person's newest draft (another tab wrote |
| 53 | // it) never destroys it: a save that commits leaves the draft as it was |
| 54 | // (Kept), and one that cannot commit is refused with ErrStaleTab rather |
| 55 | // than written over the newer draft. |
| 56 | func (store *Store) CommitFrom(ctx context.Context, who string, pageID, |
| 57 | base, held int64, body string) (Outcome, error) { |
| 58 | body = cleanBody(body) |
| 59 | outcome, stale := Saved, false |
| 60 | err := store.write(ctx, func(tx *Tx) error { |
| 61 | actor, page, record, err := tx.editable(who, pageID) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | if stale, err = tx.staleDraft(actor, pageID, held); err != nil { |
| 66 | return err |
| 67 | } |
| 68 | if outcome, body, err = tx.merged(page, base, body, |
| 69 | record.Body); err != nil { |
| 70 | return err |
| 71 | } |
| 72 | if body == record.Body { |
| 73 | return errNoChange |
| 74 | } |
| 75 | record.Body = body |
| 76 | if stale { |
| 77 | outcome = Kept |
| 78 | } else if err := tx.dropDraft(actor, page.ID); err != nil { |
| 79 | return err |
| 80 | } |
| 81 | // The person's own text, started from base (§3.2). |
| 82 | return tx.commit(&page, record, actor, VerbEdited, attachOwn(base)) |
| 83 | }) |
| 84 | return store.afterCommitFrom(ctx, who, pageID, base, held, body, |
| 85 | outcome, stale, err) |
| 86 | } |
| 87 | |
| 88 | // afterCommitFrom finishes a commit outside its transaction: a save |
| 89 | // that could not merge keeps its text as the draft, and one that changed |
| 90 | // nothing drops the draft the tab held, writing no revision and no |
| 91 | // event (level-up §6.4, step 7). |
| 92 | func (store *Store) afterCommitFrom(ctx context.Context, who string, |
| 93 | pageID, base, held int64, body string, outcome Outcome, stale bool, |
| 94 | err error) (Outcome, error) { |
| 95 | switch { |
| 96 | case errors.Is(err, errNoChange) && stale: |
| 97 | return Kept, nil |
| 98 | case errors.Is(err, errNoChange): |
| 99 | return outcome, store.dropHeldDraft(ctx, who, pageID, held) |
| 100 | case errors.Is(err, errUnmerged) && stale: |
| 101 | return outcome, ErrStaleTab |
| 102 | case errors.Is(err, errUnmerged): |
| 103 | return outcome, store.keepDraft(ctx, who, pageID, base, body) |
| 104 | } |
| 105 | return outcome, err |
| 106 | } |
| 107 | |
| 108 | // editable is a page the person may edit, with its record. |
| 109 | func (tx *Tx) editable(who string, pageID int64) (int64, Page, Record, |
| 110 | error) { |
| 111 | actor, err := tx.member(who) |
| 112 | if err != nil { |
| 113 | return 0, Page{}, Record{}, err |
| 114 | } |
| 115 | page, err := tx.visiblePage(actor, pageID) |
| 116 | if err != nil { |
| 117 | return actor, page, Record{}, err |
| 118 | } |
| 119 | record, err := tx.store.OpenPage(page) |
| 120 | return actor, page, record, err |
| 121 | } |
| 122 | |
| 123 | // staleDraft reports whether the person has a draft on the page that the |
| 124 | // saving tab had not seen. |
| 125 | func (tx *Tx) staleDraft(actor, pageID, held int64) (bool, error) { |
| 126 | draft, found, err := tx.store.draftRow(tx.db, actor, |
| 127 | DraftKey{PageID: pageID}) |
| 128 | return found && draft.Version != held, err |
| 129 | } |
| 130 | |
| 131 | // merged is the body to commit over page, and how it came to be. |
| 132 | func (tx *Tx) merged(page Page, base int64, mine, |
| 133 | theirs string) (Outcome, string, error) { |
| 134 | if page.Revision == base { |
| 135 | return Saved, mine, nil |
| 136 | } |
| 137 | var stored Revision |
| 138 | // A base past the window is gone, swept or not: between wakes a |
| 139 | // read never relies on what retention is about to remove (§5). |
| 140 | err := tx.db.Where("page_id = ? AND revision = ? AND at > ?", page.ID, |
| 141 | base, tx.at.Add(-HistoryWindow)).First(&stored).Error |
| 142 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 143 | return NoBase, mine, errUnmerged |
| 144 | } |
| 145 | if err != nil { |
| 146 | return 0, mine, err |
| 147 | } |
| 148 | original, err := tx.store.openRecord(seal.PurposeRecord, |
| 149 | stored.SealedRecord) |
| 150 | if err != nil { |
| 151 | return 0, mine, err |
| 152 | } |
| 153 | hunks, clean := markup.Merge3(original.Body, mine, theirs) |
| 154 | if !clean { |
| 155 | return Conflicted, mine, errUnmerged |
| 156 | } |
| 157 | settled, _ := markup.Settle(hunks, nil) |
| 158 | return Merged, cleanBody(settled), nil |
| 159 | } |
| 160 | |
| 161 | // Resolve settles a conflict the person has chosen through: it commits |
| 162 | // the chosen text over current, the revision they were shown, from the |
| 163 | // draft version they were shown. If another revision landed, or the |
| 164 | // draft changed in another tab, they are shown the conflict again; |
| 165 | // nothing is committed that nobody chose. |
| 166 | func (store *Store) Resolve(ctx context.Context, who string, pageID, |
| 167 | current, held int64, body string) (Outcome, error) { |
| 168 | outcome := Saved |
| 169 | err := store.write(ctx, func(tx *Tx) error { |
| 170 | actor, page, record, err := tx.editable(who, pageID) |
| 171 | if err != nil { |
| 172 | return err |
| 173 | } |
| 174 | stale, err := tx.staleDraft(actor, pageID, held) |
| 175 | if err != nil { |
| 176 | return err |
| 177 | } |
| 178 | if page.Revision != current || stale { |
| 179 | outcome = Conflicted |
| 180 | return errUnmerged |
| 181 | } |
| 182 | if cleanBody(body) == record.Body { |
| 183 | return errNoChange |
| 184 | } |
| 185 | record.Body = cleanBody(body) |
| 186 | started, err := tx.draftBase(actor, page.ID) |
| 187 | if err != nil { |
| 188 | return err |
| 189 | } |
| 190 | if err := tx.dropDraft(actor, page.ID); err != nil { |
| 191 | return err |
| 192 | } |
| 193 | return tx.commit(&page, record, actor, VerbEdited, |
| 194 | attachOwn(started)) |
| 195 | }) |
| 196 | switch { |
| 197 | case errors.Is(err, errUnmerged): |
| 198 | return outcome, nil |
| 199 | case errors.Is(err, errNoChange): |
| 200 | return outcome, store.dropHeldDraft(ctx, who, pageID, held) |
| 201 | } |
| 202 | return outcome, err |
| 203 | } |
| 204 | |
| 205 | // draftBase is the revision the person's draft on the page started |
| 206 | // from, 0 for none: what Resolve's text started from (§3.2). |
| 207 | func (tx *Tx) draftBase(actor, pageID int64) (int64, error) { |
| 208 | draft, found, err := tx.store.draftRow(tx.db, actor, |
| 209 | DraftKey{PageID: pageID}) |
| 210 | if !found { |
| 211 | return 0, err |
| 212 | } |
| 213 | return draft.BaseRevision, err |
| 214 | } |
| 215 | |