history.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. "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 getHistoryItems(c *gin.Context) {
  57. ret := gulu.Ret.NewResult()
  58. defer c.JSON(http.StatusOK, ret)
  59. arg, ok := util.JsonArg(c, ret)
  60. if !ok {
  61. return
  62. }
  63. created := arg["created"].(string)
  64. notebook := ""
  65. if nil != arg["notebook"] {
  66. notebook = arg["notebook"].(string)
  67. }
  68. typ := model.HistoryTypeDoc
  69. if nil != arg["type"] {
  70. typ = int(arg["type"].(float64))
  71. }
  72. query := arg["query"].(string)
  73. op := "all"
  74. if nil != arg["op"] {
  75. op = arg["op"].(string)
  76. }
  77. histories := model.FullTextSearchHistoryItems(created, query, notebook, op, typ)
  78. ret.Data = map[string]interface{}{
  79. "items": histories,
  80. }
  81. }
  82. func reindexHistory(c *gin.Context) {
  83. ret := gulu.Ret.NewResult()
  84. defer c.JSON(http.StatusOK, ret)
  85. model.ReindexHistory()
  86. }
  87. func getNotebookHistory(c *gin.Context) {
  88. ret := gulu.Ret.NewResult()
  89. defer c.JSON(http.StatusOK, ret)
  90. histories, err := model.GetNotebookHistory()
  91. if nil != err {
  92. ret.Code = -1
  93. ret.Msg = err.Error()
  94. return
  95. }
  96. ret.Data = map[string]interface{}{
  97. "histories": histories,
  98. }
  99. }
  100. func clearWorkspaceHistory(c *gin.Context) {
  101. ret := gulu.Ret.NewResult()
  102. defer c.JSON(http.StatusOK, ret)
  103. msgId := util.PushMsg(model.Conf.Language(100), 1000*60*15)
  104. time.Sleep(3 * time.Second)
  105. err := model.ClearWorkspaceHistory()
  106. if nil != err {
  107. ret.Code = -1
  108. ret.Msg = err.Error()
  109. return
  110. }
  111. util.PushClearMsg(msgId)
  112. time.Sleep(500 * time.Millisecond)
  113. util.PushMsg(model.Conf.Language(99), 1000*5)
  114. }
  115. func getDocHistoryContent(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. historyPath := arg["historyPath"].(string)
  123. k := arg["k"]
  124. var keyword string
  125. if nil != k {
  126. keyword = k.(string)
  127. }
  128. id, rootID, content, isLargeDoc, err := model.GetDocHistoryContent(historyPath, keyword)
  129. if nil != err {
  130. ret.Code = -1
  131. ret.Msg = err.Error()
  132. return
  133. }
  134. ret.Data = map[string]interface{}{
  135. "id": id,
  136. "rootID": rootID,
  137. "content": content,
  138. "isLargeDoc": isLargeDoc,
  139. }
  140. }
  141. func rollbackDocHistory(c *gin.Context) {
  142. ret := gulu.Ret.NewResult()
  143. defer c.JSON(http.StatusOK, ret)
  144. arg, ok := util.JsonArg(c, ret)
  145. if !ok {
  146. return
  147. }
  148. notebook := arg["notebook"].(string)
  149. historyPath := arg["historyPath"].(string)
  150. err := model.RollbackDocHistory(notebook, historyPath)
  151. if nil != err {
  152. ret.Code = -1
  153. ret.Msg = err.Error()
  154. return
  155. }
  156. ret.Data = map[string]interface{}{
  157. "box": notebook,
  158. }
  159. }
  160. func rollbackAssetsHistory(c *gin.Context) {
  161. ret := gulu.Ret.NewResult()
  162. defer c.JSON(http.StatusOK, ret)
  163. arg, ok := util.JsonArg(c, ret)
  164. if !ok {
  165. return
  166. }
  167. historyPath := arg["historyPath"].(string)
  168. err := model.RollbackAssetsHistory(historyPath)
  169. if nil != err {
  170. ret.Code = -1
  171. ret.Msg = err.Error()
  172. return
  173. }
  174. }
  175. func rollbackNotebookHistory(c *gin.Context) {
  176. ret := gulu.Ret.NewResult()
  177. defer c.JSON(http.StatusOK, ret)
  178. arg, ok := util.JsonArg(c, ret)
  179. if !ok {
  180. return
  181. }
  182. historyPath := arg["historyPath"].(string)
  183. err := model.RollbackNotebookHistory(historyPath)
  184. if nil != err {
  185. ret.Code = -1
  186. ret.Msg = err.Error()
  187. return
  188. }
  189. }