Files
skillshare/internal/server/handler_hub_drafts.go
T
Willie 13a93242f8 feat(ui): redesign the Hubs page around one list with in-place editing
Previewing a hub from the builder landed on an empty "pick a hub" state:
the builder and the browser kept separate copies of the drafts and the
browser's copy went stale. Merging both into one page removes the split,
so your own hub is shown exactly as others will see it and edited in place.

Hub entries can now be added from any repository URL, not only from
installed skills, with a branch or tag picked from the remote. The version
is written into the source URL, the only place the hub format keeps it,
and a new /api/hub/refs endpoint lists refs and rewrites sources so the UI
never builds tree/<ref> URLs itself. Skills added from installed ones keep
the version they were installed from.

Draft reads and saves read refs from the URLs only, so opening a hub never
waits on a remote. Added hubs get unique labels, since the default hub is
stored by label.
2026-09-28 23:39:19 +08:00

171 lines
3.9 KiB
Go

package server
import (
"encoding/json"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"time"
"skillshare/internal/hub"
)
func (s *Server) hubDraftStore() hub.DraftStore {
return hub.DraftStore{Dir: filepath.Join(filepath.Dir(s.configPath()), "hub-drafts")}
}
func draftError(w http.ResponseWriter, err error) {
status := http.StatusBadRequest
if errors.Is(err, hub.ErrDraftConflict) {
status = http.StatusConflict
}
if os.IsNotExist(err) {
status = http.StatusNotFound
}
if os.IsPermission(err) {
status = http.StatusInternalServerError
}
writeError(w, status, err.Error())
}
func (s *Server) writeDraft(w http.ResponseWriter, d hub.Draft) {
s.mu.RLock()
parseOpts := s.parseOpts()
s.mu.RUnlock()
writeJSON(w, map[string]any{"draft": d, "problems": hub.DraftProblems(d), "refs": hub.DraftRefs(d, parseOpts)})
}
func decodeDraftBody(w http.ResponseWriter, r *http.Request, dst any) error {
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<20))
if err := decoder.Decode(dst); err != nil {
return err
}
var extra any
if err := decoder.Decode(&extra); err != io.EOF {
return errors.New("expected a single JSON document")
}
return nil
}
func (s *Server) handleHubDraftCandidates(w http.ResponseWriter, r *http.Request) {
s.mu.RLock()
source := s.skillsSource()
s.mu.RUnlock()
entries, err := hub.DraftCandidates(source)
if err != nil {
draftError(w, err)
return
}
writeJSON(w, entries)
}
func (s *Server) handleHubDrafts(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
drafts, err := s.hubDraftStore().List()
if err != nil {
draftError(w, err)
return
}
writeJSON(w, drafts)
return
}
start := time.Now()
var d hub.Draft
if err := decodeDraftBody(w, r, &d); err != nil {
draftError(w, err)
return
}
created, err := s.hubDraftStore().Create(d)
if err != nil {
draftError(w, err)
return
}
s.writeOpsLog("hub-draft-create", "ok", start, map[string]any{"id": created.ID}, "")
s.writeDraft(w, created)
}
func (s *Server) handleHubDraft(w http.ResponseWriter, r *http.Request) {
store, id := s.hubDraftStore(), r.PathValue("id")
start := time.Now()
switch r.Method {
case http.MethodGet:
d, err := store.Get(id)
if err != nil {
draftError(w, err)
return
}
s.writeDraft(w, d)
case http.MethodPut:
var d hub.Draft
if err := decodeDraftBody(w, r, &d); err != nil {
draftError(w, err)
return
}
d.ID = id
saved, err := store.Save(d)
if err != nil {
draftError(w, err)
return
}
s.writeOpsLog("hub-draft-save", "ok", start, map[string]any{"id": id}, "")
s.writeDraft(w, saved)
case http.MethodDelete:
if err := store.Delete(id, r.URL.Query().Get("revision")); err != nil {
draftError(w, err)
return
}
s.writeOpsLog("hub-draft-delete", "ok", start, map[string]any{"id": id}, "")
writeJSON(w, map[string]bool{"success": true})
}
}
func (s *Server) handleHubDraftImport(w http.ResponseWriter, r *http.Request) {
start := time.Now()
raw, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 4<<20))
if err != nil {
draftError(w, err)
return
}
d, err := hub.ImportDraft(raw)
if err != nil {
draftError(w, err)
return
}
d, err = s.hubDraftStore().Create(d)
if err != nil {
draftError(w, err)
return
}
s.writeOpsLog("hub-draft-import", "ok", start, map[string]any{"id": d.ID}, "")
s.writeDraft(w, d)
}
func (s *Server) handleHubDraftExport(w http.ResponseWriter, r *http.Request) {
var req struct {
Revision string `json:"revision"`
}
if err := decodeDraftBody(w, r, &req); err != nil {
draftError(w, err)
return
}
d, err := s.hubDraftStore().Get(r.PathValue("id"))
if err != nil {
draftError(w, err)
return
}
if req.Revision != d.Revision {
draftError(w, hub.ErrDraftConflict)
return
}
data, err := hub.ExportDraft(d)
if err != nil {
draftError(w, err)
return
}
w.Header().Set("Content-Disposition", `attachment; filename="skillshare-hub.json"`)
w.Header().Set("Content-Type", "application/json")
w.Write(data)
}