123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- // SiYuan - Refactor your thinking
- // Copyright (c) 2020-present, b3log.org
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
- package api
- import (
- "fmt"
- "net/http"
- "strings"
- "github.com/88250/gulu"
- "github.com/gin-gonic/gin"
- "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"
- )
- func setConfSnippet(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- param, err := gulu.JSON.MarshalJSON(arg)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- snippet := &conf.Snpt{}
- if err = gulu.JSON.UnmarshalJSON(param, snippet); nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- model.Conf.Snippet = snippet
- model.Conf.Save()
- ret.Data = snippet
- }
- func addVirtualBlockRefExclude(c *gin.Context) {
- // Add internal kernel API `/api/setting/addVirtualBlockRefExclude` https://github.com/siyuan-note/siyuan/issues/9909
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- keywordsArg := arg["keywords"]
- var keywords []string
- for _, k := range keywordsArg.([]interface{}) {
- keywords = append(keywords, k.(string))
- }
- model.AddVirtualBlockRefExclude(keywords)
- util.BroadcastByType("main", "setConf", 0, "", model.Conf)
- }
- func addVirtualBlockRefInclude(c *gin.Context) {
- // Add internal kernel API `/api/setting/addVirtualBlockRefInclude` https://github.com/siyuan-note/siyuan/issues/9909
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- keywordsArg := arg["keywords"]
- var keywords []string
- for _, k := range keywordsArg.([]interface{}) {
- keywords = append(keywords, k.(string))
- }
- model.AddVirtualBlockRefInclude(keywords)
- util.BroadcastByType("main", "setConf", 0, "", model.Conf)
- }
- func refreshVirtualBlockRef(c *gin.Context) {
- // Add internal kernel API `/api/setting/refreshVirtualBlockRef` https://github.com/siyuan-note/siyuan/issues/9829
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- model.ResetVirtualBlockRefCache()
- util.BroadcastByType("main", "setConf", 0, "", model.Conf)
- }
- func setBazaar(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- param, err := gulu.JSON.MarshalJSON(arg)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- bazaar := &conf.Bazaar{}
- if err = gulu.JSON.UnmarshalJSON(param, bazaar); nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- model.Conf.Bazaar = bazaar
- model.Conf.Save()
- ret.Data = bazaar
- }
- func setAI(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- param, err := gulu.JSON.MarshalJSON(arg)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- ai := &conf.AI{}
- if err = gulu.JSON.UnmarshalJSON(param, ai); nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- if 5 > ai.OpenAI.APITimeout {
- ai.OpenAI.APITimeout = 5
- }
- if 600 < ai.OpenAI.APITimeout {
- ai.OpenAI.APITimeout = 600
- }
- if 0 > ai.OpenAI.APIMaxTokens {
- ai.OpenAI.APIMaxTokens = 0
- }
- model.Conf.AI = ai
- model.Conf.Save()
- ret.Data = ai
- }
- func setFlashcard(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- param, err := gulu.JSON.MarshalJSON(arg)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- flashcard := &conf.Flashcard{}
- if err = gulu.JSON.UnmarshalJSON(param, flashcard); nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- if 0 > flashcard.NewCardLimit {
- flashcard.NewCardLimit = 20
- }
- if 0 > flashcard.ReviewCardLimit {
- flashcard.ReviewCardLimit = 200
- }
- model.Conf.Flashcard = flashcard
- model.Conf.Save()
- ret.Data = flashcard
- }
- func setAccount(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- param, err := gulu.JSON.MarshalJSON(arg)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- account := &conf.Account{}
- if err = gulu.JSON.UnmarshalJSON(param, account); nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- model.Conf.Account = account
- model.Conf.Save()
- ret.Data = model.Conf.Account
- }
- func setEditor(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- param, err := gulu.JSON.MarshalJSON(arg)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- oldGenerateHistoryInterval := model.Conf.Editor.GenerateHistoryInterval
- editor := conf.NewEditor()
- if err = gulu.JSON.UnmarshalJSON(param, editor); nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- if "" == editor.PlantUMLServePath {
- editor.PlantUMLServePath = "https://www.plantuml.com/plantuml/svg/~1"
- }
- if "" == editor.KaTexMacros {
- editor.KaTexMacros = "{}"
- }
- oldVirtualBlockRef := model.Conf.Editor.VirtualBlockRef
- oldVirtualBlockRefInclude := model.Conf.Editor.VirtualBlockRefInclude
- oldVirtualBlockRefExclude := model.Conf.Editor.VirtualBlockRefExclude
- oldReadOnly := model.Conf.Editor.ReadOnly
- model.Conf.Editor = editor
- model.Conf.Save()
- if oldGenerateHistoryInterval != model.Conf.Editor.GenerateHistoryInterval {
- model.ChangeHistoryTick(editor.GenerateHistoryInterval)
- }
- if oldVirtualBlockRef != model.Conf.Editor.VirtualBlockRef ||
- oldVirtualBlockRefInclude != model.Conf.Editor.VirtualBlockRefInclude ||
- oldVirtualBlockRefExclude != model.Conf.Editor.VirtualBlockRefExclude {
- model.ResetVirtualBlockRefCache()
- }
- if oldReadOnly != model.Conf.Editor.ReadOnly {
- util.BroadcastByType("protyle", "readonly", 0, "", model.Conf.Editor.ReadOnly)
- util.BroadcastByType("main", "readonly", 0, "", model.Conf.Editor.ReadOnly)
- }
- ret.Data = model.Conf.Editor
- }
- func setExport(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- param, err := gulu.JSON.MarshalJSON(arg)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- export := &conf.Export{}
- if err = gulu.JSON.UnmarshalJSON(param, export); nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- ret.Data = map[string]interface{}{"closeTimeout": 5000}
- return
- }
- if "" != export.PandocBin {
- if !util.IsValidPandocBin(export.PandocBin) {
- util.PushErrMsg(fmt.Sprintf(model.Conf.Language(117), export.PandocBin), 5000)
- export.PandocBin = util.PandocBinPath
- } else {
- util.PandocBinPath = export.PandocBin
- }
- }
- model.Conf.Export = export
- model.Conf.Save()
- ret.Data = model.Conf.Export
- }
- func setFiletree(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- param, err := gulu.JSON.MarshalJSON(arg)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- fileTree := conf.NewFileTree()
- if err = gulu.JSON.UnmarshalJSON(param, fileTree); nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- fileTree.RefCreateSavePath = strings.TrimSpace(fileTree.RefCreateSavePath)
- if "" != fileTree.RefCreateSavePath {
- if !strings.HasSuffix(fileTree.RefCreateSavePath, "/") {
- fileTree.RefCreateSavePath += "/"
- }
- }
- fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
- if "../" == fileTree.DocCreateSavePath {
- fileTree.DocCreateSavePath = "../Untitled"
- }
- for strings.HasSuffix(fileTree.DocCreateSavePath, "/") {
- fileTree.DocCreateSavePath = strings.TrimSuffix(fileTree.DocCreateSavePath, "/")
- fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
- }
- if 1 > fileTree.MaxOpenTabCount {
- fileTree.MaxOpenTabCount = 8
- }
- if 32 < fileTree.MaxOpenTabCount {
- fileTree.MaxOpenTabCount = 32
- }
- model.Conf.FileTree = fileTree
- model.Conf.Save()
- util.UseSingleLineSave = model.Conf.FileTree.UseSingleLineSave
- ret.Data = model.Conf.FileTree
- }
- func setSearch(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- param, err := gulu.JSON.MarshalJSON(arg)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- s := &conf.Search{}
- if err = gulu.JSON.UnmarshalJSON(param, s); nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- if 32 > s.Limit {
- s.Limit = 32
- }
- oldCaseSensitive := model.Conf.Search.CaseSensitive
- oldIndexAssetPath := model.Conf.Search.IndexAssetPath
- oldVirtualRefName := model.Conf.Search.VirtualRefName
- oldVirtualRefAlias := model.Conf.Search.VirtualRefAlias
- oldVirtualRefAnchor := model.Conf.Search.VirtualRefAnchor
- oldVirtualRefDoc := model.Conf.Search.VirtualRefDoc
- model.Conf.Search = s
- model.Conf.Save()
- sql.SetCaseSensitive(s.CaseSensitive)
- sql.SetIndexAssetPath(s.IndexAssetPath)
- if needFullReindex := s.CaseSensitive != oldCaseSensitive || s.IndexAssetPath != oldIndexAssetPath; needFullReindex {
- model.FullReindex()
- }
- if oldVirtualRefName != s.VirtualRefName ||
- oldVirtualRefAlias != s.VirtualRefAlias ||
- oldVirtualRefAnchor != s.VirtualRefAnchor ||
- oldVirtualRefDoc != s.VirtualRefDoc {
- model.ResetVirtualBlockRefCache()
- }
- ret.Data = s
- }
- func setKeymap(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- param, err := gulu.JSON.MarshalJSON(arg["data"])
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- keymap := &conf.Keymap{}
- if err = gulu.JSON.UnmarshalJSON(param, keymap); nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- model.Conf.Keymap = keymap
- model.Conf.Save()
- }
- func setAppearance(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- param, err := gulu.JSON.MarshalJSON(arg)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- appearance := &conf.Appearance{}
- if err = gulu.JSON.UnmarshalJSON(param, appearance); nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- model.Conf.Appearance = appearance
- model.Conf.Lang = appearance.Lang
- util.Lang = model.Conf.Lang
- model.Conf.Save()
- model.InitAppearance()
- ret.Data = model.Conf.Appearance
- }
- func getCloudUser(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- t := arg["token"]
- var token string
- if nil != t {
- token = t.(string)
- }
- model.RefreshUser(token)
- ret.Data = model.Conf.GetUser()
- }
- func logoutCloudUser(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- model.LogoutUser()
- }
- func login2faCloudUser(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- token := arg["token"].(string)
- code := arg["code"].(string)
- data, err := model.Login2fa(token, code)
- if nil != err {
- ret.Code = -1
- ret.Msg = err.Error()
- return
- }
- ret.Data = data
- }
- func setEmoji(c *gin.Context) {
- ret := gulu.Ret.NewResult()
- defer c.JSON(http.StatusOK, ret)
- arg, ok := util.JsonArg(c, ret)
- if !ok {
- return
- }
- argEmoji := arg["emoji"].([]interface{})
- var emoji []string
- for _, ae := range argEmoji {
- emoji = append(emoji, ae.(string))
- }
- model.Conf.Editor.Emoji = emoji
- }
|