An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:recycle: 重构 `文件历史` 查询 https://github.com/siyuan-note/siyuan/issues/6504
Liang Ding24edac891e4c
diff --git a/kernel/api/history.go b/kernel/api/history.goindex b3652d0de2..712d712e3c 100644--- a/kernel/api/history.go+++ b/kernel/api/history.go@@ -61,6 +61,37 @@ func searchHistory(c *gin.Context) {}}+func getHistoryItems(c *gin.Context) {+ ret := gulu.Ret.NewResult()+ defer c.JSON(http.StatusOK, ret)++ arg, ok := util.JsonArg(c, ret)+ if !ok {+ return+ }++ created := arg["created"].(string)++ notebook := ""+ if nil != arg["notebook"] {+ notebook = arg["notebook"].(string)+ }+ typ := model.HistoryTypeDoc+ if nil != arg["type"] {+ typ = int(arg["type"].(float64))+ }++ query := arg["query"].(string)+ op := "all"+ if nil != arg["op"] {+ op = arg["op"].(string)+ }+ histories := model.FullTextSearchHistoryItems(created, query, notebook, op, typ)+ ret.Data = map[string]interface{}{+ "items": histories,+ }+}+func reindexHistory(c *gin.Context) {ret := gulu.Ret.NewResult()defer c.JSON(http.StatusOK, ret)diff --git a/kernel/api/router.go b/kernel/api/router.goindex 351f4f9d38..379fd30273 100644--- a/kernel/api/router.go+++ b/kernel/api/router.go@@ -111,6 +111,7 @@ func ServeAPI(ginServer *gin.Engine) {ginServer.Handle("POST", "/api/history/clearWorkspaceHistory", model.CheckAuth, model.CheckReadonly, clearWorkspaceHistory)ginServer.Handle("POST", "/api/history/reindexHistory", model.CheckAuth, model.CheckReadonly, reindexHistory)ginServer.Handle("POST", "/api/history/searchHistory", model.CheckAuth, model.CheckReadonly, searchHistory)+ ginServer.Handle("POST", "/api/history/getHistoryItems", model.CheckAuth, model.CheckReadonly, getHistoryItems)ginServer.Handle("POST", "/api/outline/getDocOutline", model.CheckAuth, getDocOutline)ginServer.Handle("POST", "/api/bookmark/getBookmark", model.CheckAuth, getBookmark)diff --git a/kernel/model/history.go b/kernel/model/history.goindex 5d5e7dd103..f9271b9a7a 100644--- a/kernel/model/history.go+++ b/kernel/model/history.go@@ -360,6 +360,38 @@ func FullTextSearchHistory(query, box, op string, typ, page int) (ret []string,return}+func FullTextSearchHistoryItems(created, query, box, op string, typ int) (ret []*HistoryItem) {+ query = gulu.Str.RemoveInvisible(query)+ if "" != query {+ query = stringQuery(query)+ }++ table := "histories_fts_case_insensitive"+ stmt := "SELECT * FROM " + table + " WHERE "+ if "" != query {+ stmt += table + " MATCH '{title content}:(" + query + ")'"+ } else {+ stmt += "1=1"+ }++ if HistoryTypeDocName == typ {+ stmt = strings.ReplaceAll(stmt, "{title content}", "{title}")+ }++ if HistoryTypeDocName == typ || HistoryTypeDoc == typ {+ if "all" != op {+ stmt += " AND op = '" + op + "'"+ }+ stmt += " AND path LIKE '%/" + box + "/%' AND path LIKE '%.sy'"+ } else if HistoryTypeAsset == typ {+ stmt += " AND path LIKE '%/assets/%'"+ }+ stmt += " AND created = '" + created + "' ORDER BY created DESC LIMIT " + fmt.Sprintf("%d", Conf.Search.Limit)+ sqlHistories := sql.SelectHistoriesRawStmt(stmt)+ ret = fromSQLHistories(sqlHistories)+ return+}+func GetNotebookHistory() (ret []*History, err error) {ret = []*History{}@@ -656,3 +688,22 @@ func indexHistoryDir(name string, luteEngine *lute.Lute) {}return}++func fromSQLHistories(sqlHistories []*sql.History) (ret []*HistoryItem) {+ if 1 > len(sqlHistories) {+ ret = []*HistoryItem{}+ return+ }++ for _, sqlHistory := range sqlHistories {+ item := &HistoryItem{+ Title: sqlHistory.Title,+ Path: filepath.Join(util.HistoryDir, sqlHistory.Path),+ }+ if HistoryTypeAsset == sqlHistory.Type {+ item.Path = filepath.ToSlash(strings.TrimPrefix(item.Path, util.WorkspaceDir))+ }+ ret = append(ret, item)+ }+ return+}diff --git a/kernel/sql/history.go b/kernel/sql/history.goindex 50e1ec6367..a939a5f537 100644--- a/kernel/sql/history.go+++ b/kernel/sql/history.go@@ -69,6 +69,31 @@ func QueryHistory(stmt string) (ret []map[string]interface{}, err error) {return}+func SelectHistoriesRawStmt(stmt string) (ret []*History) {+ rows, err := historyDB.Query(stmt)+ if nil != err {+ logging.LogWarnf("sql query [%s] failed: %s", stmt, err)+ return+ }+ defer rows.Close()+ for rows.Next() {+ if history := scanHistoryRows(rows); nil != history {+ ret = append(ret, history)+ }+ }+ return+}++func scanHistoryRows(rows *sql.Rows) (ret *History) {+ var history History+ if err := rows.Scan(&history.Type, &history.Op, &history.Title, &history.Content, &history.Path, &history.Created); nil != err {+ logging.LogErrorf("query scan field failed: %s\n%s", err, logging.ShortStack())+ return+ }+ ret = &history+ return+}+func queryHistory(query string, args ...interface{}) (*sql.Rows, error) {query = strings.TrimSpace(query)if "" == query {