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 "errors"
5 "net/http"
6 "strconv"
7
8 "amadan.net/oficina/mapa/internal/mapa/markup"
9)
10
11// POST …/draft, the editor's autosave (level-up §6.5, §8). With base,
12// version and body it writes the person's draft and answers
13//
14// {version, revision, merge?}
15//
16// where merge, present when the draft's base is not the page's current
17// revision, is computed over exactly the text this request carried:
18//
19// {against, body} the merge is clean
20// {against, conflict: true, hunks} each {at, count, base, mine, theirs}
21// {noBase: true} the base is past the window
22// {gone: true} the page was deleted
23//
24// rebase=R also moves an existing draft's base to R, if version is
25// current and R is a revision of this page inside the window; anything
26// else is refused (409 stale, 422 {refused: "rebase"}) and moves
27// nothing. commit=1 writes the draft (when body is sent) and commits it
28// in one request, only from the current version. It never touches the
29// page or its revision except through that commit (gate 11).
30
31func (app *app) autosave(writer http.ResponseWriter, request *http.Request) {
32 viewer, err := app.viewer(request)
33 key, ok := app.draftKey(request, viewer)
34 if err != nil || !ok || !app.mayDraft(request, viewer, key) {
35 notFound(writer, request)
36 return
37 }
38 if request.PostFormValue("commit") == "1" {
39 app.commitNow(writer, request, viewer, key)
40 return
41 }
42 saved, err := app.store.Autosave(request.Context(), viewer, key,
43 DraftWrite{Base: formInt(request, "base"),
44 Version: formInt(request, "version"),
45 Body: request.PostFormValue("body"),
46 Rebase: formInt(request, "rebase")})
47 switch {
48 case errors.Is(err, ErrStaleTab):
49 app.staleTab(writer, request, key)
50 case errors.Is(err, ErrRebase):
51 refuseJSON(writer, http.StatusUnprocessableEntity, "rebase")
52 case err != nil:
53 app.fail(writer, request, err)
54 default:
55 answer(writer, autosaveReply(saved))
56 }
57}
58
59// refuseJSON answers the script that nothing was done, and why.
60func refuseJSON(writer http.ResponseWriter, status int, why string) {
61 writer.Header().Set("Content-Type", "application/json; charset=utf-8")
62 writer.Header().Set("Cache-Control", "no-store")
63 writer.WriteHeader(status)
64 answer(writer, map[string]any{"refused": why})
65}
66
67// autosaveReply is an autosave's JSON. Revisions and versions are
68// int63s, past what a JavaScript number holds, so they travel as text.
69func autosaveReply(saved Autosaved) map[string]any {
70 reply := map[string]any{"version": idText(saved.Version)}
71 if saved.Revision != 0 {
72 reply["revision"] = idText(saved.Revision)
73 }
74 if saved.Behind != nil {
75 reply["merge"] = mergeReply(*saved.Behind)
76 }
77 return reply
78}
79
80func mergeReply(behind Behind) map[string]any {
81 switch {
82 case behind.Gone:
83 return map[string]any{"gone": true}
84 case behind.NoBase:
85 return map[string]any{"noBase": true}
86 case !behind.Conflict:
87 return map[string]any{"against": idText(behind.Against),
88 "body": behind.Body}
89 }
90 return map[string]any{"against": idText(behind.Against),
91 "conflict": true, "hunks": hunkReplies(behind.Hunks)}
92}
93
94// hunkReply is one conflicting hunk: at and count are its blocks in the
95// text the request carried, and base, mine and theirs the Markdown of
96// its three sides.
97type hunkReply struct {
98 At int `json:"at"`
99 Count int `json:"count"`
100 Base string `json:"base"`
101 Mine string `json:"mine"`
102 Theirs string `json:"theirs"`
103}
104
105func hunkReplies(hunks []markup.Hunk) []hunkReply {
106 out := []hunkReply{}
107 for _, hunk := range hunks {
108 if !hunk.Conflict {
109 continue
110 }
111 base, mine, theirs := hunk.Sources()
112 out = append(out, hunkReply{At: hunk.MineAt, Count: len(hunk.Mine),
113 Base: base, Mine: mine, Theirs: theirs})
114 }
115 return out
116}
117
118func idText(value int64) string { return strconv.FormatInt(value, 10) }
119
120// commitNow is commit=1: the closing tab's one keepalive POST (D3). It
121// writes the text it carries as the draft and commits it, only if
122// version is the current draft's; otherwise it writes and commits
123// nothing. Past the keepalive cap the tab sends no body, and the draft
124// as last autosaved is committed. settling=1 is a tab still settling
125// after a reload (build note 38): its version, or none, is current.
126func (app *app) commitNow(writer http.ResponseWriter, request *http.Request,
127 viewer Viewer, key DraftKey) {
128 version := formInt(request, "version")
129 if _, sent := request.PostForm["body"]; sent {
130 row, err := app.store.saveDraft(request.Context(), viewer.Address,
131 key, DraftWrite{Base: formInt(request, "base"), Version: version,
132 Body: request.PostFormValue("body"),
133 Settling: request.PostFormValue("settling") == "1"})
134 if errors.Is(err, ErrStaleTab) {
135 app.staleTab(writer, request, key)
136 return
137 }
138 if err != nil {
139 app.fail(writer, request, err)
140 return
141 }
142 version = row.Version
143 } else if version == 0 {
144 app.staleTab(writer, request, key)
145 return
146 }
147 app.commitDraft(writer, request, viewer, key, version)
148}
149