package web import ( "context" "errors" "fmt" "html/template" "net/http" "github.com/go-chi/chi/v5" "amadan.net/oficina/base/internal/catalog" "amadan.net/oficina/base/internal/install" "amadan.net/oficina/base/internal/store" ) type storeRow struct { catalog.Entry State string // "", "installing", "installed" or "failed" Error string } // IconHTML is the catalogue's own SVG, which is trusted markup. func (s storeRow) IconHTML() template.HTML { return template.HTML(s.Icon) } type storePage struct { Name string storeContent Busy bool } // storeContent is what the Store shows a member: a row per catalogue // entry, in the catalogue's order, with its install's state and error. The // Store page and the JSON (GET /api/v1/store) both draw from it. type storeContent struct { IsOwner bool Installing bool Rows []storeRow } // row is the entry id's row, and false when the catalogue has no such app. func (c storeContent) row(id string) (storeRow, bool) { for _, r := range c.Rows { if r.ID == id { return r, true } } return storeRow{}, false } func (a *app) storeContent(ctx context.Context, me store.Member) (storeContent, error) { installs, err := store.Installs(ctx, a.db) if err != nil { return storeContent{}, fmt.Errorf("installs: %w", err) } state := map[string]store.Install{} for _, in := range installs { state[in.App] = in } c := storeContent{IsOwner: me.Role == "owner"} for _, e := range catalog.All() { in := state[e.ID] c.Rows = append(c.Rows, storeRow{Entry: e, State: in.State, Error: in.Error}) if in.State == "installing" { c.Installing = true } } return c, nil } func (a *app) storePage(w http.ResponseWriter, r *http.Request) { me, _ := CurrentMember(r) c, err := a.storeContent(r.Context(), me) if err != nil { a.logger.Error("store", "err", err) http.Error(w, "unavailable", http.StatusServiceUnavailable) return } w.Header().Set("Cache-Control", "no-store") a.render(w, r, "store", storePage{Name: a.cfg.Name, storeContent: c, Busy: r.URL.Query().Get("problem") == "busy"}) } // owner returns the catalogue entry named in the URL when the signed-in // member is the owner, and otherwise answers 403 or 404 and reports false. func (a *app) owner(w http.ResponseWriter, r *http.Request) (catalog.Entry, bool) { me, _ := CurrentMember(r) if me.Role != "owner" { http.Error(w, "forbidden", http.StatusForbidden) return catalog.Entry{}, false } e, ok := catalog.Get(chi.URLParam(r, "app")) if !ok { http.NotFound(w, r) return catalog.Entry{}, false } return e, true } // storeInstall starts an install and answers at once; the Store page shows // progress. Start has claimed the install before the redirect, so the page // the browser loads next shows it installing. The install outlives the // request. func (a *app) storeInstall(w http.ResponseWriter, r *http.Request) { e, ok := a.owner(w, r) if !ok { return } if err := a.inst.Start(r.Context(), e.ID); err != nil { a.logger.Error("install", "app", e.ID, "err", err) } http.Redirect(w, r, "/store", http.StatusSeeOther) } func (a *app) uninstallPage(w http.ResponseWriter, r *http.Request) { e, ok := a.owner(w, r) if !ok { return } w.Header().Set("Cache-Control", "no-store") a.render(w, r, "uninstall", struct { Name string App catalog.Entry }{a.cfg.Name, e}) } func (a *app) storeUninstall(w http.ResponseWriter, r *http.Request) { e, ok := a.owner(w, r) if !ok { return } switch err := a.inst.Uninstall(r.Context(), e.ID); { case errors.Is(err, install.ErrBusy): http.Redirect(w, r, "/store?problem=busy", http.StatusSeeOther) return case errors.Is(err, store.ErrNoInstall): // Already uninstalled. case err != nil: a.logger.Error("uninstall", "app", e.ID, "err", err) http.Error(w, "unavailable", http.StatusServiceUnavailable) return } http.Redirect(w, r, "/store", http.StatusSeeOther) }