A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
1// SiYuan - Refactor your thinking
2// Copyright (c) 2020-present, b3log.org
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Affero General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU Affero General Public License for more details.
13//
14// You should have received a copy of the GNU Affero General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17package api
18
19import (
20 "net/http"
21
22 "github.com/88250/gulu"
23 "github.com/gin-gonic/gin"
24 "github.com/siyuan-note/siyuan/kernel/conf"
25 "github.com/siyuan-note/siyuan/kernel/model"
26 "github.com/siyuan-note/siyuan/kernel/util"
27)
28
29func resetGraph(c *gin.Context) {
30 ret := gulu.Ret.NewResult()
31 defer c.JSON(http.StatusOK, ret)
32
33 graph := conf.NewGlobalGraph()
34 model.Conf.Graph.Global = graph
35 model.Conf.Save()
36 ret.Data = map[string]interface{}{
37 "conf": graph,
38 }
39}
40
41func resetLocalGraph(c *gin.Context) {
42 ret := gulu.Ret.NewResult()
43 defer c.JSON(http.StatusOK, ret)
44
45 graph := conf.NewLocalGraph()
46 model.Conf.Graph.Local = graph
47 model.Conf.Save()
48 ret.Data = map[string]interface{}{
49 "conf": graph,
50 }
51}
52
53func getGraph(c *gin.Context) {
54 ret := gulu.Ret.NewResult()
55 defer c.JSON(http.StatusOK, ret)
56
57 arg, ok := util.JsonArg(c, ret)
58 if !ok {
59 return
60 }
61
62 reqId := arg["reqId"]
63 ret.Data = map[string]interface{}{"reqId": reqId}
64
65 query := arg["k"].(string)
66 graphConf, err := gulu.JSON.MarshalJSON(arg["conf"])
67 if err != nil {
68 ret.Code = -1
69 ret.Msg = err.Error()
70 return
71 }
72
73 global := conf.NewGlobalGraph()
74 if err = gulu.JSON.UnmarshalJSON(graphConf, global); err != nil {
75 ret.Code = -1
76 ret.Msg = err.Error()
77 return
78 }
79
80 model.Conf.Graph.Global = global
81 model.Conf.Save()
82
83 boxID, nodes, links := model.BuildGraph(query)
84 ret.Data = map[string]interface{}{
85 "nodes": nodes,
86 "links": links,
87 "conf": global,
88 "box": boxID,
89 "reqId": arg["reqId"],
90 }
91 util.RandomSleep(200, 500)
92}
93
94func getLocalGraph(c *gin.Context) {
95 ret := gulu.Ret.NewResult()
96 defer c.JSON(http.StatusOK, ret)
97
98 arg, ok := util.JsonArg(c, ret)
99 if !ok {
100 return
101 }
102
103 reqId := arg["reqId"]
104 ret.Data = map[string]interface{}{"reqId": reqId}
105 if nil == arg["id"] {
106 return
107 }
108
109 keyword := arg["k"].(string)
110 id := arg["id"].(string)
111
112 graphConf, err := gulu.JSON.MarshalJSON(arg["conf"])
113 if err != nil {
114 ret.Code = -1
115 ret.Msg = err.Error()
116 return
117 }
118
119 local := conf.NewLocalGraph()
120 if err = gulu.JSON.UnmarshalJSON(graphConf, local); err != nil {
121 ret.Code = -1
122 ret.Msg = err.Error()
123 return
124 }
125
126 model.Conf.Graph.Local = local
127 model.Conf.Save()
128
129 boxID, nodes, links := model.BuildTreeGraph(id, keyword)
130 ret.Data = map[string]interface{}{
131 "id": id,
132 "box": boxID,
133 "nodes": nodes,
134 "links": links,
135 "conf": local,
136 "reqId": arg["reqId"],
137 }
138 util.RandomSleep(200, 500)
139}