| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "html/template" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | |
| 9 | "amadan.net/oficina/mapa/internal/mapa/locale" |
| 10 | "github.com/go-chi/chi/v5" |
| 11 | ) |
| 12 | |
| 13 | // The team page screens: the list, a page, create and edit. Every one |
| 14 | // works with scripts off. |
| 15 | |
| 16 | func (app *app) teamPageRoutes(router chi.Router) { |
| 17 | router.Get("/pages", app.listPages) |
| 18 | router.Get("/new", app.newPage) |
| 19 | router.Post("/pages", app.createPage) |
| 20 | router.Get("/p/{id}", app.showPage) |
| 21 | app.editorRoutes(router) |
| 22 | } |
| 23 | |
| 24 | // pathID is a positive {name} from the path. Anything else is a page |
| 25 | // that is not there. |
| 26 | func pathID(request *http.Request, name string) (int64, bool) { |
| 27 | value, err := strconv.ParseInt(chi.URLParam(request, name), 10, 64) |
| 28 | return value, err == nil && value > 0 |
| 29 | } |
| 30 | |
| 31 | // refuse answers an error: not found is a 404, a sentence for a person |
| 32 | // is the page again with it, anything else is a 500. |
| 33 | func (app *app) refuse(writer http.ResponseWriter, request *http.Request, |
| 34 | err error) { |
| 35 | if errors.Is(err, ErrNotFound) { |
| 36 | notFound(writer, request) |
| 37 | return |
| 38 | } |
| 39 | app.fail(writer, request, err) |
| 40 | } |
| 41 | |
| 42 | // sayable reports whether err is a sentence a person can act on. |
| 43 | func sayable(err error) bool { |
| 44 | for _, known := range []error{ErrTitleEmpty, ErrTitleLong, |
| 45 | ErrTitleChars, ErrTitleDate, ErrTitleTaken, ErrBadTimeZone} { |
| 46 | if errors.Is(err, known) { |
| 47 | return true |
| 48 | } |
| 49 | } |
| 50 | return false |
| 51 | } |
| 52 | |
| 53 | // seeOther ends a POST that worked. |
| 54 | func seeOther(writer http.ResponseWriter, request *http.Request, |
| 55 | target string) { |
| 56 | http.Redirect(writer, request, target, http.StatusSeeOther) |
| 57 | } |
| 58 | |
| 59 | func (app *app) listPages(writer http.ResponseWriter, |
| 60 | request *http.Request) { |
| 61 | viewer, err := app.viewer(request) |
| 62 | if err != nil { |
| 63 | app.fail(writer, request, err) |
| 64 | return |
| 65 | } |
| 66 | pages, err := app.index.TeamPages(request.Context()) |
| 67 | if err != nil { |
| 68 | app.fail(writer, request, err) |
| 69 | return |
| 70 | } |
| 71 | app.show(writer, request, http.StatusOK, "pages", view{Title: "Pages", |
| 72 | Nav: nav("/pages"), Viewer: viewer, Data: map[string]any{ |
| 73 | "Pages": pages, "Notice": notice(request)}}) |
| 74 | } |
| 75 | |
| 76 | func (app *app) showPage(writer http.ResponseWriter, |
| 77 | request *http.Request) { |
| 78 | page, ok := app.visible(writer, request) |
| 79 | if !ok { |
| 80 | return |
| 81 | } |
| 82 | viewer, err := app.viewer(request) |
| 83 | if err != nil { |
| 84 | app.fail(writer, request, err) |
| 85 | return |
| 86 | } |
| 87 | state, err := app.editorFor(request, viewer, &page, |
| 88 | DraftKey{PageID: page.ID}) |
| 89 | data := map[string]any{"Page": page, "Notice": notice(request), |
| 90 | "Draft": state.Draft, "Saved": state.Saved, "Island": state.Island, |
| 91 | "Banner": app.bannerOf(request, viewer, page)} |
| 92 | editing, editErr := app.store.Editing(request.Context(), viewer, page.ID) |
| 93 | if err = errors.Join(err, editErr); err != nil { |
| 94 | app.fail(writer, request, err) |
| 95 | return |
| 96 | } |
| 97 | data["Editing"], data["Rename"] = editing, renameIn(request, page, |
| 98 | state.Island != "") |
| 99 | if last, ok, err := app.store.LastEditOf(request.Context(), viewer, |
| 100 | page.ID); err == nil && ok { |
| 101 | data["Last"] = &last |
| 102 | } |
| 103 | app.show(writer, request, http.StatusOK, "page", view{ |
| 104 | Title: tabTitle(page.Look, page.Label), |
| 105 | Nav: nav(navFor(page.Kind)), Viewer: viewer, Data: data, |
| 106 | Chrome: &pageChrome{Page: &page, Editing: editing, Links: true, |
| 107 | Writable: state.Island != ""}}) |
| 108 | } |
| 109 | |
| 110 | // renameIn is the rename POST the page's in-page title sends (level-up |
| 111 | // §4.1), or "": only a team page, only where the editor mounts (its |
| 112 | // script makes the title editable), and never on the history routes. |
| 113 | func renameIn(request *http.Request, page PageView, editor bool) string { |
| 114 | if page.Kind != KindPage || !editor || historyIn(request) != nil { |
| 115 | return "" |
| 116 | } |
| 117 | return pageHref(page.Public, "") + "/rename" |
| 118 | } |
| 119 | |
| 120 | // navFor is the place a page of kind belongs to, which the sidebar |
| 121 | // lights when the page has no row of its own (build note 80): Today |
| 122 | // for every day, past ones too, as Today is where days are kept; the |
| 123 | // day pill says which. |
| 124 | func navFor(kind string) string { |
| 125 | switch kind { |
| 126 | case KindDaily: |
| 127 | return "/" |
| 128 | case KindScratch: |
| 129 | return "/scratch" |
| 130 | } |
| 131 | return "/pages" |
| 132 | } |
| 133 | |
| 134 | // pageForm is the create and edit form's data. |
| 135 | type pageForm struct { |
| 136 | Page PageView |
| 137 | Title, Body string |
| 138 | Revision int64 |
| 139 | Error, Taken string |
| 140 | Notice string |
| 141 | Action, Label string |
| 142 | // Drafts is where the script layer autosaves, and Version the draft |
| 143 | // this editor holds; empty for a page that does not exist yet. |
| 144 | Drafts string |
| 145 | Version int64 |
| 146 | // Island is the editor's data (level-up §6.8), on New scratch page. |
| 147 | Island template.JS |
| 148 | // Images is "Images on this page" (images §3.3) on a Markdown |
| 149 | // screen. |
| 150 | Images []ListedImage |
| 151 | } |
| 152 | |
| 153 | func (app *app) newPage(writer http.ResponseWriter, request *http.Request) { |
| 154 | form := pageForm{Title: request.URL.Query().Get("title"), |
| 155 | Action: "/pages", Label: "Create page"} |
| 156 | app.showForm(writer, request, http.StatusOK, "page_new", form) |
| 157 | } |
| 158 | |
| 159 | func (app *app) showForm(writer http.ResponseWriter, request *http.Request, |
| 160 | status int, name string, form pageForm) { |
| 161 | viewer, err := app.viewer(request) |
| 162 | if err != nil { |
| 163 | app.fail(writer, request, err) |
| 164 | return |
| 165 | } |
| 166 | title := form.Label |
| 167 | if form.Page.ID != 0 { |
| 168 | title = form.Page.Label |
| 169 | } |
| 170 | if name == "page_edit" || name == "scratch_new" { |
| 171 | form.Images, err = app.store.PageImages(request.Context(), viewer, |
| 172 | form.Page.ID, form.Body) |
| 173 | if err != nil { |
| 174 | app.fail(writer, request, err) |
| 175 | return |
| 176 | } |
| 177 | } |
| 178 | var screen *pageChrome |
| 179 | if form.Page.ID != 0 || form.Island != "" { |
| 180 | screen = &pageChrome{Page: &form.Page, Editor: name == "page_edit", |
| 181 | Writable: form.Island != ""} |
| 182 | } |
| 183 | app.show(writer, request, status, name, view{Title: title, |
| 184 | Nav: nav(navFor(form.Page.Kind)), Viewer: viewer, Data: form, |
| 185 | Chrome: screen}) |
| 186 | } |
| 187 | |
| 188 | func (app *app) createPage(writer http.ResponseWriter, |
| 189 | request *http.Request) { |
| 190 | address, _ := app.identity(request) |
| 191 | title, body := request.PostFormValue("title"), |
| 192 | request.PostFormValue("body") |
| 193 | page, err := app.store.CreateTeamPage(request.Context(), address, title, |
| 194 | body) |
| 195 | if sayable(err) { |
| 196 | form := pageForm{Title: title, Body: body, Error: err.Error(), |
| 197 | Action: "/pages", Label: "Create page", |
| 198 | Taken: app.index.TitleHref(request.Context(), title)} |
| 199 | app.refuseCreate(writer, request, form) |
| 200 | return |
| 201 | } |
| 202 | if err != nil { |
| 203 | app.fail(writer, request, err) |
| 204 | return |
| 205 | } |
| 206 | if wantsJSON(request) { |
| 207 | answer(writer, map[string]any{"href": PagePath(page)}) |
| 208 | return |
| 209 | } |
| 210 | seeOther(writer, request, PagePath(page)) |
| 211 | } |
| 212 | |
| 213 | // refuseCreate asks for another title: the New page screen again, with |
| 214 | // what was typed, or, for the sidebar's popover, the sentence as JSON, |
| 215 | // shown where the title was typed (level-up §4.7). |
| 216 | func (app *app) refuseCreate(writer http.ResponseWriter, |
| 217 | request *http.Request, form pageForm) { |
| 218 | if !wantsJSON(request) { |
| 219 | app.showForm(writer, request, http.StatusUnprocessableEntity, |
| 220 | "page_new", form) |
| 221 | return |
| 222 | } |
| 223 | refusedJSON(writer, form.Error, form.Taken) |
| 224 | } |
| 225 | |
| 226 | // refusedJSON is a title refused, for a script: 422 {error, taken}, the |
| 227 | // form's own sentence and the page holding the title ("" if none). New |
| 228 | // team page, Make team page and the in-page rename all answer it. |
| 229 | func refusedJSON(writer http.ResponseWriter, sentence, taken string) { |
| 230 | writer.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 231 | writer.WriteHeader(http.StatusUnprocessableEntity) |
| 232 | answer(writer, map[string]any{"error": sentence, "taken": taken}) |
| 233 | } |
| 234 | |
| 235 | // notice is the sentence a redirect after a POST asked for. |
| 236 | func notice(request *http.Request) string { |
| 237 | query := request.URL.Query() |
| 238 | switch query.Get("done") { |
| 239 | case "renamed": |
| 240 | return "Renamed. The old title still leads here." |
| 241 | case "links": |
| 242 | count, _ := strconv.Atoi(query.Get("n")) |
| 243 | return "Updated links in " + pagesCount(localeIn(request), |
| 244 | count) + "." |
| 245 | case "linked": |
| 246 | return "Linked." |
| 247 | case "pinsfull": |
| 248 | return ErrTooManyPins.Error() |
| 249 | case "freed": |
| 250 | return "That old title is free for another page now." |
| 251 | case "deleted": |
| 252 | return "Deleted. You can restore it from Deleted pages for 90 days." |
| 253 | case "merged": |
| 254 | return "Saved, merged with a teammate's changes to other blocks." |
| 255 | case "restored": |
| 256 | return "Restored that version as the newest one." |
| 257 | case "discarded": |
| 258 | return "Your unsaved changes were discarded." |
| 259 | case "image-removed": |
| 260 | return "Removed the image from Mapa. Where it was, it now " + |
| 261 | "says the image is not available." |
| 262 | case "kept": |
| 263 | return "Saved. Another tab of yours has newer unsaved changes: " + |
| 264 | "they are kept, and offered below." |
| 265 | case "newer": |
| 266 | return "Not discarded: another tab of yours has changed them " + |
| 267 | "since. Here they are as they are now." |
| 268 | case "marker": |
| 269 | return "That block can be linked now: copy the link below." |
| 270 | } |
| 271 | return "" |
| 272 | } |
| 273 | |
| 274 | // pagesCount is "1 page" or "1,234 pages", the number as the reader's |
| 275 | // locale writes it (build note 85). |
| 276 | func pagesCount(dates *locale.Locale, count int) string { |
| 277 | if count == 1 { |
| 278 | return "1 page" |
| 279 | } |
| 280 | return dates.Number(count) + " pages" |
| 281 | } |
| 282 | |
| 283 | // visible is the page in the path, as the viewer sees it, or a 404 |
| 284 | // already written. |
| 285 | func (app *app) visible(writer http.ResponseWriter, |
| 286 | request *http.Request) (PageView, bool) { |
| 287 | ref, ok := app.pathPage(request, "id") |
| 288 | if !ok { |
| 289 | notFound(writer, request) |
| 290 | return PageView{}, false |
| 291 | } |
| 292 | viewer, err := app.viewer(request) |
| 293 | if err != nil { |
| 294 | app.fail(writer, request, err) |
| 295 | return PageView{}, false |
| 296 | } |
| 297 | page, err := app.index.View(request.Context(), ScopeOf(viewer), ref.ID) |
| 298 | if err != nil { |
| 299 | app.refuse(writer, request, err) |
| 300 | return PageView{}, false |
| 301 | } |
| 302 | return page, true |
| 303 | } |
| 304 | |