fix(chat): return project groups for conversation history

This commit is contained in:
Sycun committed 2026-11-23 16:56:50 +08:00
1 parent 852642187e
commit 4e7eca7d04
4 files changed
+132 -2

No files matched your search

+33
View File
@@ -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"`
+23
View File
@@ -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) {