riff.go 8.8 KB

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