setting.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // SiYuan - Refactor your thinking
  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. model.Conf.AI = ai
  80. model.Conf.Save()
  81. ret.Data = ai
  82. }
  83. func setFlashcard(c *gin.Context) {
  84. ret := gulu.Ret.NewResult()
  85. defer c.JSON(http.StatusOK, ret)
  86. arg, ok := util.JsonArg(c, ret)
  87. if !ok {
  88. return
  89. }
  90. param, err := gulu.JSON.MarshalJSON(arg)
  91. if nil != err {
  92. ret.Code = -1
  93. ret.Msg = err.Error()
  94. return
  95. }
  96. flashcard := &conf.Flashcard{}
  97. if err = gulu.JSON.UnmarshalJSON(param, flashcard); nil != err {
  98. ret.Code = -1
  99. ret.Msg = err.Error()
  100. return
  101. }
  102. if 0 > flashcard.NewCardLimit {
  103. flashcard.NewCardLimit = 20
  104. }
  105. if 0 > flashcard.ReviewCardLimit {
  106. flashcard.ReviewCardLimit = 200
  107. }
  108. model.Conf.Flashcard = flashcard
  109. model.Conf.Save()
  110. ret.Data = flashcard
  111. }
  112. func setAccount(c *gin.Context) {
  113. ret := gulu.Ret.NewResult()
  114. defer c.JSON(http.StatusOK, ret)
  115. arg, ok := util.JsonArg(c, ret)
  116. if !ok {
  117. return
  118. }
  119. param, err := gulu.JSON.MarshalJSON(arg)
  120. if nil != err {
  121. ret.Code = -1
  122. ret.Msg = err.Error()
  123. return
  124. }
  125. account := &conf.Account{}
  126. if err = gulu.JSON.UnmarshalJSON(param, account); nil != err {
  127. ret.Code = -1
  128. ret.Msg = err.Error()
  129. return
  130. }
  131. model.Conf.Account = account
  132. model.Conf.Save()
  133. ret.Data = model.Conf.Account
  134. }
  135. func setEditor(c *gin.Context) {
  136. ret := gulu.Ret.NewResult()
  137. defer c.JSON(http.StatusOK, ret)
  138. arg, ok := util.JsonArg(c, ret)
  139. if !ok {
  140. return
  141. }
  142. param, err := gulu.JSON.MarshalJSON(arg)
  143. if nil != err {
  144. ret.Code = -1
  145. ret.Msg = err.Error()
  146. return
  147. }
  148. oldGenerateHistoryInterval := model.Conf.Editor.GenerateHistoryInterval
  149. editor := conf.NewEditor()
  150. if err = gulu.JSON.UnmarshalJSON(param, editor); nil != err {
  151. ret.Code = -1
  152. ret.Msg = err.Error()
  153. return
  154. }
  155. if "" == editor.PlantUMLServePath {
  156. editor.PlantUMLServePath = "https://www.plantuml.com/plantuml/svg/~1"
  157. }
  158. if "" == editor.KaTexMacros {
  159. editor.KaTexMacros = "{}"
  160. }
  161. oldVirtualBlockRef := model.Conf.Editor.VirtualBlockRef
  162. oldVirtualBlockRefInclude := model.Conf.Editor.VirtualBlockRefInclude
  163. oldVirtualBlockRefExclude := model.Conf.Editor.VirtualBlockRefExclude
  164. oldReadOnly := model.Conf.Editor.ReadOnly
  165. model.Conf.Editor = editor
  166. model.Conf.Save()
  167. if oldGenerateHistoryInterval != model.Conf.Editor.GenerateHistoryInterval {
  168. model.ChangeHistoryTick(editor.GenerateHistoryInterval)
  169. }
  170. if oldVirtualBlockRef != model.Conf.Editor.VirtualBlockRef ||
  171. oldVirtualBlockRefInclude != model.Conf.Editor.VirtualBlockRefInclude ||
  172. oldVirtualBlockRefExclude != model.Conf.Editor.VirtualBlockRefExclude {
  173. model.ResetVirtualBlockRefCache()
  174. }
  175. if oldReadOnly != model.Conf.Editor.ReadOnly {
  176. util.BroadcastByType("protyle", "readonly", 0, "", model.Conf.Editor.ReadOnly)
  177. util.BroadcastByType("main", "readonly", 0, "", model.Conf.Editor.ReadOnly)
  178. }
  179. ret.Data = model.Conf.Editor
  180. }
  181. func setExport(c *gin.Context) {
  182. ret := gulu.Ret.NewResult()
  183. defer c.JSON(http.StatusOK, ret)
  184. arg, ok := util.JsonArg(c, ret)
  185. if !ok {
  186. return
  187. }
  188. param, err := gulu.JSON.MarshalJSON(arg)
  189. if nil != err {
  190. ret.Code = -1
  191. ret.Msg = err.Error()
  192. return
  193. }
  194. export := &conf.Export{}
  195. if err = gulu.JSON.UnmarshalJSON(param, export); nil != err {
  196. ret.Code = -1
  197. ret.Msg = err.Error()
  198. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  199. return
  200. }
  201. if "" != export.PandocBin {
  202. if !util.IsValidPandocBin(export.PandocBin) {
  203. util.PushErrMsg(fmt.Sprintf(model.Conf.Language(117), export.PandocBin), 5000)
  204. export.PandocBin = util.PandocBinPath
  205. } else {
  206. util.PandocBinPath = export.PandocBin
  207. }
  208. }
  209. model.Conf.Export = export
  210. model.Conf.Save()
  211. ret.Data = model.Conf.Export
  212. }
  213. func setFiletree(c *gin.Context) {
  214. ret := gulu.Ret.NewResult()
  215. defer c.JSON(http.StatusOK, ret)
  216. arg, ok := util.JsonArg(c, ret)
  217. if !ok {
  218. return
  219. }
  220. param, err := gulu.JSON.MarshalJSON(arg)
  221. if nil != err {
  222. ret.Code = -1
  223. ret.Msg = err.Error()
  224. return
  225. }
  226. fileTree := conf.NewFileTree()
  227. if err = gulu.JSON.UnmarshalJSON(param, fileTree); nil != err {
  228. ret.Code = -1
  229. ret.Msg = err.Error()
  230. return
  231. }
  232. fileTree.RefCreateSavePath = strings.TrimSpace(fileTree.RefCreateSavePath)
  233. if "" != fileTree.RefCreateSavePath {
  234. if !strings.HasSuffix(fileTree.RefCreateSavePath, "/") {
  235. fileTree.RefCreateSavePath += "/"
  236. }
  237. }
  238. fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
  239. if "../" == fileTree.DocCreateSavePath {
  240. fileTree.DocCreateSavePath = "../Untitled"
  241. }
  242. for strings.HasSuffix(fileTree.DocCreateSavePath, "/") {
  243. fileTree.DocCreateSavePath = strings.TrimSuffix(fileTree.DocCreateSavePath, "/")
  244. fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
  245. }
  246. if 1 > fileTree.MaxOpenTabCount {
  247. fileTree.MaxOpenTabCount = 8
  248. }
  249. if 32 < fileTree.MaxOpenTabCount {
  250. fileTree.MaxOpenTabCount = 32
  251. }
  252. model.Conf.FileTree = fileTree
  253. model.Conf.Save()
  254. util.UseSingleLineSave = model.Conf.FileTree.UseSingleLineSave
  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. model.RefreshUser(token)
  361. ret.Data = model.Conf.User
  362. }
  363. func logoutCloudUser(c *gin.Context) {
  364. ret := gulu.Ret.NewResult()
  365. defer c.JSON(http.StatusOK, ret)
  366. model.LogoutUser()
  367. }
  368. func login2faCloudUser(c *gin.Context) {
  369. ret := gulu.Ret.NewResult()
  370. defer c.JSON(http.StatusOK, ret)
  371. arg, ok := util.JsonArg(c, ret)
  372. if !ok {
  373. return
  374. }
  375. token := arg["token"].(string)
  376. code := arg["code"].(string)
  377. data, err := model.Login2fa(token, code)
  378. if nil != err {
  379. ret.Code = -1
  380. ret.Msg = err.Error()
  381. return
  382. }
  383. ret.Data = data
  384. }
  385. func setEmoji(c *gin.Context) {
  386. ret := gulu.Ret.NewResult()
  387. defer c.JSON(http.StatusOK, ret)
  388. arg, ok := util.JsonArg(c, ret)
  389. if !ok {
  390. return
  391. }
  392. argEmoji := arg["emoji"].([]interface{})
  393. var emoji []string
  394. for _, ae := range argEmoji {
  395. emoji = append(emoji, ae.(string))
  396. }
  397. model.Conf.Editor.Emoji = emoji
  398. }