| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "strconv" |
| 9 | |
| 10 | "github.com/go-chi/chi/v5" |
| 11 | "gorm.io/gorm" |
| 12 | ) |
| 13 | |
| 14 | // Public ids (review M2). A page's URL names it by its nonce, a random |
| 15 | // positive int63 written once at creation, in base 36: /p/2kq9xw3m1ab. |
| 16 | // Never by its row id, which is sequential across every page, private |
| 17 | // ones included, so /p/1 then /p/5 would tell Bo that someone wrote three |
| 18 | // private pages in between. Row ids stay inside the store. |
| 19 | |
| 20 | // pageRef is a page as a request names it: its row id, for the store, |
| 21 | // and its public id, for every link drawn back to it. |
| 22 | type pageRef struct { |
| 23 | ID int64 |
| 24 | Public string |
| 25 | } |
| 26 | |
| 27 | // publicID is a nonce as it appears in a URL. |
| 28 | func publicID(nonce int64) string { return strconv.FormatInt(nonce, 36) } |
| 29 | |
| 30 | // PagePath is a page's own URL path. |
| 31 | func PagePath(page Page) string { return "/p/" + publicID(page.Nonce) } |
| 32 | |
| 33 | // pageHref is the page with public id public, with a notice. |
| 34 | func pageHref(public, done string) string { |
| 35 | target := "/p/" + public |
| 36 | if done != "" { |
| 37 | target += "?done=" + url.QueryEscape(done) |
| 38 | } |
| 39 | return target |
| 40 | } |
| 41 | |
| 42 | // refOf resolves a public id to the page it names, gone or not. Whether |
| 43 | // the person may open it is the caller's question; a page that is not |
| 44 | // there and one they may not see are answered alike. |
| 45 | func (store *Store) refOf(ctx context.Context, |
| 46 | public string) (pageRef, bool, error) { |
| 47 | nonce, err := strconv.ParseInt(public, 36, 64) |
| 48 | if err != nil || nonce <= 0 || publicID(nonce) != public { |
| 49 | return pageRef{}, false, nil |
| 50 | } |
| 51 | var page Page |
| 52 | err = store.database.WithContext(ctx).Select("id"). |
| 53 | Where("nonce = ?", nonce).First(&page).Error |
| 54 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 55 | return pageRef{}, false, nil |
| 56 | } |
| 57 | return pageRef{ID: page.ID, Public: public}, err == nil, err |
| 58 | } |
| 59 | |
| 60 | // pathPage is the page named by {name} in the path, or false. |
| 61 | func (app *app) pathPage(request *http.Request, name string) (pageRef, |
| 62 | bool) { |
| 63 | return app.publicPage(request, chi.URLParam(request, name)) |
| 64 | } |
| 65 | |
| 66 | // publicPage is the page a public id names, or false. |
| 67 | func (app *app) publicPage(request *http.Request, |
| 68 | public string) (pageRef, bool) { |
| 69 | ref, ok, err := app.store.refOf(request.Context(), public) |
| 70 | if err != nil { |
| 71 | app.logger.Error("resolving a page id", "err", err) |
| 72 | } |
| 73 | return ref, ok && err == nil |
| 74 | } |
| 75 | |
| 76 | // publicOf is a page's public id, by row id. |
| 77 | func (store *Store) publicOf(ctx context.Context, |
| 78 | pageID int64) (string, error) { |
| 79 | var page Page |
| 80 | err := store.database.WithContext(ctx).Select("nonce"). |
| 81 | First(&page, pageID).Error |
| 82 | return publicID(page.Nonce), err |
| 83 | } |
| 84 | |