setting.go 9.5 KB

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