Skip to content

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

2.0 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package model

import (
	"errors"
	"os"
	"path/filepath"

	"github.com/88250/gulu"
	"github.com/siyuan-note/filelock"
	"github.com/siyuan-note/logging"
	"github.com/siyuan-note/siyuan/kernel/conf"
	"github.com/siyuan-note/siyuan/kernel/util"
)

type globalUpdateConf struct {
	Channel string `json:"channel"`
}

func loadGlobalUpdateChannel() string {
	configPath := globalUpdateConfPath()
	data, err := os.ReadFile(configPath)
	if err != nil {
		if !os.IsNotExist(err) {
			logging.LogWarnf("read update channel config [%s] failed: %s", configPath, err)
		}
		return conf.UpdateChannelStable
	}

	config := &globalUpdateConf{}
	if err = gulu.JSON.UnmarshalJSON(data, config); err != nil {
		logging.LogWarnf("parse update channel config [%s] failed: %s", configPath, err)
		return conf.UpdateChannelStable
	}
	if !isValidUpdateChannel(config.Channel) {
		logging.LogWarnf("invalid update channel [%s], using stable channel", config.Channel)
		return conf.UpdateChannelStable
	}
	return config.Channel
}

// SetUpdateChannel 校验并保存应用级更新通道。
func SetUpdateChannel(channel string) error {
	if !isValidUpdateChannel(channel) {
		return errors.New("update channel is invalid")
	}

	data, err := gulu.JSON.MarshalIndentJSON(&globalUpdateConf{Channel: channel}, "", "  ")
	if err != nil {
		return err
	}
	configPath := globalUpdateConfPath()
	if err = os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
		return err
	}
	if err = filelock.WriteFile(configPath, data); err != nil {
		return err
	}
	UpdateSystemConf(func(s *conf.System) { s.UpdateChannel = channel })
	return nil
}

func globalUpdateConfPath() string {
	return filepath.Join(util.HomeDir, ".config", "siyuan", "update.json")
}

func isValidUpdateChannel(channel string) bool {
	return conf.UpdateChannelStable == channel || conf.UpdateChannelBeta == channel || conf.UpdateChannelAlpha == channel
}