setting.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // SiYuan - Build Your Eternal Digital Garden
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "fmt"
  19. "net/http"
  20. "strings"
  21. "github.com/88250/gulu"
  22. "github.com/gin-gonic/gin"
  23. "github.com/siyuan-note/siyuan/kernel/conf"
  24. "github.com/siyuan-note/siyuan/kernel/model"
  25. "github.com/siyuan-note/siyuan/kernel/sql"
  26. "github.com/siyuan-note/siyuan/kernel/util"
  27. )
  28. func setAI(c *gin.Context) {
  29. ret := gulu.Ret.NewResult()
  30. defer c.JSON(http.StatusOK, ret)
  31. arg, ok := util.JsonArg(c, ret)
  32. if !ok {
  33. return
  34. }
  35. param, err := gulu.JSON.MarshalJSON(arg)
  36. if nil != err {
  37. ret.Code = -1
  38. ret.Msg = err.Error()
  39. return
  40. }
  41. ai := &conf.AI{}
  42. if err = gulu.JSON.UnmarshalJSON(param, ai); nil != err {
  43. ret.Code = -1
  44. ret.Msg = err.Error()
  45. return
  46. }
  47. if 5 > ai.OpenAI.APITimeout {
  48. ai.OpenAI.APITimeout = 5
  49. }
  50. if 600 < ai.OpenAI.APITimeout {
  51. ai.OpenAI.APITimeout = 600
  52. }
  53. if 0 > ai.OpenAI.APIMaxTokens {
  54. ai.OpenAI.APIMaxTokens = 0
  55. }
  56. if 4096 < ai.OpenAI.APIMaxTokens {
  57. ai.OpenAI.APIMaxTokens = 4096
  58. }
  59. model.Conf.AI = ai
  60. model.Conf.Save()
  61. ret.Data = ai
  62. }
  63. func setFlashcard(c *gin.Context) {
  64. ret := gulu.Ret.NewResult()
  65. defer c.JSON(http.StatusOK, ret)
  66. arg, ok := util.JsonArg(c, ret)
  67. if !ok {
  68. return
  69. }
  70. param, err := gulu.JSON.MarshalJSON(arg)
  71. if nil != err {
  72. ret.Code = -1
  73. ret.Msg = err.Error()
  74. return
  75. }
  76. flashcard := &conf.Flashcard{}
  77. if err = gulu.JSON.UnmarshalJSON(param, flashcard); nil != err {
  78. ret.Code = -1
  79. ret.Msg = err.Error()
  80. return
  81. }
  82. if 0 > flashcard.NewCardLimit {
  83. flashcard.NewCardLimit = 20
  84. }
  85. if 0 > flashcard.ReviewCardLimit {
  86. flashcard.ReviewCardLimit = 200
  87. }
  88. model.Conf.Flashcard = flashcard
  89. model.Conf.Save()
  90. ret.Data = flashcard
  91. }
  92. func setAccount(c *gin.Context) {
  93. ret := gulu.Ret.NewResult()
  94. defer c.JSON(http.StatusOK, ret)
  95. arg, ok := util.JsonArg(c, ret)
  96. if !ok {
  97. return
  98. }
  99. param, err := gulu.JSON.MarshalJSON(arg)
  100. if nil != err {
  101. ret.Code = -1
  102. ret.Msg = err.Error()
  103. return
  104. }
  105. account := &conf.Account{}
  106. if err = gulu.JSON.UnmarshalJSON(param, account); nil != err {
  107. ret.Code = -1
  108. ret.Msg = err.Error()
  109. return
  110. }
  111. model.Conf.Account = account
  112. model.Conf.Save()
  113. ret.Data = model.Conf.Account
  114. }
  115. func setEditor(c *gin.Context) {
  116. ret := gulu.Ret.NewResult()
  117. defer c.JSON(http.StatusOK, ret)
  118. arg, ok := util.JsonArg(c, ret)
  119. if !ok {
  120. return
  121. }
  122. param, err := gulu.JSON.MarshalJSON(arg)
  123. if nil != err {
  124. ret.Code = -1
  125. ret.Msg = err.Error()
  126. return
  127. }
  128. oldGenerateHistoryInterval := model.Conf.Editor.GenerateHistoryInterval
  129. editor := conf.NewEditor()
  130. if err = gulu.JSON.UnmarshalJSON(param, editor); nil != err {
  131. ret.Code = -1
  132. ret.Msg = err.Error()
  133. return
  134. }
  135. if "" == editor.PlantUMLServePath {
  136. editor.PlantUMLServePath = "https://www.plantuml.com/plantuml/svg/~1"
  137. }
  138. if "" == editor.KaTexMacros {
  139. editor.KaTexMacros = "{}"
  140. }
  141. oldVirtualBlockRef := model.Conf.Editor.VirtualBlockRef
  142. oldVirtualBlockRefInclude := model.Conf.Editor.VirtualBlockRefInclude
  143. oldVirtualBlockRefExclude := model.Conf.Editor.VirtualBlockRefExclude
  144. oldReadOnly := model.Conf.Editor.ReadOnly
  145. model.Conf.Editor = editor
  146. model.Conf.Save()
  147. if oldGenerateHistoryInterval != model.Conf.Editor.GenerateHistoryInterval {
  148. model.ChangeHistoryTick(editor.GenerateHistoryInterval)
  149. }
  150. if oldVirtualBlockRef != model.Conf.Editor.VirtualBlockRef ||
  151. oldVirtualBlockRefInclude != model.Conf.Editor.VirtualBlockRefInclude ||
  152. oldVirtualBlockRefExclude != model.Conf.Editor.VirtualBlockRefExclude {
  153. model.ResetVirtualBlockRefCache()
  154. }
  155. if oldReadOnly != model.Conf.Editor.ReadOnly {
  156. util.BroadcastByType("protyle", "readonly", 0, "", model.Conf.Editor.ReadOnly)
  157. util.BroadcastByType("main", "readonly", 0, "", model.Conf.Editor.ReadOnly)
  158. }
  159. ret.Data = model.Conf.Editor
  160. }
  161. func setExport(c *gin.Context) {
  162. ret := gulu.Ret.NewResult()
  163. defer c.JSON(http.StatusOK, ret)
  164. arg, ok := util.JsonArg(c, ret)
  165. if !ok {
  166. return
  167. }
  168. param, err := gulu.JSON.MarshalJSON(arg)
  169. if nil != err {
  170. ret.Code = -1
  171. ret.Msg = err.Error()
  172. return
  173. }
  174. export := &conf.Export{}
  175. if err = gulu.JSON.UnmarshalJSON(param, export); nil != err {
  176. ret.Code = -1
  177. ret.Msg = err.Error()
  178. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  179. return
  180. }
  181. if "" != export.PandocBin {
  182. if !util.IsValidPandocBin(export.PandocBin) {
  183. util.PushErrMsg(fmt.Sprintf(model.Conf.Language(117), export.PandocBin), 5000)
  184. export.PandocBin = util.PandocBinPath
  185. }
  186. }
  187. model.Conf.Export = export
  188. model.Conf.Save()
  189. ret.Data = model.Conf.Export
  190. }
  191. func setFiletree(c *gin.Context) {
  192. ret := gulu.Ret.NewResult()
  193. defer c.JSON(http.StatusOK, ret)
  194. arg, ok := util.JsonArg(c, ret)
  195. if !ok {
  196. return
  197. }
  198. param, err := gulu.JSON.MarshalJSON(arg)
  199. if nil != err {
  200. ret.Code = -1
  201. ret.Msg = err.Error()
  202. return
  203. }
  204. fileTree := conf.NewFileTree()
  205. if err = gulu.JSON.UnmarshalJSON(param, fileTree); nil != err {
  206. ret.Code = -1
  207. ret.Msg = err.Error()
  208. return
  209. }
  210. fileTree.RefCreateSavePath = strings.TrimSpace(fileTree.RefCreateSavePath)
  211. if "" != fileTree.RefCreateSavePath {
  212. if !strings.HasSuffix(fileTree.RefCreateSavePath, "/") {
  213. fileTree.RefCreateSavePath += "/"
  214. }
  215. }
  216. fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
  217. if "../" == fileTree.DocCreateSavePath {
  218. fileTree.DocCreateSavePath = "../Untitled"
  219. }
  220. for strings.HasSuffix(fileTree.DocCreateSavePath, "/") {
  221. fileTree.DocCreateSavePath = strings.TrimSuffix(fileTree.DocCreateSavePath, "/")
  222. fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
  223. }
  224. if 1 > fileTree.MaxOpenTabCount {
  225. fileTree.MaxOpenTabCount = 8
  226. }
  227. if 32 < fileTree.MaxOpenTabCount {
  228. fileTree.MaxOpenTabCount = 32
  229. }
  230. model.Conf.FileTree = fileTree
  231. model.Conf.Save()
  232. ret.Data = model.Conf.FileTree
  233. }
  234. func setSearch(c *gin.Context) {
  235. ret := gulu.Ret.NewResult()
  236. defer c.JSON(http.StatusOK, ret)
  237. arg, ok := util.JsonArg(c, ret)
  238. if !ok {
  239. return
  240. }
  241. param, err := gulu.JSON.MarshalJSON(arg)
  242. if nil != err {
  243. ret.Code = -1
  244. ret.Msg = err.Error()
  245. return
  246. }
  247. s := &conf.Search{}
  248. if err = gulu.JSON.UnmarshalJSON(param, s); nil != err {
  249. ret.Code = -1
  250. ret.Msg = err.Error()
  251. return
  252. }
  253. if 32 > s.Limit {
  254. s.Limit = 32
  255. }
  256. oldCaseSensitive := model.Conf.Search.CaseSensitive
  257. oldIndexAssetPath := model.Conf.Search.IndexAssetPath
  258. oldVirtualRefName := model.Conf.Search.VirtualRefName
  259. oldVirtualRefAlias := model.Conf.Search.VirtualRefAlias
  260. oldVirtualRefAnchor := model.Conf.Search.VirtualRefAnchor
  261. oldVirtualRefDoc := model.Conf.Search.VirtualRefDoc
  262. model.Conf.Search = s
  263. model.Conf.Save()
  264. sql.SetCaseSensitive(s.CaseSensitive)
  265. sql.SetIndexAssetPath(s.IndexAssetPath)
  266. if needFullReindex := s.CaseSensitive != oldCaseSensitive || s.IndexAssetPath != oldIndexAssetPath; needFullReindex {
  267. model.FullReindex()
  268. }
  269. if oldVirtualRefName != s.VirtualRefName ||
  270. oldVirtualRefAlias != s.VirtualRefAlias ||
  271. oldVirtualRefAnchor != s.VirtualRefAnchor ||
  272. oldVirtualRefDoc != s.VirtualRefDoc {
  273. model.ResetVirtualBlockRefCache()
  274. }
  275. ret.Data = s
  276. }
  277. func setKeymap(c *gin.Context) {
  278. ret := gulu.Ret.NewResult()
  279. defer c.JSON(http.StatusOK, ret)
  280. arg, ok := util.JsonArg(c, ret)
  281. if !ok {
  282. return
  283. }
  284. param, err := gulu.JSON.MarshalJSON(arg["data"])
  285. if nil != err {
  286. ret.Code = -1
  287. ret.Msg = err.Error()
  288. return
  289. }
  290. keymap := &conf.Keymap{}
  291. if err = gulu.JSON.UnmarshalJSON(param, keymap); nil != err {
  292. ret.Code = -1
  293. ret.Msg = err.Error()
  294. return
  295. }
  296. model.Conf.Keymap = keymap
  297. model.Conf.Save()
  298. }
  299. func setAppearance(c *gin.Context) {
  300. ret := gulu.Ret.NewResult()
  301. defer c.JSON(http.StatusOK, ret)
  302. arg, ok := util.JsonArg(c, ret)
  303. if !ok {
  304. return
  305. }
  306. param, err := gulu.JSON.MarshalJSON(arg)
  307. if nil != err {
  308. ret.Code = -1
  309. ret.Msg = err.Error()
  310. return
  311. }
  312. appearance := &conf.Appearance{}
  313. if err = gulu.JSON.UnmarshalJSON(param, appearance); nil != err {
  314. ret.Code = -1
  315. ret.Msg = err.Error()
  316. return
  317. }
  318. model.Conf.Appearance = appearance
  319. model.Conf.Lang = appearance.Lang
  320. util.Lang = model.Conf.Lang
  321. model.Conf.Save()
  322. model.InitAppearance()
  323. ret.Data = model.Conf.Appearance
  324. }
  325. func getCloudUser(c *gin.Context) {
  326. ret := gulu.Ret.NewResult()
  327. defer c.JSON(http.StatusOK, ret)
  328. arg, ok := util.JsonArg(c, ret)
  329. if !ok {
  330. return
  331. }
  332. t := arg["token"]
  333. var token string
  334. if nil != t {
  335. token = t.(string)
  336. }
  337. if err := model.RefreshUser(token); nil != err {
  338. ret.Code = 1
  339. ret.Msg = err.Error()
  340. return
  341. }
  342. ret.Data = model.Conf.User
  343. }
  344. func logoutCloudUser(c *gin.Context) {
  345. ret := gulu.Ret.NewResult()
  346. defer c.JSON(http.StatusOK, ret)
  347. model.LogoutUser()
  348. }
  349. func login2faCloudUser(c *gin.Context) {
  350. ret := gulu.Ret.NewResult()
  351. defer c.JSON(http.StatusOK, ret)
  352. arg, ok := util.JsonArg(c, ret)
  353. if !ok {
  354. return
  355. }
  356. token := arg["token"].(string)
  357. code := arg["code"].(string)
  358. data, err := model.Login2fa(token, code)
  359. if nil != err {
  360. ret.Code = -1
  361. ret.Msg = err.Error()
  362. return
  363. }
  364. ret.Data = data
  365. }
  366. func setEmoji(c *gin.Context) {
  367. ret := gulu.Ret.NewResult()
  368. defer c.JSON(http.StatusOK, ret)
  369. arg, ok := util.JsonArg(c, ret)
  370. if !ok {
  371. return
  372. }
  373. argEmoji := arg["emoji"].([]interface{})
  374. var emoji []string
  375. for _, ae := range argEmoji {
  376. emoji = append(emoji, ae.(string))
  377. }
  378. model.Conf.Editor.Emoji = emoji
  379. }