history.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 err != nil {
  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 err != nil {
  107. ret.Code = -1
  108. ret.Msg = err.Error()
  109. return
  110. }
  111. util.PushUpdateMsg(msgId, model.Conf.Language(99), 1000*5)
  112. }
  113. func getDocHistoryContent(c *gin.Context) {
  114. ret := gulu.Ret.NewResult()
  115. defer c.JSON(http.StatusOK, ret)
  116. arg, ok := util.JsonArg(c, ret)
  117. if !ok {
  118. return
  119. }
  120. historyPath := arg["historyPath"].(string)
  121. k := arg["k"]
  122. var keyword string
  123. if nil != k {
  124. keyword = k.(string)
  125. }
  126. highlight := true
  127. if val, ok := arg["highlight"]; ok {
  128. highlight = val.(bool)
  129. }
  130. id, rootID, content, isLargeDoc, err := model.GetDocHistoryContent(historyPath, keyword, highlight)
  131. if err != nil {
  132. ret.Code = -1
  133. ret.Msg = err.Error()
  134. return
  135. }
  136. ret.Data = map[string]interface{}{
  137. "id": id,
  138. "rootID": rootID,
  139. "content": content,
  140. "isLargeDoc": isLargeDoc,
  141. }
  142. }
  143. func rollbackDocHistory(c *gin.Context) {
  144. ret := gulu.Ret.NewResult()
  145. defer c.JSON(http.StatusOK, ret)
  146. arg, ok := util.JsonArg(c, ret)
  147. if !ok {
  148. return
  149. }
  150. notebook := arg["notebook"].(string)
  151. historyPath := arg["historyPath"].(string)
  152. err := model.RollbackDocHistory(notebook, historyPath)
  153. if err != nil {
  154. ret.Code = -1
  155. ret.Msg = err.Error()
  156. return
  157. }
  158. ret.Data = map[string]interface{}{
  159. "box": notebook,
  160. }
  161. }
  162. func rollbackAssetsHistory(c *gin.Context) {
  163. ret := gulu.Ret.NewResult()
  164. defer c.JSON(http.StatusOK, ret)
  165. arg, ok := util.JsonArg(c, ret)
  166. if !ok {
  167. return
  168. }
  169. historyPath := arg["historyPath"].(string)
  170. err := model.RollbackAssetsHistory(historyPath)
  171. if err != nil {
  172. ret.Code = -1
  173. ret.Msg = err.Error()
  174. return
  175. }
  176. }
  177. func rollbackNotebookHistory(c *gin.Context) {
  178. ret := gulu.Ret.NewResult()
  179. defer c.JSON(http.StatusOK, ret)
  180. arg, ok := util.JsonArg(c, ret)
  181. if !ok {
  182. return
  183. }
  184. historyPath := arg["historyPath"].(string)
  185. err := model.RollbackNotebookHistory(historyPath)
  186. if err != nil {
  187. ret.Code = -1
  188. ret.Msg = err.Error()
  189. return
  190. }
  191. }