Skip to content

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

:bug: Allow explicit deletion of the Android notification switch https://github.com/siyuan-note/siyuan/issues/20184

Daniel82c04f0b74c8
diff --git a/kernel/api/contract_asset_test.go b/kernel/api/contract_asset_test.go
index 777542f537..e25b57399a 100644
--- a/kernel/api/contract_asset_test.go
+++ b/kernel/api/contract_asset_test.go
@@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"os"
+ "os/exec"
"path/filepath"
"reflect"
"strings"
@@ -17,6 +18,7 @@ import (
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/conf"
"github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/util"
)
@@ -265,3 +267,75 @@ func TestAssetUnusedScanFailureContract(t *testing.T) {
t.Fatalf("successful scan changed response shape: %s, %v", recorder.Body.String(), err)
}
}
+
+func TestAndroidNotificationAssetDeletionContract(t *testing.T) {
+ if os.Getenv("SIYUAN_TEST_ANDROID_NOTIFICATION_ASSET") != "1" {
+ command := exec.Command(os.Args[0], "-test.run=^TestAndroidNotificationAssetDeletionContract$", "-test.timeout=30s")
+ command.Env = append(os.Environ(), "SIYUAN_TEST_ANDROID_NOTIFICATION_ASSET=1")
+ if output, err := command.CombinedOutput(); err != nil {
+ t.Fatalf("notification asset subprocess failed: %v\n%s", err, output)
+ }
+ return
+ }
+ assets := setupAssetContractWorkspace(t)
+ model.Conf.FileTree, model.Conf.Search = conf.NewFileTree(), conf.NewSearch()
+ util.HistoryDir, util.TempDir, util.ConfDir = t.TempDir(), t.TempDir(), t.TempDir()
+ util.QueueDir = t.TempDir()
+ util.DBPath, util.HistoryDBPath = filepath.Join(util.TempDir, "siyuan.db"), filepath.Join(util.TempDir, "history.db")
+ util.AssetContentDBPath = filepath.Join(util.TempDir, "asset_content.db")
+ util.BlockTreeDBPath = filepath.Join(util.TempDir, "blocktree.db")
+ sql.InitDatabase(true)
+ sql.InitHistoryDatabase(true)
+ sql.InitAssetContentDatabase(true)
+ defer sql.CloseDatabase()
+ p := filepath.Join(assets, "android-notification-texts.txt")
+ content := []byte("notification title\n")
+ if err := os.WriteFile(p, content, 0644); err != nil {
+ t.Fatal(err)
+ }
+ engine := gin.New()
+ engine.POST("/api/asset/getUnusedAssets", getUnusedAssets)
+ engine.POST("/api/asset/removeUnusedAssets", removeUnusedAssets)
+ engine.POST("/api/asset/removeUnusedAsset", removeUnusedAsset)
+ call := func(endpoint, body string) *httptest.ResponseRecorder {
+ t.Helper()
+ url := "/api/asset/" + endpoint
+ recorder := httptest.NewRecorder()
+ engine.ServeHTTP(recorder, httptest.NewRequest("POST", url, strings.NewReader(body)))
+ requireAPIContract(t, "POST", url, recorder)
+ var response struct{ Code int }
+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil || response.Code != 0 {
+ t.Fatalf("%s failed: %s, %v", endpoint, recorder.Body.String(), err)
+ }
+ return recorder
+ }
+ for _, endpoint := range []string{"getUnusedAssets", "removeUnusedAssets"} {
+ recorder := call(endpoint, `{}`)
+ var response struct{ Data json.RawMessage }
+ want := "[]"
+ if endpoint == "removeUnusedAssets" {
+ want = `{"paths":[]}`
+ }
+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil || !bytes.Equal(response.Data, []byte(want)) {
+ t.Fatalf("%s exposed the switch: %s, %v", endpoint, recorder.Body.String(), err)
+ }
+ if stored, err := os.ReadFile(p); err != nil || !bytes.Equal(stored, content) {
+ t.Fatalf("%s changed the switch: %q, %v", endpoint, stored, err)
+ }
+ }
+ recorder := call("removeUnusedAsset", `{"path":"assets/android-notification-texts.txt"}`)
+ var response struct{ Data apicontract.AssetPathData }
+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil || response.Data.Path != p {
+ t.Fatalf("explicit deletion result differs: %s, %v", recorder.Body.String(), err)
+ }
+ if _, err := os.Stat(p); !os.IsNotExist(err) {
+ t.Fatalf("switch remains after explicit deletion: %v", err)
+ }
+ histories, err := filepath.Glob(filepath.Join(util.HistoryDir, "*-clean", "assets", "android-notification-texts.txt"))
+ if err != nil || len(histories) != 1 {
+ t.Fatalf("missing cleanup history: %v, %v", histories, err)
+ }
+ if stored, err := os.ReadFile(histories[0]); err != nil || !bytes.Equal(stored, content) {
+ t.Fatalf("history changed notification texts: %q, %v", stored, err)
+ }
+}
diff --git a/kernel/apicontract/contracts.go b/kernel/apicontract/contracts.go
index f8d19e1680..fd559b98bc 100644
--- a/kernel/apicontract/contracts.go
+++ b/kernel/apicontract/contracts.go
@@ -894,6 +894,9 @@ var GetDocImageAssets = define[AssetDocumentRequest, []string]("getDocImageAsset
var GetDocAssets = define[AssetDocumentAssetsRequest, []string]("getDocAssets", "/api/asset/getDocAssets", JSONBody, ResponseOptions{}, "POST")
var SetFileAnnotation = define[SetAssetAnnotationRequest, Null]("setFileAnnotation", "/api/asset/setFileAnnotation", JSONBody, ResponseOptions{}, "POST")
var GetFileAnnotation = define[AssetPathRequest, AssetAnnotationData]("getFileAnnotation", "/api/asset/getFileAnnotation", JSONBody, ResponseOptions{AdditionalCodes: []int{1, 403}}, "POST")
+
+// path 为 data 相对资源路径。仅全局 assets/android-notification-texts.txt 普通文件允许显式删除,
+// 无需未引用扫描;该文件仍不参与未引用资源列表和批量清理。其余资源必须经完整扫描确认未被引用。
var RemoveUnusedAsset = define[AssetPathRequest, AssetPathData]("removeUnusedAsset", "/api/asset/removeUnusedAsset", JSONBody, ResponseOptions{}, "POST")
// 未引用资源扫描失败时返回标准错误,禁止使用不完整的引用集合清理资源。
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index 72cf3b790c..52d99985ec 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -1194,13 +1194,27 @@ func ResolveDataAssetPath(assetPath string) (relativePath, absPath string, err e
return
}
-// ResolveUnusedDataAssetPath 解析 data 相对资源路径,并确认目标当前未被引用。
+// ResolveUnusedDataAssetPath 解析可单独删除的资源路径,允许显式关闭 Android 保活通知。
func ResolveUnusedDataAssetPath(assetPath string) (relativePath, absPath string, err error) {
relativePath, absPath, err = ResolveDataAssetPath(assetPath)
if err != nil {
return
}
+ if relativePath == "assets/android-notification-texts.txt" {
+ // 保活开关不参与批量清理,但可显式删除普通文件;目录和符号链接不能使用该例外。
+ info, statErr := os.Lstat(absPath)
+ if statErr != nil {
+ err = statErr
+ return
+ }
+ if info.Mode().IsRegular() {
+ return
+ }
+ err = fmt.Errorf("Android notification texts must be a regular file: %s", relativePath)
+ return
+ }
+
unusedAssets, err := UnusedAssets(false)
if err != nil {
return
diff --git a/kernel/model/assets_cleanup_test.go b/kernel/model/assets_cleanup_test.go
index cf012b1148..0299c1b299 100644
--- a/kernel/model/assets_cleanup_test.go
+++ b/kernel/model/assets_cleanup_test.go
@@ -296,3 +296,52 @@ func TestUnusedAssetsExcludesEncryptedNotebook(t *testing.T) {
t.Fatalf("encrypted notebook must remain outside global cleanup: %v, %v", items, err)
}
}
+
+func TestAndroidNotificationAssetDeletionPaths(t *testing.T) {
+ boxID, docPath := setupUnusedAssetWorkspace(t)
+ for _, name := range []string{"assets/android-notification-texts.txt", "assets/sub/android-notification-texts.txt", "assets/other-android-notification-texts.txt", boxID + "/assets/android-notification-texts.txt", "assets/ocr-texts.json"} {
+ p := filepath.Join(util.DataDir, filepath.FromSlash(name))
+ if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(p, []byte("original"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ if _, _, err := ResolveUnusedDataAssetPath(name); (err == nil) != (name == "assets/android-notification-texts.txt") {
+ t.Fatalf("unexpected deletion admission for %s: %v", name, err)
+ }
+ }
+ if err := os.WriteFile(docPath, []byte(`{"Type":"NodeDocument","Spec":"99"}`), 0644); err != nil {
+ t.Fatal(err)
+ }
+ if _, _, err := ResolveUnusedDataAssetPath("assets/android-notification-texts.txt"); err != nil {
+ t.Fatalf("explicit switch deletion must not depend on document scanning: %v", err)
+ }
+ for _, name := range []string{"../assets/android-notification-texts.txt", "/assets/android-notification-texts.txt", "assets/../../android-notification-texts.txt"} {
+ if _, _, err := ResolveUnusedDataAssetPath(name); err == nil {
+ t.Fatalf("invalid path admitted: %s", name)
+ }
+ }
+ p := filepath.Join(util.DataDir, "assets", "android-notification-texts.txt")
+ if err := os.Remove(p); err != nil {
+ t.Fatal(err)
+ }
+ if _, _, err := ResolveUnusedDataAssetPath("assets/android-notification-texts.txt"); err == nil {
+ t.Fatal("missing switch admitted")
+ }
+ if err := os.Mkdir(p, 0755); err != nil {
+ t.Fatal(err)
+ }
+ if _, _, err := ResolveUnusedDataAssetPath("assets/android-notification-texts.txt"); err == nil {
+ t.Fatal("directory admitted as notification switch")
+ }
+ if err := os.Remove(p); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Symlink(filepath.Join(util.DataDir, "assets", "ocr-texts.json"), p); err != nil {
+ t.Skipf("symlink unavailable: %v", err)
+ }
+ if _, _, err := ResolveUnusedDataAssetPath("assets/android-notification-texts.txt"); err == nil {
+ t.Fatal("symlink admitted as notification switch")
+ }
+}