| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | ) |
| 7 | |
| 8 | // settingsData is templates/settings.html's data. |
| 9 | type settingsData struct { |
| 10 | Zone, Team string |
| 11 | Own bool |
| 12 | Value, Error string |
| 13 | Notice string |
| 14 | // Format is the date and number format's field (build note 85). |
| 15 | Format formatField |
| 16 | } |
| 17 | |
| 18 | // settings is the time zone: the person's own, defaulting to the team's |
| 19 | // (§6). The script layer offers to switch when the browser's zone |
| 20 | // differs; it never switches silently. |
| 21 | func (app *app) settings(writer http.ResponseWriter, |
| 22 | request *http.Request) { |
| 23 | app.showSettings(writer, request, http.StatusOK, "", "") |
| 24 | } |
| 25 | |
| 26 | func (app *app) showSettings(writer http.ResponseWriter, |
| 27 | request *http.Request, status int, value, problem string) { |
| 28 | viewer, err := app.viewer(request) |
| 29 | if err != nil { |
| 30 | app.fail(writer, request, err) |
| 31 | return |
| 32 | } |
| 33 | data := settingsData{Zone: viewer.TimeZone.String(), |
| 34 | Team: app.config.TimeZone.String(), Own: viewer.OwnTimeZone, |
| 35 | Value: firstOf(value, viewer.TimeZone.String()), Error: problem} |
| 36 | today := todayIn(app.store.now(), viewer.TimeZone) |
| 37 | data.Format = formatFor(viewer, browserIn(request), today) |
| 38 | data.Format.Error, _ = request.Context().Value(formatProblem{}).(string) |
| 39 | switch request.URL.Query().Get("done") { |
| 40 | case "zone": |
| 41 | data.Notice = "Saved. Today is now " + |
| 42 | viewer.Locale.Long(today) + "." |
| 43 | case "locale": |
| 44 | data.Notice = formatNotice(viewer) |
| 45 | } |
| 46 | app.show(writer, request, status, "settings", view{Title: "Settings", |
| 47 | Nav: nav(""), Viewer: viewer, Data: data}) |
| 48 | } |
| 49 | |
| 50 | func (app *app) saveSettings(writer http.ResponseWriter, |
| 51 | request *http.Request) { |
| 52 | address, _ := app.identity(request) |
| 53 | zone := request.PostFormValue("zone") |
| 54 | err := app.store.SetTimeZone(request.Context(), address, zone) |
| 55 | if errors.Is(err, ErrBadTimeZone) { |
| 56 | app.showSettings(writer, request, http.StatusUnprocessableEntity, |
| 57 | zone, "That isn't a time zone name. Use one like "+ |
| 58 | "Europe/Dublin or America/New_York.") |
| 59 | return |
| 60 | } |
| 61 | if err != nil { |
| 62 | app.fail(writer, request, err) |
| 63 | return |
| 64 | } |
| 65 | target := firstOf(safeReturn(request.PostFormValue("return")), |
| 66 | "/settings?done=zone") |
| 67 | seeOther(writer, request, target) |
| 68 | } |
| 69 | |
| 70 | // safeReturn is a same-app path to go back to, or "". |
| 71 | func safeReturn(path string) string { |
| 72 | for _, char := range path { |
| 73 | // A control character (a tab or newline among them) can split |
| 74 | // or bend a Location header's meaning in a client (review L4). |
| 75 | if char < 0x20 || char == 0x7f || char == '\\' { |
| 76 | return "" |
| 77 | } |
| 78 | } |
| 79 | if path == "/" || (len(path) > 1 && path[0] == '/' && path[1] != '/') { |
| 80 | return path |
| 81 | } |
| 82 | return "" |
| 83 | } |
| 84 | |