setting.go 9.5 KB

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