From 41736542033e26cbd4fffc933b04ecdda9c5afed Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 26 Feb 2024 21:00:09 +0800 Subject: [PATCH] :art: Add parameter `pageSize` for kernel API `/api/riff/getRiffCards` Fix https://github.com/siyuan-note/siyuan/issues/10445 --- kernel/api/riff.go | 18 +++++++++++++++--- kernel/model/flashcard.go | 21 ++++++++++----------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/kernel/api/riff.go b/kernel/api/riff.go index 53b398b2d..6178c8e18 100644 --- a/kernel/api/riff.go +++ b/kernel/api/riff.go @@ -86,7 +86,11 @@ func getNotebookRiffCards(c *gin.Context) { notebookID := arg["id"].(string) page := int(arg["page"].(float64)) - blockIDs, total, pageCount := model.GetNotebookFlashcards(notebookID, page) + pageSize := 20 + if nil != arg["pageSize"] { + pageSize = int(arg["pageSize"].(float64)) + } + blockIDs, total, pageCount := model.GetNotebookFlashcards(notebookID, page, pageSize) ret.Data = map[string]interface{}{ "blocks": blockIDs, "total": total, @@ -105,7 +109,11 @@ func getTreeRiffCards(c *gin.Context) { rootID := arg["id"].(string) page := int(arg["page"].(float64)) - blockIDs, total, pageCount := model.GetTreeFlashcards(rootID, page) + pageSize := 20 + if nil != arg["pageSize"] { + pageSize = int(arg["pageSize"].(float64)) + } + blockIDs, total, pageCount := model.GetTreeFlashcards(rootID, page, pageSize) ret.Data = map[string]interface{}{ "blocks": blockIDs, "total": total, @@ -124,7 +132,11 @@ func getRiffCards(c *gin.Context) { deckID := arg["id"].(string) page := int(arg["page"].(float64)) - blocks, total, pageCount := model.GetDeckFlashcards(deckID, page) + pageSize := 20 + if nil != arg["pageSize"] { + pageSize = int(arg["pageSize"].(float64)) + } + blocks, total, pageCount := model.GetDeckFlashcards(deckID, page, pageSize) ret.Data = map[string]interface{}{ "blocks": blocks, "total": total, diff --git a/kernel/model/flashcard.go b/kernel/model/flashcard.go index 8f9637c55..8459d0522 100644 --- a/kernel/model/flashcard.go +++ b/kernel/model/flashcard.go @@ -109,7 +109,7 @@ func ResetFlashcards(typ, id, deckID string, blockIDs []string) { switch typ { case "notebook": for i := 1; ; i++ { - pagedBlocks, _, _ := GetNotebookFlashcards(id, i) + pagedBlocks, _, _ := GetNotebookFlashcards(id, i, 20) if 1 > len(pagedBlocks) { break } @@ -120,7 +120,7 @@ func ResetFlashcards(typ, id, deckID string, blockIDs []string) { } case "tree": for i := 1; ; i++ { - pagedBlocks, _, _ := GetTreeFlashcards(id, i) + pagedBlocks, _, _ := GetTreeFlashcards(id, i, 20) if 1 > len(pagedBlocks) { break } @@ -131,7 +131,7 @@ func ResetFlashcards(typ, id, deckID string, blockIDs []string) { } case "deck": for i := 1; ; i++ { - pagedBlocks, _, _ := GetDeckFlashcards(id, i) + pagedBlocks, _, _ := GetDeckFlashcards(id, i, 20) if 1 > len(pagedBlocks) { break } @@ -232,7 +232,7 @@ var ( deckLock = sync.Mutex{} ) -func GetNotebookFlashcards(boxID string, page int) (blocks []*Block, total, pageCount int) { +func GetNotebookFlashcards(boxID string, page, pageSize int) (blocks []*Block, total, pageCount int) { blocks = []*Block{} entries, err := os.ReadDir(filepath.Join(util.DataDir, boxID)) @@ -276,14 +276,14 @@ func GetNotebookFlashcards(boxID string, page int) (blocks []*Block, total, page allBlockIDs = gulu.Str.RemoveDuplicatedElem(allBlockIDs) cards := deck.GetCardsByBlockIDs(allBlockIDs) - blocks, total, pageCount = getCardsBlocks(cards, page) + blocks, total, pageCount = getCardsBlocks(cards, page, pageSize) return } -func GetTreeFlashcards(rootID string, page int) (blocks []*Block, total, pageCount int) { +func GetTreeFlashcards(rootID string, page, pageSize int) (blocks []*Block, total, pageCount int) { blocks = []*Block{} cards := getTreeSubTreeFlashcards(rootID) - blocks, total, pageCount = getCardsBlocks(cards, page) + blocks, total, pageCount = getCardsBlocks(cards, page, pageSize) return } @@ -325,7 +325,7 @@ func getTreeFlashcards(rootID string) (ret []riff.Card) { return } -func GetDeckFlashcards(deckID string, page int) (blocks []*Block, total, pageCount int) { +func GetDeckFlashcards(deckID string, page, pageSize int) (blocks []*Block, total, pageCount int) { blocks = []*Block{} var cards []riff.Card if "" == deckID { @@ -343,11 +343,11 @@ func GetDeckFlashcards(deckID string, page int) (blocks []*Block, total, pageCou cards = append(cards, deck.GetCardsByBlockIDs(blockIDs)...) } - blocks, total, pageCount = getCardsBlocks(cards, page) + blocks, total, pageCount = getCardsBlocks(cards, page, pageSize) return } -func getCardsBlocks(cards []riff.Card, page int) (blocks []*Block, total, pageCount int) { +func getCardsBlocks(cards []riff.Card, page, pageSize int) (blocks []*Block, total, pageCount int) { // sort by due date asc https://github.com/siyuan-note/siyuan/pull/9673 sort.Slice(cards, func(i, j int) bool { due1 := cards[i].(*riff.FSRSCard).C.Due @@ -355,7 +355,6 @@ func getCardsBlocks(cards []riff.Card, page int) (blocks []*Block, total, pageCo return due1.Before(due2) }) - const pageSize = 20 total = len(cards) pageCount = int(math.Ceil(float64(total) / float64(pageSize))) start := (page - 1) * pageSize