storage.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. "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("removeLocalStorageVal", 0, util.PushModeBroadcastMainExcludeSelfApp)
  102. evt.AppId = app
  103. evt.Data = map[string]interface{}{"keys": keys}
  104. util.PushEvent(evt)
  105. }
  106. func removeLocalStorageVal(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. err := model.RemoveLocalStorageVals([]string{key})
  115. if nil != err {
  116. ret.Code = -1
  117. ret.Msg = err.Error()
  118. return
  119. }
  120. app := arg["app"].(string)
  121. evt := util.NewCmdResult("removeLocalStorageVal", 0, util.PushModeBroadcastMainExcludeSelfApp)
  122. evt.AppId = app
  123. evt.Data = map[string]interface{}{"key": key}
  124. util.PushEvent(evt)
  125. }
  126. func setLocalStorageVal(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. key := arg["key"].(string)
  134. val := arg["val"].(interface{})
  135. err := model.SetLocalStorageVal(key, 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("setLocalStorageVal", 0, util.PushModeBroadcastMainExcludeSelfApp)
  143. evt.AppId = app
  144. evt.Data = map[string]interface{}{"key": key, "val": val}
  145. util.PushEvent(evt)
  146. }
  147. func setLocalStorage(c *gin.Context) {
  148. ret := gulu.Ret.NewResult()
  149. defer c.JSON(http.StatusOK, ret)
  150. arg, ok := util.JsonArg(c, ret)
  151. if !ok {
  152. return
  153. }
  154. val := arg["val"].(interface{})
  155. err := model.SetLocalStorage(val)
  156. if nil != err {
  157. ret.Code = -1
  158. ret.Msg = err.Error()
  159. return
  160. }
  161. app := arg["app"].(string)
  162. evt := util.NewCmdResult("setLocalStorage", 0, util.PushModeBroadcastMainExcludeSelfApp)
  163. evt.AppId = app
  164. evt.Data = val
  165. util.PushEvent(evt)
  166. }
  167. func getLocalStorage(c *gin.Context) {
  168. ret := gulu.Ret.NewResult()
  169. defer c.JSON(http.StatusOK, ret)
  170. data, err := model.GetLocalStorage()
  171. if nil != err {
  172. ret.Code = -1
  173. ret.Msg = err.Error()
  174. return
  175. }
  176. ret.Data = data
  177. }