setting.go 12 KB

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