An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Improve flashcard transaction validation https://github.com/siyuan-note/siyuan/issues/18740
Danielf2800e2da993
diff --git a/kernel/model/flashcard.go b/kernel/model/flashcard.goindex 309c91629c..1f09c6f946 100644--- a/kernel/model/flashcard.go+++ b/kernel/model/flashcard.go@@ -42,8 +42,16 @@ import (// ValidateFlashcardBlockIDs 只允许普通笔记本中可确认存在的块进入全局闪卡存储。func ValidateFlashcardBlockIDs(blockIDs []string) error {+ return validateFlashcardBlockIDs(blockIDs, treenode.GetBlockTree, IsEncryptedBox)+}++func validateFlashcardBlockIDs(blockIDs []string, getBlockTree func(string) *treenode.BlockTree, isEncryptedBox func(string) bool) error {for _, blockID := range blockIDs {- if !isSupportedFlashcardBlock(blockID) {+ bt := getBlockTree(blockID)+ if bt == nil {+ return errors.New(Conf.Language(180))+ }+ if isEncryptedBox(bt.BoxID) {return errors.New(Conf.Language(313))}}@@ -94,17 +102,34 @@ func validateFlashcardCard(deck *riff.Deck, cardID string) error {// ValidateFlashcardTransactions 在事务入队前校验所有闪卡操作。func ValidateFlashcardTransactions(transactions []*Transaction) error {+ return ValidateFlashcardBlockIDs(flashcardBlockIDsToValidate(transactions))+}++func flashcardBlockIDsToValidate(transactions []*Transaction) (ret []string) {for _, transaction := range transactions {+ if transaction == nil {+ continue+ }+ insertedBlockIDs := map[string]struct{}{}for _, operation := range transaction.DoOperations {+ if operation == nil {+ continue+ }+ if operation.Action == "insert" && ast.IsNodeIDPattern(operation.ID) {+ insertedBlockIDs[operation.ID] = struct{}{}+ }if operation.Action != "addFlashcards" && operation.Action != "removeFlashcards" {continue}- if err := ValidateFlashcardBlockIDs(operation.BlockIDs); err != nil {- return err+ for _, blockID := range operation.BlockIDs {+ if _, ok := insertedBlockIDs[blockID]; ok {+ continue+ }+ ret = append(ret, blockID)}}}- return nil+ return}func GetFlashcardsByBlockIDs(blockIDs []string) (ret []*Block) {diff --git a/kernel/model/flashcard_validation_test.go b/kernel/model/flashcard_validation_test.gonew file mode 100644index 0000000000..a18e860a0b--- /dev/null+++ b/kernel/model/flashcard_validation_test.go@@ -0,0 +1,112 @@+// SiYuan - From thought to insight, with agents+// 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 (+ "slices"+ "testing"++ "github.com/siyuan-note/siyuan/kernel/treenode"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestValidateFlashcardBlockIDsReportsSpecificFailure(t *testing.T) {+ oldConf, oldLangs := Conf, util.Langs+ Conf = &AppConf{Lang: "en"}+ util.Langs = map[string]map[int]string{+ "en": {+ 180: "block does not exist",+ 313: "encrypted notebook is unsupported",+ },+ }+ t.Cleanup(func() {+ Conf, util.Langs = oldConf, oldLangs+ })++ blocks := map[string]*treenode.BlockTree{+ "ordinary": {ID: "ordinary", BoxID: "ordinary-box"},+ "encrypted": {ID: "encrypted", BoxID: "encrypted-box"},+ }+ getBlockTree := func(id string) *treenode.BlockTree {+ return blocks[id]+ }+ isEncryptedBox := func(boxID string) bool {+ return boxID == "encrypted-box"+ }++ if err := validateFlashcardBlockIDs([]string{"ordinary"}, getBlockTree, isEncryptedBox); err != nil {+ t.Fatalf("ordinary block should be supported: %s", err)+ }+ if err := validateFlashcardBlockIDs([]string{"missing"}, getBlockTree, isEncryptedBox); err == nil || err.Error() != "block does not exist" {+ t.Fatalf("missing block should report the missing-block message, got %v", err)+ }+ if err := validateFlashcardBlockIDs([]string{"encrypted"}, getBlockTree, isEncryptedBox); err == nil || err.Error() != "encrypted notebook is unsupported" {+ t.Fatalf("encrypted block should report the encrypted-notebook message, got %v", err)+ }+}++func TestFlashcardBlockIDsToValidateAllowsEarlierInsert(t *testing.T) {+ const (+ insertedID = "20260814000000-insert1"+ existingID = "20260814000001-exists1"+ )+ transactions := []*Transaction{+ {+ DoOperations: []*Operation{+ {Action: "insert", ID: insertedID},+ {Action: "addFlashcards", BlockIDs: []string{insertedID, existingID}},+ },+ },+ }++ if got := flashcardBlockIDsToValidate(transactions); !slices.Equal(got, []string{existingID}) {+ t.Fatalf("only blocks not inserted earlier should be prevalidated, got %v", got)+ }+ transactions[0].DoOperations[1].BlockIDs = []string{insertedID}+ if err := ValidateFlashcardTransactions(transactions); err != nil {+ t.Fatalf("a flashcard block inserted earlier in the transaction should pass prevalidation: %s", err)+ }+}++func TestFlashcardBlockIDsToValidatePreservesOperationOrder(t *testing.T) {+ const insertedID = "20260814000000-insert1"+ transactions := []*Transaction{+ {+ DoOperations: []*Operation{+ {Action: "addFlashcards", BlockIDs: []string{insertedID}},+ {Action: "insert", ID: insertedID},+ },+ },+ }++ if got := flashcardBlockIDsToValidate(transactions); !slices.Equal(got, []string{insertedID}) {+ t.Fatalf("blocks inserted later should still be prevalidated, got %v", got)+ }+}++func TestFlashcardBlockIDsToValidateScopesInsertsToTransaction(t *testing.T) {+ const insertedID = "20260814000000-insert1"+ transactions := []*Transaction{+ {DoOperations: []*Operation{{Action: "insert", ID: insertedID}}},+ nil,+ {DoOperations: []*Operation{nil, {Action: "addFlashcards", BlockIDs: []string{insertedID}}}},+ }++ if got := flashcardBlockIDsToValidate(transactions); !slices.Equal(got, []string{insertedID}) {+ t.Fatalf("an insert from another transaction should not bypass validation, got %v", got)+ }+}