history.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. "time"
  20. "github.com/88250/gulu"
  21. "github.com/gin-gonic/gin"
  22. "github.com/siyuan-note/siyuan/kernel/model"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. func searchHistory(c *gin.Context) {
  26. ret := gulu.Ret.NewResult()
  27. defer c.JSON(http.StatusOK, ret)
  28. arg, ok := util.JsonArg(c, ret)
  29. if !ok {
  30. return
  31. }
  32. query := arg["query"].(string)
  33. page := int(arg["page"].(float64))
  34. histories := model.FullTextSearchHistory(query, page)
  35. ret.Data = map[string]interface{}{
  36. "histories": histories,
  37. }
  38. }
  39. func reindexHistory(c *gin.Context) {
  40. ret := gulu.Ret.NewResult()
  41. defer c.JSON(http.StatusOK, ret)
  42. err := model.ReindexHistory()
  43. if nil != err {
  44. ret.Code = -1
  45. ret.Msg = err.Error()
  46. return
  47. }
  48. }
  49. func getNotebookHistory(c *gin.Context) {
  50. ret := gulu.Ret.NewResult()
  51. defer c.JSON(http.StatusOK, ret)
  52. histories, err := model.GetNotebookHistory()
  53. if nil != err {
  54. ret.Code = -1
  55. ret.Msg = err.Error()
  56. return
  57. }
  58. ret.Data = map[string]interface{}{
  59. "histories": histories,
  60. }
  61. }
  62. func getAssetsHistory(c *gin.Context) {
  63. ret := gulu.Ret.NewResult()
  64. defer c.JSON(http.StatusOK, ret)
  65. histories, err := model.GetAssetsHistory()
  66. if nil != err {
  67. ret.Code = -1
  68. ret.Msg = err.Error()
  69. return
  70. }
  71. ret.Data = map[string]interface{}{
  72. "histories": histories,
  73. }
  74. }
  75. func clearWorkspaceHistory(c *gin.Context) {
  76. ret := gulu.Ret.NewResult()
  77. defer c.JSON(http.StatusOK, ret)
  78. msgId := util.PushMsg(model.Conf.Language(100), 1000*60*15)
  79. time.Sleep(3 * time.Second)
  80. err := model.ClearWorkspaceHistory()
  81. if nil != err {
  82. ret.Code = -1
  83. ret.Msg = err.Error()
  84. return
  85. }
  86. util.PushClearMsg(msgId)
  87. time.Sleep(500 * time.Millisecond)
  88. util.PushMsg(model.Conf.Language(99), 1000*5)
  89. }
  90. func getDocHistory(c *gin.Context) {
  91. ret := gulu.Ret.NewResult()
  92. defer c.JSON(http.StatusOK, ret)
  93. arg, ok := util.JsonArg(c, ret)
  94. if !ok {
  95. return
  96. }
  97. notebook := arg["notebook"].(string)
  98. histories, err := model.GetDocHistory(notebook)
  99. if nil != err {
  100. ret.Code = -1
  101. ret.Msg = err.Error()
  102. return
  103. }
  104. ret.Data = map[string]interface{}{
  105. "box": notebook,
  106. "histories": histories,
  107. }
  108. }
  109. func getDocHistoryContent(c *gin.Context) {
  110. ret := gulu.Ret.NewResult()
  111. defer c.JSON(http.StatusOK, ret)
  112. arg, ok := util.JsonArg(c, ret)
  113. if !ok {
  114. return
  115. }
  116. historyPath := arg["historyPath"].(string)
  117. content, err := model.GetDocHistoryContent(historyPath)
  118. if nil != err {
  119. ret.Code = -1
  120. ret.Msg = err.Error()
  121. return
  122. }
  123. ret.Data = map[string]interface{}{
  124. "content": content,
  125. }
  126. }
  127. func rollbackDocHistory(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. notebook := arg["notebook"].(string)
  135. historyPath := arg["historyPath"].(string)
  136. err := model.RollbackDocHistory(notebook, historyPath)
  137. if nil != err {
  138. ret.Code = -1
  139. ret.Msg = err.Error()
  140. return
  141. }
  142. ret.Data = map[string]interface{}{
  143. "box": notebook,
  144. }
  145. }
  146. func rollbackAssetsHistory(c *gin.Context) {
  147. ret := gulu.Ret.NewResult()
  148. defer c.JSON(http.StatusOK, ret)
  149. arg, ok := util.JsonArg(c, ret)
  150. if !ok {
  151. return
  152. }
  153. historyPath := arg["historyPath"].(string)
  154. err := model.RollbackAssetsHistory(historyPath)
  155. if nil != err {
  156. ret.Code = -1
  157. ret.Msg = err.Error()
  158. return
  159. }
  160. }
  161. func rollbackNotebookHistory(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. historyPath := arg["historyPath"].(string)
  169. err := model.RollbackNotebookHistory(historyPath)
  170. if nil != err {
  171. ret.Code = -1
  172. ret.Msg = err.Error()
  173. return
  174. }
  175. }