🎨 Support setting automatic sync interval https://github.com/siyuan-note/siyuan/issues/13448

This commit is contained in:
Daniel 2024-12-14 12:02:14 +08:00
parent 152fa6e6e0
commit 45a6f7e29d
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
18 changed files with 100 additions and 12 deletions

View file

@ -237,6 +237,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/cloud/getCloudSpace", model.CheckAuth, model.CheckAdminRole, getCloudSpace)
ginServer.Handle("POST", "/api/sync/setSyncEnable", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, setSyncEnable)
ginServer.Handle("POST", "/api/sync/setSyncInterval", model.CheckAuth, setSyncInterval)
ginServer.Handle("POST", "/api/sync/setSyncPerception", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, setSyncPerception)
ginServer.Handle("POST", "/api/sync/setSyncGenerateConflictDoc", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, setSyncGenerateConflictDoc)
ginServer.Handle("POST", "/api/sync/setSyncMode", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, setSyncMode)

View file

@ -541,6 +541,17 @@ func setSyncEnable(c *gin.Context) {
model.SetSyncEnable(enabled)
}
func setSyncInterval(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
arg, ok := util.JsonArg(c, ret)
if !ok {
return
}
interval := int(arg["interval"].(float64))
model.SetSyncInterval(interval)
}
func setSyncPerception(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)

View file

@ -21,6 +21,7 @@ type Sync struct {
Enabled bool `json:"enabled"` // 是否开启同步
Perception bool `json:"perception"` // 是否开启感知
Mode int `json:"mode"` // 同步模式0未设置为兼容已有配置initConf 函数中会转换为 11自动2手动 https://github.com/siyuan-note/siyuan/issues/50893完全手动 https://github.com/siyuan-note/siyuan/issues/7295
Interval int `json:"interval"` // 自动同步间隔,单位:秒
Synced int64 `json:"synced"` // 最近同步时间
Stat string `json:"stat"` // 最近同步统计信息
GenerateConflictDoc bool `json:"generateConflictDoc"` // 云端同步冲突时是否生成冲突文档
@ -37,6 +38,7 @@ func NewSync() *Sync {
Mode: 1,
GenerateConflictDoc: false,
Provider: ProviderSiYuan,
Interval: 30,
}
}

View file

@ -344,6 +344,12 @@ func InitConf() {
if 0 == Conf.Sync.Mode {
Conf.Sync.Mode = 1
}
if 30 > Conf.Sync.Interval {
Conf.Sync.Interval = 30
}
if 60*60*12 < Conf.Sync.Interval {
Conf.Sync.Interval = 60 * 60 * 12
}
if nil == Conf.Sync.S3 {
Conf.Sync.S3 = &conf.S3{PathStyle: true, SkipTlsVerify: true}
}

View file

@ -389,6 +389,20 @@ func SetSyncEnable(b bool) {
return
}
func SetSyncInterval(interval int) {
if 30 > interval {
interval = 30
}
if 43200 < interval {
interval = 43200
}
Conf.Sync.Interval = interval
Conf.Save()
planSyncAfter(time.Duration(interval) * time.Second)
return
}
func SetSyncPerception(b bool) {
if util.ContainerDocker == util.Container {
b = false
@ -641,7 +655,7 @@ func getSyncIgnoreLines() (ret []string) {
func IncSync() {
syncSameCount.Store(0)
planSyncAfter(30 * time.Second)
planSyncAfter(time.Duration(Conf.Sync.Interval) * time.Second)
}
func planSyncAfter(d time.Duration) {