package web import ( "net/http" "strings" "amadan.net/oficina/base/internal/look" ) // Base's small JSON API for its native app (docs/api/base-native-v1.md): // reads only, behind the member gate, with the session the native exchange // minted. Each answer is what the web page of the same name shows, from the // same builders. type apiMember struct { ID string `json:"id"` Name string `json:"name"` Email string `json:"email"` Initials string `json:"initials"` Owner bool `json:"owner"` } type apiBase struct { Name string `json:"name"` Slug string `json:"slug"` Origin string `json:"origin"` Timezone string `json:"timezone"` } type apiMe struct { Member apiMember `json:"member"` Base apiBase `json:"base"` // Appearance is "system", "light" or "dark". Appearance string `json:"appearance"` // EquipoOrigin is the Equipo instance Ask Base relays to, null with // Ask off. The app asks Oficina ID for an assertion addressed to it. EquipoOrigin *string `json:"equipo_origin"` } // apiMeRead is GET /api/v1/me. func (a *app) apiMeRead(w http.ResponseWriter, r *http.Request) { me, _ := CurrentMember(r) name := displayName(me) out := apiMe{ Member: apiMember{ID: me.ID, Name: name, Email: me.Email, Initials: initials(name), Owner: me.Role == "owner"}, Base: apiBase{Name: a.cfg.Name, Slug: a.cfg.Slug, Origin: a.cfg.Origin, Timezone: "UTC"}, Appearance: "system", } if a.cfg.Timezone != nil { out.Base.Timezone = a.cfg.Timezone.String() } if me.Theme != "" { out.Appearance = me.Theme } if a.ask != nil { origin := a.cfg.EquipoOrigin out.EquipoOrigin = &origin } writeJSON(w, http.StatusOK, out) } type apiStep struct { Label string `json:"label"` Done bool `json:"done"` } // apiApp is one app on Home. An Oficina app has its icon at IconURL, a // path on Base's origin, and its native bundle ID (and, when its native app // opens by one, a URL scheme); an installed app or one of Base's own pages // has line art (IconSVG, drawn in currentColor) on a tint, one of the // catalogue's tint names. type apiApp struct { ID string `json:"id"` Kind string `json:"kind"` Name string `json:"name"` URL string `json:"url"` IconURL string `json:"icon_url,omitempty"` Tint string `json:"tint,omitempty"` IconSVG string `json:"icon_svg,omitempty"` BundleID string `json:"bundle_id,omitempty"` Scheme string `json:"scheme,omitempty"` } type apiHome struct { Greeting string `json:"greeting"` Lede string `json:"lede"` Steps []apiStep `json:"steps"` Apps []apiApp `json:"apps"` } // apiHomeRead is GET /api/v1/home: Home's steps and apps, in Home's order // (the team's Oficina apps, the installed apps, then Base's own pages). func (a *app) apiHomeRead(w http.ResponseWriter, r *http.Request) { me, _ := CurrentMember(r) c, err := a.homeContent(r.Context(), me) if err != nil { a.logger.Error("api home", "err", err) writeJSON(w, http.StatusServiceUnavailable, apiError{Code: "unavailable"}) return } out := apiHome{Greeting: c.Greeting, Lede: c.Lede, Steps: []apiStep{}, Apps: []apiApp{}} for _, s := range c.Steps { out.Steps = append(out.Steps, apiStep(s)) } bundles := map[string]struct{ bundle, scheme string }{} for _, o := range a.cfg.OficinaApps { bundles[o.ID] = struct{ bundle, scheme string }{o.BundleID(), o.Scheme()} } for _, t := range append(append([]tile{}, c.Apps...), c.Dock...) { app := apiApp{ID: t.ID, Kind: t.Kind, Name: t.Name, URL: t.Href} switch t.Kind { case "oficina": app.IconURL = look.Assets.Path(t.Image) app.BundleID, app.Scheme = bundles[t.ID].bundle, bundles[t.ID].scheme case "own": // Base's own pages open on the web. app.URL = a.cfg.Origin + t.Href fallthrough default: app.Tint, app.IconSVG = t.Tint, string(t.Icon) } out.Apps = append(out.Apps, app) } writeJSON(w, http.StatusOK, out) } // apiError is every JSON refusal's body. type apiError struct { Code string `json:"code"` } // apiRequest reports whether a request is the native app's or a script's, // which hear a refusal as JSON, never as a redirect to /signin: everything // under /api/, and anything asking for JSON or an event stream. func apiRequest(r *http.Request) bool { if strings.HasPrefix(r.URL.Path, "/api/") { return true } accept := r.Header.Get("Accept") return strings.Contains(accept, "application/json") || strings.Contains(accept, "text/event-stream") } // unauthenticated is the JSON refusal for a request with no session, or one // that names nobody who is a member now. The native app hears 401 and signs // in again. func unauthenticated(w http.ResponseWriter) { writeJSON(w, http.StatusUnauthorized, apiError{Code: "unauthenticated"}) }