mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-12-01 12:03:26 +01:00
fix(chat): return project groups for conversation history
This commit is contained in:
1 parent
852642187e
commit
4e7eca7d04
4 files changed
+132
-2
No files matched your search
@@ -17,6 +17,39 @@ import (
|
||||
// ProjectFilterUnbound 列表 API 中 project_id=__none__ 表示仅未绑定项目的对话。
|
||||
const ProjectFilterUnbound = "__none__"
|
||||
|
||||
type ConversationProjectGroup struct {
|
||||
ProjectID string `json:"projectId"`
|
||||
Total int `json:"total"`
|
||||
}
|
||||
|
||||
// ListConversationProjectGroups counts matching, accessible conversations by project.
|
||||
func (db *DB) ListConversationProjectGroups(search, userID, scope string) ([]ConversationProjectGroup, error) {
|
||||
where := " WHERE 1 = 1"
|
||||
var args []interface{}
|
||||
if search != "" {
|
||||
where += ` AND (c.title LIKE ?
|
||||
OR EXISTS (SELECT 1 FROM messages m WHERE m.conversation_id = c.id AND m.content LIKE ?))`
|
||||
pattern := "%" + search + "%"
|
||||
args = append(args, pattern, pattern)
|
||||
}
|
||||
where, args = appendConversationAccessFilter(where, args, userID, scope, "c")
|
||||
rows, err := db.Query(`SELECT TRIM(COALESCE(c.project_id, '')), COUNT(*) FROM conversations c`+where+`
|
||||
GROUP BY 1 ORDER BY MAX(c.updated_at) DESC, 1`, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
groups := []ConversationProjectGroup{}
|
||||
for rows.Next() {
|
||||
var group ConversationProjectGroup
|
||||
if err := rows.Scan(&group.ProjectID, &group.Total); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groups = append(groups, group)
|
||||
}
|
||||
return groups, rows.Err()
|
||||
}
|
||||
|
||||
// Conversation 对话
|
||||
type Conversation struct {
|
||||
ID string `json:"id"`
|
||||
|
||||
@@ -207,6 +207,29 @@ func TestRBACProjectAndConversationListAccess(t *testing.T) {
|
||||
if len(convs) != 1 || convs[0].ID != c1.ID {
|
||||
t.Fatalf("conversations = %#v, want only %s", convs, c1.ID)
|
||||
}
|
||||
|
||||
unbound, err := db.CreateConversation("visible workspace", ConversationCreateMeta{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.SetResourceOwner("conversation", unbound.ID, "u1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
groups, err := db.ListConversationProjectGroups("visible", "u1", RBACScopeOwn)
|
||||
if err != nil || len(groups) != 2 {
|
||||
t.Fatalf("groups = %#v, err = %v, want one project and the default workspace", groups, err)
|
||||
}
|
||||
totals := make(map[string]int)
|
||||
for _, group := range groups {
|
||||
totals[group.ProjectID] = group.Total
|
||||
}
|
||||
if totals[p1.ID] != 1 || totals[""] != 1 {
|
||||
t.Fatalf("unexpected group totals: %#v", totals)
|
||||
}
|
||||
groups, err = db.ListConversationProjectGroups("conv", "u1", RBACScopeOwn)
|
||||
if err != nil || len(groups) != 1 || groups[0].ProjectID != p1.ID || groups[0].Total != 1 {
|
||||
t.Fatalf("groups filtered by search = %#v, err = %v", groups, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRBACVulnerabilityAccessInheritsProject(t *testing.T) {
|
||||
|
||||
@@ -162,14 +162,19 @@ func (h *ConversationHandler) ListConversations(c *gin.Context) {
|
||||
|
||||
sortBy := strings.TrimSpace(c.Query("sort_by"))
|
||||
session, _ := security.CurrentSession(c)
|
||||
grouped := c.Query("group_by_project") == "1"
|
||||
|
||||
var conversations []*database.Conversation
|
||||
var groups []database.ConversationProjectGroup
|
||||
var total int
|
||||
var err error
|
||||
conversations, err = h.db.ListConversationsForAccess(limit, offset, search, sortBy, projectID, session.UserID, session.Scope)
|
||||
if err == nil {
|
||||
total, err = h.db.CountConversationsForAccess(search, projectID, session.UserID, session.Scope)
|
||||
}
|
||||
if err == nil && grouped {
|
||||
groups, err = h.db.ListConversationProjectGroups(search, session.UserID, session.Scope)
|
||||
}
|
||||
if err != nil {
|
||||
h.logger.Error("获取对话列表失败", zap.Error(err))
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
@@ -178,12 +183,16 @@ func (h *ConversationHandler) ListConversations(c *gin.Context) {
|
||||
if conversations == nil {
|
||||
conversations = []*database.Conversation{}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
response := gin.H{
|
||||
"conversations": conversations,
|
||||
"total": total,
|
||||
"limit": limit,
|
||||
"offset": offset,
|
||||
})
|
||||
}
|
||||
if grouped {
|
||||
response["projectGroups"] = groups
|
||||
}
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// UpdateConversationPinnedRequest 更新对话置顶状态请求
|
||||
|
||||
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
@@ -116,3 +117,67 @@ func performConversationRequest(user *database.RBACUser, method, path string, bo
|
||||
handler(c)
|
||||
return w
|
||||
}
|
||||
|
||||
func TestListConversationsProjectGroups(t *testing.T) {
|
||||
db, user := setupConversationRBACTest(t)
|
||||
project, err := db.CreateProject(&database.Project{Name: "Project history"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AssignResourceToUser(user.ID, "project", project.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
unbound, err := db.CreateConversation("history default", database.ConversationCreateMeta{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.SetResourceOwner("conversation", unbound.ID, user.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, title := range []string{"history first", "history second", "unrelated"} {
|
||||
if _, err := db.CreateConversation(title, database.ConversationCreateMeta{ProjectID: project.ID}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := db.CreateConversation("history private", database.ConversationCreateMeta{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
handler := NewConversationHandler(db, zap.NewNop())
|
||||
for _, tt := range []struct {
|
||||
name, filter, wantProject string
|
||||
offset, wantTotal int
|
||||
}{
|
||||
{"default workspace", database.ProjectFilterUnbound, "", 0, 1},
|
||||
{"named project second page", project.ID, project.ID, 1, 2},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
path := fmt.Sprintf("/api/conversations?group_by_project=1&search=history&limit=1&offset=%d&project_id=%s", tt.offset, tt.filter)
|
||||
w := performConversationRequest(user, http.MethodGet, path, nil, handler.ListConversations)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
var response struct {
|
||||
Conversations []database.Conversation
|
||||
Total int
|
||||
ProjectGroups []struct {
|
||||
ProjectID string
|
||||
Total int
|
||||
}
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Total != tt.wantTotal || len(response.Conversations) != 1 || response.Conversations[0].ProjectID != tt.wantProject {
|
||||
t.Fatalf("unexpected project page: %s", w.Body.String())
|
||||
}
|
||||
totals := map[string]int{}
|
||||
for _, group := range response.ProjectGroups {
|
||||
totals[group.ProjectID] = group.Total
|
||||
}
|
||||
if len(response.ProjectGroups) != 2 || totals[""] != 1 || totals[project.ID] != 2 {
|
||||
t.Fatalf("projectGroups = %#v, want default workspace (1) and named project (2)", response.ProjectGroups)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user