Skip to content

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

:recycle: Extract IsMobileContainer and replace the mobile ternary judgment (#17661)

Jeffrey Chenc20212594db4
diff --git a/kernel/api/workspace.go b/kernel/api/workspace.go
index a2fc03636e..9fcbddd177 100644
--- a/kernel/api/workspace.go
+++ b/kernel/api/workspace.go
@@ -195,7 +195,7 @@ func getMobileWorkspaces(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
- if util.ContainerIOS != util.Container && util.ContainerAndroid != util.Container && util.ContainerHarmony != util.Container {
+ if !util.IsMobileContainer() {
return
}
@@ -328,7 +328,7 @@ func setWorkspaceDir(c *gin.Context) {
return
}
- if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container || util.ContainerHarmony == util.Container {
+ if util.IsMobileContainer() {
util.PushMsg(model.Conf.Language(42), 1000*15)
time.Sleep(2 * time.Second)
}
diff --git a/kernel/job/cron.go b/kernel/job/cron.go
index c9a5825661..b4df3a5be1 100644
--- a/kernel/job/cron.go
+++ b/kernel/job/cron.go
@@ -47,7 +47,7 @@ func StartCron() {
go every(24*time.Hour, model.AutoPurgeRepoJob)
go every(30*time.Minute, model.AutoCheckMicrosoftDefenderJob)
go every(24*time.Hour, model.ClearOutdatedHistoryDirJob)
- if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container || util.ContainerHarmony == util.Container {
+ if util.IsMobileContainer() {
go every(7*time.Second, model.AutoConsumeShorthandsJob)
}
}
diff --git a/kernel/model/asset_content.go b/kernel/model/asset_content.go
index d763b29399..fb08bcc6c1 100644
--- a/kernel/model/asset_content.go
+++ b/kernel/model/asset_content.go
@@ -761,7 +761,7 @@ func (parser *PdfAssetParser) getTextPageWorker(instance pdfium.Pdfium, page <-c
// Parse will parse a PDF document using PDFium webassembly module using a worker pool
func (parser *PdfAssetParser) Parse(absPath string) (ret *AssetParseResult) {
- if util.ContainerIOS == util.Container || util.ContainerAndroid == util.Container || util.ContainerHarmony == util.Container {
+ if util.IsMobileContainer() {
// PDF asset content searching is not supported on mobile platforms
return
}
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index 3382bb3458..ebbd3b7f19 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -828,7 +828,7 @@ func RemoveUnusedAssets() (ret []string) {
}
}
- if !isFileWatcherAvailable() {
+ if util.IsMobileContainer() {
HandleAssetsRemoveEvent(absPath)
}
@@ -875,7 +875,7 @@ func RemoveUnusedAsset(p string) (ret string) {
cache.RemoveAssetHash(hash)
}
- if !isFileWatcherAvailable() {
+ if util.IsMobileContainer() {
HandleAssetsRemoveEvent(absPath)
}
@@ -1760,7 +1760,3 @@ func copyAssetsToDataAssets(rootPath string) {
}
}
}
-
-func isFileWatcherAvailable() bool {
- return util.ContainerAndroid != util.Container && util.ContainerIOS != util.Container && util.ContainerHarmony != util.Container
-}
diff --git a/kernel/model/assets_watcher.go b/kernel/model/assets_watcher.go
index 781c2d0414..a4d6287215 100644
--- a/kernel/model/assets_watcher.go
+++ b/kernel/model/assets_watcher.go
@@ -33,7 +33,7 @@ import (
var assetsWatcher *fsnotify.Watcher
func WatchAssets() {
- if !isFileWatcherAvailable() {
+ if util.IsMobileContainer() {
return
}
diff --git a/kernel/model/assets_watcher_darwin.go b/kernel/model/assets_watcher_darwin.go
index dfc9653e81..287b28c31e 100644
--- a/kernel/model/assets_watcher_darwin.go
+++ b/kernel/model/assets_watcher_darwin.go
@@ -33,7 +33,7 @@ import (
var assetsWatcher *watcher.Watcher
func WatchAssets() {
- if !isFileWatcherAvailable() {
+ if util.IsMobileContainer() {
return
}
diff --git a/kernel/model/block.go b/kernel/model/block.go
index d2b00b2688..6873f01ad8 100644
--- a/kernel/model/block.go
+++ b/kernel/model/block.go
@@ -398,7 +398,7 @@ func RecentUpdatedBlocks() (ret []*Block) {
ret = []*Block{}
sqlStmt := "SELECT * FROM blocks WHERE type = 'p' AND length > 1"
- if util.ContainerIOS == util.Container || util.ContainerAndroid == util.Container || util.ContainerHarmony == util.Container {
+ if util.IsMobileContainer() {
sqlStmt = "SELECT * FROM blocks WHERE type = 'd'"
}
diff --git a/kernel/model/conf.go b/kernel/model/conf.go
index 57d13c18eb..e2a3cf5995 100644
--- a/kernel/model/conf.go
+++ b/kernel/model/conf.go
@@ -139,7 +139,7 @@ func InitConf() {
if "" != util.Lang {
initialized := false
- if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container || util.ContainerHarmony == util.Container {
+ if util.IsMobileContainer() {
// 移动端以上次设置的外观语言为准
if "" != Conf.Lang && util.Lang != Conf.Lang {
util.Lang = Conf.Lang
@@ -829,7 +829,7 @@ func Close(force, setCurrentWorkspace bool, execInstallPkg int) (exitCode int) {
}
util.HttpServing = false
- if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container || util.ContainerHarmony == util.Container {
+ if util.IsMobileContainer() {
return
}
diff --git a/kernel/model/emojis_watcher.go b/kernel/model/emojis_watcher.go
index 2974e29548..e0b0b156ff 100644
--- a/kernel/model/emojis_watcher.go
+++ b/kernel/model/emojis_watcher.go
@@ -32,7 +32,7 @@ import (
var emojisWatcher *fsnotify.Watcher
func WatchEmojis() {
- if !isFileWatcherAvailable() {
+ if util.IsMobileContainer() {
return
}
diff --git a/kernel/model/emojis_watcher_darwin.go b/kernel/model/emojis_watcher_darwin.go
index 4943761123..0d45e85626 100644
--- a/kernel/model/emojis_watcher_darwin.go
+++ b/kernel/model/emojis_watcher_darwin.go
@@ -32,7 +32,7 @@ import (
var emojisWatcher *watcher.Watcher
func WatchEmojis() {
- if !isFileWatcherAvailable() {
+ if util.IsMobileContainer() {
return
}
diff --git a/kernel/model/index.go b/kernel/model/index.go
index e8af7ac429..e2e9f66f38 100644
--- a/kernel/model/index.go
+++ b/kernel/model/index.go
@@ -395,7 +395,7 @@ func subscribeSQLEvents() {
util.ContextPushMsg(context, msg)
})
eventbus.Subscribe(eventbus.EvtSQLUpdateBlocksHPaths, func(context map[string]any, blockCount int, hash string) {
- if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container || util.ContainerHarmony == util.Container {
+ if util.IsMobileContainer() {
return
}
@@ -407,7 +407,7 @@ func subscribeSQLEvents() {
})
eventbus.Subscribe(eventbus.EvtSQLInsertHistory, func(context map[string]any) {
- if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container || util.ContainerHarmony == util.Container {
+ if util.IsMobileContainer() {
return
}
@@ -419,7 +419,7 @@ func subscribeSQLEvents() {
})
eventbus.Subscribe(eventbus.EvtSQLInsertAssetContent, func(context map[string]any) {
- if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container || util.ContainerHarmony == util.Container {
+ if util.IsMobileContainer() {
return
}
diff --git a/kernel/model/index_fix.go b/kernel/model/index_fix.go
index eb76948457..5dfbc7232f 100644
--- a/kernel/model/index_fix.go
+++ b/kernel/model/index_fix.go
@@ -49,7 +49,7 @@ var (
// Index fixing should not be performed before data synchronization https://github.com/siyuan-note/siyuan/issues/10761
func checkIndex() {
checkIndexOnce.Do(func() {
- if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container || util.ContainerHarmony == util.Container {
+ if util.IsMobileContainer() {
// 移动端不执行校验 https://ld246.com/article/1734939896061
return
}
diff --git a/kernel/model/repository.go b/kernel/model/repository.go
index b572971e47..f4fb81cf02 100644
--- a/kernel/model/repository.go
+++ b/kernel/model/repository.go
@@ -1740,7 +1740,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
upsertTrees++
}
- if !isFileWatcherAvailable() && strings.HasPrefix(file.Path, "/assets/") {
+ if util.IsMobileContainer() && strings.HasPrefix(file.Path, "/assets/") {
absPath := filepath.Join(util.DataDir, file.Path)
HandleAssetsChangeEvent(absPath)
}
@@ -1790,7 +1790,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
}
}
- if !isFileWatcherAvailable() && strings.HasPrefix(file.Path, "/assets/") {
+ if util.IsMobileContainer() && strings.HasPrefix(file.Path, "/assets/") {
absPath := filepath.Join(util.DataDir, file.Path)
HandleAssetsRemoveEvent(absPath)
}
@@ -1946,7 +1946,7 @@ func indexRepoBeforeCloudSync(repo *dejavu.Repo) (beforeIndex, afterIndex *entit
FlushTxQueue()
checkChunks := true
- if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container || util.ContainerHarmony == util.Container {
+ if util.IsMobileContainer() {
// 因为移动端私有数据空间不会存在外部操作导致分块损坏的情况,所以不需要检查分块以提升性能 https://github.com/siyuan-note/siyuan/issues/13216
checkChunks = false
}
diff --git a/kernel/model/session.go b/kernel/model/session.go
index f2c32dadcb..49d9922671 100644
--- a/kernel/model/session.go
+++ b/kernel/model/session.go
@@ -312,7 +312,7 @@ func CheckAuth(c *gin.Context) {
return
}
if strings.HasPrefix(c.Request.RequestURI, "/api/sync/performSync") {
- if util.ContainerIOS == util.Container || util.ContainerAndroid == util.Container || util.ContainerHarmony == util.Container {
+ if util.IsMobileContainer() {
c.Set(RoleContextKey, RoleAdministrator)
c.Next()
return
diff --git a/kernel/model/shortcuts.go b/kernel/model/shortcuts.go
index 8562b8660b..fd5d8dd205 100644
--- a/kernel/model/shortcuts.go
+++ b/kernel/model/shortcuts.go
@@ -204,7 +204,7 @@ func MoveLocalShorthands(boxID string) (retIDs []string, err error) {
}
func consumeShorthands() {
- if util.ContainerAndroid != util.Container && util.ContainerIOS != util.Container && util.ContainerHarmony != util.Container {
+ if !util.IsMobileContainer() {
return
}
diff --git a/kernel/model/themes_watcher.go b/kernel/model/themes_watcher.go
index 2e98dc78dc..056198b384 100644
--- a/kernel/model/themes_watcher.go
+++ b/kernel/model/themes_watcher.go
@@ -32,7 +32,7 @@ import (
var themesWatcher *fsnotify.Watcher
func WatchThemes() {
- if !isFileWatcherAvailable() {
+ if util.IsMobileContainer() {
return
}
diff --git a/kernel/model/themes_watcher_darwin.go b/kernel/model/themes_watcher_darwin.go
index 332b960300..a7240d8450 100644
--- a/kernel/model/themes_watcher_darwin.go
+++ b/kernel/model/themes_watcher_darwin.go
@@ -33,7 +33,7 @@ import (
var themesWatcher *watcher.Watcher
func WatchThemes() {
- if !isFileWatcherAvailable() {
+ if util.IsMobileContainer() {
return
}
diff --git a/kernel/util/runtime.go b/kernel/util/runtime.go
index 247a5c4a51..aca546c999 100644
--- a/kernel/util/runtime.go
+++ b/kernel/util/runtime.go
@@ -127,7 +127,7 @@ func getWorkspaceDriveType() string {
return ghw.DriveTypeSSD.String()
}
- if ContainerAndroid == Container || ContainerIOS == Container || ContainerHarmony == Container {
+ if IsMobileContainer() {
return ghw.DriveTypeSSD.String()
}
diff --git a/kernel/util/working.go b/kernel/util/working.go
index c11683a72c..91a8e1b0ba 100644
--- a/kernel/util/working.go
+++ b/kernel/util/working.go
@@ -403,6 +403,11 @@ const (
FixedPort = "6806" // 固定端口
)
+// IsMobileContainer 表示当前内核运行在 Android、iOS 或鸿蒙客户端上。
+func IsMobileContainer() bool {
+ return ContainerAndroid == Container || ContainerIOS == Container || ContainerHarmony == Container
+}
+
func initPathDir() {
if err := os.MkdirAll(ConfDir, 0755); err != nil && !os.IsExist(err) {
logging.LogFatalf(logging.ExitCodeInitWorkspaceErr, "create conf folder [%s] failed: %s", ConfDir, err)