riff.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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/riff"
  23. "github.com/siyuan-note/siyuan/kernel/model"
  24. "github.com/siyuan-note/siyuan/kernel/util"
  25. )
  26. func resetRiffCards(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. typ := arg["type"].(string) // notebook, tree, deck
  34. id := arg["id"].(string) // notebook ID, root ID, deck ID
  35. blockIDsArg := arg["blockIDs"] // 如果不传入 blockIDs (或者传入实参为空数组),则重置所有卡片
  36. var blockIDs []string
  37. if nil != blockIDsArg {
  38. for _, blockID := range blockIDsArg.([]interface{}) {
  39. blockIDs = append(blockIDs, blockID.(string))
  40. }
  41. }
  42. model.ResetFlashcards(typ, id, blockIDs)
  43. }
  44. func getNotebookRiffCards(c *gin.Context) {
  45. ret := gulu.Ret.NewResult()
  46. defer c.JSON(http.StatusOK, ret)
  47. arg, ok := util.JsonArg(c, ret)
  48. if !ok {
  49. return
  50. }
  51. notebookID := arg["id"].(string)
  52. page := int(arg["page"].(float64))
  53. blockIDs, total, pageCount := model.GetNotebookFlashcards(notebookID, page)
  54. ret.Data = map[string]interface{}{
  55. "blocks": blockIDs,
  56. "total": total,
  57. "pageCount": pageCount,
  58. }
  59. }
  60. func getTreeRiffCards(c *gin.Context) {
  61. ret := gulu.Ret.NewResult()
  62. defer c.JSON(http.StatusOK, ret)
  63. arg, ok := util.JsonArg(c, ret)
  64. if !ok {
  65. return
  66. }
  67. rootID := arg["id"].(string)
  68. page := int(arg["page"].(float64))
  69. blockIDs, total, pageCount := model.GetTreeFlashcards(rootID, page)
  70. ret.Data = map[string]interface{}{
  71. "blocks": blockIDs,
  72. "total": total,
  73. "pageCount": pageCount,
  74. }
  75. }
  76. func getRiffCards(c *gin.Context) {
  77. ret := gulu.Ret.NewResult()
  78. defer c.JSON(http.StatusOK, ret)
  79. arg, ok := util.JsonArg(c, ret)
  80. if !ok {
  81. return
  82. }
  83. deckID := arg["id"].(string)
  84. page := int(arg["page"].(float64))
  85. blocks, total, pageCount := model.GetDeckFlashcards(deckID, page)
  86. ret.Data = map[string]interface{}{
  87. "blocks": blocks,
  88. "total": total,
  89. "pageCount": pageCount,
  90. }
  91. }
  92. func reviewRiffCard(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. rating := int(arg["rating"].(float64))
  102. reviewedCardIDs := getReviewedCards(arg)
  103. err := model.ReviewFlashcard(deckID, cardID, riff.Rating(rating), reviewedCardIDs)
  104. if nil != err {
  105. ret.Code = -1
  106. ret.Msg = err.Error()
  107. return
  108. }
  109. }
  110. func skipReviewRiffCard(c *gin.Context) {
  111. ret := gulu.Ret.NewResult()
  112. defer c.JSON(http.StatusOK, ret)
  113. arg, ok := util.JsonArg(c, ret)
  114. if !ok {
  115. return
  116. }
  117. deckID := arg["deckID"].(string)
  118. cardID := arg["cardID"].(string)
  119. err := model.SkipReviewFlashcard(deckID, cardID)
  120. if nil != err {
  121. ret.Code = -1
  122. ret.Msg = err.Error()
  123. return
  124. }
  125. }
  126. func getNotebookRiffDueCards(c *gin.Context) {
  127. ret := gulu.Ret.NewResult()
  128. defer c.JSON(http.StatusOK, ret)
  129. arg, ok := util.JsonArg(c, ret)
  130. if !ok {
  131. return
  132. }
  133. notebookID := arg["notebook"].(string)
  134. reviewedCardIDs := getReviewedCards(arg)
  135. cards, unreviewedCount, err := model.GetNotebookDueFlashcards(notebookID, reviewedCardIDs)
  136. if nil != err {
  137. ret.Code = -1
  138. ret.Msg = err.Error()
  139. return
  140. }
  141. ret.Data = map[string]interface{}{
  142. "cards": cards,
  143. "unreviewedCount": unreviewedCount,
  144. }
  145. }
  146. func getTreeRiffDueCards(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. rootID := arg["rootID"].(string)
  154. reviewedCardIDs := getReviewedCards(arg)
  155. cards, unreviewedCount, err := model.GetTreeDueFlashcards(rootID, reviewedCardIDs)
  156. if nil != err {
  157. ret.Code = -1
  158. ret.Msg = err.Error()
  159. return
  160. }
  161. ret.Data = map[string]interface{}{
  162. "cards": cards,
  163. "unreviewedCount": unreviewedCount,
  164. }
  165. }
  166. func getRiffDueCards(c *gin.Context) {
  167. ret := gulu.Ret.NewResult()
  168. defer c.JSON(http.StatusOK, ret)
  169. arg, ok := util.JsonArg(c, ret)
  170. if !ok {
  171. return
  172. }
  173. deckID := arg["deckID"].(string)
  174. reviewedCardIDs := getReviewedCards(arg)
  175. cards, unreviewedCount, err := model.GetDueFlashcards(deckID, reviewedCardIDs)
  176. if nil != err {
  177. ret.Code = -1
  178. ret.Msg = err.Error()
  179. return
  180. }
  181. ret.Data = map[string]interface{}{
  182. "cards": cards,
  183. "unreviewedCount": unreviewedCount,
  184. }
  185. }
  186. func getReviewedCards(arg map[string]interface{}) (ret []string) {
  187. if nil == arg["reviewedCards"] {
  188. return
  189. }
  190. reviewedCardsArg := arg["reviewedCards"].([]interface{})
  191. for _, card := range reviewedCardsArg {
  192. c := card.(map[string]interface{})
  193. cardID := c["cardID"].(string)
  194. ret = append(ret, cardID)
  195. }
  196. return
  197. }
  198. func removeRiffCards(c *gin.Context) {
  199. ret := gulu.Ret.NewResult()
  200. defer c.JSON(http.StatusOK, ret)
  201. arg, ok := util.JsonArg(c, ret)
  202. if !ok {
  203. return
  204. }
  205. deckID := arg["deckID"].(string)
  206. blockIDsArg := arg["blockIDs"].([]interface{})
  207. var blockIDs []string
  208. for _, blockID := range blockIDsArg {
  209. blockIDs = append(blockIDs, blockID.(string))
  210. }
  211. transactions := []*model.Transaction{
  212. {
  213. DoOperations: []*model.Operation{
  214. {
  215. Action: "removeFlashcards",
  216. DeckID: deckID,
  217. BlockIDs: blockIDs,
  218. },
  219. },
  220. },
  221. }
  222. model.PerformTransactions(&transactions)
  223. model.WaitForWritingFiles()
  224. if "" != deckID {
  225. deck := model.Decks[deckID]
  226. ret.Data = deckData(deck)
  227. }
  228. // All 卡包不返回数据
  229. }
  230. func addRiffCards(c *gin.Context) {
  231. ret := gulu.Ret.NewResult()
  232. defer c.JSON(http.StatusOK, ret)
  233. arg, ok := util.JsonArg(c, ret)
  234. if !ok {
  235. return
  236. }
  237. deckID := arg["deckID"].(string)
  238. blockIDsArg := arg["blockIDs"].([]interface{})
  239. var blockIDs []string
  240. for _, blockID := range blockIDsArg {
  241. blockIDs = append(blockIDs, blockID.(string))
  242. }
  243. transactions := []*model.Transaction{
  244. {
  245. DoOperations: []*model.Operation{
  246. {
  247. Action: "addFlashcards",
  248. DeckID: deckID,
  249. BlockIDs: blockIDs,
  250. },
  251. },
  252. },
  253. }
  254. model.PerformTransactions(&transactions)
  255. model.WaitForWritingFiles()
  256. deck := model.Decks[deckID]
  257. ret.Data = deckData(deck)
  258. }
  259. func renameRiffDeck(c *gin.Context) {
  260. ret := gulu.Ret.NewResult()
  261. defer c.JSON(http.StatusOK, ret)
  262. arg, ok := util.JsonArg(c, ret)
  263. if !ok {
  264. return
  265. }
  266. deckID := arg["deckID"].(string)
  267. name := arg["name"].(string)
  268. err := model.RenameDeck(deckID, name)
  269. if nil != err {
  270. ret.Code = -1
  271. ret.Msg = err.Error()
  272. return
  273. }
  274. }
  275. func removeRiffDeck(c *gin.Context) {
  276. ret := gulu.Ret.NewResult()
  277. defer c.JSON(http.StatusOK, ret)
  278. arg, ok := util.JsonArg(c, ret)
  279. if !ok {
  280. return
  281. }
  282. deckID := arg["deckID"].(string)
  283. err := model.RemoveDeck(deckID)
  284. if nil != err {
  285. ret.Code = -1
  286. ret.Msg = err.Error()
  287. return
  288. }
  289. }
  290. func createRiffDeck(c *gin.Context) {
  291. ret := gulu.Ret.NewResult()
  292. defer c.JSON(http.StatusOK, ret)
  293. arg, ok := util.JsonArg(c, ret)
  294. if !ok {
  295. return
  296. }
  297. name := arg["name"].(string)
  298. deck, err := model.CreateDeck(name)
  299. if nil != err {
  300. ret.Code = -1
  301. ret.Msg = err.Error()
  302. return
  303. }
  304. ret.Data = deckData(deck)
  305. }
  306. func getRiffDecks(c *gin.Context) {
  307. ret := gulu.Ret.NewResult()
  308. defer c.JSON(http.StatusOK, ret)
  309. decks := model.GetDecks()
  310. var data []interface{}
  311. for _, deck := range decks {
  312. data = append(data, deckData(deck))
  313. }
  314. if 1 > len(data) {
  315. data = []interface{}{}
  316. }
  317. ret.Data = data
  318. }
  319. func deckData(deck *riff.Deck) map[string]interface{} {
  320. return map[string]interface{}{
  321. "id": deck.ID,
  322. "name": deck.Name,
  323. "size": deck.CountCards(),
  324. "created": time.UnixMilli(deck.Created).Format("2006-01-02 15:04:05"),
  325. "updated": time.UnixMilli(deck.Updated).Format("2006-01-02 15:04:05"),
  326. }
  327. }