oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1package web
2
3import (
4 "net/http"
5 "strings"
6
7 "amadan.net/oficina/base/internal/look"
8)
9
10// Base's small JSON API for its native app (docs/api/base-native-v1.md):
11// reads only, behind the member gate, with the session the native exchange
12// minted. Each answer is what the web page of the same name shows, from the
13// same builders.
14
15type apiMember struct {
16 ID string `json:"id"`
17 Name string `json:"name"`
18 Email string `json:"email"`
19 Initials string `json:"initials"`
20 Owner bool `json:"owner"`
21}
22
23type apiBase struct {
24 Name string `json:"name"`
25 Slug string `json:"slug"`
26 Origin string `json:"origin"`
27 Timezone string `json:"timezone"`
28}
29
30type apiMe struct {
31 Member apiMember `json:"member"`
32 Base apiBase `json:"base"`
33 // Appearance is "system", "light" or "dark".
34 Appearance string `json:"appearance"`
35 // EquipoOrigin is the Equipo instance Ask Base relays to, null with
36 // Ask off. The app asks Oficina ID for an assertion addressed to it.
37 EquipoOrigin *string `json:"equipo_origin"`
38}
39
40// apiMeRead is GET /api/v1/me.
41func (a *app) apiMeRead(w http.ResponseWriter, r *http.Request) {
42 me, _ := CurrentMember(r)
43 name := displayName(me)
44 out := apiMe{
45 Member: apiMember{ID: me.ID, Name: name, Email: me.Email, Initials: initials(name), Owner: me.Role == "owner"},
46 Base: apiBase{Name: a.cfg.Name, Slug: a.cfg.Slug, Origin: a.cfg.Origin, Timezone: "UTC"},
47 Appearance: "system",
48 }
49 if a.cfg.Timezone != nil {
50 out.Base.Timezone = a.cfg.Timezone.String()
51 }
52 if me.Theme != "" {
53 out.Appearance = me.Theme
54 }
55 if a.ask != nil {
56 origin := a.cfg.EquipoOrigin
57 out.EquipoOrigin = &origin
58 }
59 writeJSON(w, http.StatusOK, out)
60}
61
62type apiStep struct {
63 Label string `json:"label"`
64 Done bool `json:"done"`
65}
66
67// apiApp is one app on Home. An Oficina app has its icon at IconURL, a
68// path on Base's origin, and its native bundle ID (and, when its native app
69// opens by one, a URL scheme); an installed app or one of Base's own pages
70// has line art (IconSVG, drawn in currentColor) on a tint, one of the
71// catalogue's tint names.
72type apiApp struct {
73 ID string `json:"id"`
74 Kind string `json:"kind"`
75 Name string `json:"name"`
76 URL string `json:"url"`
77 IconURL string `json:"icon_url,omitempty"`
78 Tint string `json:"tint,omitempty"`
79 IconSVG string `json:"icon_svg,omitempty"`
80 BundleID string `json:"bundle_id,omitempty"`
81 Scheme string `json:"scheme,omitempty"`
82}
83
84type apiHome struct {
85 Greeting string `json:"greeting"`
86 Lede string `json:"lede"`
87 Steps []apiStep `json:"steps"`
88 Apps []apiApp `json:"apps"`
89}
90
91// apiHomeRead is GET /api/v1/home: Home's steps and apps, in Home's order
92// (the team's Oficina apps, the installed apps, then Base's own pages).
93func (a *app) apiHomeRead(w http.ResponseWriter, r *http.Request) {
94 me, _ := CurrentMember(r)
95 c, err := a.homeContent(r.Context(), me)
96 if err != nil {
97 a.logger.Error("api home", "err", err)
98 writeJSON(w, http.StatusServiceUnavailable, apiError{Code: "unavailable"})
99 return
100 }
101 out := apiHome{Greeting: c.Greeting, Lede: c.Lede, Steps: []apiStep{}, Apps: []apiApp{}}
102 for _, s := range c.Steps {
103 out.Steps = append(out.Steps, apiStep(s))
104 }
105 bundles := map[string]struct{ bundle, scheme string }{}
106 for _, o := range a.cfg.OficinaApps {
107 bundles[o.ID] = struct{ bundle, scheme string }{o.BundleID(), o.Scheme()}
108 }
109 for _, t := range append(append([]tile{}, c.Apps...), c.Dock...) {
110 app := apiApp{ID: t.ID, Kind: t.Kind, Name: t.Name, URL: t.Href}
111 switch t.Kind {
112 case "oficina":
113 app.IconURL = look.Assets.Path(t.Image)
114 app.BundleID, app.Scheme = bundles[t.ID].bundle, bundles[t.ID].scheme
115 case "own":
116 // Base's own pages open on the web.
117 app.URL = a.cfg.Origin + t.Href
118 fallthrough
119 default:
120 app.Tint, app.IconSVG = t.Tint, string(t.Icon)
121 }
122 out.Apps = append(out.Apps, app)
123 }
124 writeJSON(w, http.StatusOK, out)
125}
126
127// apiError is every JSON refusal's body.
128type apiError struct {
129 Code string `json:"code"`
130}
131
132// apiRequest reports whether a request is the native app's or a script's,
133// which hear a refusal as JSON, never as a redirect to /signin: everything
134// under /api/, and anything asking for JSON or an event stream.
135func apiRequest(r *http.Request) bool {
136 if strings.HasPrefix(r.URL.Path, "/api/") {
137 return true
138 }
139 accept := r.Header.Get("Accept")
140 return strings.Contains(accept, "application/json") || strings.Contains(accept, "text/event-stream")
141}
142
143// unauthenticated is the JSON refusal for a request with no session, or one
144// that names nobody who is a member now. The native app hears 401 and signs
145// in again.
146func unauthenticated(w http.ResponseWriter) {
147 writeJSON(w, http.StatusUnauthorized, apiError{Code: "unauthenticated"})
148}
149