storage.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "net/http"
  19. "github.com/88250/gulu"
  20. "github.com/gin-gonic/gin"
  21. "github.com/siyuan-note/siyuan/kernel/model"
  22. "github.com/siyuan-note/siyuan/kernel/util"
  23. )
  24. func getRecentDocs(c *gin.Context) {
  25. ret := gulu.Ret.NewResult()
  26. defer c.JSON(http.StatusOK, ret)
  27. data, err := model.GetRecentDocs()
  28. if nil != err {
  29. ret.Code = -1
  30. ret.Msg = err.Error()
  31. return
  32. }
  33. ret.Data = data
  34. }
  35. func removeCriterion(c *gin.Context) {
  36. ret := gulu.Ret.NewResult()
  37. defer c.JSON(http.StatusOK, ret)
  38. arg, ok := util.JsonArg(c, ret)
  39. if !ok {
  40. return
  41. }
  42. name := arg["name"].(string)
  43. err := model.RemoveCriterion(name)
  44. if nil != err {
  45. ret.Code = -1
  46. ret.Msg = err.Error()
  47. return
  48. }
  49. }
  50. func setCriterion(c *gin.Context) {
  51. ret := gulu.Ret.NewResult()
  52. defer c.JSON(http.StatusOK, ret)
  53. arg, ok := util.JsonArg(c, ret)
  54. if !ok {
  55. return
  56. }
  57. param, err := gulu.JSON.MarshalJSON(arg["criterion"])
  58. if nil != err {
  59. ret.Code = -1
  60. ret.Msg = err.Error()
  61. return
  62. }
  63. criterion := &model.Criterion{}
  64. if err = gulu.JSON.UnmarshalJSON(param, criterion); nil != err {
  65. ret.Code = -1
  66. ret.Msg = err.Error()
  67. return
  68. }
  69. err = model.SetCriterion(criterion)
  70. if nil != err {
  71. ret.Code = -1
  72. ret.Msg = err.Error()
  73. return
  74. }
  75. }
  76. func getCriteria(c *gin.Context) {
  77. ret := gulu.Ret.NewResult()
  78. defer c.JSON(http.StatusOK, ret)
  79. data := model.GetCriteria()
  80. ret.Data = data
  81. }
  82. func removeLocalStorageVals(c *gin.Context) {
  83. ret := gulu.Ret.NewResult()
  84. defer c.JSON(http.StatusOK, ret)
  85. arg, ok := util.JsonArg(c, ret)
  86. if !ok {
  87. return
  88. }
  89. var keys []string
  90. keysArg := arg["keys"].([]interface{})
  91. for _, key := range keysArg {
  92. keys = append(keys, key.(string))
  93. }
  94. err := model.RemoveLocalStorageVals(keys)
  95. if nil != err {
  96. ret.Code = -1
  97. ret.Msg = err.Error()
  98. return
  99. }
  100. app := arg["app"].(string)
  101. evt := util.NewCmdResult("removeLocalStorageVals", 0, util.PushModeBroadcastMainExcludeSelfApp)
  102. evt.AppId = app
  103. evt.Data = map[string]interface{}{"keys": keys}
  104. util.PushEvent(evt)
  105. }
  106. func setLocalStorageVal(c *gin.Context) {
  107. ret := gulu.Ret.NewResult()
  108. defer c.JSON(http.StatusOK, ret)
  109. arg, ok := util.JsonArg(c, ret)
  110. if !ok {
  111. return
  112. }
  113. key := arg["key"].(string)
  114. val := arg["val"].(interface{})
  115. err := model.SetLocalStorageVal(key, val)
  116. if nil != err {
  117. ret.Code = -1
  118. ret.Msg = err.Error()
  119. return
  120. }
  121. app := arg["app"].(string)
  122. evt := util.NewCmdResult("setLocalStorageVal", 0, util.PushModeBroadcastMainExcludeSelfApp)
  123. evt.AppId = app
  124. evt.Data = map[string]interface{}{"key": key, "val": val}
  125. util.PushEvent(evt)
  126. }
  127. func setLocalStorage(c *gin.Context) {
  128. ret := gulu.Ret.NewResult()
  129. defer c.JSON(http.StatusOK, ret)
  130. arg, ok := util.JsonArg(c, ret)
  131. if !ok {
  132. return
  133. }
  134. val := arg["val"].(interface{})
  135. err := model.SetLocalStorage(val)
  136. if nil != err {
  137. ret.Code = -1
  138. ret.Msg = err.Error()
  139. return
  140. }
  141. app := arg["app"].(string)
  142. evt := util.NewCmdResult("setLocalStorage", 0, util.PushModeBroadcastMainExcludeSelfApp)
  143. evt.AppId = app
  144. evt.Data = val
  145. util.PushEvent(evt)
  146. }
  147. func getLocalStorage(c *gin.Context) {
  148. ret := gulu.Ret.NewResult()
  149. defer c.JSON(http.StatusOK, ret)
  150. data := model.GetLocalStorage()
  151. ret.Data = data
  152. }