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 "strconv"
22
23 "github.com/88250/gulu"
24 "github.com/gin-gonic/gin"
25 "github.com/siyuan-note/siyuan/kernel/model"
26 "github.com/siyuan-note/siyuan/kernel/util"
27)
28
29func refreshBacklink(c *gin.Context) {
30 ret := gulu.Ret.NewResult()
31 defer c.JSON(http.StatusOK, ret)
32
33 arg, ok := util.JsonArg(c, ret)
34 if !ok {
35 return
36 }
37
38 id := arg["id"].(string)
39 model.RefreshBacklink(id)
40 model.FlushTxQueue()
41}
42
43func getBackmentionDoc(c *gin.Context) {
44 ret := gulu.Ret.NewResult()
45 defer c.JSON(http.StatusOK, ret)
46
47 arg, ok := util.JsonArg(c, ret)
48 if !ok {
49 return
50 }
51
52 defID := arg["defID"].(string)
53 refTreeID := arg["refTreeID"].(string)
54 keyword := arg["keyword"].(string)
55 containChildren := model.Conf.Editor.BacklinkContainChildren
56 if val, ok := arg["containChildren"]; ok {
57 containChildren = val.(bool)
58 }
59 highlight := true
60 if val, ok := arg["highlight"]; ok {
61 highlight = val.(bool)
62 }
63 backlinks, keywords := model.GetBackmentionDoc(defID, refTreeID, keyword, containChildren, highlight)
64 ret.Data = map[string]interface{}{
65 "backmentions": backlinks,
66 "keywords": keywords,
67 }
68}
69
70func getBacklinkDoc(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 defID := arg["defID"].(string)
80 refTreeID := arg["refTreeID"].(string)
81 keyword := arg["keyword"].(string)
82 containChildren := model.Conf.Editor.BacklinkContainChildren
83 if val, ok := arg["containChildren"]; ok {
84 containChildren = val.(bool)
85 }
86 highlight := true
87 if val, ok := arg["highlight"]; ok {
88 highlight = val.(bool)
89 }
90 backlinks, keywords := model.GetBacklinkDoc(defID, refTreeID, keyword, containChildren, highlight)
91 ret.Data = map[string]interface{}{
92 "backlinks": backlinks,
93 "keywords": keywords,
94 }
95}
96
97func getBacklink2(c *gin.Context) {
98 ret := gulu.Ret.NewResult()
99 defer c.JSON(http.StatusOK, ret)
100
101 arg, ok := util.JsonArg(c, ret)
102 if !ok {
103 return
104 }
105
106 if nil == arg["id"] {
107 return
108 }
109
110 id := arg["id"].(string)
111 keyword := arg["k"].(string)
112 mentionKeyword := arg["mk"].(string)
113 sortArg := arg["sort"]
114 sort := util.SortModeUpdatedDESC
115 if nil != sortArg {
116 sort, _ = strconv.Atoi(sortArg.(string))
117 }
118 mentionSortArg := arg["mSort"]
119 mentionSort := util.SortModeUpdatedDESC
120 if nil != mentionSortArg {
121 mentionSort, _ = strconv.Atoi(mentionSortArg.(string))
122 }
123 containChildren := model.Conf.Editor.BacklinkContainChildren
124 if val, ok := arg["containChildren"]; ok {
125 containChildren = val.(bool)
126 }
127 boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.GetBacklink2(id, keyword, mentionKeyword, sort, mentionSort, containChildren)
128 ret.Data = map[string]interface{}{
129 "backlinks": backlinks,
130 "linkRefsCount": linkRefsCount,
131 "backmentions": backmentions,
132 "mentionsCount": mentionsCount,
133 "k": keyword,
134 "mk": mentionKeyword,
135 "box": boxID,
136 }
137}
138
139func getBacklink(c *gin.Context) {
140 ret := gulu.Ret.NewResult()
141 defer c.JSON(http.StatusOK, ret)
142
143 arg, ok := util.JsonArg(c, ret)
144 if !ok {
145 return
146 }
147
148 if nil == arg["id"] {
149 return
150 }
151
152 id := arg["id"].(string)
153 keyword := arg["k"].(string)
154 mentionKeyword := arg["mk"].(string)
155 beforeLen := 12
156 if nil != arg["beforeLen"] {
157 beforeLen = int(arg["beforeLen"].(float64))
158 }
159 containChildren := model.Conf.Editor.BacklinkContainChildren
160 if val, ok := arg["containChildren"]; ok {
161 containChildren = val.(bool)
162 }
163 boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.GetBacklink(id, keyword, mentionKeyword, beforeLen, containChildren)
164 ret.Data = map[string]interface{}{
165 "backlinks": backlinks,
166 "linkRefsCount": linkRefsCount,
167 "backmentions": backmentions,
168 "mentionsCount": mentionsCount,
169 "k": keyword,
170 "mk": mentionKeyword,
171 "box": boxID,
172 }
173 util.RandomSleep(200, 500)
174}