Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

6.4 KB
Raw Blame
// SiYuan - https://github.com/siyuan-note/siyuan
// Copyright (c) 2020-present, b3log.org
// SPDX-License-Identifier: AGPL-3.0-or-later

package model

import (
	"path"
	"sort"
	"strings"

	"github.com/88250/lute/ast"
	"github.com/88250/lute/parse"
	"github.com/siyuan-note/siyuan/kernel/av"
	"github.com/siyuan-note/siyuan/kernel/sql"
	"github.com/siyuan-note/siyuan/kernel/task"
	"github.com/siyuan-note/siyuan/kernel/treenode"
	"github.com/siyuan-note/siyuan/kernel/util"
)

func queueAttributeViewRefIndex(avID, avBoxID string) {
	if "" == avID {
		return
	}
	attrView, err := av.ParseAttributeViewForIndexInBox(avID, avBoxID)
	if nil != err || nil == attrView {
		return
	}
	var definitions []string
	if av.RichTextSpec <= attrView.Spec {
		definitions = attributeViewRichTextRefDefIDs(attrView)
	}
	// 普通字段同样影响反链数据库筛选,通过载体索引提交通知现有面板刷新。
	queueAttributeViewRefCarrierIndex(avID, avBoxID, definitions)
}

// queueExternalAttributeViewRefIndex 用于同步、历史和快照直接替换 AV 文件后的引用重建。
// 这里不能按当前规范版本跳过,因为旧的富文本文件可能被纯文本文件替换,需要删除既有引用。
func queueExternalAttributeViewRefIndex(avID, avBoxID string) {
	if "" == avID || ("" != avBoxID && !IsEncryptedBox(avBoxID)) {
		return
	}
	var newDefIDs []string
	if attrView, err := av.ParseAttributeViewForIndexInBox(avID, avBoxID); nil == err && nil != attrView &&
		av.RichTextSpec <= attrView.Spec {
		newDefIDs = attributeViewRichTextRefDefIDs(attrView)
	}
	queueAttributeViewRefCarrierIndex(avID, avBoxID, newDefIDs)
}

func queueAttributeViewRefCarrierIndex(avID, avBoxID string, newDefIDs []string) {
	trees := attributeViewRefCarrierTrees(avID, avBoxID)
	if 0 == len(trees) {
		return
	}
	carrierBlockIDs := attributeViewRefCarrierBlockIDs(trees, avID)
	oldDefIDs := sql.QueryAttributeViewRefDefIDsByBlockIDsInBox(carrierBlockIDs,
		attributeViewRefQueryBoxID(avBoxID))
	for _, tree := range trees {
		sql.UpdateRefsTreeQueue(tree)
	}
	for _, defID := range changedAttributeViewRefDefIDs(oldDefIDs, newDefIDs) {
		task.AppendAsyncTaskWithDelay(task.SetDefRefCount, util.SQLFlushInterval, refreshRefCount, defID)
	}
}

func attributeViewRefCarrierBlockIDs(trees []*parse.Tree, avID string) (ret []string) {
	for _, tree := range trees {
		if nil == tree || nil == tree.Root {
			continue
		}
		ast.Walk(tree.Root, func(node *ast.Node, entering bool) ast.WalkStatus {
			if entering && ast.NodeAttributeView == node.Type && avID == node.AttributeViewID && "" != node.ID {
				ret = append(ret, node.ID)
			}
			return ast.WalkContinue
		})
	}
	return
}

func attributeViewRichTextRefDefIDs(attrView *av.AttributeView) (ret []string) {
	seen := map[string]struct{}{}
	if nil == attrView {
		return
	}
	for _, keyValues := range attrView.KeyValues {
		if nil == keyValues || nil == keyValues.Key || av.KeyTypeText != keyValues.Key.Type {
			continue
		}
		for _, value := range keyValues.Values {
			if nil == value || nil == value.Text || !value.Text.IsRich() || "" == value.Text.Rich.Content {
				continue
			}
			fragmentTree, err := av.ParseValueTextRich(value.Text.Rich)
			if nil != err || nil == fragmentTree || nil == fragmentTree.Root {
				continue
			}
			for _, defID := range getRefDefIDs(fragmentTree.Root) {
				if "" == defID {
					continue
				}
				if _, ok := seen[defID]; ok {
					continue
				}
				seen[defID] = struct{}{}
				ret = append(ret, defID)
			}
		}
	}
	sort.Strings(ret)
	return
}

