setting.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // SiYuan - Build Your Eternal Digital Garden
  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 setAI(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. ai := &conf.AI{}
  42. if err = gulu.JSON.UnmarshalJSON(param, ai); nil != err {
  43. ret.Code = -1
  44. ret.Msg = err.Error()
  45. return
  46. }
  47. model.Conf.AI = ai
  48. model.Conf.Save()
  49. ret.Data = ai
  50. }
  51. func setFlashcard(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. flashcard := &conf.Flashcard{}
  65. if err = gulu.JSON.UnmarshalJSON(param, flashcard); nil != err {
  66. ret.Code = -1
  67. ret.Msg = err.Error()
  68. return
  69. }
  70. if 1 > flashcard.DailyNewCardLimit {
  71. flashcard.DailyNewCardLimit = 1
  72. }
  73. if 1 > flashcard.DailyReviewCardLimit {
  74. flashcard.DailyReviewCardLimit = 1
  75. }
  76. model.Conf.Flashcard = flashcard
  77. model.Conf.Save()
  78. ret.Data = flashcard
  79. }
  80. func setAccount(c *gin.Context) {
  81. ret := gulu.Ret.NewResult()
  82. defer c.JSON(http.StatusOK, ret)
  83. arg, ok := util.JsonArg(c, ret)
  84. if !ok {
  85. return
  86. }
  87. param, err := gulu.JSON.MarshalJSON(arg)
  88. if nil != err {
  89. ret.Code = -1
  90. ret.Msg = err.Error()
  91. return
  92. }
  93. account := &conf.Account{}
  94. if err = gulu.JSON.UnmarshalJSON(param, account); nil != err {
  95. ret.Code = -1
  96. ret.Msg = err.Error()
  97. return
  98. }
  99. model.Conf.Account = account
  100. model.Conf.Save()
  101. ret.Data = model.Conf.Account
  102. }
  103. func setEditor(c *gin.Context) {
  104. ret := gulu.Ret.NewResult()
  105. defer c.JSON(http.StatusOK, ret)
  106. arg, ok := util.JsonArg(c, ret)
  107. if !ok {
  108. return
  109. }
  110. param, err := gulu.JSON.MarshalJSON(arg)
  111. if nil != err {
  112. ret.Code = -1
  113. ret.Msg = err.Error()
  114. return
  115. }
  116. oldGenerateHistoryInterval := model.Conf.Editor.GenerateHistoryInterval
  117. editor := conf.NewEditor()
  118. if err = gulu.JSON.UnmarshalJSON(param, editor); nil != err {
  119. ret.Code = -1
  120. ret.Msg = err.Error()
  121. return
  122. }
  123. if "" == editor.PlantUMLServePath {
  124. editor.PlantUMLServePath = "https://www.plantuml.com/plantuml/svg/~1"
  125. }
  126. if "" == editor.KaTexMacros {
  127. editor.KaTexMacros = "{}"
  128. }
  129. oldVirtualBlockRef := model.Conf.Editor.VirtualBlockRef
  130. oldVirtualBlockRefInclude := model.Conf.Editor.VirtualBlockRefInclude
  131. oldVirtualBlockRefExclude := model.Conf.Editor.VirtualBlockRefExclude
  132. oldReadOnly := model.Conf.Editor.ReadOnly
  133. model.Conf.Editor = editor
  134. model.Conf.Save()
  135. if oldGenerateHistoryInterval != model.Conf.Editor.GenerateHistoryInterval {
  136. model.ChangeHistoryTick(editor.GenerateHistoryInterval)
  137. }
  138. if oldVirtualBlockRef != model.Conf.Editor.VirtualBlockRef ||
  139. oldVirtualBlockRefInclude != model.Conf.Editor.VirtualBlockRefInclude ||
  140. oldVirtualBlockRefExclude != model.Conf.Editor.VirtualBlockRefExclude {
  141. model.ResetVirtualBlockRefCache()
  142. }
  143. if oldReadOnly != model.Conf.Editor.ReadOnly {
  144. util.BroadcastByType("protyle", "readonly", 0, "", model.Conf.Editor.ReadOnly)
  145. util.BroadcastByType("main", "readonly", 0, "", model.Conf.Editor.ReadOnly)
  146. }
  147. ret.Data = model.Conf.Editor
  148. }
  149. func setExport(c *gin.Context) {
  150. ret := gulu.Ret.NewResult()
  151. defer c.JSON(http.StatusOK, ret)
  152. arg, ok := util.JsonArg(c, ret)
  153. if !ok {
  154. return
  155. }
  156. param, err := gulu.JSON.MarshalJSON(arg)
  157. if nil != err {
  158. ret.Code = -1
  159. ret.Msg = err.Error()
  160. return
  161. }
  162. export := &conf.Export{}
  163. if err = gulu.JSON.UnmarshalJSON(param, export); nil != err {
  164. ret.Code = -1
  165. ret.Msg = err.Error()
  166. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  167. return
  168. }
  169. if "" != export.PandocBin {
  170. if !util.IsValidPandocBin(export.PandocBin) {
  171. util.PushErrMsg(fmt.Sprintf(model.Conf.Language(117), export.PandocBin), 5000)
  172. export.PandocBin = util.PandocBinPath
  173. }
  174. }
  175. model.Conf.Export = export
  176. model.Conf.Save()
  177. ret.Data = model.Conf.Export
  178. }
  179. func setFiletree(c *gin.Context) {
  180. ret := gulu.Ret.NewResult()
  181. defer c.JSON(http.StatusOK, ret)
  182. arg, ok := util.JsonArg(c, ret)
  183. if !ok {
  184. return
  185. }
  186. param, err := gulu.JSON.MarshalJSON(arg)
  187. if nil != err {
  188. ret.Code = -1
  189. ret.Msg = err.Error()
  190. return
  191. }
  192. fileTree := conf.NewFileTree()
  193. if err = gulu.JSON.UnmarshalJSON(param, fileTree); nil != err {
  194. ret.Code = -1
  195. ret.Msg = err.Error()
  196. return
  197. }
  198. fileTree.RefCreateSavePath = strings.TrimSpace(fileTree.RefCreateSavePath)
  199. if "" != fileTree.RefCreateSavePath {
  200. if !strings.HasSuffix(fileTree.RefCreateSavePath, "/") {
  201. fileTree.RefCreateSavePath += "/"
  202. }
  203. }
  204. fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
  205. for strings.HasSuffix(fileTree.DocCreateSavePath, "/") {
  206. fileTree.DocCreateSavePath = strings.TrimSuffix(fileTree.DocCreateSavePath, "/")
  207. fileTree.DocCreateSavePath = strings.TrimSpace(fileTree.DocCreateSavePath)
  208. }
  209. if 1 > fileTree.MaxOpenTabCount {
  210. fileTree.MaxOpenTabCount = 8
  211. }
  212. if 32 < fileTree.MaxOpenTabCount {
  213. fileTree.MaxOpenTabCount = 32
  214. }
  215. model.Conf.FileTree = fileTree
  216. model.Conf.Save()
  217. ret.Data = model.Conf.FileTree
  218. }
  219. func setSearch(c *gin.Context) {
  220. ret := gulu.Ret.NewResult()
  221. defer c.JSON(http.StatusOK, ret)
  222. arg, ok := util.JsonArg(c, ret)
  223. if !ok {
  224. return
  225. }
  226. param, err := gulu.JSON.MarshalJSON(arg)
  227. if nil != err {
  228. ret.Code = -1
  229. ret.Msg = err.Error()
  230. return
  231. }
  232. s := &conf.Search{}
  233. if err = gulu.JSON.UnmarshalJSON(param, s); nil != err {
  234. ret.Code = -1
  235. ret.Msg = err.Error()
  236. return
  237. }
  238. if 32 > s.Limit {
  239. s.Limit = 32
  240. }
  241. oldCaseSensitive := model.Conf.Search.CaseSensitive
  242. oldVirtualRefName := model.Conf.Search.VirtualRefName
  243. oldVirtualRefAlias := model.Conf.Search.VirtualRefAlias
  244. oldVirtualRefAnchor := model.Conf.Search.VirtualRefAnchor
  245. oldVirtualRefDoc := model.Conf.Search.VirtualRefDoc
  246. model.Conf.Search = s
  247. model.Conf.Save()
  248. sql.SetCaseSensitive(s.CaseSensitive)
  249. if s.CaseSensitive != oldCaseSensitive {
  250. model.FullReindex()
  251. }
  252. if oldVirtualRefName != s.VirtualRefName ||
  253. oldVirtualRefAlias != s.VirtualRefAlias ||
  254. oldVirtualRefAnchor != s.VirtualRefAnchor ||
  255. oldVirtualRefDoc != s.VirtualRefDoc {
  256. model.ResetVirtualBlockRefCache()
  257. }
  258. ret.Data = s
  259. }
  260. func setKeymap(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["data"])
  268. if nil != err {
  269. ret.Code = -1
  270. ret.Msg = err.Error()
  271. return
  272. }
  273. keymap := &conf.Keymap{}
  274. if err = gulu.JSON.UnmarshalJSON(param, keymap); nil != err {
  275. ret.Code = -1
  276. ret.Msg = err.Error()
  277. return
  278. }
  279. model.Conf.Keymap = keymap
  280. model.Conf.Save()
  281. }
  282. func setAppearance(c *gin.Context) {
  283. ret := gulu.Ret.NewResult()
  284. defer c.JSON(http.StatusOK, ret)
  285. arg, ok := util.JsonArg(c, ret)
  286. if !ok {
  287. return
  288. }
  289. param, err := gulu.JSON.MarshalJSON(arg)
  290. if nil != err {
  291. ret.Code = -1
  292. ret.Msg = err.Error()
  293. return
  294. }
  295. appearance := &conf.Appearance{}
  296. if err = gulu.JSON.UnmarshalJSON(param, appearance); nil != err {
  297. ret.Code = -1
  298. ret.Msg = err.Error()
  299. return
  300. }
  301. model.Conf.Appearance = appearance
  302. model.Conf.Lang = appearance.Lang
  303. util.Lang = model.Conf.Lang
  304. model.Conf.Save()
  305. model.InitAppearance()
  306. ret.Data = model.Conf.Appearance
  307. }
  308. func getCloudUser(c *gin.Context) {
  309. ret := gulu.Ret.NewResult()
  310. defer c.JSON(http.StatusOK, ret)
  311. arg, ok := util.JsonArg(c, ret)
  312. if !ok {
  313. return
  314. }
  315. t := arg["token"]
  316. var token string
  317. if nil != t {
  318. token = t.(string)
  319. }
  320. if err := model.RefreshUser(token); nil != err {
  321. ret.Code = 1
  322. ret.Msg = err.Error()
  323. return
  324. }
  325. ret.Data = model.Conf.User
  326. }
  327. func logoutCloudUser(c *gin.Context) {
  328. ret := gulu.Ret.NewResult()
  329. defer c.JSON(http.StatusOK, ret)
  330. model.LogoutUser()
  331. }
  332. func login2faCloudUser(c *gin.Context) {
  333. ret := gulu.Ret.NewResult()
  334. defer c.JSON(http.StatusOK, ret)
  335. arg, ok := util.JsonArg(c, ret)
  336. if !ok {
  337. return
  338. }
  339. token := arg["token"].(string)
  340. code := arg["code"].(string)
  341. data, err := model.Login2fa(token, code)
  342. if nil != err {
  343. ret.Code = -1
  344. ret.Msg = err.Error()
  345. return
  346. }
  347. ret.Data = data
  348. }
  349. func getCustomCSS(c *gin.Context) {
  350. ret := gulu.Ret.NewResult()
  351. defer c.JSON(http.StatusOK, ret)
  352. arg, ok := util.JsonArg(c, ret)
  353. if !ok {
  354. return
  355. }
  356. themeName := arg["theme"].(string)
  357. customCSS, err := model.ReadCustomCSS(themeName)
  358. if nil != err {
  359. ret.Code = -1
  360. ret.Msg = err.Error()
  361. return
  362. }
  363. ret.Data = customCSS
  364. }
  365. func setCustomCSS(c *gin.Context) {
  366. ret := gulu.Ret.NewResult()
  367. defer c.JSON(http.StatusOK, ret)
  368. arg, ok := util.JsonArg(c, ret)
  369. if !ok {
  370. return
  371. }
  372. themeName := arg["theme"].(string)
  373. css := arg["css"].(map[string]interface{})
  374. if err := model.WriteCustomCSS(themeName, css); nil != err {
  375. ret.Code = -1
  376. ret.Msg = err.Error()
  377. return
  378. }
  379. }
  380. func setEmoji(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. argEmoji := arg["emoji"].([]interface{})
  388. var emoji []string
  389. for _, ae := range argEmoji {
  390. emoji = append(emoji, ae.(string))
  391. }
  392. model.Conf.Editor.Emoji = emoji
  393. }