history.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. notebook := ""
  33. if nil != arg["notebook"] {
  34. notebook = arg["notebook"].(string)
  35. }
  36. typ := model.HistoryTypeDoc
  37. if nil != arg["type"] {
  38. typ = int(arg["type"].(float64))
  39. }
  40. query := arg["query"].(string)
  41. page := 1
  42. if nil != arg["page"] {
  43. page = int(arg["page"].(float64))
  44. }
  45. op := "all"
  46. if nil != arg["op"] {
  47. op = arg["op"].(string)
  48. }
  49. histories, pageCount, totalCount := model.FullTextSearchHistory(query, notebook, op, typ, page)
  50. ret.Data = map[string]interface{}{
  51. "histories": histories,
  52. "pageCount": pageCount,
  53. "totalCount": totalCount,
  54. }
  55. }
  56. func reindexHistory(c *gin.Context) {
  57. ret := gulu.Ret.NewResult()
  58. defer c.JSON(http.StatusOK, ret)
  59. err := model.ReindexHistory()
  60. if nil != err {
  61. ret.Code = -1
  62. ret.Msg = err.Error()
  63. return
  64. }
  65. }
  66. func getNotebookHistory(c *gin.Context) {
  67. ret := gulu.Ret.NewResult()
  68. defer c.JSON(http.StatusOK, ret)
  69. histories, err := model.GetNotebookHistory()
  70. if nil != err {
  71. ret.Code = -1
  72. ret.Msg = err.Error()
  73. return
  74. }
  75. ret.Data = map[string]interface{}{
  76. "histories": histories,
  77. }
  78. }
  79. func clearWorkspaceHistory(c *gin.Context) {
  80. ret := gulu.Ret.NewResult()
  81. defer c.JSON(http.StatusOK, ret)
  82. msgId := util.PushMsg(model.Conf.Language(100), 1000*60*15)
  83. time.Sleep(3 * time.Second)
  84. err := model.ClearWorkspaceHistory()
  85. if nil != err {
  86. ret.Code = -1
  87. ret.Msg = err.Error()
  88. return
  89. }
  90. util.PushClearMsg(msgId)
  91. time.Sleep(500 * time.Millisecond)
  92. util.PushMsg(model.Conf.Language(99), 1000*5)
  93. }
  94. func getDocHistoryContent(c *gin.Context) {
  95. ret := gulu.Ret.NewResult()
  96. defer c.JSON(http.StatusOK, ret)
  97. arg, ok := util.JsonArg(c, ret)
  98. if !ok {
  99. return
  100. }
  101. historyPath := arg["historyPath"].(string)
  102. k := arg["k"]
  103. var keyword string
  104. if nil != k {
  105. keyword = k.(string)
  106. }
  107. content, isLargeDoc, err := model.GetDocHistoryContent(historyPath, keyword)
  108. if nil != err {
  109. ret.Code = -1
  110. ret.Msg = err.Error()
  111. return
  112. }
  113. ret.Data = map[string]interface{}{
  114. "content": content,
  115. "isLargeDoc": isLargeDoc,
  116. }
  117. }
  118. func rollbackDocHistory(c *gin.Context) {
  119. ret := gulu.Ret.NewResult()
  120. defer c.JSON(http.StatusOK, ret)
  121. arg, ok := util.JsonArg(c, ret)
  122. if !ok {
  123. return
  124. }
  125. notebook := arg["notebook"].(string)
  126. historyPath := arg["historyPath"].(string)
  127. err := model.RollbackDocHistory(notebook, historyPath)
  128. if nil != err {
  129. ret.Code = -1
  130. ret.Msg = err.Error()
  131. return
  132. }
  133. ret.Data = map[string]interface{}{
  134. "box": notebook,
  135. }
  136. }
  137. func rollbackAssetsHistory(c *gin.Context) {
  138. ret := gulu.Ret.NewResult()
  139. defer c.JSON(http.StatusOK, ret)
  140. arg, ok := util.JsonArg(c, ret)
  141. if !ok {
  142. return
  143. }
  144. historyPath := arg["historyPath"].(string)
  145. err := model.RollbackAssetsHistory(historyPath)
  146. if nil != err {
  147. ret.Code = -1
  148. ret.Msg = err.Error()
  149. return
  150. }
  151. }
  152. func rollbackNotebookHistory(c *gin.Context) {
  153. ret := gulu.Ret.NewResult()
  154. defer c.JSON(http.StatusOK, ret)
  155. arg, ok := util.JsonArg(c, ret)
  156. if !ok {
  157. return
  158. }
  159. historyPath := arg["historyPath"].(string)
  160. err := model.RollbackNotebookHistory(historyPath)
  161. if nil != err {
  162. ret.Code = -1
  163. ret.Msg = err.Error()
  164. return
  165. }
  166. }