This commit is contained in:
Liang Ding 2023-02-27 09:46:39 +08:00
parent 990b31dc00
commit 73ec3714cc
No known key found for this signature in database
GPG Key ID: 136F30F901A2231D
3 changed files with 96 additions and 33 deletions

View File

@ -85,6 +85,26 @@ func reviewRiffCard(c *gin.Context) {
}
}
func getNotebookRiffDueCards(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
arg, ok := util.JsonArg(c, ret)
if !ok {
return
}
notebookID := arg["notebook"].(string)
cards, err := model.GetNotebookDueFlashcards(notebookID)
if nil != err {
ret.Code = -1
ret.Msg = err.Error()
return
}
ret.Data = cards
}
func getTreeRiffDueCards(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)

View File

@ -312,6 +312,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/riff/removeRiffCards", model.CheckAuth, removeRiffCards)
ginServer.Handle("POST", "/api/riff/getRiffDueCards", model.CheckAuth, getRiffDueCards)
ginServer.Handle("POST", "/api/riff/getTreeRiffDueCards", model.CheckAuth, getTreeRiffDueCards)
ginServer.Handle("POST", "/api/riff/getNotebookRiffDueCards", model.CheckAuth, getNotebookRiffDueCards)
ginServer.Handle("POST", "/api/riff/reviewRiffCard", model.CheckAuth, reviewRiffCard)
ginServer.Handle("POST", "/api/riff/getRiffCards", model.CheckAuth, getRiffCards)
ginServer.Handle("POST", "/api/riff/getTreeRiffCards", model.CheckAuth, getTreeRiffCards)

View File

@ -180,6 +180,78 @@ type Flashcard struct {
NextDues map[riff.Rating]string `json:"nextDues"`
}
func newFlashcard(card riff.Card, blockID, deckID string, now time.Time) *Flashcard {
nextDues := map[riff.Rating]string{}
for rating, due := range card.NextDues() {
nextDues[rating] = strings.TrimSpace(util.HumanizeRelTime(due, now, Conf.Lang))
}
return &Flashcard{
DeckID: deckID,
CardID: card.ID(),
BlockID: blockID,
NextDues: nextDues,
}
}
func GetNotebookDueFlashcards(boxID string) (ret []*Flashcard, err error) {
deckLock.Lock()
defer deckLock.Unlock()
if syncingStorages {
err = errors.New(Conf.Language(81))
return
}
entries, err := os.ReadDir(filepath.Join(util.DataDir, boxID))
if nil != err {
logging.LogErrorf("read dir failed: %s", err)
return
}
var rootIDs []string
for _, entry := range entries {
if entry.IsDir() {
continue
}
if !strings.HasSuffix(entry.Name(), ".sy") {
continue
}
rootIDs = append(rootIDs, strings.TrimSuffix(entry.Name(), ".sy"))
}
treeBlockIDs := map[string]bool{}
for _, rootID := range rootIDs {
blockIDs := getTreeSubTreeChildBlocks(rootID)
for blockID, _ := range blockIDs {
treeBlockIDs[blockID] = true
}
}
deck := Decks[builtinDeckID]
if nil == deck {
logging.LogWarnf("builtin deck not found")
return
}
cards := deck.Dues()
now := time.Now()
for _, card := range cards {
blockID := card.BlockID()
if !treeBlockIDs[blockID] {
continue
}
ret = append(ret, newFlashcard(card, blockID, builtinDeckID, now))
}
if 1 > len(ret) {
ret = []*Flashcard{}
}
return
}
func GetTreeDueFlashcards(rootID string) (ret []*Flashcard, err error) {
deckLock.Lock()
defer deckLock.Unlock()
@ -204,17 +276,7 @@ func GetTreeDueFlashcards(rootID string) (ret []*Flashcard, err error) {
continue
}
nextDues := map[riff.Rating]string{}
for rating, due := range card.NextDues() {
nextDues[rating] = strings.TrimSpace(util.HumanizeRelTime(due, now, Conf.Lang))
}
ret = append(ret, &Flashcard{
DeckID: builtinDeckID,
CardID: card.ID(),
BlockID: blockID,
NextDues: nextDues,
})
ret = append(ret, newFlashcard(card, blockID, builtinDeckID, now))
}
if 1 > len(ret) {
ret = []*Flashcard{}
@ -294,17 +356,7 @@ func getDueFlashcards(deckID string) (ret []*Flashcard) {
continue
}
nextDues := map[riff.Rating]string{}
for rating, due := range card.NextDues() {
nextDues[rating] = strings.TrimSpace(util.HumanizeRelTime(due, now, Conf.Lang))
}
ret = append(ret, &Flashcard{
DeckID: deckID,
CardID: card.ID(),
BlockID: blockID,
NextDues: nextDues,
})
ret = append(ret, newFlashcard(card, blockID, deckID, now))
}
if 1 > len(ret) {
ret = []*Flashcard{}
@ -322,17 +374,7 @@ func getAllDueFlashcards() (ret []*Flashcard) {
continue
}
nextDues := map[riff.Rating]string{}
for rating, due := range card.NextDues() {
nextDues[rating] = strings.TrimSpace(util.HumanizeRelTime(due, now, Conf.Lang))
}
ret = append(ret, &Flashcard{
DeckID: deck.ID,
CardID: card.ID(),
BlockID: blockID,
NextDues: nextDues,
})
ret = append(ret, newFlashcard(card, blockID, deck.ID, now))
}
}
if 1 > len(ret) {