Files
skillshare/internal/server/handler_search.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

118 lines
3.2 KiB
Go

package server
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"skillshare/internal/config"
"skillshare/internal/hub"
"skillshare/internal/install"
"skillshare/internal/search"
)
func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before I/O.
s.mu.RLock()
source := s.skillsSource()
parseOpts := s.parseOpts()
hubCfg := s.cfg.Hub
if s.IsProjectMode() && s.projectCfg != nil {
hubCfg = s.projectCfg.Hub
}
s.mu.RUnlock()
query := r.URL.Query().Get("q")
hubParam := r.URL.Query().Get("hub")
limit := 0 // default: no limit for hub search
if l := r.URL.Query().Get("limit"); l != "" {
if parsed, err := strconv.Atoi(l); err == nil {
limit = parsed
}
}
var results []search.SearchResult
var err error
switch {
case hubParam == "@builtin":
results, err = searchBuiltinIndex(source, query, limit)
case hubParam != "":
// SSH hub sources trigger a git clone using the host's SSH credentials.
// In the web server, restrict that to URLs the user has explicitly saved
// (via 'skillshare hub add') to avoid cloning arbitrary request-supplied
// hosts.
if install.IsSSHURL(hubParam) && !savedHubURLSet(hubCfg)[strings.TrimSpace(hubParam)] {
writeError(w, http.StatusForbidden, "SSH hub sources must be added first via 'skillshare hub add'")
return
}
results, err = search.SearchFromIndexURL(query, limit, hubParam)
default:
// GitHub search always needs a limit
if limit <= 0 {
limit = 20
}
results, err = search.Search(query, limit)
}
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
type resultItem struct {
Name string `json:"name"`
Description string `json:"description"`
Source string `json:"source"`
Ref string `json:"ref,omitempty"`
Skill string `json:"skill,omitempty"`
Stars int `json:"stars"`
Owner string `json:"owner"`
Repo string `json:"repo"`
Tags []string `json:"tags,omitempty"`
}
items := make([]resultItem, 0, len(results))
for _, r := range results {
items = append(items, resultItem{
Name: r.Name,
Description: r.Description,
Source: r.Source,
// A hosted index can hold hundreds of entries, so the remote is not
// asked; a branch containing "/" shows as its first segment.
Ref: install.SourceRef(r.Source, parseOpts, nil),
Skill: r.Skill,
Stars: r.Stars,
Owner: r.Owner,
Repo: r.Repo,
Tags: r.Tags,
})
}
writeJSON(w, map[string]any{"results": items})
}
// savedHubURLSet returns the set of saved hub URLs for fast membership checks.
func savedHubURLSet(hub config.HubConfig) map[string]bool {
set := make(map[string]bool, len(hub.Hubs))
for _, e := range hub.Hubs {
if u := strings.TrimSpace(e.URL); u != "" {
set[u] = true
}
}
return set
}
// searchBuiltinIndex builds the hub index from local skills and searches it in-memory.
func searchBuiltinIndex(sourcePath, query string, limit int) ([]search.SearchResult, error) {
idx, err := hub.BuildIndex(sourcePath, false, false)
if err != nil {
return nil, err
}
data, err := json.Marshal(idx)
if err != nil {
return nil, err
}
return search.SearchFromIndexJSON(query, limit, data)
}