diff --git a/kernel/api/riff.go b/kernel/api/riff.go index cdd2d0b99..84f8b6b2c 100644 --- a/kernel/api/riff.go +++ b/kernel/api/riff.go @@ -27,6 +27,16 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) +func getRiffCardNotebooks(c *gin.Context) { + ret := gulu.Ret.NewResult() + defer c.JSON(http.StatusOK, ret) + + notebooks := model.GetFlashcardNotebooks() + ret.Data = map[string]interface{}{ + "notebooks": notebooks, + } +} + func getNotebookRiffCards(c *gin.Context) { ret := gulu.Ret.NewResult() defer c.JSON(http.StatusOK, ret) diff --git a/kernel/model/flashcard.go b/kernel/model/flashcard.go index c230b47d1..6051e6067 100644 --- a/kernel/model/flashcard.go +++ b/kernel/model/flashcard.go @@ -38,8 +38,53 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) -var Decks = map[string]*riff.Deck{} -var deckLock = sync.Mutex{} +func GetFlashcardNotebooks() (ret []*Box) { + deck := Decks[builtinDeckID] + if nil == deck { + return + } + deckBlockIDs := deck.GetBlockIDs() + + boxes := Conf.GetOpenedBoxes() + for _, box := range boxes { + if isNotebookContainFlashcard(box.ID, deckBlockIDs) { + ret = append(ret, box) + } + } + return +} + +func isNotebookContainFlashcard(boxID string, deckBlockIDs []string) (ret bool) { + entries, err := os.ReadDir(filepath.Join(util.DataDir, boxID)) + if nil != err { + logging.LogErrorf("read dir failed: %s", err) + return + } + + for _, entry := range entries { + if entry.IsDir() { + continue + } + + if !strings.HasSuffix(entry.Name(), ".sy") { + continue + } + + rootID := strings.TrimSuffix(entry.Name(), ".sy") + blockIDs := getTreeSubTreeChildBlocks(rootID) + for _, blockID := range deckBlockIDs { + if gulu.Str.Contains(blockID, blockIDs) { + return true + } + } + } + return +} + +var ( + Decks = map[string]*riff.Deck{} + deckLock = sync.Mutex{} +) func GetNotebookFlashcards(boxID string, page int) (blocks []*Block, total, pageCount int) { blocks = []*Block{} @@ -66,9 +111,7 @@ func GetNotebookFlashcards(boxID string, page int) (blocks []*Block, total, page var treeBlockIDs []string for _, rootID := range rootIDs { blockIDs := getTreeSubTreeChildBlocks(rootID) - for _, blockID := range blockIDs { - treeBlockIDs = append(treeBlockIDs, blockID) - } + treeBlockIDs = append(treeBlockIDs, blockIDs...) } treeBlockIDs = gulu.Str.RemoveDuplicatedElem(treeBlockIDs) @@ -307,9 +350,7 @@ func GetNotebookDueFlashcards(boxID string, reviewedCardIDs []string) (ret []*Fl var treeBlockIDs []string for _, rootID := range rootIDs { blockIDs := getTreeSubTreeChildBlocks(rootID) - for _, blockID := range blockIDs { - treeBlockIDs = append(treeBlockIDs, blockID) - } + treeBlockIDs = append(treeBlockIDs, blockIDs...) } treeBlockIDs = gulu.Str.RemoveDuplicatedElem(treeBlockIDs)