feat: add admin settings control flow

This commit is contained in:
姚章浩
2026-08-13 12:45:08 +08:00
parent 8d2d4b469b
commit 462ee1f3ff
9 changed files with 792 additions and 0 deletions
+127
View File
@@ -0,0 +1,127 @@
package settings
import (
"context"
"errors"
"fmt"
"strings"
"time"
)
type ResourceBinding struct {
Kind, TenantKey, ConversationID, ConversationTitle string
BaseID, SheetID, ViewID, OperatorID string
CreatedByStaffID string
}
type BootstrapCandidate struct {
TenantKey, Code, StaffID, UnionID, DisplayName, Department string
DetectedAt time.Time
}
type SettingInput struct{ TenantKey, Channel, StaffID, UnionID, DisplayName, Department, Text, ConversationType, ConversationID, ConversationTitle string }
type ControllerRepository interface {
IsAdministrator(context.Context, string, string) (bool, error)
SetPreference(context.Context, string, string, string, string) error
SaveBinding(context.Context, ResourceBinding) error
DetectBootstrapCandidate(context.Context, BootstrapCandidate) error
}
type Controller struct {
repo ControllerRepository
now func() time.Time
}
func NewController(repo ControllerRepository) *Controller {
return &Controller{repo: repo, now: time.Now}
}
func (c *Controller) Handle(ctx context.Context, in SettingInput) (string, error) {
if c == nil || c.repo == nil {
return "", errors.New("settings repository is required")
}
cmd, ok := ParseCommand(in.Text)
if !ok {
return "", nil
}
if len(cmd.Args) == 1 && len(cmd.Args[0]) == 6 && allDigits(cmd.Args[0]) {
if strings.TrimSpace(in.ConversationType) != "direct" {
return "管理员绑定码只能私聊机器人使用。", nil
}
err := c.repo.DetectBootstrapCandidate(ctx, BootstrapCandidate{TenantKey: in.TenantKey, Code: cmd.Args[0], StaffID: in.StaffID, UnionID: in.UnionID, DisplayName: in.DisplayName, Department: in.Department, DetectedAt: c.now()})
if err != nil {
return "", err
}
return "身份已验证,请回到后台确认首位管理员。", nil
}
if len(cmd.Args) == 0 {
admin, err := c.repo.IsAdministrator(ctx, in.TenantKey, in.StaffID)
if err != nil {
return "", err
}
if admin {
return "⚙️ 我的设置:/setting language <auto|zh-CN|en|ja|ko|es>\n🛠 系统管理:support、question、faq", nil
}
return "⚙️ 我的设置:/setting language <auto|zh-CN|en|ja|ko|es>", nil
}
action := strings.ToLower(cmd.Args[0])
if action == "language" {
if len(cmd.Args) != 2 || !validLocale(cmd.Args[1]) {
return "可选语言:auto、zh-CN、en、ja、ko、es。", nil
}
if err := c.repo.SetPreference(ctx, in.TenantKey, defaultChannel(in.Channel), in.StaffID, cmd.Args[1]); err != nil {
return "", err
}
return "✅ 回答语言已设置为 " + cmd.Args[1] + "。", nil
}
admin, err := c.repo.IsAdministrator(ctx, in.TenantKey, in.StaffID)
if err != nil {
return "", err
}
if !admin {
return "你没有系统管理权限。", nil
}
switch action {
case "support":
if in.ConversationType != "group" || strings.TrimSpace(in.ConversationID) == "" {
return "请在目标技术支持群中@机器人发送 /setting support。", nil
}
err = c.repo.SaveBinding(ctx, ResourceBinding{Kind: "support_group", TenantKey: in.TenantKey, ConversationID: in.ConversationID, ConversationTitle: in.ConversationTitle, CreatedByStaffID: in.StaffID})
if err == nil {
return "✅ 当前群已保存为技术支持群(待后台验证)。", nil
}
case "question", "faq":
if len(cmd.Args) != 2 {
return "请在命令后发送钉钉AI表格链接。", nil
}
link, e := ParseNotableLink(cmd.Args[1])
if e != nil {
return "AI表格链接无法识别。", nil
}
kind := action + "_table"
err = c.repo.SaveBinding(ctx, ResourceBinding{Kind: kind, TenantKey: in.TenantKey, BaseID: link.BaseID, SheetID: link.SheetID, ViewID: link.ViewID, OperatorID: in.UnionID, CreatedByStaffID: in.StaffID})
if err == nil {
return fmt.Sprintf("✅ %s已解析并保存为待验证配置。", map[string]string{"question": "问题AI表格", "faq": "FAQ表格"}[action]), nil
}
default:
return "未知设置项。发送 /setting 查看菜单。", nil
}
return "", err
}
func validLocale(s string) bool {
switch s {
case "auto", "zh-CN", "en", "ja", "ko", "es":
return true
}
return false
}
func defaultChannel(s string) string {
if strings.TrimSpace(s) == "" {
return "dingtalk"
}
return strings.TrimSpace(s)
}
func allDigits(s string) bool {
for _, r := range s {
if r < '0' || r > '9' {
return false
}
}
return s != ""
}
+91
View File
@@ -0,0 +1,91 @@
package settings
import (
"context"
"testing"
"time"
)
type memoryRepo struct {
admins map[string]bool
prefs map[string]string
bindings map[string]ResourceBinding
candidate BootstrapCandidate
}
func newMemoryRepo() *memoryRepo {
return &memoryRepo{admins: map[string]bool{}, prefs: map[string]string{}, bindings: map[string]ResourceBinding{}}
}
func (r *memoryRepo) IsAdministrator(_ context.Context, tenant, staff string) (bool, error) {
return r.admins[tenant+"/"+staff], nil
}
func (r *memoryRepo) SetPreference(_ context.Context, tenant, channel, staff, locale string) error {
r.prefs[tenant+"/"+staff] = locale
return nil
}
func (r *memoryRepo) SaveBinding(_ context.Context, b ResourceBinding) error {
r.bindings[b.Kind] = b
return nil
}
func (r *memoryRepo) DetectBootstrapCandidate(_ context.Context, c BootstrapCandidate) error {
r.candidate = c
return nil
}
func TestControllerLanguageForOrdinaryUser(t *testing.T) {
r := newMemoryRepo()
c := NewController(r)
out, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", Channel: "dingtalk", StaffID: "u", Text: "/setting language ja", ConversationType: "direct"})
if err != nil {
t.Fatal(err)
}
if r.prefs["t/u"] != "ja" || out == "" {
t.Fatalf("prefs=%v out=%q", r.prefs, out)
}
}
func TestControllerRejectsOrdinaryUserSystemBinding(t *testing.T) {
r := newMemoryRepo()
c := NewController(r)
out, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "u", Text: "/setting support", ConversationType: "group", ConversationID: "g"})
if err != nil {
t.Fatal(err)
}
if len(r.bindings) != 0 || out != "你没有系统管理权限。" {
t.Fatalf("bindings=%v out=%q", r.bindings, out)
}
}
func TestControllerBindsAdminSupportGroup(t *testing.T) {
r := newMemoryRepo()
r.admins["t/a"] = true
c := NewController(r)
_, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "a", Text: "/setting support", ConversationType: "group", ConversationID: "g", ConversationTitle: "技术支持"})
if err != nil {
t.Fatal(err)
}
if r.bindings["support_group"].ConversationID != "g" {
t.Fatalf("binding=%+v", r.bindings)
}
}
func TestControllerParsesAdminQuestionTable(t *testing.T) {
r := newMemoryRepo()
r.admins["t/a"] = true
c := NewController(r)
_, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "a", Text: "/setting question https://alidocs.dingtalk.com/i/nodes/base?sheetId=sheet&viewId=view", ConversationType: "direct"})
if err != nil {
t.Fatal(err)
}
if r.bindings["question_table"].BaseID != "base" {
t.Fatalf("binding=%+v", r.bindings)
}
}
func TestControllerDetectsBootstrapCandidate(t *testing.T) {
r := newMemoryRepo()
c := NewController(r)
c.now = func() time.Time { return time.Unix(100, 0) }
_, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", StaffID: "a", UnionID: "union", DisplayName: "Alice", Text: "/setting 123456", ConversationType: "direct"})
if err != nil {
t.Fatal(err)
}
if r.candidate.Code != "123456" || r.candidate.StaffID != "a" {
t.Fatalf("candidate=%+v", r.candidate)
}
}