Skip to content

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

:bug: Refresh parent documents after batch moves https://github.com/siyuan-note/siyuan/issues/18370

Daniele373c184478f
diff --git a/kernel/model/box.go b/kernel/model/box.go
index 1e4dd52cf3..37c2bcfbf6 100644
--- a/kernel/model/box.go
+++ b/kernel/model/box.go
@@ -548,7 +548,7 @@ func moveTree(tree *parse.Tree) {
util.PushStatusBar(msg)
}
- refreshDocInfo(tree)
+ refreshDocInfoWithoutParent(tree)
}
func parseKTree(kramdown []byte) (ret *parse.Tree) {
diff --git a/kernel/model/file.go b/kernel/model/file.go
index 8db2545d55..2ce72ddd64 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -1548,6 +1548,54 @@ func GetIDsByHPath(hpath, boxID string) (ret []string, err error) {
return
}
+type moveDocsRefreshKey struct {
+ boxID string
+ rootID string
+}
+
+type moveDocsRefresh struct {
+ parents map[moveDocsRefreshKey]*parse.Tree
+ notebooks map[string]struct{}
+}
+
+func newMoveDocsRefresh() *moveDocsRefresh {
+ return &moveDocsRefresh{
+ parents: map[moveDocsRefreshKey]*parse.Tree{},
+ notebooks: map[string]struct{}{},
+ }
+}
+
+func (refresh *moveDocsRefresh) addParent(tree *parse.Tree) {
+ if nil == refresh || nil == tree {
+ return
+ }
+ key := moveDocsRefreshKey{boxID: tree.Box, rootID: tree.ID}
+ refresh.parents[key] = tree
+}
+
+func (refresh *moveDocsRefresh) addNotebook(boxID string) {
+ if nil == refresh || "" == boxID {
+ return
+ }
+ refresh.notebooks[boxID] = struct{}{}
+}
+
+func (refresh *moveDocsRefresh) flush() {
+ refresh.flushWith(refreshDocInfo, refreshBoxDocInfoByBoxID)
+}
+
+func (refresh *moveDocsRefresh) flushWith(refreshParent func(*parse.Tree), refreshNotebook func(string)) {
+ if nil == refresh {
+ return
+ }
+ for _, tree := range refresh.parents {
+ refreshParent(tree)
+ }
+ for boxID := range refresh.notebooks {
+ refreshNotebook(boxID)
+ }
+}
+
func MoveDocs(fromPaths []string, toBoxID, toPath string, callback any) (err error) {
toBox := Conf.Box(toBoxID)
if nil == toBox {
@@ -1611,6 +1659,8 @@ func MoveDocs(fromPaths []string, toBoxID, toPath string, callback any) (err err
FlushTxQueue()
luteEngine := util.NewLute()
+ refresh := newMoveDocsRefresh()
+ defer refresh.flush()
count := 0
for fromPath, fromBox := range pathsBoxes {
count++
@@ -1618,7 +1668,7 @@ func MoveDocs(fromPaths []string, toBoxID, toPath string, callback any) (err err
util.PushEndlessProgress(fmt.Sprintf(Conf.Language(70), fmt.Sprintf("%d/%d", count, len(fromPaths))))
}
- _, err = moveDoc(fromBox, fromPath, toBox, toPath, luteEngine, callback)
+ _, err = moveDoc(fromBox, fromPath, toBox, toPath, luteEngine, callback, refresh)
if err != nil {
return
}
@@ -1645,7 +1695,7 @@ func countSubDocs(box, p string) (ret int) {
return
}
-func moveDoc(fromBox *Box, fromPath string, toBox *Box, toPath string, luteEngine *lute.Lute, callback any) (newPath string, err error) {
+func moveDoc(fromBox *Box, fromPath string, toBox *Box, toPath string, luteEngine *lute.Lute, callback any, refresh *moveDocsRefresh) (newPath string, err error) {
isSameBox := fromBox.ID == toBox.ID
if isSameBox {
@@ -1667,12 +1717,18 @@ func moveDoc(fromBox *Box, fromPath string, toBox *Box, toPath string, luteEngin
}
fromParentTree := loadParentTree(tree)
+ refresh.addParent(fromParentTree)
+ if path.Dir(fromPath) == "/" {
+ refresh.addNotebook(fromBox.ID)
+ }
moveToRoot := "/" == toPath
toBlockID := tree.ID
fromFolder := path.Join(path.Dir(fromPath), tree.ID)
toFolder := "/"
- if !moveToRoot {
+ if moveToRoot {
+ refresh.addNotebook(toBox.ID)
+ } else {
var toTree *parse.Tree
if isSameBox {
toTree, err = filesys.LoadTree(fromBox.ID, toPath, luteEngine)
@@ -1684,6 +1740,7 @@ func moveDoc(fromBox *Box, fromPath string, toBox *Box, toPath string, luteEngin
return
}
+ refresh.addParent(toTree)
toBlockID = toTree.ID
toFolder = path.Join(path.Dir(toPath), toBlockID)
}
@@ -1787,14 +1844,6 @@ func moveDoc(fromBox *Box, fromPath string, toBox *Box, toPath string, luteEngin
evt.Callback = callback
util.PushEvent(evt)
- refreshDocInfo(fromParentTree)
- fromRoot := path.Dir(fromPath) == "/"
- if fromRoot {
- refreshBoxDocInfoByBoxID(fromBox.ID)
- }
- if moveToRoot && (!isSameBox || !fromRoot) {
- refreshBoxDocInfoByBoxID(toBox.ID)
- }
return
}
diff --git a/kernel/model/file_test.go b/kernel/model/file_test.go
index fdc3594fb5..0fe6db440a 100644
--- a/kernel/model/file_test.go
+++ b/kernel/model/file_test.go
@@ -153,6 +153,45 @@ func TestMoveDocsRejectsInvalidPathsBeforeMoving(t *testing.T) {
}
}
+func TestMoveDocsRefreshDeduplicatesParentsAndNotebooks(t *testing.T) {
+ refresh := newMoveDocsRefresh()
+ parent1 := &parse.Tree{Box: "20260718000000-abcdefg", ID: "20260718000001-abcdefg"}
+ parent2 := &parse.Tree{Box: "20260718000002-abcdefg", ID: "20260718000001-abcdefg"}
+
+ refresh.addParent(parent1)
+ refresh.addParent(parent1)
+ refresh.addParent(parent2)
+ refresh.addNotebook(parent1.Box)
+ refresh.addNotebook(parent1.Box)
+ refresh.addNotebook(parent2.Box)
+
+ parentCalls := map[moveDocsRefreshKey]int{}
+ notebookCalls := map[string]int{}
+ refresh.flushWith(func(tree *parse.Tree) {
+ key := moveDocsRefreshKey{boxID: tree.Box, rootID: tree.ID}
+ parentCalls[key]++
+ }, func(boxID string) {
+ notebookCalls[boxID]++
+ })
+
+ if 2 != len(parentCalls) {
+ t.Fatalf("unexpected refreshed parent count: got %d, want 2", len(parentCalls))
+ }
+ for key, count := range parentCalls {
+ if 1 != count {
+ t.Fatalf("parent [%+v] refreshed %d times, want 1", key, count)
+ }
+ }
+ if 2 != len(notebookCalls) {
+ t.Fatalf("unexpected refreshed notebook count: got %d, want 2", len(notebookCalls))
+ }
+ for boxID, count := range notebookCalls {
+ if 1 != count {
+ t.Fatalf("notebook [%s] refreshed %d times, want 1", boxID, count)
+ }
+ }
+}
+
func TestSetFileTreeSort(t *testing.T) {
fixture := setupFileOperationTest(t)
diff --git a/kernel/model/push_reload.go b/kernel/model/push_reload.go
index 691b61dd22..2d7b7d19fd 100644
--- a/kernel/model/push_reload.go
+++ b/kernel/model/push_reload.go
@@ -99,6 +99,14 @@ func refreshDocInfo(tree *parse.Tree) {
refreshDocInfoWithSize(tree, filesys.TreeSize(tree))
}
+func refreshDocInfoWithoutParent(tree *parse.Tree) {
+ if nil == tree {
+ return
+ }
+
+ refreshDocInfo0(tree, filesys.TreeSize(tree))
+}
+
func refreshDocInfoWithSize(tree *parse.Tree, size uint64) {
if nil == tree {
return