Files
skillshare/internal/server/handler_source_links.go
T
Willie 1e7e23f587 fix(ui): keep empty source links available to unlink
Source links disappeared from the dashboard when discovery found no skills, leaving empty or unavailable folders without an Unlink action. Return first-level links independently with their follow-policy availability and preserve their groups in every skills view, including empty unlink confirmations.

Cover empty and dangling inventory, disabled following, and empty-link removal in list, cards, and tree views; document the API field and rendering.
2026-10-05 21:17:30 +08:00

125 lines
3.6 KiB
Go

package server
import (
"errors"
"net/http"
"os"
"path/filepath"
"time"
"skillshare/internal/config"
"skillshare/internal/sourcelink"
"skillshare/internal/sourcewalk"
"skillshare/internal/utils"
)
type sourceLinkRequest struct {
Path string `json:"path"`
Name string `json:"name,omitempty"`
Enable bool `json:"enable,omitempty"`
}
type sourceLinkItem struct {
Name string `json:"name"`
Target string `json:"target"`
Available bool `json:"available"`
Warning string `json:"warning,omitempty"`
}
func listSourceLinks(source string, walk sourcewalk.Options) ([]sourceLinkItem, error) {
entries, err := sourcewalk.ReadDir(source, sourcewalk.Options{})
if err != nil && !os.IsNotExist(err) {
return nil, err
}
links := []sourceLinkItem{}
for _, entry := range entries {
path := filepath.Join(source, entry.Name())
if !utils.IsLinkMode(path, entry.Type()) {
continue
}
item := sourceLinkItem{Name: entry.Name()}
item.Target, err = utils.ResolveLinkTarget(path)
if err != nil {
item.Warning = err.Error()
} else if resolved, followed := walk.Follow.Resolve(path); followed {
item.Target, item.Available = resolved, true
} else if walk.Follow == nil {
item.Warning = "follow_source_links is off"
} else {
item.Warning = "target is not a directory"
for _, skipped := range walk.Follow.Skipped() {
if skipped.Name == item.Name {
item.Warning = skipped.Reason
break
}
}
}
links = append(links, item)
}
return links, nil
}
func (s *Server) handleCreateSourceLink(w http.ResponseWriter, r *http.Request) {
start := time.Now()
var body sourceLinkRequest
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
if !errors.Is(err, errBodyTooLarge) {
writeError(w, http.StatusBadRequest, "invalid JSON body")
}
return
}
if body.Path == "" {
writeError(w, http.StatusBadRequest, "path is required")
return
}
s.mu.Lock()
defer s.mu.Unlock()
res, err := sourcelink.Create(s.skillsSource(), config.SkillsTargetPaths(s.cfg.Targets), body.Path, body.Name)
args := map[string]any{"name": body.Name, "target": body.Path, "enable": body.Enable, "scope": "ui"}
if err != nil {
s.writeOpsLog("link", "error", start, args, err.Error())
writeError(w, http.StatusBadRequest, err.Error())
return
}
defer s.snapshotFollow()
following := s.cfg.FollowSourceLinks
if s.IsProjectMode() {
following = s.projectCfg.FollowSourceLinks
}
if body.Enable && !following {
if s.IsProjectMode() {
s.projectCfg.FollowSourceLinks = true
} else {
s.cfg.FollowSourceLinks = true
}
if err := s.saveConfig(); err != nil {
if s.IsProjectMode() {
s.projectCfg.FollowSourceLinks = false
} else {
s.cfg.FollowSourceLinks = false
}
s.writeOpsLog("link", "error", start, args, err.Error())
writeError(w, http.StatusInternalServerError, "link created, but failed to enable follow_source_links: "+err.Error())
return
}
}
s.writeOpsLog("link", "ok", start, args, "")
writeJSON(w, map[string]any{"path": res.Path, "target": res.Target, "kind": res.Kind, "warning": res.Warning})
}
func (s *Server) handleRemoveSourceLink(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
name := r.PathValue("name")
args := map[string]any{"name": name, "scope": "ui"}
if err := sourcelink.Remove(s.skillsSource(), s.trashBase(), name); err != nil {
s.writeOpsLog("unlink", "error", start, args, err.Error())
writeError(w, http.StatusBadRequest, err.Error())
return
}
s.snapshotFollow()
s.writeOpsLog("unlink", "ok", start, args, "")
writeJSON(w, map[string]any{"success": true, "name": name})
}