Skip to content

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

:bug: Avoid repeated closed notebook notifications https://github.com/siyuan-note/siyuan/issues/19868

Daniela1bd7874fa37
diff --git a/kernel/model/mount.go b/kernel/model/mount.go
index 900fda0414..fabf5b5630 100644
--- a/kernel/model/mount.go
+++ b/kernel/model/mount.go
@@ -539,6 +539,7 @@ func mountBox(boxID string) (alreadyMount bool, err error) {
for _, box := range Conf.GetOpenedBoxes() {
if box.ID == boxID {
+ clearClosedBoxNotification(boxID)
return true, nil
}
}
@@ -570,6 +571,9 @@ func mountBox(boxID string) (alreadyMount bool, err error) {
if 0 < len(files) || (nil != box && box.Exist(boxDocPath(box.ID))) {
box.Index()
}
+ if Conf.Box(boxID) != nil {
+ clearClosedBoxNotification(boxID)
+ }
if reMountGuide {
return true, nil
diff --git a/kernel/model/tree.go b/kernel/model/tree.go
index 6e63c4eaa6..b4002d6575 100644
--- a/kernel/model/tree.go
+++ b/kernel/model/tree.go
@@ -24,6 +24,7 @@ import (
"path"
"path/filepath"
"strings"
+ "sync"
"time"
"github.com/88250/lute"
@@ -315,6 +316,18 @@ func loadTreeByBlockIDInBox0(id, boxID string, logNotFound bool) (ret *parse.Tre
var searchTreeLimiter = rate.NewLimiter(rate.Every(3*time.Second), 1)
+// 关闭笔记本的提示按工作区和笔记本去重,重新打开后允许再次提示。
+var closedBoxNotifications sync.Map
+
+func markClosedBoxNotification(boxID string) bool {
+ _, loaded := closedBoxNotifications.LoadOrStore(filepath.Join(util.DataDir, boxID), struct{}{})
+ return !loaded
+}
+
+func clearClosedBoxNotification(boxID string) {
+ closedBoxNotifications.Delete(filepath.Join(util.DataDir, boxID))
+}
+
func indexTreeInFilesystem(blockID string) error {
return indexTreeInFilesystem0(blockID, false)
}
@@ -357,7 +370,9 @@ func indexTreeInFilesystem0(blockID string, normalOnly bool) error {
for _, b := range Conf.GetClosedBoxes() {
if b.ID == boxID {
logging.LogInfof("box [%s] is closed", boxID)
- util.PushErrMsg(fmt.Sprintf(Conf.language(197), b.Name), 7000)
+ if markClosedBoxNotification(boxID) {
+ util.PushErrMsg(fmt.Sprintf(Conf.language(197), b.Name), 7000)
+ }
return ErrBoxUnindexed
}
}
diff --git a/kernel/model/tree_notification_test.go b/kernel/model/tree_notification_test.go
new file mode 100644
index 0000000000..00b7ffe65f
--- /dev/null
+++ b/kernel/model/tree_notification_test.go
@@ -0,0 +1,92 @@
+package model
+
+import (
+ "errors"
+ "path/filepath"
+ "sync"
+ "sync/atomic"
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+ "golang.org/x/time/rate"
+)
+
+func TestClosedBoxNotification(t *testing.T) {
+ originalDataDir := util.DataDir
+ util.DataDir = t.TempDir()
+ t.Cleanup(func() {
+ clearClosedBoxNotification("first")
+ clearClosedBoxNotification("second")
+ util.DataDir = originalDataDir
+ })
+ var count atomic.Int32
+ var group sync.WaitGroup
+ for i := 0; i < 32; i++ {
+ group.Add(1)
+ go func() {
+ defer group.Done()
+ if markClosedBoxNotification("first") {
+ count.Add(1)
+ }
+ }()
+ }
+ group.Wait()
+ if count.Load() != 1 {
+ t.Fatalf("concurrent requests produced %d notifications", count.Load())
+ }
+ if !markClosedBoxNotification("second") {
+ t.Fatal("another notebook must have its own notification")
+ }
+ clearClosedBoxNotification("first")
+ if !markClosedBoxNotification("first") || markClosedBoxNotification("first") {
+ t.Fatal("reopening must allow exactly one new notification")
+ }
+ if markClosedBoxNotification("second") {
+ t.Fatal("reopening one notebook reset another notebook")
+ }
+ workspace := util.DataDir
+ util.DataDir = filepath.Join(workspace, "another-workspace")
+ if !markClosedBoxNotification("first") {
+ t.Fatal("another workspace must have its own notification")
+ }
+ clearClosedBoxNotification("first")
+ util.DataDir = workspace
+}
+
+func TestClosedBoxRepeatedLookupPreservesError(t *testing.T) {
+ fixture := setupFileOperationTest(t)
+ originalLimiter := searchTreeLimiter
+ searchTreeLimiter = rate.NewLimiter(rate.Inf, 1)
+ t.Cleanup(func() {
+ searchTreeLimiter = originalLimiter
+ clearClosedBoxNotification(fixture.box.ID)
+ })
+ boxConf := fixture.box.GetConf()
+ boxConf.Closed = true
+ if err := fixture.box.SaveConf(boxConf); err != nil {
+ t.Fatal(err)
+ }
+ treenode.RemoveBlockTreesByBoxID(fixture.box.ID)
+ for _, id := range []string{fixture.sourceID, fixture.targetID, fixture.sourceID} {
+ if err := ReindexMissingNormalBlock(id); !errors.Is(err, ErrBoxUnindexed) {
+ t.Fatalf("lookup %s returned %v", id, err)
+ }
+ if treenode.GetBlockTree(id) != nil {
+ t.Fatal("closed notebook was indexed")
+ }
+ }
+ if markClosedBoxNotification(fixture.box.ID) {
+ t.Fatal("lookup did not record the notebook notification")
+ }
+ boxConf.Closed = false
+ if err := fixture.box.SaveConf(boxConf); err != nil {
+ t.Fatal(err)
+ }
+ if mounted, err := mountBox(fixture.box.ID); err != nil || !mounted {
+ t.Fatalf("open notebook: mounted=%v err=%v", mounted, err)
+ }
+ if !markClosedBoxNotification(fixture.box.ID) {
+ t.Fatal("opening the notebook did not reset its notification")
+ }
+}