| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | |
| 8 | "amadan.net/oficina/estilo.git/chrome" |
| 9 | "amadan.net/rastrillo/rastrillo" |
| 10 | ) |
| 11 | |
| 12 | // The share popover (spec 2026-11-11-editor-i2-design.md, "Share as a |
| 13 | // popover" and revision 2's P1.1). The editor's Share opens a panel |
| 14 | // under its button instead of a page of its own. The panel is the |
| 15 | // share page's state rendered as a fragment, "docs-share-panel" in |
| 16 | // views/share.html, and every control in it is a real form posting to |
| 17 | // the share routes as the page's do. brand/share-pop.js sends each with |
| 18 | // fetch and draws the answer, which is the fragment again: the handlers |
| 19 | // redirect a panel form back to ?panel=1 and the redirected GET, a fetch, |
| 20 | // gets the panel. The server stays the one authority, and the script |
| 21 | // composes nothing. |
| 22 | // |
| 23 | // The one-time public link (oneshot.go) is taken by whichever render |
| 24 | // follows the publish. With the panel's redirect, that render is the |
| 25 | // fragment the popover shows, so the person who published sees it. |
| 26 | |
| 27 | // sharePanelRequest reports whether r asks for the panel rather than |
| 28 | // the page: panel=1, in the query or a POST body, and a request a |
| 29 | // script made (Sec-Fetch-Dest: empty, which is what fetch sends). Every |
| 30 | // other destination gets the whole page: a navigation (document), a |
| 31 | // frame or embed (iframe, frame, embed, object), and a client that |
| 32 | // sends no Sec-Fetch-Dest at all. So a typed, reloaded, bookmarked or |
| 33 | // framed ?panel=1 is never a bare fragment. An allow-list, not a |
| 34 | // deny-list of "document" (review on #106). |
| 35 | func sharePanelRequest(r *http.Request) bool { |
| 36 | if r.FormValue("panel") != "1" { |
| 37 | return false |
| 38 | } |
| 39 | return r.Header.Get("Sec-Fetch-Dest") == "empty" |
| 40 | } |
| 41 | |
| 42 | // shareBack is where a share mutation's 303 goes: the panel when the |
| 43 | // form came from it, otherwise the page. |
| 44 | func shareBack(r *http.Request, sqid string) string { |
| 45 | if r.PostFormValue("panel") == "1" { |
| 46 | return "/d/" + sqid + "/share?panel=1" |
| 47 | } |
| 48 | return "/d/" + sqid + "/share" |
| 49 | } |
| 50 | |
| 51 | // sharePanelLabels are the panel's own words, beside the page's. |
| 52 | type sharePanelLabels struct { |
| 53 | Heading, Close, Failed string |
| 54 | InvitePlaceholder, Invite, Access string |
| 55 | CanRead, CanEdit string |
| 56 | People, General, Owner, You string |
| 57 | TeamOn, TeamOff, TeamSwitch string |
| 58 | LinkHeading, LinkOn, LinkOff string |
| 59 | LinkSwitch, Once, Shown, NewLink string |
| 60 | } |
| 61 | |
| 62 | // sharePanelGrant is one person in the panel's list. |
| 63 | type sharePanelGrant struct { |
| 64 | Address, Initial, Access, Remove string |
| 65 | } |
| 66 | |
| 67 | // initialOf is the avatar letter for an address, estilo's rule, and a |
| 68 | // question mark when it has none. |
| 69 | func initialOf(address string) string { |
| 70 | if i := chrome.Initials("", address); i != "" { |
| 71 | return i |
| 72 | } |
| 73 | return "?" |
| 74 | } |
| 75 | |
| 76 | // panelData fills page's panel fields for r. |
| 77 | func (h *handlers) panelData(r *http.Request, page *sharePage, grants []*Grant) { |
| 78 | me, _ := h.viewer(r) |
| 79 | page.Me = me |
| 80 | page.MeInitial = initialOf(me) |
| 81 | page.Panel = sharePanelLabels{ |
| 82 | Heading: rastrillo.Tf(r, "docs.share.panel.heading", "title", page.DocTitle), |
| 83 | Close: rastrillo.T(r, "docs.share.panel.close"), |
| 84 | Failed: rastrillo.T(r, "docs.share.panel.failed"), |
| 85 | InvitePlaceholder: rastrillo.T(r, "docs.share.panel.invite_placeholder"), |
| 86 | Invite: rastrillo.T(r, "docs.share.panel.invite"), |
| 87 | Access: rastrillo.T(r, "docs.share.panel.access"), |
| 88 | CanRead: rastrillo.T(r, "docs.share.panel.can_read"), |
| 89 | CanEdit: rastrillo.T(r, "docs.share.panel.can_edit"), |
| 90 | People: rastrillo.T(r, "docs.share.panel.people"), |
| 91 | General: rastrillo.T(r, "docs.share.panel.general"), |
| 92 | Owner: rastrillo.T(r, "docs.share.panel.owner"), |
| 93 | You: rastrillo.T(r, "docs.share.panel.you"), |
| 94 | TeamOn: rastrillo.T(r, "docs.share.panel.team_on"), |
| 95 | TeamOff: rastrillo.T(r, "docs.share.panel.team_off"), |
| 96 | TeamSwitch: rastrillo.T(r, "docs.share.panel.team_switch"), |
| 97 | LinkHeading: rastrillo.T(r, "docs.share.panel.link_heading"), |
| 98 | LinkOn: rastrillo.T(r, "docs.share.panel.link_on"), |
| 99 | LinkOff: rastrillo.T(r, "docs.share.panel.link_off"), |
| 100 | LinkSwitch: rastrillo.T(r, "docs.share.panel.link_switch"), |
| 101 | Once: rastrillo.T(r, "docs.share.panel.once"), |
| 102 | Shown: rastrillo.T(r, "docs.share.panel.shown"), |
| 103 | NewLink: rastrillo.T(r, "docs.share.panel.new_link"), |
| 104 | } |
| 105 | page.PanelGrants = make([]sharePanelGrant, 0, len(grants)) |
| 106 | for _, g := range grants { |
| 107 | access := page.Panel.CanRead |
| 108 | if g.Role == RoleWriter { |
| 109 | access = page.Panel.CanEdit |
| 110 | } |
| 111 | page.PanelGrants = append(page.PanelGrants, sharePanelGrant{ |
| 112 | Address: g.Subject, |
| 113 | Initial: initialOf(g.Subject), |
| 114 | Access: access, |
| 115 | Remove: rastrillo.Tf(r, "docs.share.panel.remove", "address", g.Subject), |
| 116 | }) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // fragmentStatus renders one named template of a page, with no layout, |
| 121 | // into w: the share panel. Buffered for the same reason pageStatus is. |
| 122 | func (rd *renderer) fragmentStatus(w http.ResponseWriter, r *http.Request, page, name string, data any, status int) error { |
| 123 | tmpl, ok := rd.pages[page] |
| 124 | if !ok { |
| 125 | return fmt.Errorf("docs: unknown page %q", page) |
| 126 | } |
| 127 | var buf bytes.Buffer |
| 128 | if err := tmpl.ExecuteTemplate(&buf, name, data); err != nil { |
| 129 | return fmt.Errorf("docs: render %q of %q: %w", name, page, err) |
| 130 | } |
| 131 | w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 132 | pageHeaders(w) |
| 133 | return writeGuardedBody(w, r, status, buf.Bytes()) |
| 134 | } |
| 135 | |