feat(skills): 技能管理 - 增加可选的平台子分类策展 (#693)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5976389ed4
commit
c963fc7b89
32 files changed
+981
-24
No files matched your search
@@ -0,0 +1,148 @@
|
||||
package skills
|
||||
|
||||
import (
|
||||
"infinite-canvas/backend/internal/kernel"
|
||||
"infinite-canvas/backend/internal/model"
|
||||
"infinite-canvas/backend/internal/repository"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type SkillCuration = repository.SkillCuration
|
||||
type SkillCurationAssignmentInput struct {
|
||||
SkillID string `json:"skillId"`
|
||||
CategoryIDs []string `json:"categoryIds"`
|
||||
}
|
||||
type SkillCurationUpdate struct {
|
||||
ExpectedRevision *int64 `json:"expectedRevision"`
|
||||
Enabled *bool `json:"enabled,omitempty"`
|
||||
Category *model.SkillCurationCategory `json:"category,omitempty"`
|
||||
Assignment *SkillCurationAssignmentInput `json:"assignment,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Service) SkillCuration(actor *model.User, admin bool) (*SkillCuration, error) {
|
||||
if err := s.curationActor(actor, admin); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.SkillCuration(admin)
|
||||
}
|
||||
func (s *Service) curationActor(actor *model.User, admin bool) error {
|
||||
if actor == nil || actor.ID == "" {
|
||||
return kernel.Unauthorized("请先登录")
|
||||
}
|
||||
if admin {
|
||||
current, err := s.repo.User(actor.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current.Role != model.UserRoleAdmin || current.Status != model.UserStatusActive {
|
||||
return kernel.Forbidden("需要管理员权限")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (s *Service) UpdateSkillCuration(actor *model.User, req SkillCurationUpdate, requestID string) (*SkillCuration, error) {
|
||||
if err := s.curationActor(actor, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
count := 0
|
||||
if req.Enabled != nil {
|
||||
count++
|
||||
}
|
||||
if req.Category != nil {
|
||||
count++
|
||||
}
|
||||
if req.Assignment != nil {
|
||||
count++
|
||||
}
|
||||
if req.ExpectedRevision == nil || *req.ExpectedRevision < 0 || count != 1 {
|
||||
return nil, kernel.BadAuthRequest("需要版本号和一项修改")
|
||||
}
|
||||
err := s.repo.UpdateSkillCuration(actor.ID, *req.ExpectedRevision, requestID, func(repo *repository.Repository) error {
|
||||
if req.Enabled != nil {
|
||||
return repo.SetSkillCurationEnabled(*req.Enabled)
|
||||
}
|
||||
state, err := repo.SkillCuration(true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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] == "" {
|
||||
return kernel.BadAuthRequest("分类名称或一级分类无效")
|
||||
}
|
||||
exists := c.ID == ""
|
||||
for _, old := range state.Categories {
|
||||
if old.ID == c.ID {
|
||||
exists = true
|
||||
if old.RootTag != c.RootTag {
|
||||
return kernel.BadAuthRequest("不能移动子分类")
|
||||
}
|
||||
}
|
||||
if old.ID != c.ID && old.RootTag == c.RootTag && old.NormalizedName == c.NormalizedName {
|
||||
return kernel.BadAuthRequest("同一级分类下名称不能重复")
|
||||
}
|
||||
}
|
||||
if !exists {
|
||||
return kernel.NotFound("分类不存在")
|
||||
}
|
||||
if c.ID == "" {
|
||||
c.ID = kernel.NewID()
|
||||
}
|
||||
return repo.SaveSkillCurationCategory(&c)
|
||||
}
|
||||
a := req.Assignment
|
||||
if a.CategoryIDs == nil || len(a.CategoryIDs) > 32 {
|
||||
return kernel.BadAuthRequest("需要完整分类集合,最多32项")
|
||||
}
|
||||
skill, err := repo.CurationSkill(a.SkillID)
|
||||
if err != nil {
|
||||
return kernel.NotFound("公开技能不存在")
|
||||
}
|
||||
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 {
|
||||
valid = true
|
||||
}
|
||||
}
|
||||
if !valid || seen[id] {
|
||||
return kernel.BadAuthRequest("分类必须启用、同根且不能重复")
|
||||
}
|
||||
seen[id] = true
|
||||
}
|
||||
return repo.AssignSkillCuration(a.SkillID, a.CategoryIDs)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.SkillCuration(true)
|
||||
}
|
||||
|
||||
func (s *Service) curationFilter(req SkillListRequest) (string, bool, error) {
|
||||
if req.PlatformCategoryID == "" && !req.PlatformUncategorized {
|
||||
return "", false, nil
|
||||
}
|
||||
state, err := s.repo.SkillCuration(false)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
if !state.Enabled {
|
||||
return "", false, nil
|
||||
}
|
||||
if req.PlatformCategoryID != "" && req.PlatformUncategorized {
|
||||
return "", false, kernel.BadAuthRequest("分类和未细分不能同时筛选")
|
||||
}
|
||||
if req.PlatformCategoryID != "" {
|
||||
for _, c := range state.Categories {
|
||||
if c.ID == req.PlatformCategoryID {
|
||||
return c.ID, false, nil
|
||||
}
|
||||
}
|
||||
return "", false, kernel.BadAuthRequest("分类已停用或不存在,请重新选择")
|
||||
}
|
||||
return "", req.PlatformUncategorized, nil
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package skills
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"gorm.io/gorm"
|
||||
"infinite-canvas/backend/internal/model"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSkillCurationManagementAvailable(t *testing.T) {
|
||||
if _, ok := reflect.TypeOf((*Service)(nil)).MethodByName("UpdateSkillCuration"); !ok {
|
||||
t.Fatal("missing platform curation management: skill categories are fixed and cannot be maintained by an administrator")
|
||||
}
|
||||
}
|
||||
|
||||
func newCurationTest(t *testing.T) (*Service, *gorm.DB, *model.User) {
|
||||
t.Helper()
|
||||
svc, db := newSkillLibraryCategoryTestService(t)
|
||||
if err := db.AutoMigrate(&model.SkillCurationSetting{}, &model.SkillCurationCategory{}, &model.SkillCurationAssignment{}, &model.AdminAuditEvent{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.SkillCurationSetting{ID: 1}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actor := &model.User{ID: "curator", Role: model.UserRoleAdmin, Status: model.UserStatusActive, Username: "curator"}
|
||||
if err := db.Create(actor).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return svc, db, actor
|
||||
}
|
||||
|
||||
func TestSkillCurationDisabledSnapshot(t *testing.T) {
|
||||
svc, db, actor := newCurationTest(t)
|
||||
// Snapshot of the original empty /skills data and fixed category order.
|
||||
const original = `{"skills":[],"totalCount":0,"hasMore":false,"nextOffset":0,"page":1,"pageSize":20,"categories":[{"value":"drama","label":"短剧影视"},{"value":"ecommerce","label":"电商营销"},{"value":"creative","label":"创意设计"},{"value":"social","label":"社媒内容"},{"value":"others","label":"其他"}]}`
|
||||
if err := db.Create(&model.SkillCurationCategory{ID: "hidden", RootTag: "drama", Name: "Hidden", NormalizedName: "hidden", Enabled: true}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, req := range []SkillListRequest{{}, {PlatformCategoryID: "invalid", PlatformUncategorized: true}} {
|
||||
result, err := svc.Skills(actor.ID, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, _ := json.Marshal(result)
|
||||
if string(data) != original {
|
||||
t.Fatalf("legacy response changed:\n%s", data)
|
||||
}
|
||||
}
|
||||
result, err := svc.SkillCuration(actor, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Enabled || len(result.Categories) != 0 || len(result.Assignments) != 0 {
|
||||
t.Fatalf("disabled mode leaked curation: %#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkillCurationLifecycleAndGuards(t *testing.T) {
|
||||
svc, db, actor := newCurationTest(t)
|
||||
update := func(req SkillCurationUpdate) *SkillCuration {
|
||||
t.Helper()
|
||||
state, err := svc.SkillCuration(actor, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.ExpectedRevision = &state.Revision
|
||||
out, err := svc.UpdateSkillCuration(actor, req, "test-request")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return out
|
||||
}
|
||||
on := true
|
||||
update(SkillCurationUpdate{Enabled: &on})
|
||||
state := update(SkillCurationUpdate{Category: &model.SkillCurationCategory{RootTag: "drama", Name: "Story", SortOrder: 2, Enabled: true}})
|
||||
c := state.Categories[0]
|
||||
for _, s := range []model.Skill{{ID: "public", Name: "Story", Tag: "drama", Status: 1}, {ID: "other", Name: "Other", Tag: "social", Status: 1}, {ID: "private", Name: "Secret", Tag: "drama", Status: 1, IsPrivate: true, OwnerID: actor.ID}} {
|
||||
if err := db.Create(&s).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
state = update(SkillCurationUpdate{Assignment: &SkillCurationAssignmentInput{SkillID: "public", CategoryIDs: []string{c.ID}}})
|
||||
list, err := svc.Skills(actor.ID, SkillListRequest{PlatformCategoryID: c.ID})
|
||||
if err != nil || list.TotalCount != 1 {
|
||||
t.Fatalf("filtered: %#v %v", list, err)
|
||||
}
|
||||
list, err = svc.Skills(actor.ID, SkillListRequest{PlatformUncategorized: true})
|
||||
if err != nil || list.TotalCount != 1 {
|
||||
t.Fatalf("uncategorized: %#v %v", list, err)
|
||||
}
|
||||
for _, id := range []string{"other", "private"} {
|
||||
_, err := svc.UpdateSkillCuration(actor, SkillCurationUpdate{ExpectedRevision: &state.Revision, Assignment: &SkillCurationAssignmentInput{SkillID: id, CategoryIDs: []string{c.ID}}}, "")
|
||||
if err == nil {
|
||||
t.Fatalf("accepted invalid assignment: %s", id)
|
||||
}
|
||||
}
|
||||
stale := int64(0)
|
||||
if _, err := svc.UpdateSkillCuration(actor, SkillCurationUpdate{ExpectedRevision: &stale, Enabled: &on}, ""); appErrorStatus(err) != 409 {
|
||||
t.Fatalf("stale update: %v", err)
|
||||
}
|
||||
c.Name = "Renamed"
|
||||
c.SortOrder = -1
|
||||
c.Enabled = false
|
||||
state = update(SkillCurationUpdate{Category: &c})
|
||||
public, err := svc.SkillCuration(actor, false)
|
||||
if err != nil || len(public.Categories) != 0 || len(public.Assignments) != 0 {
|
||||
t.Fatalf("disabled category: %#v %v", public, err)
|
||||
}
|
||||
var assignments int64
|
||||
db.Model(&model.SkillCurationAssignment{}).Count(&assignments)
|
||||
if assignments != 1 {
|
||||
t.Fatal("disable deleted assignments")
|
||||
}
|
||||
c.Enabled = true
|
||||
update(SkillCurationUpdate{Category: &c})
|
||||
off := false
|
||||
update(SkillCurationUpdate{Enabled: &off})
|
||||
list, err = svc.Skills(actor.ID, SkillListRequest{PlatformCategoryID: c.ID})
|
||||
if err != nil || list.TotalCount != 2 {
|
||||
t.Fatalf("disabled switch must ignore filters: %#v %v", list, err)
|
||||
}
|
||||
// A stale in-memory administrator cannot keep writing after revocation.
|
||||
db.Model(&model.User{}).Where("id = ?", actor.ID).Update("role", model.UserRoleUser)
|
||||
if _, err := svc.UpdateSkillCuration(actor, SkillCurationUpdate{ExpectedRevision: &state.Revision, Enabled: &on}, ""); appErrorStatus(err) != 403 {
|
||||
t.Fatalf("revoked administrator: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkillCurationAuditFailureRollsBack(t *testing.T) {
|
||||
svc, db, actor := newCurationTest(t)
|
||||
if err := db.Exec(`CREATE TRIGGER reject_curation_audit BEFORE INSERT ON admin_audit_events BEGIN SELECT RAISE(ABORT, 'audit unavailable'); END`).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
revision := int64(0)
|
||||
on := true
|
||||
if _, err := svc.UpdateSkillCuration(actor, SkillCurationUpdate{ExpectedRevision: &revision, Enabled: &on}, ""); err == nil {
|
||||
t.Fatal("audit failure accepted")
|
||||
}
|
||||
state, err := svc.SkillCuration(actor, true)
|
||||
if err != nil || state.Revision != 0 || state.Enabled {
|
||||
t.Fatalf("partial commit: %#v %v", state, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkillCurationDeletionCleanup(t *testing.T) {
|
||||
svc, db, _ := newCurationTest(t)
|
||||
if err := db.AutoMigrate(&model.SkillVersion{}, &model.SkillFile{}, &model.BuiltinSkillTombstone{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, id := range []string{"normal", "builtin"} {
|
||||
db.Create(&model.Skill{ID: id, Status: 1})
|
||||
db.Create(&model.SkillCurationAssignment{SkillID: id, CategoryID: "category"})
|
||||
}
|
||||
if err := svc.repo.DeleteSkill("normal"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := svc.repo.DeleteBuiltinSkill("builtin", "admin"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var count int64
|
||||
db.Model(&model.SkillCurationAssignment{}).Count(&count)
|
||||
if count != 0 {
|
||||
t.Fatal("deleted skills retain assignments")
|
||||
}
|
||||
}
|
||||
@@ -108,14 +108,16 @@ type SkillCategory struct {
|
||||
}
|
||||
|
||||
type SkillListRequest struct {
|
||||
Page int
|
||||
PageSize int
|
||||
Scope string
|
||||
Search string
|
||||
Tag string
|
||||
LibraryCategoryID string
|
||||
LibraryUncategorized bool
|
||||
Sort string
|
||||
PlatformCategoryID string
|
||||
PlatformUncategorized bool
|
||||
Page int
|
||||
PageSize int
|
||||
Scope string
|
||||
Search string
|
||||
Tag string
|
||||
LibraryCategoryID string
|
||||
LibraryUncategorized bool
|
||||
Sort string
|
||||
}
|
||||
|
||||
type SkillList struct {
|
||||
@@ -141,6 +143,10 @@ type SkillMutationRequest struct {
|
||||
|
||||
func (s *Service) Skills(userID string, req SkillListRequest) (*SkillList, error) {
|
||||
req = normalizeSkillListRequest(req)
|
||||
categoryID, uncategorized, err := s.curationFilter(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.LibraryCategoryID != "" && req.LibraryUncategorized {
|
||||
return nil, kernel.BadAuthRequest("技能库分类与未分类筛选不能同时使用")
|
||||
}
|
||||
@@ -150,6 +156,7 @@ func (s *Service) Skills(userID string, req SkillListRequest) (*SkillList, error
|
||||
}
|
||||
}
|
||||
rows, total, err := s.repo.Skills(repository.SkillListFilter{
|
||||
PlatformCategoryID: categoryID, PlatformUncategorized: uncategorized,
|
||||
UserID: userID,
|
||||
Scope: req.Scope,
|
||||
Search: req.Search,
|
||||
|
||||
Reference in new issue
Block a user