Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Match untitled relation candidates by their displayed name https://github.com/siyuan-note/siyuan/issues/19904

Daniel749febe31c5e
diff --git a/app/src/types/api/index.d.ts b/app/src/types/api/index.d.ts
index cfdf707cc4..b4258b580d 100644
--- a/app/src/types/api/index.d.ts
+++ b/app/src/types/api/index.d.ts
@@ -3589,6 +3589,7 @@ export interface APIPOSTRoutes {
* 可选的 `sort: {column, order}` 按关联数据库字段排序,order 为 ASC 或 DESC。
* 全部候选排序后分页,仅影响本次查询,不修改视图和 selectedRows 顺序。
* 省略 sort 时按创建时间倒序;不存在的字段或无效方向返回错误。
+ * keyword 在分页前匹配字段内容;无显示模板的空主键也匹配当前语言的未命名占位文字,遵循搜索大小写设置。
*/
"/api/av/getAttributeViewRelationCandidates": {
request: GetAttributeViewRelationCandidatesRequestInput;
diff --git a/kernel/api/contract_av_relation_sort_test.go b/kernel/api/contract_av_relation_sort_test.go
index 955e86085a..6f7dea4559 100644
--- a/kernel/api/contract_av_relation_sort_test.go
+++ b/kernel/api/contract_av_relation_sort_test.go
@@ -9,8 +9,51 @@ import (
"github.com/88250/lute/ast"
"github.com/siyuan-note/siyuan/kernel/apicontract"
"github.com/siyuan-note/siyuan/kernel/av"
+ "github.com/siyuan-note/siyuan/kernel/util"
)
+func TestAVRelationCandidateUntitledContract(t *testing.T) {
+ fixture := setupAttributeViewContextFilterAPITest(t)
+ oldWorkingDir := util.WorkingDir
+ util.WorkingDir = "../../app"
+ t.Cleanup(func() { util.WorkingDir = oldWorkingDir })
+ oldCaseSensitive := util.SearchCaseSensitive.Load()
+ util.SearchCaseSensitive.Store(false)
+ t.Cleanup(func() { util.SearchCaseSensitive.Store(oldCaseSensitive) })
+ key := fixture.attrView.GetBlockKey()
+ var ids []string
+ for i := 0; i < 2; i++ {
+ id := ast.NewNodeID()
+ ids = append(ids, id)
+ fixture.attrView.KeyValues[0].Values = append(fixture.attrView.KeyValues[0].Values, &av.Value{
+ ID: ast.NewNodeID(), KeyID: key.ID, BlockID: id, Type: av.KeyTypeBlock, IsDetached: true,
+ CreatedAt: int64(i + 1), Block: &av.ValueBlock{},
+ })
+ }
+ if err := av.SaveAttributeView(fixture.attrView); err != nil {
+ t.Fatal(err)
+ }
+ const path = "/api/av/getAttributeViewRelationCandidates"
+ for page := 1; page <= 2; page++ {
+ response := callAttributeViewContextFilterAPI(t, path, map[string]any{
+ "avID": fixture.attrView.ID, "keyID": fixture.relationKeyID, "keyword": "unt",
+ "page": page, "pageSize": 1, "selectedBlockIDs": []string{ids[0]},
+ }, getAttributeViewRelationCandidates)
+ requireAPIContract(t, http.MethodPost, path, response)
+ var result struct {
+ Code int `json:"code"`
+ Data apicontract.AVRelationCandidatesData `json:"data"`
+ }
+ if err := json.Unmarshal(response.Body.Bytes(), &result); err != nil || result.Code != 0 {
+ t.Fatalf("candidate request failed: %s (%v)", response.Body.String(), err)
+ }
+ if result.Data.Total != 2 || len(result.Data.Rows) != 1 || result.Data.Rows[0].ID != ids[2-page] ||
+ len(result.Data.SelectedRows) != 1 || result.Data.SelectedRows[0].ID != ids[0] {
+ t.Fatalf("unexpected candidate page or selected rows: %s", response.Body.String())
+ }
+ }
+}
+
func TestAVRelationCandidateSortContract(t *testing.T) {
fixture := setupAttributeViewContextFilterAPITest(t)
key := fixture.attrView.GetBlockKey()
diff --git a/kernel/apicontract/contracts.go b/kernel/apicontract/contracts.go
index 1df748ac16..7211c347a8 100644
--- a/kernel/apicontract/contracts.go
+++ b/kernel/apicontract/contracts.go
@@ -1177,6 +1177,7 @@ var GetAttributeViewPrimaryKeyValues = define[GetAttributeViewPrimaryKeyValuesRe
// 可选的 `sort: {column, order}` 按关联数据库字段排序,order 为 ASC 或 DESC。
// 全部候选排序后分页,仅影响本次查询,不修改视图和 selectedRows 顺序。
// 省略 sort 时按创建时间倒序;不存在的字段或无效方向返回错误。
+// keyword 在分页前匹配字段内容;无显示模板的空主键也匹配当前语言的未命名占位文字,遵循搜索大小写设置。
var GetAttributeViewRelationCandidates = define[GetAttributeViewRelationCandidatesRequest, AVRelationCandidatesData]("getAttributeViewRelationCandidates", "/api/av/getAttributeViewRelationCandidates", AuthenticatedAccess|AdminAccess|WritableAccess, JSONBody, ResponseOptions{}, "POST")
var AppendAttributeViewDetachedBlocksWithValues = define[AppendAttributeViewDetachedBlocksWithValuesRequest, Null]("appendAttributeViewDetachedBlocksWithValues", "/api/av/appendAttributeViewDetachedBlocksWithValues", AuthenticatedAccess|AdminAccess|WritableAccess, JSONBody, ResponseOptions{}, "POST")
var AddAttributeViewBlocks = define[AddAttributeViewBlocksRequest, Null]("addAttributeViewBlocks", "/api/av/addAttributeViewBlocks", AuthenticatedAccess|AdminAccess|WritableAccess, JSONBody, ResponseOptions{}, "POST")
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 3cf34b0440..75d97870ca 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -2787,8 +2787,12 @@ func GetAttributeViewRelationCandidates(srcAvID, relationKeyID, keyword string,
func filterSortPageRelationCandidates(table *av.Table, attrView *av.AttributeView, keyword string, page, pageSize int, candidateSort *av.ViewSort) (
rows []*av.TableRow, total int,
) {
+ untitled := ""
+ if strings.TrimSpace(keyword) != "" {
+ untitled = util.I18nTerm(util.Lang, "untitled")
+ }
for _, row := range table.Rows {
- if relationCandidateMatches(row, keyword) {
+ if relationCandidateMatches(row, keyword, untitled, table.Columns...) {
rows = append(rows, row)
}
}
@@ -2876,7 +2880,7 @@ func renderAttributeViewRelationCandidates(attrView *av.AttributeView) (ret *av.
return
}
-func relationCandidateMatches(row *av.TableRow, keyword string) bool {
+func relationCandidateMatches(row *av.TableRow, keyword, untitled string, columns ...*av.TableColumn) bool {
keywords := strings.Fields(strings.TrimSpace(keyword))
if 1 > len(keywords) {
return true
@@ -2886,6 +2890,20 @@ func relationCandidateMatches(row *av.TableRow, keyword string) bool {
continue
}
content := cell.Value.String(true)
+ // 仅匹配主键实际显示的未命名占位文字,不改变字段内容或显示模板的语义。
+ if cell.ValueType == av.KeyTypeBlock && (cell.Value.Block == nil || cell.Value.Block.Content == "") &&
+ !cell.Value.HasRenderTemplate && cell.Value.RenderedContent == "" {
+ hasTemplate := false
+ for _, column := range columns {
+ if column.ID == cell.Value.KeyID && strings.TrimSpace(column.RenderTemplate) != "" {
+ hasTemplate = true
+ break
+ }
+ }
+ if !hasTemplate {
+ content = untitled
+ }
+ }
allKeywordsHit := true
for _, currentKeyword := range keywords {
if util.SearchCaseSensitive.Load() {
diff --git a/kernel/model/attribute_view_relation_test.go b/kernel/model/attribute_view_relation_test.go
index 0d9273f374..bd5d0b746e 100644
--- a/kernel/model/attribute_view_relation_test.go
+++ b/kernel/model/attribute_view_relation_test.go
@@ -9,12 +9,87 @@
package model
import (
+ "encoding/json"
+ "os"
"testing"
"github.com/88250/lute/ast"
"github.com/siyuan-note/siyuan/kernel/av"
+ "github.com/siyuan-note/siyuan/kernel/util"
)
+func TestRelationCandidateUntitledSearch(t *testing.T) {
+ oldLang, oldWorkingDir, oldCaseSensitive := util.Lang, util.WorkingDir, util.SearchCaseSensitive.Load()
+ util.WorkingDir = "../../app"
+ t.Cleanup(func() {
+ util.Lang, util.WorkingDir = oldLang, oldWorkingDir
+ util.SearchCaseSensitive.Store(oldCaseSensitive)
+ })
+ for _, language := range []struct{ lang, keyword string }{{"en", "unt"}, {"zh-CN", "未命"}, {"de", "unbet"}} {
+ data, err := os.ReadFile("../../app/appearance/langs/" + language.lang + ".json")
+ if err != nil {
+ t.Fatal(err)
+ }
+ var labels struct {
+ Untitled string `json:"untitled"`
+ }
+ if err = json.Unmarshal(data, &labels); err != nil {
+ t.Fatal(err)
+ }
+ if labels.Untitled == "" {
+ t.Fatal("frontend untitled label must be available")
+ }
+ util.Lang = language.lang
+ util.SearchCaseSensitive.Store(false)
+ attrView, alphaID, betaID := newRelationCandidateTestAttributeView()
+ table := renderAttributeViewRelationCandidates(attrView)
+ for _, row := range table.Rows {
+ row.GetBlockValue().Block.Content = ""
+ }
+ for page, id := range []string{betaID, alphaID} {
+ rows, total := filterSortPageRelationCandidates(table, attrView, language.keyword, page+1, 1, nil)
+ if total != 2 || len(rows) != 1 || rows[0].ID != id {
+ t.Fatalf("%s page %d: total=%d rows=%+v", language.lang, page+1, total, rows)
+ }
+ }
+ row := table.Rows[0]
+ value := row.GetBlockValue()
+ if value.Block.Content != "" {
+ t.Fatal("search must preserve the stored empty value")
+ }
+ value.HasRenderTemplate = true
+ if relationCandidateMatches(row, language.keyword, labels.Untitled, table.Columns...) {
+ t.Fatal("an empty template result must not match the placeholder")
+ }
+ value.HasRenderTemplate = false
+ value.RenderedContent = "custom"
+ if relationCandidateMatches(row, language.keyword, labels.Untitled, table.Columns...) {
+ t.Fatal("a rendered template must not match the placeholder")
+ }
+ value.RenderedContent = ""
+ table.Columns[0].RenderTemplate = "{{.name}}"
+ if relationCandidateMatches(row, language.keyword, labels.Untitled, table.Columns...) {
+ t.Fatal("a configured template must not match the placeholder")
+ }
+ table.Columns[0].RenderTemplate = ""
+ value.Block.Content = " "
+ if relationCandidateMatches(row, language.keyword, labels.Untitled, table.Columns...) {
+ t.Fatal("whitespace that is not displayed as untitled must not match")
+ }
+ value.Block.Content = "named"
+ if relationCandidateMatches(row, language.keyword, labels.Untitled, table.Columns...) {
+ t.Fatal("empty non-primary fields must not match the placeholder")
+ }
+ value.Block.Content = ""
+ if language.lang == "en" {
+ util.SearchCaseSensitive.Store(true)
+ if relationCandidateMatches(row, "unt", labels.Untitled, table.Columns...) || !relationCandidateMatches(row, "Unt", labels.Untitled, table.Columns...) {
+ t.Fatal("placeholder search must respect case sensitivity")
+ }
+ }
+ }
+}
+
func TestRenderAttributeViewRelationCandidates(t *testing.T) {
attrView, alphaID, betaID := newRelationCandidateTestAttributeView()
@@ -34,7 +109,7 @@ func TestRenderAttributeViewRelationCandidates(t *testing.T) {
if row.ID == betaID {
selectedRows = append(selectedRows, row)
}
- if relationCandidateMatches(row, "alpha red") {
+ if relationCandidateMatches(row, "alpha red", "") {
rows = append(rows, row)
}
}