feat(skills): 技能管理 - 支持后台维护一级分类 (#694)
Co-authored-by: ddcat666 <26564566+ddcat666@users.noreply.github.com>
This commit is contained in:
1 parent
fb4af9bd34
commit
fe2b892f1f
29 files changed
+579
-37
No files matched your search
@@ -10,10 +10,12 @@ import (
|
||||
|
||||
type SkillCuration = repository.SkillCuration
|
||||
type SkillCurationAssignmentInput struct {
|
||||
RootID *string `json:"rootId,omitempty"`
|
||||
SkillID string `json:"skillId"`
|
||||
CategoryIDs []string `json:"categoryIds"`
|
||||
}
|
||||
type SkillCurationUpdate struct {
|
||||
Root *model.SkillCurationRoot `json:"root,omitempty"`
|
||||
ExpectedRevision *int64 `json:"expectedRevision"`
|
||||
Enabled *bool `json:"enabled,omitempty"`
|
||||
Category *model.SkillCurationCategory `json:"category,omitempty"`
|
||||
@@ -46,6 +48,9 @@ func (s *Service) UpdateSkillCuration(actor *model.User, req SkillCurationUpdate
|
||||
return nil, err
|
||||
}
|
||||
count := 0
|
||||
if req.Root != nil {
|
||||
count++
|
||||
}
|
||||
if req.Enabled != nil {
|
||||
count++
|
||||
}
|
||||
@@ -66,11 +71,14 @@ func (s *Service) UpdateSkillCuration(actor *model.User, req SkillCurationUpdate
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if req.Root != nil {
|
||||
return saveCurationRoot(repo, state, *req.Root)
|
||||
}
|
||||
if req.Category != nil {
|
||||
c := *req.Category
|
||||
c.Name = strings.Join(strings.Fields(c.Name), " ")
|
||||
c.NormalizedName = strings.ToLower(c.Name)
|
||||
if utf8.RuneCountInString(c.Name) < 1 || utf8.RuneCountInString(c.Name) > 64 || skillCategoryLabels[c.RootTag] == "" {
|
||||
if utf8.RuneCountInString(c.Name) < 1 || utf8.RuneCountInString(c.Name) > 64 || !activeCurationRoot(state, c.RootTag) {
|
||||
return kernel.BadAuthRequest("分类名称或一级分类无效")
|
||||
}
|
||||
exists := c.ID == ""
|
||||
@@ -101,11 +109,24 @@ func (s *Service) UpdateSkillCuration(actor *model.User, req SkillCurationUpdate
|
||||
if err != nil {
|
||||
return kernel.NotFound("公开技能不存在")
|
||||
}
|
||||
rootID := state.EffectiveRoot(skill)
|
||||
if a.RootID != nil {
|
||||
if *a.RootID != "" && !activeCurationRoot(state, *a.RootID) {
|
||||
return kernel.BadAuthRequest("一级分类必须存在且启用")
|
||||
}
|
||||
rootID = *a.RootID
|
||||
if rootID == "" {
|
||||
rootID = skill.Tag
|
||||
if !activeCurationRoot(state, rootID) {
|
||||
rootID = repository.UnassignedSkillRoot
|
||||
}
|
||||
}
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, id := range a.CategoryIDs {
|
||||
valid := false
|
||||
for _, c := range state.Categories {
|
||||
if c.ID == id && c.Enabled && c.RootTag == skill.Tag {
|
||||
if c.ID == id && c.Enabled && c.RootTag == rootID {
|
||||
valid = true
|
||||
}
|
||||
}
|
||||
@@ -114,6 +135,11 @@ func (s *Service) UpdateSkillCuration(actor *model.User, req SkillCurationUpdate
|
||||
}
|
||||
seen[id] = true
|
||||
}
|
||||
if a.RootID != nil {
|
||||
if err := repo.AssignSkillCurationRoot(a.SkillID, *a.RootID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return repo.AssignSkillCuration(a.SkillID, a.CategoryIDs)
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package skills
|
||||
|
||||
import (
|
||||
"infinite-canvas/backend/internal/kernel"
|
||||
"infinite-canvas/backend/internal/model"
|
||||
"infinite-canvas/backend/internal/repository"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func activeCurationRoot(state *SkillCuration, id string) bool {
|
||||
if id == repository.UnassignedSkillRoot {
|
||||
return false
|
||||
}
|
||||
for _, root := range state.Roots {
|
||||
if root.ID == id && root.Enabled {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func saveCurationRoot(repo *repository.Repository, state *SkillCuration, root model.SkillCurationRoot) error {
|
||||
root.Name = strings.Join(strings.Fields(root.Name), " ")
|
||||
root.NormalizedName = strings.ToLower(root.Name)
|
||||
if utf8.RuneCountInString(root.Name) < 1 || utf8.RuneCountInString(root.Name) > 64 {
|
||||
return kernel.BadAuthRequest("一级分类名称必须为1-64个字符")
|
||||
}
|
||||
switch root.IconKey {
|
||||
case "", "shapes", "film", "shopping-bag", "megaphone", "landmark", "palette", "users", "wrench":
|
||||
default:
|
||||
return kernel.BadAuthRequest("图示键无效")
|
||||
}
|
||||
exists := root.ID == ""
|
||||
for _, old := range state.Roots {
|
||||
if old.ID == root.ID {
|
||||
exists = true
|
||||
}
|
||||
if old.ID != root.ID && old.NormalizedName == root.NormalizedName {
|
||||
return kernel.BadAuthRequest("一级分类名称不能重复")
|
||||
}
|
||||
}
|
||||
if !exists {
|
||||
return kernel.NotFound("一级分类不存在")
|
||||
}
|
||||
if root.ID == "" {
|
||||
root.ID = kernel.NewID()
|
||||
}
|
||||
return repo.SaveSkillCurationRoot(&root)
|
||||
}
|
||||
|
||||
func (s *Service) curationRootFilter(id string) (string, error) {
|
||||
if id == "" {
|
||||
return "", nil
|
||||
}
|
||||
state, err := s.repo.SkillCuration(false)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !state.Enabled {
|
||||
return "", nil
|
||||
}
|
||||
if id == repository.UnassignedSkillRoot || activeCurationRoot(state, id) {
|
||||
return id, nil
|
||||
}
|
||||
return "", kernel.BadAuthRequest("一级分类已停用或不存在,请重新选择")
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package skills
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"infinite-canvas/backend/internal/model"
|
||||
"infinite-canvas/backend/internal/repository"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSkillCurationRootManagementAvailable(t *testing.T) {
|
||||
if _, ok := reflect.TypeOf(SkillCurationUpdate{}).FieldByName("Root"); !ok {
|
||||
t.Fatal("missing root management: U39 only supports the five hard-coded root tags")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkillCurationRootsLifecycle(t *testing.T) {
|
||||
svc, db, actor := newCurationTest(t)
|
||||
state, err := svc.SkillCuration(actor, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defaults := model.DefaultSkillCurationRoots()
|
||||
if !reflect.DeepEqual(state.Roots, defaults) {
|
||||
t.Fatalf("empty table fallback changed: %#v", state.Roots)
|
||||
}
|
||||
update := func(req SkillCurationUpdate) *SkillCuration {
|
||||
t.Helper()
|
||||
current, err := svc.SkillCuration(actor, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.ExpectedRevision = ¤t.Revision
|
||||
out, err := svc.UpdateSkillCuration(actor, req, "root-test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return out
|
||||
}
|
||||
enabled := true
|
||||
update(SkillCurationUpdate{Enabled: &enabled})
|
||||
state = update(SkillCurationUpdate{Root: &model.SkillCurationRoot{Name: "New root", IconKey: "landmark", SortOrder: -10, Enabled: true}})
|
||||
root := state.Roots[0]
|
||||
if len(state.Roots) != 6 || root.Name != "New root" {
|
||||
t.Fatalf("root creation/order: %#v", state.Roots)
|
||||
}
|
||||
state = update(SkillCurationUpdate{Category: &model.SkillCurationCategory{Name: "Child", RootTag: root.ID, Enabled: true}})
|
||||
child := state.Categories[0]
|
||||
for _, skill := range []model.Skill{{ID: "assigned", Name: "Assigned", Tag: "drama", Status: 1}, {ID: "legacy", Name: "Legacy", Tag: "drama", Status: 1}, {ID: "unknown", Name: "Unknown", Tag: "unknown", Status: 1}, {ID: "private", Name: "Private", Tag: "drama", Status: 1, IsPrivate: true, OwnerID: actor.ID}} {
|
||||
if err := db.Create(&skill).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
state = update(SkillCurationUpdate{Assignment: &SkillCurationAssignmentInput{SkillID: "assigned", RootID: &root.ID, CategoryIDs: []string{child.ID}}})
|
||||
for _, req := range []SkillListRequest{{PlatformRootID: root.ID}, {PlatformCategoryID: child.ID}, {PlatformRootID: repository.UnassignedSkillRoot}} {
|
||||
list, err := svc.Skills(actor.ID, req)
|
||||
if err != nil || list.TotalCount != 1 {
|
||||
t.Fatalf("root/child/default list: %#v %v", list, err)
|
||||
}
|
||||
}
|
||||
skill, err := svc.repo.Skill("assigned")
|
||||
if err != nil || skill.Tag != "drama" {
|
||||
t.Fatal("root assignment changed source tag")
|
||||
}
|
||||
// Root changes must replace children atomically; reject incompatible old children.
|
||||
drama := "drama"
|
||||
_, err = svc.UpdateSkillCuration(actor, SkillCurationUpdate{ExpectedRevision: &state.Revision, Assignment: &SkillCurationAssignmentInput{SkillID: "assigned", RootID: &drama, CategoryIDs: []string{child.ID}}}, "")
|
||||
if appErrorStatus(err) != 400 {
|
||||
t.Fatalf("cross-root update: %v", err)
|
||||
}
|
||||
current, _ := svc.SkillCuration(actor, true)
|
||||
if current.Revision != state.Revision || current.EffectiveRoot(skill) != root.ID {
|
||||
t.Fatal("failed change partially committed")
|
||||
}
|
||||
root.Enabled = false
|
||||
root.Name = "Renamed root"
|
||||
state = update(SkillCurationUpdate{Root: &root})
|
||||
public, err := svc.SkillCuration(actor, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(public.Categories) != 0 || len(public.Assignments) != 0 {
|
||||
t.Fatal("disabled root still exposes children")
|
||||
}
|
||||
list, err := svc.Skills(actor.ID, SkillListRequest{PlatformRootID: repository.UnassignedSkillRoot})
|
||||
if err != nil || list.TotalCount != 2 {
|
||||
t.Fatalf("disabled root lost skills: %#v %v", list, err)
|
||||
}
|
||||
root.Enabled = true
|
||||
update(SkillCurationUpdate{Root: &root})
|
||||
clear := ""
|
||||
update(SkillCurationUpdate{Assignment: &SkillCurationAssignmentInput{SkillID: "assigned", RootID: &clear, CategoryIDs: []string{}}})
|
||||
list, err = svc.Skills(actor.ID, SkillListRequest{PlatformRootID: "drama"})
|
||||
if err != nil || list.TotalCount != 2 {
|
||||
t.Fatalf("clear override: %#v %v", list, err)
|
||||
}
|
||||
off := false
|
||||
update(SkillCurationUpdate{Enabled: &off})
|
||||
original, err := svc.Skills(actor.ID, SkillListRequest{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
filtered, err := svc.Skills(actor.ID, SkillListRequest{PlatformRootID: "does-not-exist"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a, _ := json.Marshal(original)
|
||||
b, _ := json.Marshal(filtered)
|
||||
if string(a) != string(b) {
|
||||
t.Fatal("disabled root filter changed original response")
|
||||
}
|
||||
public, err = svc.SkillCuration(actor, false)
|
||||
if err != nil || len(public.Roots) != 0 || len(public.RootAssignments) != 0 {
|
||||
t.Fatalf("disabled mode leaked roots: %#v %v", public, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkillCurationRootsValidation(t *testing.T) {
|
||||
svc, _, actor := newCurationTest(t)
|
||||
revision := int64(0)
|
||||
for _, root := range []model.SkillCurationRoot{
|
||||
{Name: "Bad icon", IconKey: "https://example.invalid/icon.svg", Enabled: true},
|
||||
{Name: "短剧影视", Enabled: true},
|
||||
{ID: "unknown", Name: "Unknown", Enabled: true},
|
||||
} {
|
||||
if _, err := svc.UpdateSkillCuration(actor, SkillCurationUpdate{ExpectedRevision: &revision, Root: &root}, ""); err == nil {
|
||||
t.Fatalf("accepted invalid root: %#v", root)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkillCurationRootsDeletionAndPrivateVisibility(t *testing.T) {
|
||||
svc, db, actor := newCurationTest(t)
|
||||
if err := db.AutoMigrate(&model.SkillVersion{}, &model.SkillFile{}, &model.BuiltinSkillTombstone{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, id := range []string{"normal", "builtin", "private"} {
|
||||
db.Create(&model.Skill{ID: id, Tag: "drama", Status: 1, IsPrivate: id == "private", OwnerID: actor.ID})
|
||||
db.Create(&model.SkillCurationRootAssignment{SkillID: id, RootID: "social"})
|
||||
}
|
||||
db.Model(&model.SkillCurationSetting{}).Where("id = 1").Update("enabled", true)
|
||||
state, err := svc.SkillCuration(actor, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(state.RootAssignments) != 2 {
|
||||
t.Fatalf("private override exposed: %#v", state.RootAssignments)
|
||||
}
|
||||
list, err := svc.Skills(actor.ID, SkillListRequest{Scope: "mine", PlatformRootID: "drama"})
|
||||
if err != nil || list.TotalCount != 1 || list.Skills[0].SkillID != "private" {
|
||||
t.Fatalf("private skill root changed: %#v %v", list, err)
|
||||
}
|
||||
if err := svc.repo.DeleteSkill("normal"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := svc.repo.DeleteBuiltinSkill("builtin", actor.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var count int64
|
||||
db.Model(&model.SkillCurationRootAssignment{}).Where("skill_id != ?", "private").Count(&count)
|
||||
if count != 0 {
|
||||
t.Fatal("deleted root assignment retained")
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,9 @@ func TestSkillCurationManagementAvailable(t *testing.T) {
|
||||
func newCurationTest(t *testing.T) (*Service, *gorm.DB, *model.User) {
|
||||
t.Helper()
|
||||
svc, db := newSkillLibraryCategoryTestService(t)
|
||||
if err := db.AutoMigrate(&model.SkillCurationRoot{}, &model.SkillCurationRootAssignment{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.SkillCurationSetting{}, &model.SkillCurationCategory{}, &model.SkillCurationAssignment{}, &model.AdminAuditEvent{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -108,6 +108,7 @@ type SkillCategory struct {
|
||||
}
|
||||
|
||||
type SkillListRequest struct {
|
||||
PlatformRootID string
|
||||
PlatformCategoryID string
|
||||
PlatformUncategorized bool
|
||||
Page int
|
||||
@@ -147,6 +148,10 @@ func (s *Service) Skills(userID string, req SkillListRequest) (*SkillList, error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rootID, err := s.curationRootFilter(req.PlatformRootID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.LibraryCategoryID != "" && req.LibraryUncategorized {
|
||||
return nil, kernel.BadAuthRequest("技能库分类与未分类筛选不能同时使用")
|
||||
}
|
||||
@@ -156,6 +161,7 @@ func (s *Service) Skills(userID string, req SkillListRequest) (*SkillList, error
|
||||
}
|
||||
}
|
||||
rows, total, err := s.repo.Skills(repository.SkillListFilter{
|
||||
PlatformRootID: rootID,
|
||||
PlatformCategoryID: categoryID, PlatformUncategorized: uncategorized,
|
||||
UserID: userID,
|
||||
Scope: req.Scope,
|
||||
|
||||
Reference in new issue
Block a user