| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "regexp" |
| 7 | "sync" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // A new scratch page's first commit (level-up §6.5, D12). The editor on |
| 12 | // New scratch page has no Save button, so its first autosave is the |
| 13 | // save: a commit that creates the page, refused when empty (ErrEmpty), |
| 14 | // and single-flight, so two quick autosaves create one page. |
| 15 | // |
| 16 | // The key is a token the tab draws once for the new page and sends as |
| 17 | // "first" with every autosave until it learns the page's address. It is |
| 18 | // held only here, in memory, for firstHold: never in the database, so |
| 19 | // §4's plaintext list is unchanged. The first request with a token |
| 20 | // creates the page; any later one with the same token, from the same |
| 21 | // person, waits for it and is an autosave of a draft on that page |
| 22 | // instead. A token forgotten (past firstHold, or by a restart) is a new |
| 23 | // page, which only a tab that kept sending it for that long could meet. |
| 24 | |
| 25 | // firstHold is how long a token is remembered. |
| 26 | const firstHold = 10 * time.Minute |
| 27 | |
| 28 | // firstCap bounds how many tokens one person has remembered at once |
| 29 | // (review round 4: per person, so no one can use up anyone else's). |
| 30 | // Past it that person's new token is refused, never forgotten: a token |
| 31 | // the server could not remember would make a page on every autosave. |
| 32 | // A tab draws one token per New scratch page. |
| 33 | const firstCap = 256 |
| 34 | |
| 35 | var firstToken = regexp.MustCompile(`^[A-Za-z0-9_-]{16,64}$`) |
| 36 | |
| 37 | // firsts remembers which scratch page each first-commit token made, by |
| 38 | // person and then token. |
| 39 | type firsts struct { |
| 40 | mutex sync.Mutex |
| 41 | made map[string]map[string]firstMade |
| 42 | } |
| 43 | |
| 44 | type firstMade struct { |
| 45 | pageID, revision int64 |
| 46 | public string |
| 47 | at time.Time |
| 48 | } |
| 49 | |
| 50 | // recall is the page a token made, forgetting tokens past firstHold. |
| 51 | // The caller holds the mutex. |
| 52 | func (held *firsts) recall(person, token string, |
| 53 | now time.Time) (firstMade, bool) { |
| 54 | if held.made == nil { |
| 55 | held.made = map[string]map[string]firstMade{} |
| 56 | } |
| 57 | for who, tokens := range held.made { |
| 58 | for other, made := range tokens { |
| 59 | if now.Sub(made.at) > firstHold { |
| 60 | delete(tokens, other) |
| 61 | } |
| 62 | } |
| 63 | if len(tokens) == 0 { |
| 64 | delete(held.made, who) |
| 65 | } |
| 66 | } |
| 67 | made, ok := held.made[person][token] |
| 68 | return made, ok |
| 69 | } |
| 70 | |
| 71 | func (held *firsts) remember(person, token string, made firstMade) { |
| 72 | if held.made[person] == nil { |
| 73 | held.made[person] = map[string]firstMade{} |
| 74 | } |
| 75 | held.made[person][token] = made |
| 76 | } |
| 77 | |
| 78 | // full is whether the person can have no new token remembered. The |
| 79 | // caller holds the mutex, after recall has forgotten the expired ones. |
| 80 | func (held *firsts) full(person string) bool { |
| 81 | return len(held.made[person]) >= firstCap |
| 82 | } |
| 83 | |
| 84 | // firstScratch is POST /scratch with a "first" token: the new page's |
| 85 | // first autosave. It answers JSON: {href, page, revision} when it made |
| 86 | // the page; when the page was already made, this is an autosave of a |
| 87 | // draft on it, answered {href, page, version, revision, merge?}; and |
| 88 | // 422 {empty: true} when there is nothing to save. |
| 89 | func (app *app) firstScratch(writer http.ResponseWriter, |
| 90 | request *http.Request, token string) { |
| 91 | viewer, err := app.viewer(request) |
| 92 | if err != nil || !firstToken.MatchString(token) { |
| 93 | notFound(writer, request) |
| 94 | return |
| 95 | } |
| 96 | person := viewer.Address |
| 97 | app.firsts.mutex.Lock() |
| 98 | defer app.firsts.mutex.Unlock() |
| 99 | made, ok := app.firsts.recall(person, token, app.store.now()) |
| 100 | if ok { |
| 101 | app.firstDraft(writer, request, viewer, made) |
| 102 | return |
| 103 | } |
| 104 | if app.firsts.full(person) { |
| 105 | // Nothing is made; the editor keeps the text and tries again. |
| 106 | refuseJSON(writer, http.StatusServiceUnavailable, "busy") |
| 107 | return |
| 108 | } |
| 109 | page, err := app.store.CreateScratch(request.Context(), viewer.Address, |
| 110 | request.PostFormValue("body")) |
| 111 | if errors.Is(err, ErrEmpty) { |
| 112 | writer.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 113 | writer.WriteHeader(http.StatusUnprocessableEntity) |
| 114 | answer(writer, map[string]any{"empty": true}) |
| 115 | return |
| 116 | } |
| 117 | if err != nil { |
| 118 | app.fail(writer, request, err) |
| 119 | return |
| 120 | } |
| 121 | made = firstMade{pageID: page.ID, revision: page.Revision, |
| 122 | public: publicID(page.Nonce), at: app.store.now()} |
| 123 | app.firsts.remember(person, token, made) |
| 124 | answer(writer, map[string]any{"href": PagePath(page), |
| 125 | "page": made.public, "revision": idText(page.Revision)}) |
| 126 | } |
| 127 | |
| 128 | // firstDraft is a later autosave carrying a token whose page exists: a |
| 129 | // draft on that page, based on its first revision, answered as any |
| 130 | // autosave is, with the page's address. |
| 131 | func (app *app) firstDraft(writer http.ResponseWriter, |
| 132 | request *http.Request, viewer Viewer, made firstMade) { |
| 133 | key := DraftKey{PageID: made.pageID} |
| 134 | saved, err := app.store.Autosave(request.Context(), viewer, key, |
| 135 | DraftWrite{Base: made.revision, Version: formInt(request, "version"), |
| 136 | Body: request.PostFormValue("body")}) |
| 137 | if errors.Is(err, ErrStaleTab) { |
| 138 | app.staleTab(writer, request, key) |
| 139 | return |
| 140 | } |
| 141 | if err != nil { |
| 142 | app.fail(writer, request, err) |
| 143 | return |
| 144 | } |
| 145 | reply := autosaveReply(saved) |
| 146 | reply["href"], reply["page"] = "/p/"+made.public, made.public |
| 147 | answer(writer, reply) |
| 148 | } |
| 149 | |