setting.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 setBazaar(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. bazaar := &conf.Bazaar{}
  42. if err = gulu.JSON.UnmarshalJSON(param, bazaar); nil != err {
  43. ret.Code = -1
  44. ret.Msg = err.Error()
  45. return
  46. }
  47. model.Conf.Bazaar = bazaar
  48. model.Conf.Save()
  49. ret.Data = bazaar
  50. }
  51. func setAI(c *gin.Context) {
  52. ret := gulu.Ret.NewResult()
  53. defer c.JSON(http.StatusOK, ret)
  54. arg, ok := util.JsonArg(c, ret)
  55. if !ok {
  56. return
  57. }
  58. param, err := gulu.JSON.MarshalJSON(arg)
  59. if nil != err {
  60. ret.Code = -1
  61. ret.Msg = err.Error()
  62. return
  63. }
  64. ai := &conf.AI{}
  65. if err = gulu.JSON.UnmarshalJSON(param, ai); nil != err {
  66. ret.Code = -1
  67. ret.Msg = err.Error()
  68. return
  69. }
  70. if 5 > ai.OpenAI.APITimeout {
  71. ai.OpenAI.APITimeout = 5
  72. }
  73. if 600 < ai.OpenAI.APITimeout {
  74. ai.OpenAI.APITimeout = 600
  75. }
  76. if 0 > ai.OpenAI.APIMaxTokens {
  77. ai.OpenAI.APIMaxTokens = 0
  78. }
  79. if 4096 < ai.OpenAI.APIMaxTokens {
  80. ai.OpenAI.APIMaxTokens = 4096
  81. }
  82. model.Conf.AI = ai
  83. model.Conf.Save()
  84. ret.Data = ai
  85. }
  86. func setFlashcard(c *gin.Context) {
  87. ret := gulu.Ret.NewResult()
  88. defer c.JSON(http.StatusOK, ret)
  89. arg, ok := util.JsonArg(c, ret)
  90. if !ok {
  91. return
  92. }
  93. param, err := gulu.JSON.MarshalJSON(arg)
  94. if nil != err {
  95. ret.Code = -1
  96. ret.Msg = err.Error()
  97. return
  98. }
  99. flashcard := &conf.Flashcard{}
  100. if err = gulu.JSON.UnmarshalJSON(param, flashcard); nil != err {
  101. ret.Code = -1
  102. ret.Msg = err.Error()
  103. return
  104. }
  105. if 0 > flashcard.NewCardLimit {
  106. flashcard.NewCardLimit = 20
  107. }
  108. if 0 > flashcard.ReviewCardLimit {
  109. flashcard.ReviewCardLimit = 200
  110. }
  111. model.Conf.Flashcard = flashcard
  112. model.Conf.Save()
  113. ret.Data = flashcard
  114. }
  115. func setAccount(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. account := &conf.Account{}
  129. if err = gulu.JSON.UnmarshalJSON(param, account); nil != err {
  130. ret.Code = -1
  131. ret.Msg = err.Error()
  132. return
  133. }
  134. model.Conf.Account = account
  135. model.Conf.Save()
  136. ret.Data = model.Conf.Account
  137. }
  138. func setEditor(c *gin.Context) {
  139. ret := gulu.Ret.NewResult()
  140. defer c.JSON(http.StatusOK, ret)
  141. arg, ok := util.JsonArg(c, ret)
  142. if !ok {
  143. return
  144. }
  145. param, err := gulu.JSON.MarshalJSON(arg)
  146. if nil != err {
  147. ret.Code = -1
  148. ret.Msg = err.Error()
  149. return
  150. }
  151. oldGenerateHistoryInterval := model.Conf.Editor.GenerateHistoryInterval
  152. editor := conf.NewEditor()
  153. if err = gulu.JSON.UnmarshalJSON(param, editor); nil != err {
  154. ret.Code = -1
  155. ret.Msg = err.Error()
  156. return
  157. }
  158. if "" == editor.PlantUMLServePath {
  159. editor.PlantUMLServePath = "https://www.plantuml.com/plantuml/svg/~1"
  160. }
  161. if "" == editor.KaTexMacros {
  162. editor.KaTexMacros = "{}"
  163. }
  164. oldVirtualBlockRef := model.Conf.Editor.VirtualBlockRef
  165. oldVirtualBlockRefInclude := model.Conf.Editor.VirtualBlockRefInclude
  166. oldVirtualBlockRefExclude := model.Conf.Editor.VirtualBlockRefExclude
  167. oldReadOnly := model.Conf.Editor.ReadOnly
  168. model.Conf.Editor = editor
  169. model.Conf.Save()
  170. if oldGenerateHistoryInterval != model.Conf.Editor.GenerateHistoryInterval {
  171. model.ChangeHistoryTick(editor.GenerateHistoryInterval)
  172. }
  173. if oldVirtualBlockRef != model.Conf.Editor.VirtualBlockRef ||
  174. oldVirtualBlockRefInclude != model.Conf.Editor.VirtualBlockRefInclude ||
  175. oldVirtualBlockRefExclude != model.Conf.Editor.VirtualBlockRefExclude {
  176. model.ResetVirtualBlockRefCache()
  177. }
  178. if oldReadOnly != model.Conf.Editor.ReadOnly {
  179. util.BroadcastByType("protyle", "readonly", 0, "", model.Conf.Editor.ReadOnly)
  180. util.BroadcastByType("main", "readonly", 0, "", model.Conf.Editor.ReadOnly)
  181. }
  182. ret.Data = model.Conf.Editor
  183. }
  184. func setExport(c *gin.Context) {
  185. ret := gulu.Ret.NewResult()
  186. defer c.JSON(http.StatusOK, ret)
  187. arg, ok := util.JsonArg(c, ret)
  188. if !ok {
  189. return
  190. }
  191. param, err := gulu.JSON.MarshalJSON(arg)
  192. if nil != err {
  193. ret.Code = -1
  194. ret.Msg = err.Error()
  195. return
  196. }
  197. export := &conf.Export{}
  198. if err = gulu.JSON.UnmarshalJSON(param, export); nil != err {
  199. ret.Code = -1
  200. ret.Msg = err.Error()
  201. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  202. return
  203. }
  204. if "" != export.PandocBin {
  205. if !util.IsValidPandocBin(export.PandocBin) {
  206. util.PushErrMsg(fmt.Sprintf(model.Conf.Language(117), export.PandocBin), 5000)
  207. export.PandocBin = util.PandocBinPath
  208. }
  209. }
  210. model.Conf.Export = export
  211. model.Conf.Save()
  212. ret.Data = model.Conf.Export
  213. }
  214. func setFiletree(c *gin.Context) {
  215. ret := gulu.Ret.NewResult()
  216. defer c.JSON(http.StatusOK, ret)
  217. arg, ok := util.JsonArg(c, ret)
  218. if !ok {
  219. return
  220. }
  221. param, err := gulu.JSON.MarshalJSON(arg)
  222. if nil != err {
  223. ret.Code = -1
  224. ret.Msg = err.Error()
  225. return
  226. }
  227. fileTree := conf.NewFileTree()
  228. if err = gulu.JSON.UnmarshalJSON(param, fileTree); nil != err {
  229. ret.Code = -1
  230. ret.Msg = err.Error()
  231. return
  232. }
  233. fileTree.RefCreateSavePath = strings.TrimSpace(fileTree.RefCreateSavePath)
  234. if "" != fileTree.RefCreateSavePath {
  235. if !strings.HasSuffix(fileTree.RefCreateSavePath, "/") {
  236. fileTree.RefCreateSavePath += "/"
  237. }
  238. }
  239. fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
  240. if "../" == fileTree.DocCreateSavePath {
  241. fileTree.DocCreateSavePath = "../Untitled"
  242. }
  243. for strings.HasSuffix(fileTree.DocCreateSavePath, "/") {
  244. fileTree.DocCreateSavePath = strings.TrimSuffix(fileTree.DocCreateSavePath, "/")
  245. fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
  246. }
  247. if 1 > fileTree.MaxOpenTabCount {
  248. fileTree.MaxOpenTabCount = 8
  249. }
  250. if 32 < fileTree.MaxOpenTabCount {
  251. fileTree.MaxOpenTabCount = 32
  252. }
  253. model.Conf.FileTree = fileTree
  254. model.Conf.Save()
  255. ret.Data = model.Conf.FileTree
  256. }
  257. func setSearch(c *gin.Context) {
  258. ret := gulu.Ret.NewResult()
  259. defer c.JSON(http.StatusOK, ret)
  260. arg, ok := util.JsonArg(c, ret)
  261. if !ok {
  262. return
  263. }
  264. param, err := gulu.JSON.MarshalJSON(arg)
  265. if nil != err {
  266. ret.Code = -1
  267. ret.Msg = err.Error()
  268. return
  269. }
  270. s := &conf.Search{}
  271. if err = gulu.JSON.UnmarshalJSON(param, s); nil != err {
  272. ret.Code = -1
  273. ret.Msg = err.Error()
  274. return
  275. }
  276. if 32 > s.Limit {
  277. s.Limit = 32
  278. }
  279. oldCaseSensitive := model.Conf.Search.CaseSensitive
  280. oldIndexAssetPath := model.Conf.Search.IndexAssetPath
  281. oldVirtualRefName := model.Conf.Search.VirtualRefName
  282. oldVirtualRefAlias := model.Conf.Search.VirtualRefAlias
  283. oldVirtualRefAnchor := model.Conf.Search.VirtualRefAnchor
  284. oldVirtualRefDoc := model.Conf.Search.VirtualRefDoc
  285. model.Conf.Search = s
  286. model.Conf.Save()
  287. sql.SetCaseSensitive(s.CaseSensitive)
  288. sql.SetIndexAssetPath(s.IndexAssetPath)
  289. if needFullReindex := s.CaseSensitive != oldCaseSensitive || s.IndexAssetPath != oldIndexAssetPath; needFullReindex {
  290. model.FullReindex()
  291. }
  292. if oldVirtualRefName != s.VirtualRefName ||
  293. oldVirtualRefAlias != s.VirtualRefAlias ||
  294. oldVirtualRefAnchor != s.VirtualRefAnchor ||
  295. oldVirtualRefDoc != s.VirtualRefDoc {
  296. model.ResetVirtualBlockRefCache()
  297. }
  298. ret.Data = s
  299. }
  300. func setKeymap(c *gin.Context) {
  301. ret := gulu.Ret.NewResult()
  302. defer c.JSON(http.StatusOK, ret)
  303. arg, ok := util.JsonArg(c, ret)
  304. if !ok {
  305. return
  306. }
  307. param, err := gulu.JSON.MarshalJSON(arg["data"])
  308. if nil != err {
  309. ret.Code = -1
  310. ret.Msg = err.Error()
  311. return
  312. }
  313. keymap := &conf.Keymap{}
  314. if err = gulu.JSON.UnmarshalJSON(param, keymap); nil != err {
  315. ret.Code = -1
  316. ret.Msg = err.Error()
  317. return
  318. }
  319. model.Conf.Keymap = keymap
  320. model.Conf.Save()
  321. }
  322. func setAppearance(c *gin.Context) {
  323. ret := gulu.Ret.NewResult()
  324. defer c.JSON(http.StatusOK, ret)
  325. arg, ok := util.JsonArg(c, ret)
  326. if !ok {
  327. return
  328. }
  329. param, err := gulu.JSON.MarshalJSON(arg)
  330. if nil != err {
  331. ret.Code = -1
  332. ret.Msg = err.Error()
  333. return
  334. }
  335. appearance := &conf.Appearance{}
  336. if err = gulu.JSON.UnmarshalJSON(param, appearance); nil != err {
  337. ret.Code = -1
  338. ret.Msg = err.Error()
  339. return
  340. }
  341. model.Conf.Appearance = appearance
  342. model.Conf.Lang = appearance.Lang
  343. util.Lang = model.Conf.Lang
  344. model.Conf.Save()
  345. model.InitAppearance()
  346. ret.Data = model.Conf.Appearance
  347. }
  348. func getCloudUser(c *gin.Context) {
  349. ret := gulu.Ret.NewResult()
  350. defer c.JSON(http.StatusOK, ret)
  351. arg, ok := util.JsonArg(c, ret)
  352. if !ok {
  353. return
  354. }
  355. t := arg["token"]
  356. var token string
  357. if nil != t {
  358. token = t.(string)
  359. }
  360. if err := model.RefreshUser(token); nil != err {
  361. ret.Code = 1
  362. ret.Msg = err.Error()
  363. return
  364. }
  365. ret.Data = model.Conf.User
  366. }
  367. func logoutCloudUser(c *gin.Context) {
  368. ret := gulu.Ret.NewResult()
  369. defer c.JSON(http.StatusOK, ret)
  370. model.LogoutUser()
  371. }
  372. func login2faCloudUser(c *gin.Context) {
  373. ret := gulu.Ret.NewResult()
  374. defer c.JSON(http.StatusOK, ret)
  375. arg, ok := util.JsonArg(c, ret)
  376. if !ok {
  377. return
  378. }
  379. token := arg["token"].(string)
  380. code := arg["code"].(string)
  381. data, err := model.Login2fa(token, code)
  382. if nil != err {
  383. ret.Code = -1
  384. ret.Msg = err.Error()
  385. return
  386. }
  387. ret.Data = data
  388. }
  389. func setEmoji(c *gin.Context) {
  390. ret := gulu.Ret.NewResult()
  391. defer c.JSON(http.StatusOK, ret)
  392. arg, ok := util.JsonArg(c, ret)
  393. if !ok {
  394. return
  395. }
  396. argEmoji := arg["emoji"].([]interface{})
  397. var emoji []string
  398. for _, ae := range argEmoji {
  399. emoji = append(emoji, ae.(string))
  400. }
  401. model.Conf.Editor.Emoji = emoji
  402. }