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/model"
25 "github.com/siyuan-note/siyuan/kernel/util"
26)
27
28func getRecentDocs(c *gin.Context) {
29 ret := gulu.Ret.NewResult()
30 defer c.JSON(http.StatusOK, ret)
31
32 arg, ok := util.JsonArg(c, ret)
33 if !ok {
34 return
35 }
36
37 // 获取排序参数
38 sortBy := "viewedAt" // 默认按浏览时间排序,openAt:按打开时间排序,closedAt:按关闭时间排序
39 if arg["sortBy"] != nil {
40 sortBy = arg["sortBy"].(string)
41 }
42
43 data, err := model.GetRecentDocs(sortBy)
44 if err != nil {
45 ret.Code = -1
46 ret.Msg = err.Error()
47 return
48 }
49 ret.Data = data
50}
51
52func removeCriterion(c *gin.Context) {
53 ret := gulu.Ret.NewResult()
54 defer c.JSON(http.StatusOK, ret)
55
56 arg, ok := util.JsonArg(c, ret)
57 if !ok {
58 return
59 }
60
61 name := arg["name"].(string)
62 err := model.RemoveCriterion(name)
63 if err != nil {
64 ret.Code = -1
65 ret.Msg = err.Error()
66 return
67 }
68}
69
70func setCriterion(c *gin.Context) {
71 ret := gulu.Ret.NewResult()
72 defer c.JSON(http.StatusOK, ret)
73
74 arg, ok := util.JsonArg(c, ret)
75 if !ok {
76 return
77 }
78
79 param, err := gulu.JSON.MarshalJSON(arg["criterion"])
80 if err != nil {
81 ret.Code = -1
82 ret.Msg = err.Error()
83 return
84 }
85
86 criterion := &model.Criterion{}
87 if err = gulu.JSON.UnmarshalJSON(param, criterion); err != nil {
88 ret.Code = -1
89 ret.Msg = err.Error()
90 return
91 }
92
93 err = model.SetCriterion(criterion)
94 if err != nil {
95 ret.Code = -1
96 ret.Msg = err.Error()
97 return
98 }
99}
100
101func getCriteria(c *gin.Context) {
102 ret := gulu.Ret.NewResult()
103 defer c.JSON(http.StatusOK, ret)
104
105 data := model.GetCriteria()
106 ret.Data = data
107}
108
109func removeLocalStorageVals(c *gin.Context) {
110 ret := gulu.Ret.NewResult()
111 defer c.JSON(http.StatusOK, ret)
112
113 arg, ok := util.JsonArg(c, ret)
114 if !ok {
115 return
116 }
117
118 var keys []string
119 keysArg := arg["keys"].([]interface{})
120 for _, key := range keysArg {
121 keys = append(keys, key.(string))
122 }
123
124 err := model.RemoveLocalStorageVals(keys)
125 if err != nil {
126 ret.Code = -1
127 ret.Msg = err.Error()
128 return
129 }
130
131 app := arg["app"].(string)
132 evt := util.NewCmdResult("removeLocalStorageVals", 0, util.PushModeBroadcastMainExcludeSelfApp)
133 evt.AppId = app
134 evt.Data = map[string]interface{}{"keys": keys}
135 util.PushEvent(evt)
136}
137
138func setLocalStorageVal(c *gin.Context) {
139 ret := gulu.Ret.NewResult()
140 defer c.JSON(http.StatusOK, ret)
141
142 arg, ok := util.JsonArg(c, ret)
143 if !ok {
144 return
145 }
146
147 key := arg["key"].(string)
148 val := arg["val"].(interface{})
149 err := model.SetLocalStorageVal(key, val)
150 if err != nil {
151 ret.Code = -1
152 ret.Msg = err.Error()
153 return
154 }
155
156 app := arg["app"].(string)
157 evt := util.NewCmdResult("setLocalStorageVal", 0, util.PushModeBroadcastMainExcludeSelfApp)
158 evt.AppId = app
159 evt.Data = map[string]interface{}{"key": key, "val": val}
160 util.PushEvent(evt)
161}
162
163func setLocalStorage(c *gin.Context) {
164 ret := gulu.Ret.NewResult()
165 defer c.JSON(http.StatusOK, ret)
166
167 arg, ok := util.JsonArg(c, ret)
168 if !ok {
169 return
170 }
171
172 val := arg["val"].(interface{})
173 err := model.SetLocalStorage(val)
174 if err != nil {
175 ret.Code = -1
176 ret.Msg = err.Error()
177 return
178 }
179
180 app := arg["app"].(string)
181 evt := util.NewCmdResult("setLocalStorage", 0, util.PushModeBroadcastMainExcludeSelfApp)
182 evt.AppId = app
183 evt.Data = val
184 util.PushEvent(evt)
185}
186
187func getLocalStorage(c *gin.Context) {
188 ret := gulu.Ret.NewResult()
189 defer c.JSON(http.StatusOK, ret)
190
191 data := model.GetLocalStorage()
192 ret.Data = data
193}
194
195func getOutlineStorage(c *gin.Context) {
196 ret := gulu.Ret.NewResult()
197 defer c.JSON(http.StatusOK, ret)
198
199 arg, ok := util.JsonArg(c, ret)
200 if !ok {
201 return
202 }
203
204 docID := arg["docID"].(string)
205 data, err := model.GetOutlineStorage(docID)
206 if err != nil {
207 ret.Code = -1
208 ret.Msg = err.Error()
209 return
210 }
211 ret.Data = data
212}
213
214func setOutlineStorage(c *gin.Context) {
215 ret := gulu.Ret.NewResult()
216 defer c.JSON(http.StatusOK, ret)
217
218 arg, ok := util.JsonArg(c, ret)
219 if !ok {
220 return
221 }
222
223 docID := arg["docID"].(string)
224 val := arg["val"].(interface{})
225 err := model.SetOutlineStorage(docID, val)
226 if err != nil {
227 ret.Code = -1
228 ret.Msg = err.Error()
229 return
230 }
231}
232
233func removeOutlineStorage(c *gin.Context) {
234 ret := gulu.Ret.NewResult()
235 defer c.JSON(http.StatusOK, ret)
236
237 arg, ok := util.JsonArg(c, ret)
238 if !ok {
239 return
240 }
241
242 docID := arg["docID"].(string)
243 err := model.RemoveOutlineStorage(docID)
244 if err != nil {
245 ret.Code = -1
246 ret.Msg = err.Error()
247 return
248 }
249}
250
251func updateRecentDocViewTime(c *gin.Context) {
252 ret := gulu.Ret.NewResult()
253 defer c.JSON(http.StatusOK, ret)
254
255 arg, ok := util.JsonArg(c, ret)
256 if !ok {
257 return
258 }
259
260 rootID := arg["rootID"].(string)
261 err := model.UpdateRecentDocViewTime(rootID)
262 if err != nil {
263 ret.Code = -1
264 ret.Msg = err.Error()
265 return
266 }
267}
268
269func updateRecentDocOpenTime(c *gin.Context) {
270 ret := gulu.Ret.NewResult()
271 defer c.JSON(http.StatusOK, ret)
272
273 arg, ok := util.JsonArg(c, ret)
274 if !ok {
275 return
276 }
277
278 rootID := arg["rootID"].(string)
279 err := model.UpdateRecentDocOpenTime(rootID)
280 if err != nil {
281 ret.Code = -1
282 ret.Msg = err.Error()
283 return
284 }
285}
286
287func updateRecentDocCloseTime(c *gin.Context) {
288 ret := gulu.Ret.NewResult()
289 defer c.JSON(http.StatusOK, ret)
290
291 arg, ok := util.JsonArg(c, ret)
292 if !ok {
293 return
294 }
295
296 rootID := arg["rootID"].(string)
297 err := model.UpdateRecentDocCloseTime(rootID)
298 if err != nil {
299 ret.Code = -1
300 ret.Msg = err.Error()
301 return
302 }
303}