setting.go 14 KB

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