setting.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. 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. } else {
  209. util.PandocBinPath = export.PandocBin
  210. }
  211. }
  212. model.Conf.Export = export
  213. model.Conf.Save()
  214. ret.Data = model.Conf.Export
  215. }
  216. func setFiletree(c *gin.Context) {
  217. ret := gulu.Ret.NewResult()
  218. defer c.JSON(http.StatusOK, ret)
  219. arg, ok := util.JsonArg(c, ret)
  220. if !ok {
  221. return
  222. }
  223. param, err := gulu.JSON.MarshalJSON(arg)
  224. if nil != err {
  225. ret.Code = -1
  226. ret.Msg = err.Error()
  227. return
  228. }
  229. fileTree := conf.NewFileTree()
  230. if err = gulu.JSON.UnmarshalJSON(param, fileTree); nil != err {
  231. ret.Code = -1
  232. ret.Msg = err.Error()
  233. return
  234. }
  235. fileTree.RefCreateSavePath = strings.TrimSpace(fileTree.RefCreateSavePath)
  236. if "" != fileTree.RefCreateSavePath {
  237. if !strings.HasSuffix(fileTree.RefCreateSavePath, "/") {
  238. fileTree.RefCreateSavePath += "/"
  239. }
  240. }
  241. fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
  242. if "../" == fileTree.DocCreateSavePath {
  243. fileTree.DocCreateSavePath = "../Untitled"
  244. }
  245. for strings.HasSuffix(fileTree.DocCreateSavePath, "/") {
  246. fileTree.DocCreateSavePath = strings.TrimSuffix(fileTree.DocCreateSavePath, "/")
  247. fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
  248. }
  249. if 1 > fileTree.MaxOpenTabCount {
  250. fileTree.MaxOpenTabCount = 8
  251. }
  252. if 32 < fileTree.MaxOpenTabCount {
  253. fileTree.MaxOpenTabCount = 32
  254. }
  255. model.Conf.FileTree = fileTree
  256. model.Conf.Save()
  257. util.UseSingleLineSave = model.Conf.FileTree.UseSingleLineSave
  258. ret.Data = model.Conf.FileTree
  259. }
  260. func setSearch(c *gin.Context) {
  261. ret := gulu.Ret.NewResult()
  262. defer c.JSON(http.StatusOK, ret)
  263. arg, ok := util.JsonArg(c, ret)
  264. if !ok {
  265. return
  266. }
  267. param, err := gulu.JSON.MarshalJSON(arg)
  268. if nil != err {
  269. ret.Code = -1
  270. ret.Msg = err.Error()
  271. return
  272. }
  273. s := &conf.Search{}
  274. if err = gulu.JSON.UnmarshalJSON(param, s); nil != err {
  275. ret.Code = -1
  276. ret.Msg = err.Error()
  277. return
  278. }
  279. if 32 > s.Limit {
  280. s.Limit = 32
  281. }
  282. oldCaseSensitive := model.Conf.Search.CaseSensitive
  283. oldIndexAssetPath := model.Conf.Search.IndexAssetPath
  284. oldVirtualRefName := model.Conf.Search.VirtualRefName
  285. oldVirtualRefAlias := model.Conf.Search.VirtualRefAlias
  286. oldVirtualRefAnchor := model.Conf.Search.VirtualRefAnchor
  287. oldVirtualRefDoc := model.Conf.Search.VirtualRefDoc
  288. model.Conf.Search = s
  289. model.Conf.Save()
  290. sql.SetCaseSensitive(s.CaseSensitive)
  291. sql.SetIndexAssetPath(s.IndexAssetPath)
  292. if needFullReindex := s.CaseSensitive != oldCaseSensitive || s.IndexAssetPath != oldIndexAssetPath; needFullReindex {
  293. model.FullReindex()
  294. }
  295. if oldVirtualRefName != s.VirtualRefName ||
  296. oldVirtualRefAlias != s.VirtualRefAlias ||
  297. oldVirtualRefAnchor != s.VirtualRefAnchor ||
  298. oldVirtualRefDoc != s.VirtualRefDoc {
  299. model.ResetVirtualBlockRefCache()
  300. }
  301. ret.Data = s
  302. }
  303. func setKeymap(c *gin.Context) {
  304. ret := gulu.Ret.NewResult()
  305. defer c.JSON(http.StatusOK, ret)
  306. arg, ok := util.JsonArg(c, ret)
  307. if !ok {
  308. return
  309. }
  310. param, err := gulu.JSON.MarshalJSON(arg["data"])
  311. if nil != err {
  312. ret.Code = -1
  313. ret.Msg = err.Error()
  314. return
  315. }
  316. keymap := &conf.Keymap{}
  317. if err = gulu.JSON.UnmarshalJSON(param, keymap); nil != err {
  318. ret.Code = -1
  319. ret.Msg = err.Error()
  320. return
  321. }
  322. model.Conf.Keymap = keymap
  323. model.Conf.Save()
  324. }
  325. func setAppearance(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. param, err := gulu.JSON.MarshalJSON(arg)
  333. if nil != err {
  334. ret.Code = -1
  335. ret.Msg = err.Error()
  336. return
  337. }
  338. appearance := &conf.Appearance{}
  339. if err = gulu.JSON.UnmarshalJSON(param, appearance); nil != err {
  340. ret.Code = -1
  341. ret.Msg = err.Error()
  342. return
  343. }
  344. model.Conf.Appearance = appearance
  345. model.Conf.Lang = appearance.Lang
  346. util.Lang = model.Conf.Lang
  347. model.Conf.Save()
  348. model.InitAppearance()
  349. ret.Data = model.Conf.Appearance
  350. }
  351. func getCloudUser(c *gin.Context) {
  352. ret := gulu.Ret.NewResult()
  353. defer c.JSON(http.StatusOK, ret)
  354. arg, ok := util.JsonArg(c, ret)
  355. if !ok {
  356. return
  357. }
  358. t := arg["token"]
  359. var token string
  360. if nil != t {
  361. token = t.(string)
  362. }
  363. if err := model.RefreshUser(token); nil != err {
  364. ret.Code = 1
  365. ret.Msg = err.Error()
  366. return
  367. }
  368. ret.Data = model.Conf.User
  369. }
  370. func logoutCloudUser(c *gin.Context) {
  371. ret := gulu.Ret.NewResult()
  372. defer c.JSON(http.StatusOK, ret)
  373. model.LogoutUser()
  374. }
  375. func login2faCloudUser(c *gin.Context) {
  376. ret := gulu.Ret.NewResult()
  377. defer c.JSON(http.StatusOK, ret)
  378. arg, ok := util.JsonArg(c, ret)
  379. if !ok {
  380. return
  381. }
  382. token := arg["token"].(string)
  383. code := arg["code"].(string)
  384. data, err := model.Login2fa(token, code)
  385. if nil != err {
  386. ret.Code = -1
  387. ret.Msg = err.Error()
  388. return
  389. }
  390. ret.Data = data
  391. }
  392. func setEmoji(c *gin.Context) {
  393. ret := gulu.Ret.NewResult()
  394. defer c.JSON(http.StatusOK, ret)
  395. arg, ok := util.JsonArg(c, ret)
  396. if !ok {
  397. return
  398. }
  399. argEmoji := arg["emoji"].([]interface{})
  400. var emoji []string
  401. for _, ae := range argEmoji {
  402. emoji = append(emoji, ae.(string))
  403. }
  404. model.Conf.Editor.Emoji = emoji
  405. }