setting.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 1 > fileTree.MaxOpenTabCount {
  321. fileTree.MaxOpenTabCount = 8
  322. }
  323. if 32 < fileTree.MaxOpenTabCount {
  324. fileTree.MaxOpenTabCount = 32
  325. }
  326. model.Conf.FileTree = fileTree
  327. model.Conf.Save()
  328. util.UseSingleLineSave = model.Conf.FileTree.UseSingleLineSave
  329. ret.Data = model.Conf.FileTree
  330. }
  331. func setSearch(c *gin.Context) {
  332. ret := gulu.Ret.NewResult()
  333. defer c.JSON(http.StatusOK, ret)
  334. arg, ok := util.JsonArg(c, ret)
  335. if !ok {
  336. return
  337. }
  338. param, err := gulu.JSON.MarshalJSON(arg)
  339. if nil != err {
  340. ret.Code = -1
  341. ret.Msg = err.Error()
  342. return
  343. }
  344. s := &conf.Search{}
  345. if err = gulu.JSON.UnmarshalJSON(param, s); nil != err {
  346. ret.Code = -1
  347. ret.Msg = err.Error()
  348. return
  349. }
  350. if 32 > s.Limit {
  351. s.Limit = 32
  352. }
  353. oldCaseSensitive := model.Conf.Search.CaseSensitive
  354. oldIndexAssetPath := model.Conf.Search.IndexAssetPath
  355. oldVirtualRefName := model.Conf.Search.VirtualRefName
  356. oldVirtualRefAlias := model.Conf.Search.VirtualRefAlias
  357. oldVirtualRefAnchor := model.Conf.Search.VirtualRefAnchor
  358. oldVirtualRefDoc := model.Conf.Search.VirtualRefDoc
  359. model.Conf.Search = s
  360. model.Conf.Save()
  361. sql.SetCaseSensitive(s.CaseSensitive)
  362. sql.SetIndexAssetPath(s.IndexAssetPath)
  363. if needFullReindex := s.CaseSensitive != oldCaseSensitive || s.IndexAssetPath != oldIndexAssetPath; needFullReindex {
  364. model.FullReindex()
  365. }
  366. if oldVirtualRefName != s.VirtualRefName ||
  367. oldVirtualRefAlias != s.VirtualRefAlias ||
  368. oldVirtualRefAnchor != s.VirtualRefAnchor ||
  369. oldVirtualRefDoc != s.VirtualRefDoc {
  370. model.ResetVirtualBlockRefCache()
  371. }
  372. ret.Data = s
  373. }
  374. func setKeymap(c *gin.Context) {
  375. ret := gulu.Ret.NewResult()
  376. defer c.JSON(http.StatusOK, ret)
  377. arg, ok := util.JsonArg(c, ret)
  378. if !ok {
  379. return
  380. }
  381. param, err := gulu.JSON.MarshalJSON(arg["data"])
  382. if nil != err {
  383. ret.Code = -1
  384. ret.Msg = err.Error()
  385. return
  386. }
  387. keymap := &conf.Keymap{}
  388. if err = gulu.JSON.UnmarshalJSON(param, keymap); nil != err {
  389. ret.Code = -1
  390. ret.Msg = err.Error()
  391. return
  392. }
  393. model.Conf.Keymap = keymap
  394. model.Conf.Save()
  395. }
  396. func setAppearance(c *gin.Context) {
  397. ret := gulu.Ret.NewResult()
  398. defer c.JSON(http.StatusOK, ret)
  399. arg, ok := util.JsonArg(c, ret)
  400. if !ok {
  401. return
  402. }
  403. param, err := gulu.JSON.MarshalJSON(arg)
  404. if nil != err {
  405. ret.Code = -1
  406. ret.Msg = err.Error()
  407. return
  408. }
  409. appearance := &conf.Appearance{}
  410. if err = gulu.JSON.UnmarshalJSON(param, appearance); nil != err {
  411. ret.Code = -1
  412. ret.Msg = err.Error()
  413. return
  414. }
  415. model.Conf.Appearance = appearance
  416. model.Conf.Lang = appearance.Lang
  417. util.Lang = model.Conf.Lang
  418. model.Conf.Save()
  419. model.InitAppearance()
  420. ret.Data = model.Conf.Appearance
  421. }
  422. func getCloudUser(c *gin.Context) {
  423. ret := gulu.Ret.NewResult()
  424. defer c.JSON(http.StatusOK, ret)
  425. arg, ok := util.JsonArg(c, ret)
  426. if !ok {
  427. return
  428. }
  429. t := arg["token"]
  430. var token string
  431. if nil != t {
  432. token = t.(string)
  433. }
  434. model.RefreshUser(token)
  435. ret.Data = model.Conf.GetUser()
  436. }
  437. func logoutCloudUser(c *gin.Context) {
  438. ret := gulu.Ret.NewResult()
  439. defer c.JSON(http.StatusOK, ret)
  440. model.LogoutUser()
  441. }
  442. func login2faCloudUser(c *gin.Context) {
  443. ret := gulu.Ret.NewResult()
  444. defer c.JSON(http.StatusOK, ret)
  445. arg, ok := util.JsonArg(c, ret)
  446. if !ok {
  447. return
  448. }
  449. token := arg["token"].(string)
  450. code := arg["code"].(string)
  451. data, err := model.Login2fa(token, code)
  452. if nil != err {
  453. ret.Code = -1
  454. ret.Msg = err.Error()
  455. return
  456. }
  457. ret.Data = data
  458. }
  459. func setEmoji(c *gin.Context) {
  460. ret := gulu.Ret.NewResult()
  461. defer c.JSON(http.StatusOK, ret)
  462. arg, ok := util.JsonArg(c, ret)
  463. if !ok {
  464. return
  465. }
  466. argEmoji := arg["emoji"].([]interface{})
  467. var emoji []string
  468. for _, ae := range argEmoji {
  469. emoji = append(emoji, ae.(string))
  470. }
  471. model.Conf.Editor.Emoji = emoji
  472. }