oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "context"
5 "errors"
6
7 "gorm.io/gorm"
8
9 "amadan.net/oficina/mapa/internal/mapa/markup"
10)
11
12// The editor's autosave (level-up §6.5). An autosave writes the
13// person's draft and nothing else, and its reply tells the editor
14// whether a teammate's revision landed since the draft's base: the
15// merge of exactly the text the request carried. A rebase moves an
16// existing draft's base once the editor has taken that merge in. Every
17// write here is a draft write, exempt from gate 8 by name (§5).
18
19// DraftWrite is one autosave: the base a new draft starts from, the
20// draft version the tab holds (0 for none), and its text. Rebase, when
21// set, moves an existing draft's base to that revision of its page.
22// Settling is a closing tab whose page loaded while its own draft at
23// Version was being committed (build note 38): that version, or no
24// draft at all, is current. It allows nothing version 0 does not.
25type DraftWrite struct {
26 Base, Version int64
27 Body string
28 Rebase int64
29 Settling bool
30}
31
32// ErrRebase is a rebase the store refuses: no draft to move, or a
33// revision that is not this page's inside the history window. Nothing
34// is written.
35var ErrRebase = errors.New("mapa: that is not a revision of this page")
36
37// Autosaved is an autosave's answer: the draft's new version, the
38// page's current revision (0 for an unwritten day or a deleted page),
39// and Behind when the draft's base is not that revision.
40type Autosaved struct {
41 Version, Revision int64
42 Behind *Behind
43}
44
45// Behind is the merge an autosave's reply carries (level-up §6.5): a
46// clean merge's body, a conflict's hunks, no merge base, or a page that
47// was deleted.
48type Behind struct {
49 Against int64
50 Body string
51 Conflict bool
52 Hunks []markup.Hunk
53 NoBase bool
54 Gone bool
55}
56
57// Autosave writes the draft and says how it stands against the page.
58func (store *Store) Autosave(ctx context.Context, viewer Viewer,
59 key DraftKey, write DraftWrite) (Autosaved, error) {
60 row, err := store.saveDraft(ctx, viewer.Address, key, write)
61 if err != nil {
62 return Autosaved{}, err
63 }
64 saved := Autosaved{Version: row.Version}
65 if key.PageID == 0 {
66 return saved, nil
67 }
68 viewer.MemberID = row.PersonID
69 page, err := store.readable(ctx, viewer, key.PageID)
70 if errors.Is(err, ErrNotFound) {
71 saved.Behind = &Behind{Gone: true}
72 return saved, nil
73 }
74 if err != nil || row.BaseRevision == page.Revision {
75 saved.Revision = page.Revision
76 return saved, err
77 }
78 saved.Revision = page.Revision
79 saved.Behind, err = store.behind(ctx, page, row.BaseRevision,
80 cleanBody(write.Body))
81 return saved, err
82}
83
84// behind merges sent, the text an autosave carried, from base over the
85// page's current revision.
86func (store *Store) behind(ctx context.Context, page Page, base int64,
87 sent string) (*Behind, error) {
88 original, found, err := store.baseBody(ctx, page.ID, base)
89 if err != nil {
90 return nil, err
91 }
92 if !found {
93 return &Behind{NoBase: true}, nil
94 }
95 current, err := store.OpenPage(page)
96 if err != nil {
97 return nil, err
98 }
99 hunks, clean := markup.Merge3(original, sent, current.Body)
100 out := &Behind{Against: page.Revision}
101 if !clean {
102 out.Conflict, out.Hunks = true, hunks
103 return out, nil
104 }
105 merged, _ := markup.Settle(hunks, nil)
106 out.Body = cleanBody(merged)
107 return out, nil
108}
109
110// rebaseTo moves an existing draft's base to revision, which must be a
111// revision of the draft's own live page inside the history window.
112func (store *Store) rebaseTo(database *gorm.DB, row *Draft, found bool,
113 key DraftKey, revision int64) error {
114 if !found || key.PageID == 0 {
115 return ErrRebase
116 }
117 var held int64
118 err := database.Model(&Revision{}).
119 Joins("JOIN pages ON pages.id = revisions.page_id").
120 Where("revisions.page_id = ? AND revisions.revision = ? AND "+
121 "revisions.at > ? AND pages.gone = ?", key.PageID, revision,
122 store.now().UTC().Add(-HistoryWindow), false).
123 Count(&held).Error
124 if err != nil {
125 return err
126 }
127 if held == 0 {
128 return ErrRebase
129 }
130 row.BaseRevision = revision
131 return nil
132}
133