From 1c5eccd51074e5516d59e1f94eac160c9ba067d6 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 21 Dec 2022 14:45:43 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=94=AF=E6=8C=81=E9=97=B4=E9=9A=94?= =?UTF-8?q?=E5=A4=8D=E4=B9=A0=20https://github.com/siyuan-note/siyuan/issu?= =?UTF-8?q?es/6710?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/go.mod | 2 + kernel/go.sum | 4 ++ kernel/main.go | 1 + kernel/mobile/kernel.go | 1 + kernel/model/flashcard.go | 99 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 kernel/model/flashcard.go diff --git a/kernel/go.mod b/kernel/go.mod index deeaf01d5..f644c60b8 100644 --- a/kernel/go.mod +++ b/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 diff --git a/kernel/go.sum b/kernel/go.sum index 033957999..b74ea762a 100644 --- a/kernel/go.sum +++ b/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= diff --git a/kernel/main.go b/kernel/main.go index 9946462d4..2dd9c9c3b 100644 --- a/kernel/main.go +++ b/kernel/main.go @@ -39,6 +39,7 @@ func main() { model.BootSyncData() model.InitBoxes() + model.InitFlashcards() go model.AutoGenerateDocHistory() go model.AutoSync() diff --git a/kernel/mobile/kernel.go b/kernel/mobile/kernel.go index 4ab1d32e5..221b79383 100644 --- a/kernel/mobile/kernel.go +++ b/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() diff --git a/kernel/model/flashcard.go b/kernel/model/flashcard.go new file mode 100644 index 000000000..1a6d3059a --- /dev/null +++ b/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 . + +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 +}