瀏覽代碼

:art: 支持间隔复习 https://github.com/siyuan-note/siyuan/issues/6710

Liang Ding 2 年之前
父節點
當前提交
1c5eccd510
共有 5 個文件被更改,包括 107 次插入0 次删除
  1. 2 0
      kernel/go.mod
  2. 4 0
      kernel/go.sum
  3. 1 0
      kernel/main.go
  4. 1 0
      kernel/mobile/kernel.go
  5. 99 0
      kernel/model/flashcard.go

+ 2 - 0
kernel/go.mod

@@ -45,6 +45,7 @@ require (
 	github.com/siyuan-note/filelock v0.0.0-20221117095924-e1947438a35e
 	github.com/siyuan-note/httpclient v0.0.0-20221213030227-fa8d21fd9cf8
 	github.com/siyuan-note/logging v0.0.0-20221031125421-9b7234d79d8a
+	github.com/siyuan-note/riff v0.0.0-20221221064021-0e7597311cb0
 	github.com/steambap/captcha v1.4.1
 	github.com/studio-b12/gowebdav v0.0.0-20221109171924-60ec5ad56012
 	github.com/vmihailenco/msgpack/v5 v5.3.5
@@ -105,6 +106,7 @@ require (
 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 	github.com/modern-go/reflect2 v1.0.2 // indirect
 	github.com/onsi/ginkgo/v2 v2.6.1 // indirect
+	github.com/open-spaced-repetition/go-fsrs v0.1.0 // indirect
 	github.com/pelletier/go-toml/v2 v2.0.6 // indirect
 	github.com/pkg/errors v0.9.1 // indirect
 	github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect

+ 4 - 0
kernel/go.sum

@@ -303,6 +303,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
 github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
 github.com/onsi/gomega v1.13.0/go.mod h1:lRk9szgn8TxENtWd0Tp4c3wjlRfMTMH27I+3Je41yGY=
 github.com/onsi/gomega v1.24.1 h1:KORJXNNTzJXzu4ScJWssJfJMnJ+2QJqhoQSRwNlze9E=
+github.com/open-spaced-repetition/go-fsrs v0.1.0 h1:6H1nCuxuR9p/GmKji0zET1uT5KDwOmW++k7jgr8L0Gk=
+github.com/open-spaced-repetition/go-fsrs v0.1.0/go.mod h1:H07GOB0A1OBeu3401x8qWKGaa43QjfrDoWy9nba7QCc=
 github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
 github.com/panjf2000/ants/v2 v2.7.1 h1:qBy5lfSdbxvrR0yUnZfaEDjf0FlCw4ufsbcsxmE7r+M=
 github.com/panjf2000/ants/v2 v2.7.1/go.mod h1:KIBmYG9QQX5U2qzFP/yQJaq/nSb6rahS9iEHkrCMgM8=
@@ -383,6 +385,8 @@ github.com/siyuan-note/httpclient v0.0.0-20221213030227-fa8d21fd9cf8 h1:cv0U38yu
 github.com/siyuan-note/httpclient v0.0.0-20221213030227-fa8d21fd9cf8/go.mod h1:aLj3LQVz2iuA3AUfkqk1UvPhqafRpthAVtjV1+ZAq+U=
 github.com/siyuan-note/logging v0.0.0-20221031125421-9b7234d79d8a h1:b9VJCE8IccYjsadwNBI11he+Wn25hI9lCma4uYoIYEM=
 github.com/siyuan-note/logging v0.0.0-20221031125421-9b7234d79d8a/go.mod h1:t1zRGxK13L/9ZFoGyTD39IbFCbee3CsypDj4b5dt4qM=
+github.com/siyuan-note/riff v0.0.0-20221221064021-0e7597311cb0 h1:i2vxI2USCiNqyExpsy96TuvTO6Yhk7s+4hrPJ5MDOhI=
+github.com/siyuan-note/riff v0.0.0-20221221064021-0e7597311cb0/go.mod h1:A3G3qOTyGVOu17ui9Qg9DNkAWOgwVMxHDzHYG8sQxHc=
 github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
 github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
 github.com/smartystreets/goconvey v1.6.7 h1:I6tZjLXD2Q1kjvNbIzB1wvQBsXmKXiVrhpRE8ZjP5jY=

+ 1 - 0
kernel/main.go

@@ -39,6 +39,7 @@ func main() {
 
 	model.BootSyncData()
 	model.InitBoxes()
+	model.InitFlashcards()
 
 	go model.AutoGenerateDocHistory()
 	go model.AutoSync()

+ 1 - 0
kernel/mobile/kernel.go

@@ -53,6 +53,7 @@ func StartKernel(container, appDir, workspaceDir, nativeLibDir, privateDataDir,
 
 		model.BootSyncData()
 		model.InitBoxes()
+		model.InitFlashcards()
 
 		go model.AutoGenerateDocHistory()
 		go model.AutoSync()

+ 99 - 0
kernel/model/flashcard.go

@@ -0,0 +1,99 @@
+// SiYuan - Build Your Eternal Digital Garden
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+	"github.com/88250/lute/ast"
+	"github.com/siyuan-note/logging"
+	"github.com/siyuan-note/riff"
+	"github.com/siyuan-note/siyuan/kernel/util"
+	"os"
+	"path/filepath"
+	"strings"
+	"sync"
+)
+
+var Decks = map[string]*riff.Deck{}
+var deckLock = sync.Mutex{}
+
+func AddFlashcard(blockID string, deckName string) (err error) {
+	deckLock.Lock()
+	deck := Decks[deckName]
+	deckLock.Unlock()
+
+	cardID := ast.NewNodeID()
+	deck.AddCard(cardID, blockID)
+	err = deck.Save()
+	if nil != err {
+		logging.LogErrorf("save deck [%s] failed: %s", deckName, err)
+		return
+	}
+	return
+}
+
+func InitFlashcards() {
+	riffSavePath := filepath.Join(util.DataDir, "storage", "riff")
+	entries, err := os.ReadDir(riffSavePath)
+	if nil != err {
+		logging.LogErrorf("read riff dir failed: %s", err)
+		return
+	}
+	for _, entry := range entries {
+		name := entry.Name()
+		if strings.HasSuffix(name, "-deck.msgpack") {
+			name = name[:len(name)-len("-deck.msgpack")]
+			deckName := strings.Split(name, "-")[0]
+			algo := strings.Split(name, "-")[1]
+			deck, loadErr := riff.LoadDeck(riffSavePath, deckName, riff.Algo(algo))
+			if nil != loadErr {
+				logging.LogErrorf("load deck [%s] failed: %s", name, loadErr)
+				continue
+			}
+
+			deckLock.Lock()
+			Decks[deckName] = deck
+			deckLock.Unlock()
+		}
+	}
+}
+
+func CreateDeck(name string) (err error) {
+	riffSavePath := filepath.Join(util.DataDir, "storage", "riff")
+	deck, err := riff.LoadDeck(riffSavePath, name, riff.AlgoFSRS)
+	if nil != err {
+		logging.LogErrorf("load deck [%s] failed: %s", name, err)
+		return
+	}
+
+	deckLock.Lock()
+	Decks[name] = deck
+	deckLock.Unlock()
+	return
+}
+
+func SaveDeck(name string) (err error) {
+	deckLock.Lock()
+	deck := Decks[name]
+	deckLock.Unlock()
+
+	err = deck.Save()
+	if nil != err {
+		logging.LogErrorf("save deck [%s] failed: %s", name, err)
+		return
+	}
+	return
+}