riff.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/riff"
  23. "github.com/siyuan-note/siyuan/kernel/model"
  24. "github.com/siyuan-note/siyuan/kernel/util"
  25. )
  26. func getNotebookRiffCards(c *gin.Context) {
  27. ret := gulu.Ret.NewResult()
  28. defer c.JSON(http.StatusOK, ret)
  29. arg, ok := util.JsonArg(c, ret)
  30. if !ok {
  31. return
  32. }
  33. notebookID := arg["id"].(string)
  34. page := int(arg["page"].(float64))
  35. blockIDs, total, pageCount := model.GetNotebookFlashcards(notebookID, page)
  36. ret.Data = map[string]interface{}{
  37. "blocks": blockIDs,
  38. "total": total,
  39. "pageCount": pageCount,
  40. }
  41. }
  42. func getTreeRiffCards(c *gin.Context) {
  43. ret := gulu.Ret.NewResult()
  44. defer c.JSON(http.StatusOK, ret)
  45. arg, ok := util.JsonArg(c, ret)
  46. if !ok {
  47. return
  48. }
  49. rootID := arg["id"].(string)
  50. page := int(arg["page"].(float64))
  51. blockIDs, total, pageCount := model.GetTreeFlashcards(rootID, page)
  52. ret.Data = map[string]interface{}{
  53. "blocks": blockIDs,
  54. "total": total,
  55. "pageCount": pageCount,
  56. }
  57. }
  58. func getRiffCards(c *gin.Context) {
  59. ret := gulu.Ret.NewResult()
  60. defer c.JSON(http.StatusOK, ret)
  61. arg, ok := util.JsonArg(c, ret)
  62. if !ok {
  63. return
  64. }
  65. deckID := arg["id"].(string)
  66. page := int(arg["page"].(float64))
  67. blocks, total, pageCount := model.GetFlashcards(deckID, page)
  68. ret.Data = map[string]interface{}{
  69. "blocks": blocks,
  70. "total": total,
  71. "pageCount": pageCount,
  72. }
  73. }
  74. func reviewRiffCard(c *gin.Context) {
  75. ret := gulu.Ret.NewResult()
  76. defer c.JSON(http.StatusOK, ret)
  77. arg, ok := util.JsonArg(c, ret)
  78. if !ok {
  79. return
  80. }
  81. deckID := arg["deckID"].(string)
  82. cardID := arg["cardID"].(string)
  83. rating := int(arg["rating"].(float64))
  84. reviewedCardIDs := getReviewedCards(arg)
  85. err := model.ReviewFlashcard(deckID, cardID, riff.Rating(rating), reviewedCardIDs)
  86. if nil != err {
  87. ret.Code = -1
  88. ret.Msg = err.Error()
  89. return
  90. }
  91. }
  92. func skipReviewRiffCard(c *gin.Context) {
  93. ret := gulu.Ret.NewResult()
  94. defer c.JSON(http.StatusOK, ret)
  95. arg, ok := util.JsonArg(c, ret)
  96. if !ok {
  97. return
  98. }
  99. deckID := arg["deckID"].(string)
  100. cardID := arg["cardID"].(string)
  101. err := model.SkipReviewFlashcard(deckID, cardID)
  102. if nil != err {
  103. ret.Code = -1
  104. ret.Msg = err.Error()
  105. return
  106. }
  107. }
  108. func getNotebookRiffDueCards(c *gin.Context) {
  109. ret := gulu.Ret.NewResult()
  110. defer c.JSON(http.StatusOK, ret)
  111. arg, ok := util.JsonArg(c, ret)
  112. if !ok {
  113. return
  114. }
  115. notebookID := arg["notebook"].(string)
  116. reviewedCardIDs := getReviewedCards(arg)
  117. cards, err := model.GetNotebookDueFlashcards(notebookID, reviewedCardIDs)
  118. if nil != err {
  119. ret.Code = -1
  120. ret.Msg = err.Error()
  121. return
  122. }
  123. ret.Data = cards
  124. }
  125. func getTreeRiffDueCards(c *gin.Context) {
  126. ret := gulu.Ret.NewResult()
  127. defer c.JSON(http.StatusOK, ret)
  128. arg, ok := util.JsonArg(c, ret)
  129. if !ok {
  130. return
  131. }
  132. rootID := arg["rootID"].(string)
  133. reviewedCardIDs := getReviewedCards(arg)
  134. cards, err := model.GetTreeDueFlashcards(rootID, reviewedCardIDs)
  135. if nil != err {
  136. ret.Code = -1
  137. ret.Msg = err.Error()
  138. return
  139. }
  140. ret.Data = cards
  141. }
  142. func getRiffDueCards(c *gin.Context) {
  143. ret := gulu.Ret.NewResult()
  144. defer c.JSON(http.StatusOK, ret)
  145. arg, ok := util.JsonArg(c, ret)
  146. if !ok {
  147. return
  148. }
  149. deckID := arg["deckID"].(string)
  150. reviewedCardIDs := getReviewedCards(arg)
  151. cards, err := model.GetDueFlashcards(deckID, reviewedCardIDs)
  152. if nil != err {
  153. ret.Code = -1
  154. ret.Msg = err.Error()
  155. return
  156. }
  157. ret.Data = cards
  158. }
  159. func getReviewedCards(arg map[string]interface{}) (ret []string) {
  160. if nil == arg["reviewedCards"] {
  161. return
  162. }
  163. reviewedCardsArg := arg["reviewedCards"].([]interface{})
  164. for _, card := range reviewedCardsArg {
  165. c := card.(map[string]interface{})
  166. cardID := c["cardID"].(string)
  167. ret = append(ret, cardID)
  168. }
  169. return
  170. }
  171. func removeRiffCards(c *gin.Context) {
  172. ret := gulu.Ret.NewResult()
  173. defer c.JSON(http.StatusOK, ret)
  174. arg, ok := util.JsonArg(c, ret)
  175. if !ok {
  176. return
  177. }
  178. deckID := arg["deckID"].(string)
  179. blockIDsArg := arg["blockIDs"].([]interface{})
  180. var blockIDs []string
  181. for _, blockID := range blockIDsArg {
  182. blockIDs = append(blockIDs, blockID.(string))
  183. }
  184. err := model.RemoveFlashcardsByBlockIDs(deckID, blockIDs)
  185. if nil != err {
  186. ret.Code = -1
  187. ret.Msg = err.Error()
  188. return
  189. }
  190. if "" != deckID {
  191. deck := model.Decks[deckID]
  192. ret.Data = deckData(deck)
  193. }
  194. // All 卡包不返回数据
  195. }
  196. func addRiffCards(c *gin.Context) {
  197. ret := gulu.Ret.NewResult()
  198. defer c.JSON(http.StatusOK, ret)
  199. arg, ok := util.JsonArg(c, ret)
  200. if !ok {
  201. return
  202. }
  203. deckID := arg["deckID"].(string)
  204. blockIDsArg := arg["blockIDs"].([]interface{})
  205. var blockIDs []string
  206. for _, blockID := range blockIDsArg {
  207. blockIDs = append(blockIDs, blockID.(string))
  208. }
  209. err := model.AddFlashcards(deckID, blockIDs)
  210. if nil != err {
  211. ret.Code = -1
  212. ret.Msg = err.Error()
  213. return
  214. }
  215. deck := model.Decks[deckID]
  216. ret.Data = deckData(deck)
  217. }
  218. func renameRiffDeck(c *gin.Context) {
  219. ret := gulu.Ret.NewResult()
  220. defer c.JSON(http.StatusOK, ret)
  221. arg, ok := util.JsonArg(c, ret)
  222. if !ok {
  223. return
  224. }
  225. deckID := arg["deckID"].(string)
  226. name := arg["name"].(string)
  227. err := model.RenameDeck(deckID, name)
  228. if nil != err {
  229. ret.Code = -1
  230. ret.Msg = err.Error()
  231. return
  232. }
  233. }
  234. func removeRiffDeck(c *gin.Context) {
  235. ret := gulu.Ret.NewResult()
  236. defer c.JSON(http.StatusOK, ret)
  237. arg, ok := util.JsonArg(c, ret)
  238. if !ok {
  239. return
  240. }
  241. deckID := arg["deckID"].(string)
  242. err := model.RemoveDeck(deckID)
  243. if nil != err {
  244. ret.Code = -1
  245. ret.Msg = err.Error()
  246. return
  247. }
  248. }
  249. func createRiffDeck(c *gin.Context) {
  250. ret := gulu.Ret.NewResult()
  251. defer c.JSON(http.StatusOK, ret)
  252. arg, ok := util.JsonArg(c, ret)
  253. if !ok {
  254. return
  255. }
  256. name := arg["name"].(string)
  257. deck, err := model.CreateDeck(name)
  258. if nil != err {
  259. ret.Code = -1
  260. ret.Msg = err.Error()
  261. return
  262. }
  263. ret.Data = deckData(deck)
  264. }
  265. func getRiffDecks(c *gin.Context) {
  266. ret := gulu.Ret.NewResult()
  267. defer c.JSON(http.StatusOK, ret)
  268. decks := model.GetDecks()
  269. var data []interface{}
  270. for _, deck := range decks {
  271. data = append(data, deckData(deck))
  272. }
  273. if 1 > len(data) {
  274. data = []interface{}{}
  275. }
  276. ret.Data = data
  277. }
  278. func deckData(deck *riff.Deck) map[string]interface{} {
  279. return map[string]interface{}{
  280. "id": deck.ID,
  281. "name": deck.Name,
  282. "size": deck.CountCards(),
  283. "created": time.UnixMilli(deck.Created).Format("2006-01-02 15:04:05"),
  284. "updated": time.UnixMilli(deck.Updated).Format("2006-01-02 15:04:05"),
  285. }
  286. }