func changedAttributeViewRefDefIDs(oldDefIDs, newDefIDs []string) (ret []string) {
	oldSet := map[string]struct{}{}
	newSet := map[string]struct{}{}
	for _, defID := range oldDefIDs {
		if "" != defID {
			oldSet[defID] = struct{}{}
		}
	}
	for _, defID := range newDefIDs {
		if "" != defID {
			newSet[defID] = struct{}{}
		}
	}
	for defID := range oldSet {
		if _, ok := newSet[defID]; !ok {
			ret = append(ret, defID)
		}
	}
	for defID := range newSet {
		if _, ok := oldSet[defID]; !ok {
			ret = append(ret, defID)
		}
	}
	sort.Strings(ret)
	return
}

func queueExternalAttributeViewRefIndexByRepoPath(repoPath string) {
	avID, avBoxID, ok := parseAttributeViewRefRepoPath(repoPath)
	if !ok {
		return
	}
	queueExternalAttributeViewRefIndex(avID, avBoxID)
}

func parseAttributeViewRefRepoPath(repoPath string) (avID, avBoxID string, ok bool) {
	repoPath = "/" + strings.TrimPrefix(path.Clean(strings.ReplaceAll(repoPath, "\\", "/")), "/")
	parts := strings.Split(strings.TrimPrefix(repoPath, "/"), "/")
	switch {
	case 3 == len(parts) && "storage" == parts[0] && "av" == parts[1]:
	case 4 == len(parts) && ast.IsNodeIDPattern(parts[0]) && "storage" == parts[1] && "av" == parts[2]:
		avBoxID = parts[0]
	default:
		return "", "", false
	}
	filename := parts[len(parts)-1]
	if ".json" != path.Ext(filename) {
		return "", "", false
	}
	avID = strings.TrimSuffix(filename, ".json")
	if !ast.IsNodeIDPattern(avID) {
		return "", "", false
	}
	return avID, avBoxID, true
}

func attributeViewRefCarrierTrees(avID, avBoxID string) (ret []*parse.Tree) {
	queuedRootIDs := map[string]bool{}
	for _, blockID := range treenode.GetMirrorAttrViewBlockIDs(avID) {
		blockTree := attributeViewCarrierBlockTree(blockID, avBoxID)
		if nil == blockTree || !sameAttributeViewRefBoundary(avBoxID, blockTree.BoxID) || queuedRootIDs[blockTree.RootID] {
			continue
		}
		tree, err := LoadTreeByBlockIDInExactBox(blockID, attributeViewRefQueryBoxID(avBoxID))
		if nil != err || nil == tree {
			continue
		}
		databaseNode := treenode.GetNodeInTree(tree, blockID)
		if nil == databaseNode || ast.NodeAttributeView != databaseNode.Type || avID != databaseNode.AttributeViewID {
			continue
		}
		queuedRootIDs[tree.ID] = true
		ret = append(ret, tree)
	}
	return
}

func attributeViewCarrierBlockTree(blockID, avBoxID string) *treenode.BlockTree {
	if "" != avBoxID && IsEncryptedBox(avBoxID) {
		return treenode.GetBlockTreeInBox(blockID, avBoxID)
	}
	return treenode.GetBlockTreeInExactBox(blockID, "")
}

func attributeViewRefQueryBoxID(avBoxID string) string {
	if "" != avBoxID && IsEncryptedBox(avBoxID) {
		return avBoxID
	}
	return ""
}

func sameAttributeViewRefBoundary(avBoxID, carrierBoxID string) bool {
	avEncrypted := "" != avBoxID && IsEncryptedBox(avBoxID)
	carrierEncrypted := "" != carrierBoxID && IsEncryptedBox(carrierBoxID)
	if !avEncrypted && !carrierEncrypted {
		return true
	}
	return avEncrypted && carrierEncrypted && avBoxID == carrierBoxID
}