setting.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 setEditorReadOnly(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. readOnly := arg["readonly"].(bool)
  36. oldReadOnly := model.Conf.Editor.ReadOnly
  37. model.Conf.Editor.ReadOnly = readOnly
  38. model.Conf.Save()
  39. if oldReadOnly != model.Conf.Editor.ReadOnly {
  40. util.BroadcastByType("protyle", "readonly", 0, "", model.Conf.Editor.ReadOnly)
  41. util.BroadcastByType("main", "readonly", 0, "", model.Conf.Editor.ReadOnly)
  42. }
  43. }
  44. func setConfSnippet(c *gin.Context) {
  45. ret := gulu.Ret.NewResult()
  46. defer c.JSON(http.StatusOK, ret)
  47. arg, ok := util.JsonArg(c, ret)
  48. if !ok {
  49. return
  50. }
  51. param, err := gulu.JSON.MarshalJSON(arg)
  52. if nil != err {
  53. ret.Code = -1
  54. ret.Msg = err.Error()
  55. return
  56. }
  57. snippet := &conf.Snpt{}
  58. if err = gulu.JSON.UnmarshalJSON(param, snippet); nil != err {
  59. ret.Code = -1
  60. ret.Msg = err.Error()
  61. return
  62. }
  63. model.Conf.Snippet = snippet
  64. model.Conf.Save()
  65. ret.Data = snippet
  66. }
  67. func addVirtualBlockRefExclude(c *gin.Context) {
  68. // Add internal kernel API `/api/setting/addVirtualBlockRefExclude` https://github.com/siyuan-note/siyuan/issues/9909
  69. ret := gulu.Ret.NewResult()
  70. defer c.JSON(http.StatusOK, ret)
  71. arg, ok := util.JsonArg(c, ret)
  72. if !ok {
  73. return
  74. }
  75. keywordsArg := arg["keywords"]
  76. var keywords []string
  77. for _, k := range keywordsArg.([]interface{}) {
  78. keywords = append(keywords, k.(string))
  79. }
  80. model.AddVirtualBlockRefExclude(keywords)
  81. util.BroadcastByType("main", "setConf", 0, "", model.Conf)
  82. }
  83. func addVirtualBlockRefInclude(c *gin.Context) {
  84. // Add internal kernel API `/api/setting/addVirtualBlockRefInclude` https://github.com/siyuan-note/siyuan/issues/9909
  85. ret := gulu.Ret.NewResult()
  86. defer c.JSON(http.StatusOK, ret)
  87. arg, ok := util.JsonArg(c, ret)
  88. if !ok {
  89. return
  90. }
  91. keywordsArg := arg["keywords"]
  92. var keywords []string
  93. for _, k := range keywordsArg.([]interface{}) {
  94. keywords = append(keywords, k.(string))
  95. }
  96. model.AddVirtualBlockRefInclude(keywords)
  97. util.BroadcastByType("main", "setConf", 0, "", model.Conf)
  98. }
  99. func refreshVirtualBlockRef(c *gin.Context) {
  100. // Add internal kernel API `/api/setting/refreshVirtualBlockRef` https://github.com/siyuan-note/siyuan/issues/9829
  101. ret := gulu.Ret.NewResult()
  102. defer c.JSON(http.StatusOK, ret)
  103. model.ResetVirtualBlockRefCache()
  104. util.BroadcastByType("main", "setConf", 0, "", model.Conf)
  105. }
  106. func setBazaar(c *gin.Context) {
  107. ret := gulu.Ret.NewResult()
  108. defer c.JSON(http.StatusOK, ret)
  109. arg, ok := util.JsonArg(c, ret)
  110. if !ok {
  111. return
  112. }
  113. param, err := gulu.JSON.MarshalJSON(arg)
  114. if nil != err {
  115. ret.Code = -1
  116. ret.Msg = err.Error()
  117. return
  118. }
  119. bazaar := &conf.Bazaar{}
  120. if err = gulu.JSON.UnmarshalJSON(param, bazaar); nil != err {
  121. ret.Code = -1
  122. ret.Msg = err.Error()
  123. return
  124. }
  125. model.Conf.Bazaar = bazaar
  126. model.Conf.Save()
  127. ret.Data = bazaar
  128. }
  129. func setAI(c *gin.Context) {
  130. ret := gulu.Ret.NewResult()
  131. defer c.JSON(http.StatusOK, ret)
  132. arg, ok := util.JsonArg(c, ret)
  133. if !ok {
  134. return
  135. }
  136. param, err := gulu.JSON.MarshalJSON(arg)
  137. if nil != err {
  138. ret.Code = -1
  139. ret.Msg = err.Error()
  140. return
  141. }
  142. ai := &conf.AI{}
  143. if err = gulu.JSON.UnmarshalJSON(param, ai); nil != err {
  144. ret.Code = -1
  145. ret.Msg = err.Error()
  146. return
  147. }
  148. if 5 > ai.OpenAI.APITimeout {
  149. ai.OpenAI.APITimeout = 5
  150. }
  151. if 600 < ai.OpenAI.APITimeout {
  152. ai.OpenAI.APITimeout = 600
  153. }
  154. if 0 > ai.OpenAI.APIMaxTokens {
  155. ai.OpenAI.APIMaxTokens = 0
  156. }
  157. model.Conf.AI = ai
  158. model.Conf.Save()
  159. ret.Data = ai
  160. }
  161. func setFlashcard(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. flashcard := &conf.Flashcard{}
  175. if err = gulu.JSON.UnmarshalJSON(param, flashcard); nil != err {
  176. ret.Code = -1
  177. ret.Msg = err.Error()
  178. return
  179. }
  180. if 0 > flashcard.NewCardLimit {
  181. flashcard.NewCardLimit = 20
  182. }
  183. if 0 > flashcard.ReviewCardLimit {
  184. flashcard.ReviewCardLimit = 200
  185. }
  186. model.Conf.Flashcard = flashcard
  187. model.Conf.Save()
  188. ret.Data = flashcard
  189. }
  190. func setAccount(c *gin.Context) {
  191. ret := gulu.Ret.NewResult()
  192. defer c.JSON(http.StatusOK, ret)
  193. arg, ok := util.JsonArg(c, ret)
  194. if !ok {
  195. return
  196. }
  197. param, err := gulu.JSON.MarshalJSON(arg)
  198. if nil != err {
  199. ret.Code = -1
  200. ret.Msg = err.Error()
  201. return
  202. }
  203. account := &conf.Account{}
  204. if err = gulu.JSON.UnmarshalJSON(param, account); nil != err {
  205. ret.Code = -1
  206. ret.Msg = err.Error()
  207. return
  208. }
  209. model.Conf.Account = account
  210. model.Conf.Save()
  211. ret.Data = model.Conf.Account
  212. }
  213. func setEditor(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. oldGenerateHistoryInterval := model.Conf.Editor.GenerateHistoryInterval
  227. editor := conf.NewEditor()
  228. if err = gulu.JSON.UnmarshalJSON(param, editor); nil != err {
  229. ret.Code = -1
  230. ret.Msg = err.Error()
  231. return
  232. }
  233. if "" == editor.PlantUMLServePath {
  234. editor.PlantUMLServePath = "https://www.plantuml.com/plantuml/svg/~1"
  235. }
  236. if "" == editor.KaTexMacros {
  237. editor.KaTexMacros = "{}"
  238. }
  239. oldVirtualBlockRef := model.Conf.Editor.VirtualBlockRef
  240. oldVirtualBlockRefInclude := model.Conf.Editor.VirtualBlockRefInclude
  241. oldVirtualBlockRefExclude := model.Conf.Editor.VirtualBlockRefExclude
  242. oldReadOnly := model.Conf.Editor.ReadOnly
  243. model.Conf.Editor = editor
  244. model.Conf.Save()
  245. if oldGenerateHistoryInterval != model.Conf.Editor.GenerateHistoryInterval {
  246. model.ChangeHistoryTick(editor.GenerateHistoryInterval)
  247. }
  248. if oldVirtualBlockRef != model.Conf.Editor.VirtualBlockRef ||
  249. oldVirtualBlockRefInclude != model.Conf.Editor.VirtualBlockRefInclude ||
  250. oldVirtualBlockRefExclude != model.Conf.Editor.VirtualBlockRefExclude {
  251. model.ResetVirtualBlockRefCache()
  252. }
  253. if oldReadOnly != model.Conf.Editor.ReadOnly {
  254. util.BroadcastByType("protyle", "readonly", 0, "", model.Conf.Editor.ReadOnly)
  255. util.BroadcastByType("main", "readonly", 0, "", model.Conf.Editor.ReadOnly)
  256. }
  257. ret.Data = model.Conf.Editor
  258. }
  259. func setExport(c *gin.Context) {
  260. ret := gulu.Ret.NewResult()
  261. defer c.JSON(http.StatusOK, ret)
  262. arg, ok := util.JsonArg(c, ret)
  263. if !ok {
  264. return
  265. }
  266. param, err := gulu.JSON.MarshalJSON(arg)
  267. if nil != err {
  268. ret.Code = -1
  269. ret.Msg = err.Error()
  270. return
  271. }
  272. export := &conf.Export{}
  273. if err = gulu.JSON.UnmarshalJSON(param, export); nil != err {
  274. ret.Code = -1
  275. ret.Msg = err.Error()
  276. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  277. return
  278. }
  279. if "" != export.PandocBin {
  280. if !util.IsValidPandocBin(export.PandocBin) {
  281. util.PushErrMsg(fmt.Sprintf(model.Conf.Language(117), export.PandocBin), 5000)
  282. export.PandocBin = util.PandocBinPath
  283. } else {
  284. util.PandocBinPath = export.PandocBin
  285. }
  286. }
  287. model.Conf.Export = export
  288. model.Conf.Save()
  289. ret.Data = model.Conf.Export
  290. }
  291. func setFiletree(c *gin.Context) {
  292. ret := gulu.Ret.NewResult()
  293. defer c.JSON(http.StatusOK, ret)
  294. arg, ok := util.JsonArg(c, ret)
  295. if !ok {
  296. return
  297. }
  298. param, err := gulu.JSON.MarshalJSON(arg)
  299. if nil != err {
  300. ret.Code = -1
  301. ret.Msg = err.Error()
  302. return
  303. }
  304. fileTree := conf.NewFileTree()
  305. if err = gulu.JSON.UnmarshalJSON(param, fileTree); nil != err {
  306. ret.Code = -1
  307. ret.Msg = err.Error()
  308. return
  309. }
  310. fileTree.RefCreateSavePath = strings.TrimSpace(fileTree.RefCreateSavePath)
  311. if "" != fileTree.RefCreateSavePath {
  312. if !strings.HasSuffix(fileTree.RefCreateSavePath, "/") {
  313. fileTree.RefCreateSavePath += "/"
  314. }
  315. }
  316. fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
  317. if "../" == fileTree.DocCreateSavePath {
  318. fileTree.DocCreateSavePath = "../Untitled"
  319. }
  320. if "/" == fileTree.DocCreateSavePath {
  321. fileTree.DocCreateSavePath = "/Untitled"
  322. }
  323. if 1 > fileTree.MaxOpenTabCount {
  324. fileTree.MaxOpenTabCount = 8
  325. }
  326. if 32 < fileTree.MaxOpenTabCount {
  327. fileTree.MaxOpenTabCount = 32
  328. }
  329. model.Conf.FileTree = fileTree
  330. model.Conf.Save()
  331. util.UseSingleLineSave = model.Conf.FileTree.UseSingleLineSave
  332. ret.Data = model.Conf.FileTree
  333. }
  334. func setSearch(c *gin.Context) {
  335. ret := gulu.Ret.NewResult()
  336. defer c.JSON(http.StatusOK, ret)
  337. arg, ok := util.JsonArg(c, ret)
  338. if !ok {
  339. return
  340. }
  341. param, err := gulu.JSON.MarshalJSON(arg)
  342. if nil != err {
  343. ret.Code = -1
  344. ret.Msg = err.Error()
  345. return
  346. }
  347. s := &conf.Search{}
  348. if err = gulu.JSON.UnmarshalJSON(param, s); nil != err {
  349. ret.Code = -1
  350. ret.Msg = err.Error()
  351. return
  352. }
  353. if 32 > s.Limit {
  354. s.Limit = 32
  355. }
  356. oldCaseSensitive := model.Conf.Search.CaseSensitive
  357. oldIndexAssetPath := model.Conf.Search.IndexAssetPath
  358. oldVirtualRefName := model.Conf.Search.VirtualRefName
  359. oldVirtualRefAlias := model.Conf.Search.VirtualRefAlias
  360. oldVirtualRefAnchor := model.Conf.Search.VirtualRefAnchor
  361. oldVirtualRefDoc := model.Conf.Search.VirtualRefDoc
  362. model.Conf.Search = s
  363. model.Conf.Save()
  364. sql.SetCaseSensitive(s.CaseSensitive)
  365. sql.SetIndexAssetPath(s.IndexAssetPath)
  366. if needFullReindex := s.CaseSensitive != oldCaseSensitive || s.IndexAssetPath != oldIndexAssetPath; needFullReindex {
  367. model.FullReindex()
  368. }
  369. if oldVirtualRefName != s.VirtualRefName ||
  370. oldVirtualRefAlias != s.VirtualRefAlias ||
  371. oldVirtualRefAnchor != s.VirtualRefAnchor ||
  372. oldVirtualRefDoc != s.VirtualRefDoc {
  373. model.ResetVirtualBlockRefCache()
  374. }
  375. ret.Data = s
  376. }
  377. func setKeymap(c *gin.Context) {
  378. ret := gulu.Ret.NewResult()
  379. defer c.JSON(http.StatusOK, ret)
  380. arg, ok := util.JsonArg(c, ret)
  381. if !ok {
  382. return
  383. }
  384. param, err := gulu.JSON.MarshalJSON(arg["data"])
  385. if nil != err {
  386. ret.Code = -1
  387. ret.Msg = err.Error()
  388. return
  389. }
  390. keymap := &conf.Keymap{}
  391. if err = gulu.JSON.UnmarshalJSON(param, keymap); nil != err {
  392. ret.Code = -1
  393. ret.Msg = err.Error()
  394. return
  395. }
  396. model.Conf.Keymap = keymap
  397. model.Conf.Save()
  398. }
  399. func setAppearance(c *gin.Context) {
  400. ret := gulu.Ret.NewResult()
  401. defer c.JSON(http.StatusOK, ret)
  402. arg, ok := util.JsonArg(c, ret)
  403. if !ok {
  404. return
  405. }
  406. param, err := gulu.JSON.MarshalJSON(arg)
  407. if nil != err {
  408. ret.Code = -1
  409. ret.Msg = err.Error()
  410. return
  411. }
  412. appearance := &conf.Appearance{}
  413. if err = gulu.JSON.UnmarshalJSON(param, appearance); nil != err {
  414. ret.Code = -1
  415. ret.Msg = err.Error()
  416. return
  417. }
  418. model.Conf.Appearance = appearance
  419. model.Conf.Lang = appearance.Lang
  420. util.Lang = model.Conf.Lang
  421. model.Conf.Save()
  422. model.InitAppearance()
  423. ret.Data = model.Conf.Appearance
  424. }
  425. func getCloudUser(c *gin.Context) {
  426. ret := gulu.Ret.NewResult()
  427. defer c.JSON(http.StatusOK, ret)
  428. arg, ok := util.JsonArg(c, ret)
  429. if !ok {
  430. return
  431. }
  432. t := arg["token"]
  433. var token string
  434. if nil != t {
  435. token = t.(string)
  436. }
  437. model.RefreshUser(token)
  438. ret.Data = model.Conf.GetUser()
  439. }
  440. func logoutCloudUser(c *gin.Context) {
  441. ret := gulu.Ret.NewResult()
  442. defer c.JSON(http.StatusOK, ret)
  443. model.LogoutUser()
  444. }
  445. func login2faCloudUser(c *gin.Context) {
  446. ret := gulu.Ret.NewResult()
  447. defer c.JSON(http.StatusOK, ret)
  448. arg, ok := util.JsonArg(c, ret)
  449. if !ok {
  450. return
  451. }
  452. token := arg["token"].(string)
  453. code := arg["code"].(string)
  454. data, err := model.Login2fa(token, code)
  455. if nil != err {
  456. ret.Code = -1
  457. ret.Msg = err.Error()
  458. return
  459. }
  460. ret.Data = data
  461. }
  462. func setEmoji(c *gin.Context) {
  463. ret := gulu.Ret.NewResult()
  464. defer c.JSON(http.StatusOK, ret)
  465. arg, ok := util.JsonArg(c, ret)
  466. if !ok {
  467. return
  468. }
  469. argEmoji := arg["emoji"].([]interface{})
  470. var emoji []string
  471. for _, ae := range argEmoji {
  472. emoji = append(emoji, ae.(string))
  473. }
  474. model.Conf.Editor.Emoji = emoji
  475. }