feat: add DingTalk directory-backed session titles
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
||||
)
|
||||
|
||||
// TitleIdentity contains only the display inputs required to name a WeKnora
|
||||
// session. Stable IDs remain the session identity; titles are display-only.
|
||||
type TitleIdentity struct {
|
||||
ConversationType core.ConversationType
|
||||
ExternalUserID string
|
||||
ExternalChatID string
|
||||
SenderName string
|
||||
ConversationTitle string
|
||||
}
|
||||
|
||||
// BuildDisplayTitle returns a readable DingTalk session title without exposing
|
||||
// the full stable ID. Direct titles contain only the employee name; department
|
||||
// and region are deliberately not part of this contract.
|
||||
func BuildDisplayTitle(identity TitleIdentity) string {
|
||||
switch identity.ConversationType {
|
||||
case core.ConversationGroup:
|
||||
if title := strings.TrimSpace(identity.ConversationTitle); title != "" {
|
||||
return title
|
||||
}
|
||||
return fallbackTitle("钉钉群聊", identity.ExternalChatID)
|
||||
default:
|
||||
if name := strings.TrimSpace(identity.SenderName); name != "" {
|
||||
return name
|
||||
}
|
||||
return fallbackTitle("钉钉用户", identity.ExternalUserID)
|
||||
}
|
||||
}
|
||||
|
||||
func fallbackTitle(prefix, stableID string) string {
|
||||
stableID = strings.TrimSpace(stableID)
|
||||
if stableID == "" {
|
||||
return prefix
|
||||
}
|
||||
runes := []rune(stableID)
|
||||
if utf8.RuneCountInString(stableID) > 4 {
|
||||
runes = runes[len(runes)-4:]
|
||||
}
|
||||
return prefix + "-" + string(runes)
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
||||
)
|
||||
|
||||
func TestBuildDisplayTitle(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
in TitleIdentity
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "direct uses employee name only",
|
||||
in: TitleIdentity{
|
||||
ConversationType: core.ConversationDirect,
|
||||
ExternalUserID: "staff-123456",
|
||||
SenderName: " 张三 ",
|
||||
},
|
||||
want: "张三",
|
||||
},
|
||||
{
|
||||
name: "direct masks stable id when name unavailable",
|
||||
in: TitleIdentity{
|
||||
ConversationType: core.ConversationDirect,
|
||||
ExternalUserID: "staff-123456",
|
||||
},
|
||||
want: "钉钉用户-3456",
|
||||
},
|
||||
{
|
||||
name: "group uses conversation title",
|
||||
in: TitleIdentity{
|
||||
ConversationType: core.ConversationGroup,
|
||||
ExternalChatID: "cid-987654",
|
||||
ConversationTitle: " NOVA项目群 ",
|
||||
},
|
||||
want: "NOVA项目群",
|
||||
},
|
||||
{
|
||||
name: "group masks stable id when title unavailable",
|
||||
in: TitleIdentity{
|
||||
ConversationType: core.ConversationGroup,
|
||||
ExternalChatID: "cid-987654",
|
||||
},
|
||||
want: "钉钉群聊-7654",
|
||||
},
|
||||
{
|
||||
name: "short id remains usable",
|
||||
in: TitleIdentity{
|
||||
ConversationType: core.ConversationDirect,
|
||||
ExternalUserID: "u7",
|
||||
},
|
||||
want: "钉钉用户-u7",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if got := BuildDisplayTitle(tt.in); got != tt.want {
|
||||
t.Fatalf("BuildDisplayTitle() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user