Something went wrong. Try again.
A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
Something went wrong. Try again.
Go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671// 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/bazaar" "github.com/siyuan-note/siyuan/kernel/conf" "github.com/siyuan-note/logging" "github.com/siyuan-note/siyuan/kernel/model" "github.com/siyuan-note/siyuan/kernel/server/proxy" "github.com/siyuan-note/siyuan/kernel/sql" "github.com/siyuan-note/siyuan/kernel/util")
func setEditorReadOnly(c *gin.Context) { ret := gulu.Ret.NewResult() defer c.JSON(http.StatusOK, ret)
arg, ok := util.JsonArg(c, ret) if !ok { return }
readOnly := arg["readonly"].(bool)
oldReadOnly := model.Conf.Editor.ReadOnly model.Conf.Editor.ReadOnly = readOnly model.Conf.Save()
if oldReadOnly != model.Conf.Editor.ReadOnly { util.BroadcastByType("protyle", "readonly", 0, "", model.Conf.Editor.ReadOnly) util.BroadcastByType("main", "readonly", 0, "", model.Conf.Editor.ReadOnly) }}
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 err != nil { ret.Code = -1 ret.Msg = err.Error() return }
snippet := &conf.Snpt{} if err = gulu.JSON.UnmarshalJSON(param, snippet); err != nil { ret.Code = -1 ret.Msg = err.Error() return }
model.Conf.Snippet = snippet model.Conf.Save()
ret.Data = snippet model.PushReloadSnippet(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 err != nil { ret.Code = -1 ret.Msg = err.Error() return }
bazaar := &conf.Bazaar{} if err = gulu.JSON.UnmarshalJSON(param, bazaar); err != nil { 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 err != nil { ret.Code = -1 ret.Msg = err.Error() return }
ai := &conf.AI{} if err = gulu.JSON.UnmarshalJSON(param, ai); err != nil { 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 }
if 0 >= ai.OpenAI.APITemperature || 2 < ai.OpenAI.APITemperature { ai.OpenAI.APITemperature = 1.0 }
if 1 > ai.OpenAI.APIMaxContexts || 64 < ai.OpenAI.APIMaxContexts { ai.OpenAI.APIMaxContexts = 7 }
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 err != nil { ret.Code = -1 ret.Msg = err.Error() return }
flashcard := &conf.Flashcard{} if err = gulu.JSON.UnmarshalJSON(param, flashcard); err != nil { 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) { logging.LogErrorf("Disabled API Call 'setAccount' called: %s", c.Request) return}
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 err != nil { ret.Code = -1 ret.Msg = err.Error() return }
oldGenerateHistoryInterval := model.Conf.Editor.GenerateHistoryInterval
editor := conf.NewEditor() if err = gulu.JSON.UnmarshalJSON(param, editor); err != nil { 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 = "{}" }
if 1 > editor.HistoryRetentionDays { editor.HistoryRetentionDays = 30 } if 3650 < editor.HistoryRetentionDays { editor.HistoryRetentionDays = 3650 }
if nil == editor.FloatWindowDelay { v := 620 editor.FloatWindowDelay = &v } else { *editor.FloatWindowDelay = max(0, min(2000, *editor.FloatWindowDelay)) }
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.GenerateFileHistory() 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) }
util.MarkdownSettings = model.Conf.Editor.Markdown
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 err != nil { ret.Code = -1 ret.Msg = err.Error() return }
export := &conf.Export{} if err = gulu.JSON.UnmarshalJSON(param, export); err != nil { 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 err != nil { ret.Code = -1 ret.Msg = err.Error() return }
fileTree := conf.NewFileTree() if err = gulu.JSON.UnmarshalJSON(param, fileTree); err != nil { ret.Code = -1 ret.Msg = err.Error() return }
fileTree.RefCreateSavePath = util.TrimSpaceInPath(fileTree.RefCreateSavePath) if "" != fileTree.RefCreateSavePath { if !strings.HasSuffix(fileTree.RefCreateSavePath, "/") { fileTree.RefCreateSavePath += "/" } }
fileTree.DocCreateSavePath = util.TrimSpaceInPath(fileTree.DocCreateSavePath)
if 1 > fileTree.MaxOpenTabCount { fileTree.MaxOpenTabCount = 8 } if 32 < fileTree.MaxOpenTabCount { fileTree.MaxOpenTabCount = 32 }
if conf.MinFileTreeRecentDocsListCount > fileTree.RecentDocsMaxListCount { fileTree.RecentDocsMaxListCount = conf.MinFileTreeRecentDocsListCount } if conf.MaxFileTreeRecentDocsListCount < fileTree.RecentDocsMaxListCount { fileTree.RecentDocsMaxListCount = conf.MaxFileTreeRecentDocsListCount }
model.Conf.FileTree = fileTree model.Conf.Save()
util.UseSingleLineSave = model.Conf.FileTree.UseSingleLineSave util.LargeFileWarningSize = model.Conf.FileTree.LargeFileWarningSize
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 err != nil { ret.Code = -1 ret.Msg = err.Error() return }
s := &conf.Search{} if err = gulu.JSON.UnmarshalJSON(param, s); err != nil { 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 err != nil { ret.Code = -1 ret.Msg = err.Error() return }
keymap := &conf.Keymap{} if err = gulu.JSON.UnmarshalJSON(param, keymap); err != nil { 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 err != nil { ret.Code = -1 ret.Msg = err.Error() return }
appearance := &conf.Appearance{} if err = gulu.JSON.UnmarshalJSON(param, appearance); err != nil { ret.Code = -1 ret.Msg = err.Error() return }
model.Conf.Appearance = appearance util.StatusBarCfg = model.Conf.Appearance.StatusBar model.Conf.Lang = appearance.Lang oldLang := util.Lang util.Lang = model.Conf.Lang model.Conf.Save() model.InitAppearance()
if oldLang != util.Lang { // The marketplace language does not change after switching the appearance language https://github.com/siyuan-note/siyuan/issues/12892 bazaar.CleanBazaarPackageCache() }
ret.Data = model.Conf.Appearance util.BroadcastByType("main", "setAppearance", 0, "", model.Conf.Appearance)}
func setPublish(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 err != nil { ret.Code = -1 ret.Msg = err.Error() return }
publish := &conf.Publish{} if err = gulu.JSON.UnmarshalJSON(param, publish); err != nil { ret.Code = -1 ret.Msg = err.Error() return }
model.Conf.Publish = publish model.Conf.Save()
if port, err := proxy.InitPublishService(); err != nil { ret.Code = -1 ret.Msg = err.Error() } else { ret.Data = map[string]any{ "port": port, "publish": model.Conf.Publish, } }}
func getPublish(c *gin.Context) { ret := gulu.Ret.NewResult() defer c.JSON(http.StatusOK, ret)
if port, err := proxy.InitPublishService(); err != nil { ret.Code = -1 ret.Msg = err.Error() } else { ret.Data = map[string]any{ "port": port, "publish": model.Conf.Publish, } }}
func getCloudUser(c *gin.Context) { ret := gulu.Ret.NewResult() defer c.JSON(http.StatusOK, ret)
if !model.IsAdminRoleContext(c) { return }
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 err != nil { 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 { e := ae.(string) if strings.Contains(e, ".") { // XSS through emoji name https://github.com/siyuan-note/siyuan/issues/15034 e = util.FilterUploadEmojiFileName(e) } emoji = append(emoji, e) }
model.Conf.Editor.Emoji = emoji}