| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 11 | |
| 12 | "github.com/go-chi/chi/v5" |
| 13 | ) |
| 14 | |
| 15 | // The look's routes (columns and icons spec §2.5, §3.3). POST |
| 16 | // /p/{id}/look sets or removes a team page's icon and banner: the |
| 17 | // picker's and the banner's controls send it asking for JSON, and the |
| 18 | // scriptless page /p/{id}/look sends it as a form, its file inputs as |
| 19 | // multipart with ?upload=1&for=icon or for=banner. |
| 20 | |
| 21 | func (app *app) lookRoutes(router chi.Router) { |
| 22 | router.Get("/p/{id}/look", app.lookForm) |
| 23 | router.Post("/p/{id}/look", app.setLook) |
| 24 | router.Get(emojiPath, serveEmoji) |
| 25 | } |
| 26 | |
| 27 | // lookPage is the scriptless page's data. |
| 28 | type lookPage struct { |
| 29 | Page PageView |
| 30 | Banner *BannerView |
| 31 | Emoji string |
| 32 | Error, Notice string |
| 33 | } |
| 34 | |
| 35 | func (app *app) lookForm(writer http.ResponseWriter, request *http.Request) { |
| 36 | page, ok := app.teamOnly(writer, request) |
| 37 | if !ok { |
| 38 | return |
| 39 | } |
| 40 | app.showLook(writer, request, http.StatusOK, lookPage{Page: page, |
| 41 | Notice: notice(request)}) |
| 42 | } |
| 43 | |
| 44 | func (app *app) showLook(writer http.ResponseWriter, request *http.Request, |
| 45 | status int, data lookPage) { |
| 46 | viewer, err := app.viewer(request) |
| 47 | if err != nil { |
| 48 | app.fail(writer, request, err) |
| 49 | return |
| 50 | } |
| 51 | data.Banner = app.bannerOf(request, viewer, data.Page) |
| 52 | app.show(writer, request, status, "look", view{ |
| 53 | Title: "Icon and banner", Nav: nav("/pages"), Viewer: viewer, |
| 54 | Data: data}) |
| 55 | } |
| 56 | |
| 57 | // setLook is the look's POST. |
| 58 | func (app *app) setLook(writer http.ResponseWriter, request *http.Request) { |
| 59 | page, ok := app.teamOnly(writer, request) |
| 60 | if !ok { |
| 61 | return |
| 62 | } |
| 63 | address, _ := app.identity(request) |
| 64 | change, uploaded, err := app.lookChange(writer, request) |
| 65 | if errors.Is(err, errUnread) { |
| 66 | writer.Header().Set("Retry-After", "5") |
| 67 | ErrorPage(writer, request, http.StatusServiceUnavailable, "") |
| 68 | return |
| 69 | } |
| 70 | var done Restyled |
| 71 | if err == nil { |
| 72 | done, err = app.store.SetLook(request.Context(), address, page.ID, |
| 73 | change) |
| 74 | } |
| 75 | if err != nil && uploaded != "" { |
| 76 | // A picture the look refused is not kept for seven days. |
| 77 | app.dropUpload(request.Context(), address, uploaded) |
| 78 | } |
| 79 | app.answerLook(writer, request, page, done, err) |
| 80 | } |
| 81 | |
| 82 | // answerLook is the look's reply: JSON for the script, the page (or |
| 83 | // the look page again, with the refusal) for a form. |
| 84 | func (app *app) answerLook(writer http.ResponseWriter, |
| 85 | request *http.Request, page PageView, done Restyled, err error) { |
| 86 | say, sayable := lookRefusal(request, err) |
| 87 | switch { |
| 88 | case err != nil && !sayable: |
| 89 | app.refuse(writer, request, err) |
| 90 | case wantsJSON(request) && sayable: |
| 91 | refusedJSON(writer, say, "") |
| 92 | case wantsJSON(request): |
| 93 | answer(writer, map[string]any{"from": idText(done.From), |
| 94 | "to": idText(done.To)}) |
| 95 | case sayable: |
| 96 | app.showLook(writer, request, http.StatusUnprocessableEntity, |
| 97 | lookPage{Page: page, Error: say, |
| 98 | Emoji: request.PostFormValue("emoji")}) |
| 99 | default: |
| 100 | seeOther(writer, request, page.Href) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func (app *app) dropUpload(ctx context.Context, address, public string) { |
| 105 | if err := app.store.DropOwnPending(ctx, address, public); err != nil { |
| 106 | app.logger.Error("a refused look image was not removed", |
| 107 | "err", err) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // lookChange reads what the post asks for, uploading its file first |
| 112 | // when it carries one. uploaded is that file's public id. |
| 113 | func (app *app) lookChange(writer http.ResponseWriter, |
| 114 | request *http.Request) (LookChange, string, error) { |
| 115 | if isUpload(request) { |
| 116 | image, err := app.receiveImage(writer, request, maxLookForm) |
| 117 | if err != nil { |
| 118 | return LookChange{}, "", err |
| 119 | } |
| 120 | upload := request.URL.Query().Get("for") |
| 121 | if upload != "icon" && upload != "banner" { |
| 122 | return LookChange{}, image.PublicID, errLookUnknown |
| 123 | } |
| 124 | change, err := lookOf(upload, image.PublicID, request) |
| 125 | return change, image.PublicID, err |
| 126 | } |
| 127 | image := request.PostFormValue("image") |
| 128 | change, err := lookOf(request.PostFormValue("do"), image, request) |
| 129 | if err != nil || !(change.SetIcon && change.Icon != nil && |
| 130 | change.Icon.Image != "") && !(change.Banner != nil && |
| 131 | change.Banner.Image != "") { |
| 132 | image = "" |
| 133 | } |
| 134 | // A picture the script uploaded is dropped if the look refuses it: |
| 135 | // only the person's own pending picture (DropOwnPending), so a |
| 136 | // hand-made post can at most drop a picture of their own. |
| 137 | return change, image, err |
| 138 | } |
| 139 | |
| 140 | // maxLookForm bounds the look form's text fields. |
| 141 | const maxLookForm = 4 << 10 |
| 142 | |
| 143 | // errLookUnknown is a post that asks for nothing the look knows: the |
| 144 | // app's 404, as for a route that is not there. |
| 145 | var errLookUnknown = ErrNotFound |
| 146 | |
| 147 | // lookOf is one look change: what do names, with image or the form's |
| 148 | // other fields. |
| 149 | func lookOf(do, image string, request *http.Request) (LookChange, error) { |
| 150 | position, err := strconv.Atoi(request.PostFormValue("y")) |
| 151 | if err != nil { |
| 152 | if do == "position" { |
| 153 | // No position chosen keeps the one stored (§3.2). |
| 154 | return LookChange{}, nil |
| 155 | } |
| 156 | position = 50 |
| 157 | } |
| 158 | switch do { |
| 159 | case "emoji": |
| 160 | icon, err := EmojiIcon(strings.TrimSpace( |
| 161 | request.PostFormValue("emoji"))) |
| 162 | return LookChange{SetIcon: true, Icon: icon}, err |
| 163 | case "icon": |
| 164 | if !markup.IsImageID(image) { |
| 165 | return LookChange{}, errLookUnknown |
| 166 | } |
| 167 | return LookChange{SetIcon: true, Icon: &Icon{Image: image}}, nil |
| 168 | case "remove-icon": |
| 169 | return LookChange{SetIcon: true}, nil |
| 170 | case "banner": |
| 171 | if !markup.IsImageID(image) { |
| 172 | return LookChange{}, errLookUnknown |
| 173 | } |
| 174 | return LookChange{SetBanner: true, |
| 175 | Banner: &Banner{Image: image, Y: position}}, nil |
| 176 | case "remove-banner": |
| 177 | return LookChange{SetBanner: true}, nil |
| 178 | case "position": |
| 179 | return LookChange{SetBanner: true, Banner: &Banner{Y: position}}, |
| 180 | nil |
| 181 | } |
| 182 | return LookChange{}, errLookUnknown |
| 183 | } |
| 184 | |
| 185 | // lookRefusal is a refused look in words, and whether it is one a |
| 186 | // person can act on. The sizes are written in the reader's locale. |
| 187 | func lookRefusal(request *http.Request, err error) (string, bool) { |
| 188 | dates := localeIn(request) |
| 189 | switch { |
| 190 | case err == nil: |
| 191 | return "", false |
| 192 | case errors.Is(err, ErrIconEmoji), errors.Is(err, errNoFile): |
| 193 | return err.Error(), true |
| 194 | case errors.Is(err, ErrIconTooLarge): |
| 195 | return "An icon picture can be up to " + |
| 196 | byteSize(dates, MaxIconBytes) + " and " + |
| 197 | dates.Number(MaxIconSide) + " pixels on each side.", true |
| 198 | } |
| 199 | if _, say, ok := imageRefusal(err, dates); ok { |
| 200 | return say, true |
| 201 | } |
| 202 | return "", false |
| 203 | } |
| 204 | |