2023-06-24 20:39:55 +08:00
|
|
|
// SiYuan - Refactor your thinking
|
2022-05-26 15:18:53 +08:00
|
|
|
// 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"
|
2024-10-26 21:36:08 +08:00
|
|
|
"github.com/siyuan-note/siyuan/kernel/bazaar"
|
2022-05-26 15:18:53 +08:00
|
|
|
"github.com/siyuan-note/siyuan/kernel/conf"
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/model"
|
2024-06-12 21:03:51 +08:00
|
|
|
"github.com/siyuan-note/siyuan/kernel/server/proxy"
|
2022-05-26 15:18:53 +08:00
|
|
|
"github.com/siyuan-note/siyuan/kernel/sql"
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
|
|
)
|
|
|
|
|
2024-01-28 10:13:51 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 10:04:42 +08:00
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2023-12-18 10:04:42 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
snippet := &conf.Snpt{}
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = gulu.JSON.UnmarshalJSON(param, snippet); err != nil {
|
2023-12-18 10:04:42 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
model.Conf.Snippet = snippet
|
|
|
|
model.Conf.Save()
|
|
|
|
|
|
|
|
ret.Data = snippet
|
|
|
|
}
|
|
|
|
|
2023-12-17 22:34:35 +08:00
|
|
|
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)
|
2023-12-17 22:45:09 +08:00
|
|
|
util.BroadcastByType("main", "setConf", 0, "", model.Conf)
|
2023-12-17 22:34:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2023-12-17 22:45:09 +08:00
|
|
|
util.BroadcastByType("main", "setConf", 0, "", model.Conf)
|
2023-12-17 22:34:35 +08:00
|
|
|
}
|
|
|
|
|
2023-12-06 16:25:49 +08:00
|
|
|
func refreshVirtualBlockRef(c *gin.Context) {
|
2023-12-06 16:26:23 +08:00
|
|
|
// Add internal kernel API `/api/setting/refreshVirtualBlockRef` https://github.com/siyuan-note/siyuan/issues/9829
|
|
|
|
|
2023-12-06 16:25:49 +08:00
|
|
|
ret := gulu.Ret.NewResult()
|
|
|
|
defer c.JSON(http.StatusOK, ret)
|
|
|
|
|
|
|
|
model.ResetVirtualBlockRefCache()
|
2023-12-17 22:45:09 +08:00
|
|
|
util.BroadcastByType("main", "setConf", 0, "", model.Conf)
|
2023-12-06 16:25:49 +08:00
|
|
|
}
|
|
|
|
|
2023-05-25 10:40:09 +08:00
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2023-05-25 10:40:09 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bazaar := &conf.Bazaar{}
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = gulu.JSON.UnmarshalJSON(param, bazaar); err != nil {
|
2023-05-25 10:40:09 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
model.Conf.Bazaar = bazaar
|
|
|
|
model.Conf.Save()
|
|
|
|
|
|
|
|
ret.Data = bazaar
|
|
|
|
}
|
|
|
|
|
2023-03-21 18:59:54 +08:00
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2023-03-21 18:59:54 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ai := &conf.AI{}
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = gulu.JSON.UnmarshalJSON(param, ai); err != nil {
|
2023-03-21 18:59:54 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:23:58 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-03-20 10:54:22 +08:00
|
|
|
if 0 >= ai.OpenAI.APITemperature || 2 < ai.OpenAI.APITemperature {
|
|
|
|
ai.OpenAI.APITemperature = 1.0
|
|
|
|
}
|
|
|
|
|
2024-03-20 11:45:22 +08:00
|
|
|
if 1 > ai.OpenAI.APIMaxContexts || 64 < ai.OpenAI.APIMaxContexts {
|
|
|
|
ai.OpenAI.APIMaxContexts = 7
|
|
|
|
}
|
|
|
|
|
2023-03-21 18:59:54 +08:00
|
|
|
model.Conf.AI = ai
|
|
|
|
model.Conf.Save()
|
|
|
|
|
|
|
|
ret.Data = ai
|
|
|
|
}
|
|
|
|
|
2023-03-19 17:06:09 +08:00
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2023-03-19 17:06:09 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
flashcard := &conf.Flashcard{}
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = gulu.JSON.UnmarshalJSON(param, flashcard); err != nil {
|
2023-03-19 17:06:09 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-31 11:52:56 +08:00
|
|
|
if 0 > flashcard.NewCardLimit {
|
|
|
|
flashcard.NewCardLimit = 20
|
2023-03-19 17:06:09 +08:00
|
|
|
}
|
|
|
|
|
2023-03-31 11:52:56 +08:00
|
|
|
if 0 > flashcard.ReviewCardLimit {
|
|
|
|
flashcard.ReviewCardLimit = 200
|
2023-03-19 17:06:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
model.Conf.Flashcard = flashcard
|
|
|
|
model.Conf.Save()
|
|
|
|
|
|
|
|
ret.Data = flashcard
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
account := &conf.Account{}
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = gulu.JSON.UnmarshalJSON(param, account); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
oldGenerateHistoryInterval := model.Conf.Editor.GenerateHistoryInterval
|
|
|
|
|
|
|
|
editor := conf.NewEditor()
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = gulu.JSON.UnmarshalJSON(param, editor); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if "" == editor.PlantUMLServePath {
|
|
|
|
editor.PlantUMLServePath = "https://www.plantuml.com/plantuml/svg/~1"
|
|
|
|
}
|
|
|
|
|
2022-08-27 21:46:32 +08:00
|
|
|
if "" == editor.KaTexMacros {
|
|
|
|
editor.KaTexMacros = "{}"
|
|
|
|
}
|
|
|
|
|
2023-02-17 15:17:25 +08:00
|
|
|
oldVirtualBlockRef := model.Conf.Editor.VirtualBlockRef
|
|
|
|
oldVirtualBlockRefInclude := model.Conf.Editor.VirtualBlockRefInclude
|
|
|
|
oldVirtualBlockRefExclude := model.Conf.Editor.VirtualBlockRefExclude
|
2023-02-27 11:41:24 +08:00
|
|
|
oldReadOnly := model.Conf.Editor.ReadOnly
|
2023-02-17 15:17:25 +08:00
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
model.Conf.Editor = editor
|
|
|
|
model.Conf.Save()
|
|
|
|
|
|
|
|
if oldGenerateHistoryInterval != model.Conf.Editor.GenerateHistoryInterval {
|
|
|
|
model.ChangeHistoryTick(editor.GenerateHistoryInterval)
|
|
|
|
}
|
|
|
|
|
2023-02-17 15:17:25 +08:00
|
|
|
if oldVirtualBlockRef != model.Conf.Editor.VirtualBlockRef ||
|
|
|
|
oldVirtualBlockRefInclude != model.Conf.Editor.VirtualBlockRefInclude ||
|
|
|
|
oldVirtualBlockRefExclude != model.Conf.Editor.VirtualBlockRefExclude {
|
|
|
|
model.ResetVirtualBlockRefCache()
|
|
|
|
}
|
|
|
|
|
2023-02-27 11:34:47 +08:00
|
|
|
if oldReadOnly != model.Conf.Editor.ReadOnly {
|
|
|
|
util.BroadcastByType("protyle", "readonly", 0, "", model.Conf.Editor.ReadOnly)
|
2023-03-03 10:16:52 +08:00
|
|
|
util.BroadcastByType("main", "readonly", 0, "", model.Conf.Editor.ReadOnly)
|
2023-02-27 11:34:47 +08:00
|
|
|
}
|
|
|
|
|
2024-04-26 12:08:01 +08:00
|
|
|
util.MarkdownSettings = model.Conf.Editor.Markdown
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
export := &conf.Export{}
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = gulu.JSON.UnmarshalJSON(param, export); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
ret.Data = map[string]interface{}{"closeTimeout": 5000}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if "" != export.PandocBin {
|
|
|
|
if !util.IsValidPandocBin(export.PandocBin) {
|
2022-09-07 10:30:25 +08:00
|
|
|
util.PushErrMsg(fmt.Sprintf(model.Conf.Language(117), export.PandocBin), 5000)
|
|
|
|
export.PandocBin = util.PandocBinPath
|
2023-06-19 18:28:15 +08:00
|
|
|
} else {
|
|
|
|
util.PandocBinPath = export.PandocBin
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-23 11:42:14 +08:00
|
|
|
fileTree := conf.NewFileTree()
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = gulu.JSON.UnmarshalJSON(param, fileTree); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-31 15:29:59 +08:00
|
|
|
fileTree.RefCreateSavePath = util.TrimSpaceInPath(fileTree.RefCreateSavePath)
|
2022-05-26 15:18:53 +08:00
|
|
|
if "" != fileTree.RefCreateSavePath {
|
|
|
|
if !strings.HasSuffix(fileTree.RefCreateSavePath, "/") {
|
|
|
|
fileTree.RefCreateSavePath += "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-31 15:29:59 +08:00
|
|
|
fileTree.DocCreateSavePath = util.TrimSpaceInPath(fileTree.DocCreateSavePath)
|
2023-02-09 21:05:29 +08:00
|
|
|
|
2022-06-23 11:43:01 +08:00
|
|
|
if 1 > fileTree.MaxOpenTabCount {
|
2022-06-23 11:47:32 +08:00
|
|
|
fileTree.MaxOpenTabCount = 8
|
2022-06-23 11:43:01 +08:00
|
|
|
}
|
|
|
|
if 32 < fileTree.MaxOpenTabCount {
|
|
|
|
fileTree.MaxOpenTabCount = 32
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
model.Conf.FileTree = fileTree
|
|
|
|
model.Conf.Save()
|
|
|
|
|
2023-07-09 22:24:32 +08:00
|
|
|
util.UseSingleLineSave = model.Conf.FileTree.UseSingleLineSave
|
2025-08-24 10:49:13 +08:00
|
|
|
util.LargeFileWarningSize = model.Conf.FileTree.LargeFileWarningSize
|
2023-07-09 22:24:32 +08:00
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &conf.Search{}
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = gulu.JSON.UnmarshalJSON(param, s); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-16 15:12:33 +08:00
|
|
|
if 32 > s.Limit {
|
|
|
|
s.Limit = 32
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
2022-09-16 09:20:36 +08:00
|
|
|
oldCaseSensitive := model.Conf.Search.CaseSensitive
|
2023-05-10 10:30:05 +08:00
|
|
|
oldIndexAssetPath := model.Conf.Search.IndexAssetPath
|
|
|
|
|
2023-02-17 15:01:47 +08:00
|
|
|
oldVirtualRefName := model.Conf.Search.VirtualRefName
|
|
|
|
oldVirtualRefAlias := model.Conf.Search.VirtualRefAlias
|
|
|
|
oldVirtualRefAnchor := model.Conf.Search.VirtualRefAnchor
|
|
|
|
oldVirtualRefDoc := model.Conf.Search.VirtualRefDoc
|
2022-09-16 09:20:36 +08:00
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
model.Conf.Search = s
|
|
|
|
model.Conf.Save()
|
2023-05-10 10:30:05 +08:00
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
sql.SetCaseSensitive(s.CaseSensitive)
|
2023-05-10 10:30:05 +08:00
|
|
|
sql.SetIndexAssetPath(s.IndexAssetPath)
|
|
|
|
|
|
|
|
if needFullReindex := s.CaseSensitive != oldCaseSensitive || s.IndexAssetPath != oldIndexAssetPath; needFullReindex {
|
2022-09-16 19:45:46 +08:00
|
|
|
model.FullReindex()
|
2022-09-16 09:20:36 +08:00
|
|
|
}
|
2023-02-17 15:01:47 +08:00
|
|
|
|
|
|
|
if oldVirtualRefName != s.VirtualRefName ||
|
|
|
|
oldVirtualRefAlias != s.VirtualRefAlias ||
|
|
|
|
oldVirtualRefAnchor != s.VirtualRefAnchor ||
|
2023-03-12 16:29:14 +08:00
|
|
|
oldVirtualRefDoc != s.VirtualRefDoc {
|
2023-02-17 15:17:25 +08:00
|
|
|
model.ResetVirtualBlockRefCache()
|
2023-02-17 15:01:47 +08:00
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
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"])
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
keymap := &conf.Keymap{}
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = gulu.JSON.UnmarshalJSON(param, keymap); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
appearance := &conf.Appearance{}
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = gulu.JSON.UnmarshalJSON(param, appearance); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
model.Conf.Appearance = appearance
|
|
|
|
model.Conf.Lang = appearance.Lang
|
2024-10-26 21:36:08 +08:00
|
|
|
oldLang := util.Lang
|
2023-01-24 21:50:10 +08:00
|
|
|
util.Lang = model.Conf.Lang
|
2022-05-26 15:18:53 +08:00
|
|
|
model.Conf.Save()
|
|
|
|
model.InitAppearance()
|
|
|
|
|
2024-10-26 21:36:08 +08:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
ret.Data = model.Conf.Appearance
|
|
|
|
}
|
|
|
|
|
2024-06-12 21:03:51 +08:00
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2024-06-12 21:03:51 +08:00
|
|
|
ret.Code = -1
|
|
|
|
ret.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
publish := &conf.Publish{}
|
2024-09-04 04:40:50 +03:00
|
|
|
if err = gulu.JSON.UnmarshalJSON(param, publish); err != nil {
|
2024-06-12 21:03:51 +08:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
func getCloudUser(c *gin.Context) {
|
|
|
|
ret := gulu.Ret.NewResult()
|
|
|
|
defer c.JSON(http.StatusOK, ret)
|
|
|
|
|
2024-07-07 22:39:53 +08:00
|
|
|
if !model.IsAdminRoleContext(c) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
arg, ok := util.JsonArg(c, ret)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t := arg["token"]
|
|
|
|
var token string
|
|
|
|
if nil != t {
|
|
|
|
token = t.(string)
|
|
|
|
}
|
2023-09-26 10:30:08 +08:00
|
|
|
model.RefreshUser(token)
|
2023-12-08 21:46:46 +08:00
|
|
|
ret.Data = model.Conf.GetUser()
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-09-04 04:40:50 +03:00
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
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 {
|
2025-06-17 17:00:29 +08:00
|
|
|
e := ae.(string)
|
|
|
|
if strings.Contains(e, ".") {
|
|
|
|
// XSS through emoji name https://github.com/siyuan-note/siyuan/issues/15034
|
2025-06-23 11:03:39 +08:00
|
|
|
e = util.FilterUploadEmojiFileName(e)
|
2025-06-17 17:00:29 +08:00
|
|
|
}
|
|
|
|
emoji = append(emoji, e)
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
model.Conf.Editor.Emoji = emoji
|
|
|
|
}
|