| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strconv" |
| 6 | |
| 7 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 8 | ) |
| 9 | |
| 10 | // The block grammar's version (columns and icons spec D17, §1.10). Block |
| 11 | // indices are counted by a grammar, and column layouts changed it: a |
| 12 | // tab on an editor from before them counts a layout's lines as one |
| 13 | // paragraph. So every route that posts a block or hunk index sends the |
| 14 | // grammar it counted with, and one counted with another is refused, the |
| 15 | // person's draft kept, with a sentence the old editor already shows |
| 16 | // (it shows a reply's error). |
| 17 | |
| 18 | // staleGrammarSaid is the refusal's sentence. |
| 19 | const staleGrammarSaid = "Mapa has been updated. Reload the page to " + |
| 20 | "keep editing." |
| 21 | |
| 22 | var grammarField = strconv.Itoa(markup.Grammar) |
| 23 | |
| 24 | // countedHere wraps a route that reads block or hunk indices: a post |
| 25 | // whose grammar field is not this one's is refused with 409. |
| 26 | func countedHere(next http.HandlerFunc) http.HandlerFunc { |
| 27 | return func(writer http.ResponseWriter, request *http.Request) { |
| 28 | if request.PostFormValue("grammar") == grammarField { |
| 29 | next(writer, request) |
| 30 | return |
| 31 | } |
| 32 | writer.Header().Set("Cache-Control", "no-store") |
| 33 | if wantsJSON(request) { |
| 34 | writer.Header().Set("Content-Type", |
| 35 | "application/json; charset=utf-8") |
| 36 | writer.WriteHeader(http.StatusConflict) |
| 37 | answer(writer, map[string]any{"error": staleGrammarSaid}) |
| 38 | return |
| 39 | } |
| 40 | writer.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 41 | writer.WriteHeader(http.StatusConflict) |
| 42 | _, _ = writer.Write([]byte(staleGrammarSaid + "\n")) |
| 43 | } |
| 44 | } |
| 45 | |