| 1 | package memoria |
| 2 | |
| 3 | // The shared chrome (estilo chrome, round 2): every page's bar, account |
| 4 | // menu and <title> are built here, once, so no page can drift from |
| 5 | // another. |
| 6 | // |
| 7 | // Before this the header was rastrillo's rst-page-header with a hand-made |
| 8 | // nav: at 1440px it wrapped onto two rows (97px), the Help dropdown was a |
| 9 | // block inside an inline nav and its open panel covered Sources and Sign |
| 10 | // out, Sign out was a button beside navigation, and nothing said who was |
| 11 | // signed in. Titles mixed "—" and "·". |
| 12 | // |
| 13 | // Spec: estilo spec/shared-chrome, docs/superpowers/specs/ |
| 14 | // 2026-09-23-shared-chrome.md, §5 (bar), §6 (switcher), §8 (account), §10 |
| 15 | // (titles), §12.1 (Memoria's row). Memoria follows Docs, Sheets and Slides. |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "net/http" |
| 20 | "net/url" |
| 21 | "strings" |
| 22 | |
| 23 | "amadan.net/oficina/estilo.git/chrome" |
| 24 | estilomenu "amadan.net/oficina/estilo.git/menu" |
| 25 | "amadan.net/oficina/home/connect" |
| 26 | "amadan.net/oficina/home/connect/linkstore" |
| 27 | "amadan.net/rastrillo/rastrillo/sessions" |
| 28 | ) |
| 29 | |
| 30 | // appName is the brand and the last part of every <title>. |
| 31 | const appName = "Memoria" |
| 32 | |
| 33 | // Frame is what layout.html reads from every page: the bar, the parts of |
| 34 | // the <title> before the app's name, the Appearance preference for |
| 35 | // <html data-theme>, and whether anybody is signed in (the footer's |
| 36 | // passage count and egress claim are said only to someone who can |
| 37 | // search). |
| 38 | type Frame struct { |
| 39 | Chrome chrome.Bar |
| 40 | TitleParts []string |
| 41 | // WholeTitle, when set, is the whole <title>, in place of TitleParts |
| 42 | // and the app's name: the sign-in door's is rastrillo's for its state |
| 43 | // ("Sign in to Memoria"), which names the app itself (signin.go). |
| 44 | WholeTitle string |
| 45 | Theme string |
| 46 | Signed bool |
| 47 | } |
| 48 | |
| 49 | // frame describes one page's chrome. The zero value is a page whose |
| 50 | // <title> is just "Memoria". |
| 51 | type frame struct { |
| 52 | // parts precede "Memoria" in <title>, most specific first. The first |
| 53 | // is the page's h1 (spec §10). |
| 54 | parts []string |
| 55 | // title, when set, is the whole <title> (Frame.WholeTitle). |
| 56 | title string |
| 57 | // context is the open document's title, shown in the bar. |
| 58 | context string |
| 59 | // signinPage is one of the sign-in, sent and signed-out pages: the |
| 60 | // corner is empty, and the brand is plain text, because everything it |
| 61 | // could link to is behind the session gate and would bounce straight |
| 62 | // back. |
| 63 | signinPage bool |
| 64 | // ret, when set, is where Appearance returns to instead of this |
| 65 | // request's own path and query. |
| 66 | ret string |
| 67 | } |
| 68 | |
| 69 | // titled is the frame for a page whose <title> is these parts. |
| 70 | func titled(parts ...string) frame { return frame{parts: parts} } |
| 71 | |
| 72 | // viewer is who a page's chrome is for. |
| 73 | type viewer struct { |
| 74 | address string |
| 75 | method string |
| 76 | ok bool |
| 77 | // owner is the roster's owner role, in team mode only (team_access.go). |
| 78 | // Outside it the owners list decides, as it always has. |
| 79 | owner bool |
| 80 | } |
| 81 | |
| 82 | // admission is what a request's session says about its person. |
| 83 | type admission int |
| 84 | |
| 85 | const ( |
| 86 | // signedOut: no session at all. |
| 87 | signedOut admission = iota |
| 88 | // admitted: a session whose person may use this instance. |
| 89 | admitted |
| 90 | // removed: a session whose person was resolved and is NOT admitted — |
| 91 | // taken off the owners list after signing in. The session is ended and |
| 92 | // the person told so (offboard), rather than being 404'd on every page |
| 93 | // or bounced between /signin and a page that refuses them. |
| 94 | removed |
| 95 | // unresolved: a session that could not be resolved to a person (a Home |
| 96 | // subject whose lookup failed). Not signed out, not removed: nothing is |
| 97 | // ended on a lookup that may be transient. |
| 98 | unresolved |
| 99 | ) |
| 100 | |
| 101 | // admissionOf reads the request's session without requiring one, and |
| 102 | // without side effects. It is the one place a page learns who is signed |
| 103 | // in outside the session gate (error pages, the sign-in page's shortcut, |
| 104 | // Appearance), and it runs the SAME predicate the gate does: Admit. |
| 105 | func (a *app) admissionOf(r *http.Request) (viewer, admission) { |
| 106 | if a.allowUnauthenticated { |
| 107 | // AppForTest: the stub identity is signed in and admitted. |
| 108 | if a.testIdentity == "" { |
| 109 | return viewer{}, signedOut |
| 110 | } |
| 111 | return viewer{address: a.testIdentity, method: "magiclink", ok: true}, admitted |
| 112 | } |
| 113 | s, ok := sessions.Current(r) |
| 114 | if !ok && a.sess != nil { |
| 115 | s, ok = a.sess.From(r) |
| 116 | } |
| 117 | if !ok || s.Subject == "" { |
| 118 | return viewer{}, signedOut |
| 119 | } |
| 120 | if a.teamMode() { |
| 121 | // The roster decides, never the owners list (team_access.go). |
| 122 | return a.teamAdmission(r, s) |
| 123 | } |
| 124 | switch s.Method { |
| 125 | case connect.HomeMethod: |
| 126 | if a.home == nil { |
| 127 | return viewer{}, unresolved |
| 128 | } |
| 129 | person, err := a.home.bridge.Resolve(r.Context(), s.Subject) |
| 130 | if errors.Is(err, linkstore.ErrRefused) { |
| 131 | // The link store refuses a subject whose linked address Admit |
| 132 | // no longer admits (lookupHomeLocalIdentity answers ErrNoRows |
| 133 | // for it), or that has no link at all: either way the session |
| 134 | // speaks for nobody this instance admits. Removed. A storage |
| 135 | // failure (ErrStorage) may be transient, and ends nothing. |
| 136 | return viewer{}, removed |
| 137 | } |
| 138 | if err != nil { |
| 139 | return viewer{}, unresolved |
| 140 | } |
| 141 | if !a.Admit(person.Label) { |
| 142 | return viewer{}, removed |
| 143 | } |
| 144 | return viewer{address: normaliseAddress(person.Label), method: s.Method, ok: true}, admitted |
| 145 | case "magiclink", "keymail": |
| 146 | if !a.Admit(s.Subject) { |
| 147 | return viewer{}, removed |
| 148 | } |
| 149 | return viewer{address: normaliseAddress(s.Subject), method: s.Method, ok: true}, admitted |
| 150 | } |
| 151 | // Any other method is not a sign-in memoria recognises (identity says |
| 152 | // the same), and it is treated as nobody. |
| 153 | return viewer{}, signedOut |
| 154 | } |
| 155 | |
| 156 | // offboard ends the session of a person who is no longer admitted and |
| 157 | // sends a page request to the sign-in page, which says so. Anything else |
| 158 | // gets the 404 the gate has always answered. |
| 159 | // |
| 160 | // Under the door's admission probe (signin_admission.go) it only answers: |
| 161 | // the probe decides what to end, and must not revoke a session it was |
| 162 | // merely asked about, such as the one MintHomeSession has just stored, |
| 163 | // which then lands on its first page and is told why there. The door |
| 164 | // ends a session it refuses itself (refusedSession). |
| 165 | func (a *app) offboard(w http.ResponseWriter, r *http.Request) { |
| 166 | if a.sess != nil && !probing(r.Context()) { |
| 167 | a.sess.SignOut(w, r) |
| 168 | } |
| 169 | w.Header().Set("Cache-Control", "no-store") |
| 170 | if r.Method == http.MethodGet || r.Method == http.MethodHead { |
| 171 | http.Redirect(w, r, signinPath+"?err=removed", http.StatusSeeOther) |
| 172 | return |
| 173 | } |
| 174 | a.errorPage(w, r, http.StatusNotFound, kindRoute, "") |
| 175 | } |
| 176 | |
| 177 | // frameFor builds a page's Frame for the request's viewer. |
| 178 | func (a *app) frameFor(r *http.Request, f frame) Frame { |
| 179 | var v viewer |
| 180 | if !f.signinPage { |
| 181 | v, _ = a.admissionOf(r) |
| 182 | } |
| 183 | return a.frameAs(r, v, f) |
| 184 | } |
| 185 | |
| 186 | // frameAs builds a page's Frame for viewer v. |
| 187 | func (a *app) frameAs(r *http.Request, v viewer, f frame) Frame { |
| 188 | bar := chrome.Bar{ |
| 189 | App: chrome.App{ |
| 190 | Name: appName, |
| 191 | Slug: "memoria", |
| 192 | Home: "/", |
| 193 | Icon: assets.Path("static/brand/icon.svg"), |
| 194 | }, |
| 195 | Context: f.context, |
| 196 | Labels: chrome.BarLabels{Skip: "Skip to content", Menu: "Menu"}, |
| 197 | } |
| 198 | switch { |
| 199 | case f.signinPage: |
| 200 | bar.App.Home = "" |
| 201 | case v.ok: |
| 202 | bar.Account = a.account(r, v, f.ret) |
| 203 | bar.Switcher = a.switcher() |
| 204 | bar.Nav, bar.NavLabel = a.nav(r, v), "Memoria" |
| 205 | default: |
| 206 | bar.SignIn = &chrome.Link{Href: signinPath + "?return_to=" + url.QueryEscape(returnTarget(r)), Label: "Sign in"} |
| 207 | } |
| 208 | return Frame{ |
| 209 | Chrome: bar, |
| 210 | TitleParts: f.parts, |
| 211 | WholeTitle: f.title, |
| 212 | Theme: chrome.ThemeAttr(chrome.Appearance(r)), |
| 213 | Signed: v.ok, |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // nav is the bar's section links. Sources is rendered only for owners: |
| 218 | // /connectors is behind requireOwner, which 404s everyone else on purpose, |
| 219 | // so a non-owner must never be shown the link (spec §12.1, Memoria). |
| 220 | func (a *app) nav(r *http.Request, v viewer) []chrome.Link { |
| 221 | links := []chrome.Link{{Href: "/", Label: "Search", Current: r.URL.Path == "/"}} |
| 222 | // Everything stored (Paul, 2026-10-29: the list moved off the front |
| 223 | // page). A document's own page is under it too. |
| 224 | links = append(links, chrome.Link{Href: "/documents", Label: "Documents", Current: r.URL.Path == "/documents" || strings.HasPrefix(r.URL.Path, "/documents/") || r.URL.Path == "/add"}) |
| 225 | // Every signed-in person's own corrections (§8.10). |
| 226 | links = append(links, chrome.Link{Href: "/reports", Label: "Reports", Current: r.URL.Path == "/reports"}) |
| 227 | owner := a.isOwner(v.address) |
| 228 | if a.teamMode() { |
| 229 | // The roster's owner role: an address on MEMORIA_OWNERS only |
| 230 | // admits a local sign-in in team mode. |
| 231 | owner = v.owner |
| 232 | } |
| 233 | if owner { |
| 234 | links = append(links, chrome.Link{Href: "/connectors", Label: "Sources", Current: r.URL.Path == "/connectors" || strings.HasPrefix(r.URL.Path, "/connectors/")}) |
| 235 | } |
| 236 | return links |
| 237 | } |
| 238 | |
| 239 | // returnTarget is where a person should come back to: this page, if it is |
| 240 | // a page (a GET), otherwise the index. |
| 241 | func returnTarget(r *http.Request) string { |
| 242 | if r.Method != http.MethodGet && r.Method != http.MethodHead { |
| 243 | return "/" |
| 244 | } |
| 245 | return r.URL.RequestURI() |
| 246 | } |
| 247 | |
| 248 | // account is the account menu (spec §8.2): who is signed in, "Connect your |
| 249 | // Oficina account" when that means something, Appearance, Help (Open |
| 250 | // source, last), and the one Sign out in the app. |
| 251 | func (a *app) account(r *http.Request, v viewer, ret string) *chrome.Account { |
| 252 | if ret == "" { |
| 253 | ret = returnTarget(r) |
| 254 | } |
| 255 | acc := &chrome.Account{ |
| 256 | TriggerLabel: "Account", |
| 257 | Initials: chrome.Initials("", v.address), |
| 258 | Address: v.address, |
| 259 | Labels: chrome.AccountLabels{SignedInAs: "Signed in as", Appearance: "Appearance", Help: "Help"}, |
| 260 | Appearance: &chrome.AppearanceMenu{ |
| 261 | Action: "/appearance", |
| 262 | Return: ret, |
| 263 | Options: chrome.AppearanceOptions(chrome.Appearance(r), "Light", "Dark", "System"), |
| 264 | }, |
| 265 | Help: []estilomenu.Item{{Href: "/help/open-source", Label: "Open source"}}, |
| 266 | SignOut: chrome.SignOut{Action: "/signout", Label: "Sign out"}, |
| 267 | } |
| 268 | if v.address != "" { |
| 269 | acc.TriggerLabel = "Account: " + v.address |
| 270 | } |
| 271 | if a.offersHomeLink(r, v) { |
| 272 | acc.Items = []estilomenu.Item{{Href: "/account/home", Label: "Connect your Oficina account"}} |
| 273 | } |
| 274 | return acc |
| 275 | } |
| 276 | |
| 277 | // offersHomeLink is whether the account menu offers to connect the |
| 278 | // person's Oficina account: only when Home is configured, to somebody |
| 279 | // signed in locally whose account has NO stored link (spec §12.1 item 4). |
| 280 | // The stored link decides, not how this session signed in: a linked |
| 281 | // person who used Memoria's own email link is still linked (Paul, |
| 282 | // 2026-10-24). A Home session is the link itself. Anything that cannot be |
| 283 | // determined offers nothing. One indexed lookup per menu render, two once |
| 284 | // the person has a local identity row. |
| 285 | func (a *app) offersHomeLink(r *http.Request, v viewer) bool { |
| 286 | if a.home == nil || !v.ok || (v.method != "magiclink" && v.method != "keymail") { |
| 287 | return false |
| 288 | } |
| 289 | id, found, err := a.homeLocalID(r.Context(), v.address) |
| 290 | if err != nil { |
| 291 | return false |
| 292 | } |
| 293 | if !found { |
| 294 | // No local identity row, so no link can name this person. |
| 295 | return true |
| 296 | } |
| 297 | linked, err := a.home.bridge.Linked(r.Context(), id) |
| 298 | return err == nil && !linked |
| 299 | } |
| 300 | |
| 301 | // switcher is the app switcher, or nil when no hub directory is |
| 302 | // configured: the suite's six apps as tiles, each at its own host under |
| 303 | // the directory's (estilo's chrome.SuiteApps), with Memoria marked |
| 304 | // current, then "All apps". The tile icons are served by brand.Handler, |
| 305 | // mounted in app.go. |
| 306 | func (a *app) switcher() *chrome.Switcher { |
| 307 | if a.home == nil || a.directory == "" { |
| 308 | return nil |
| 309 | } |
| 310 | return &chrome.Switcher{ |
| 311 | Label: "Oficina apps", |
| 312 | CurrentLabel: "(this app)", |
| 313 | Apps: chrome.SuiteApps(a.directory, "memoria"), |
| 314 | AllApps: estilomenu.Item{Href: a.directory + "/", Label: "All apps"}, |
| 315 | } |
| 316 | } |
| 317 | |