| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | ) |
| 7 | |
| 8 | // The in-page title's answers (level-up §4.1, build note 78). The H1 |
| 9 | // and the header's title field rename through the same POST |
| 10 | // /p/{id}/rename as the form, asking for JSON; the form's redirect to |
| 11 | // the Update links confirm page is unchanged, and stays the floor. |
| 12 | |
| 13 | // renamedJSON answers a rename the script sent. A refusal is |
| 14 | // refusedJSON's 422 {error, taken}, as New team page answers. A rename |
| 15 | // is {title, href, from, to, links, count, aliases}: from and to are |
| 16 | // the revisions it was made over and minted ("0" both when the title |
| 17 | // was already the page's), links and count the confirm page's honest |
| 18 | // count of pages the viewer may edit that link by an old title (base |
| 19 | // §3.4), and aliases the page's old titles now. |
| 20 | func (app *app) renamedJSON(writer http.ResponseWriter, |
| 21 | request *http.Request, page PageView, done Renamed, err error) { |
| 22 | if sayable(err) { |
| 23 | refusedJSON(writer, err.Error(), app.index.TitleHref( |
| 24 | request.Context(), request.PostFormValue("title"))) |
| 25 | return |
| 26 | } |
| 27 | viewer, viewErr := app.viewer(request) |
| 28 | if err != nil || viewErr != nil { |
| 29 | app.refuse(writer, request, errors.Join(err, viewErr)) |
| 30 | return |
| 31 | } |
| 32 | scope := ScopeOf(viewer) |
| 33 | now, err := app.index.View(request.Context(), scope, page.ID) |
| 34 | if err != nil { |
| 35 | app.refuse(writer, request, err) |
| 36 | return |
| 37 | } |
| 38 | links, err := app.index.AliasLinkers(request.Context(), scope, page.ID) |
| 39 | if err != nil { |
| 40 | app.refuse(writer, request, err) |
| 41 | return |
| 42 | } |
| 43 | aliases := make([]string, 0, len(now.Aliases)) |
| 44 | for _, alias := range now.Aliases { |
| 45 | aliases = append(aliases, alias.Title) |
| 46 | } |
| 47 | answer(writer, map[string]any{"title": now.Title, "href": now.Href, |
| 48 | "from": idText(done.From), "to": idText(done.To), |
| 49 | "links": len(links), "count": pagesCount(localeIn(request), len(links)), |
| 50 | "aliases": aliases}) |
| 51 | } |
| 52 | |