block.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SiYuan - Build Your Eternal Digital Garden
  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. package api
  17. import (
  18. "errors"
  19. "fmt"
  20. "net/http"
  21. "github.com/88250/gulu"
  22. "github.com/88250/lute/html"
  23. "github.com/gin-gonic/gin"
  24. "github.com/siyuan-note/filelock"
  25. "github.com/siyuan-note/logging"
  26. "github.com/siyuan-note/siyuan/kernel/model"
  27. "github.com/siyuan-note/siyuan/kernel/sql"
  28. "github.com/siyuan-note/siyuan/kernel/util"
  29. )
  30. func getHeadingLevelTransaction(c *gin.Context) {
  31. ret := gulu.Ret.NewResult()
  32. defer c.JSON(http.StatusOK, ret)
  33. arg, ok := util.JsonArg(c, ret)
  34. if !ok {
  35. return
  36. }
  37. id := arg["id"].(string)
  38. level := int(arg["level"].(float64))
  39. transaction, err := model.GetHeadingLevelTransaction(id, level)
  40. if nil != err {
  41. ret.Code = -1
  42. ret.Msg = err.Error()
  43. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  44. return
  45. }
  46. ret.Data = transaction
  47. }
  48. func setBlockReminder(c *gin.Context) {
  49. ret := gulu.Ret.NewResult()
  50. defer c.JSON(http.StatusOK, ret)
  51. arg, ok := util.JsonArg(c, ret)
  52. if !ok {
  53. return
  54. }
  55. id := arg["id"].(string)
  56. timed := arg["timed"].(string) // yyyyMMddHHmmss
  57. err := model.SetBlockReminder(id, timed)
  58. if nil != err {
  59. ret.Code = -1
  60. ret.Msg = err.Error()
  61. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  62. return
  63. }
  64. }
  65. func checkBlockFold(c *gin.Context) {
  66. ret := gulu.Ret.NewResult()
  67. defer c.JSON(http.StatusOK, ret)
  68. arg, ok := util.JsonArg(c, ret)
  69. if !ok {
  70. return
  71. }
  72. id := arg["id"].(string)
  73. ret.Data = sql.IsBlockFolded(id)
  74. }
  75. func checkBlockExist(c *gin.Context) {
  76. ret := gulu.Ret.NewResult()
  77. defer c.JSON(http.StatusOK, ret)
  78. arg, ok := util.JsonArg(c, ret)
  79. if !ok {
  80. return
  81. }
  82. id := arg["id"].(string)
  83. b, err := model.GetBlock(id)
  84. if errors.Is(err, filelock.ErrUnableLockFile) {
  85. ret.Code = 2
  86. ret.Data = id
  87. return
  88. }
  89. ret.Data = nil != b
  90. }
  91. func getDocInfo(c *gin.Context) {
  92. ret := gulu.Ret.NewResult()
  93. defer c.JSON(http.StatusOK, ret)
  94. arg, ok := util.JsonArg(c, ret)
  95. if !ok {
  96. return
  97. }
  98. id := arg["id"].(string)
  99. info := model.GetDocInfo(id)
  100. if nil == info {
  101. ret.Code = -1
  102. ret.Msg = fmt.Sprintf(model.Conf.Language(15), id)
  103. return
  104. }
  105. ret.Data = info
  106. }
  107. func getRecentUpdatedBlocks(c *gin.Context) {
  108. ret := gulu.Ret.NewResult()
  109. defer c.JSON(http.StatusOK, ret)
  110. blocks := model.RecentUpdatedBlocks()
  111. ret.Data = blocks
  112. }
  113. func getContentWordCount(c *gin.Context) {
  114. ret := gulu.Ret.NewResult()
  115. defer c.JSON(http.StatusOK, ret)
  116. arg, ok := util.JsonArg(c, ret)
  117. if !ok {
  118. return
  119. }
  120. content := arg["content"].(string)
  121. runeCount, wordCount := model.ContentWordCount(content)
  122. ret.Data = map[string]interface{}{
  123. "runeCount": runeCount,
  124. "wordCount": wordCount,
  125. }
  126. }
  127. func getBlocksWordCount(c *gin.Context) {
  128. ret := gulu.Ret.NewResult()
  129. defer c.JSON(http.StatusOK, ret)
  130. arg, ok := util.JsonArg(c, ret)
  131. if !ok {
  132. return
  133. }
  134. idsArg := arg["ids"].([]interface{})
  135. var ids []string
  136. for _, id := range idsArg {
  137. ids = append(ids, id.(string))
  138. }
  139. runeCount, wordCount := model.BlocksWordCount(ids)
  140. ret.Data = map[string]interface{}{
  141. "runeCount": runeCount,
  142. "wordCount": wordCount,
  143. }
  144. }
  145. func getBlockWordCount(c *gin.Context) {
  146. ret := gulu.Ret.NewResult()
  147. defer c.JSON(http.StatusOK, ret)
  148. arg, ok := util.JsonArg(c, ret)
  149. if !ok {
  150. return
  151. }
  152. id := arg["id"].(string)
  153. blockRuneCount, blockWordCount, rootBlockRuneCount, rootBlockWordCount := model.BlockWordCount(id)
  154. ret.Data = map[string]interface{}{
  155. "blockRuneCount": blockRuneCount,
  156. "blockWordCount": blockWordCount,
  157. "rootBlockRuneCount": rootBlockRuneCount,
  158. "rootBlockWordCount": rootBlockWordCount,
  159. }
  160. }
  161. func getRefText(c *gin.Context) {
  162. ret := gulu.Ret.NewResult()
  163. defer c.JSON(http.StatusOK, ret)
  164. arg, ok := util.JsonArg(c, ret)
  165. if !ok {
  166. return
  167. }
  168. id := arg["id"].(string)
  169. ret.Data = model.GetBlockRefText(id)
  170. }
  171. func getRefIDs(c *gin.Context) {
  172. ret := gulu.Ret.NewResult()
  173. defer c.JSON(http.StatusOK, ret)
  174. arg, ok := util.JsonArg(c, ret)
  175. if !ok {
  176. return
  177. }
  178. id := arg["id"].(string)
  179. refIDs, refTexts, defIDs := model.GetBlockRefIDs(id)
  180. ret.Data = map[string][]string{
  181. "refIDs": refIDs,
  182. "refTexts": refTexts,
  183. "defIDs": defIDs,
  184. }
  185. }
  186. func getRefIDsByFileAnnotationID(c *gin.Context) {
  187. ret := gulu.Ret.NewResult()
  188. defer c.JSON(http.StatusOK, ret)
  189. arg, ok := util.JsonArg(c, ret)
  190. if !ok {
  191. return
  192. }
  193. id := arg["id"].(string)
  194. refIDs, refTexts := model.GetBlockRefIDsByFileAnnotationID(id)
  195. ret.Data = map[string][]string{
  196. "refIDs": refIDs,
  197. "refTexts": refTexts,
  198. }
  199. }
  200. func getBlockDefIDsByRefText(c *gin.Context) {
  201. ret := gulu.Ret.NewResult()
  202. defer c.JSON(http.StatusOK, ret)
  203. arg, ok := util.JsonArg(c, ret)
  204. if !ok {
  205. return
  206. }
  207. anchor := arg["anchor"].(string)
  208. excludeIDsArg := arg["excludeIDs"].([]interface{})
  209. var excludeIDs []string
  210. for _, excludeID := range excludeIDsArg {
  211. excludeIDs = append(excludeIDs, excludeID.(string))
  212. }
  213. excludeIDs = nil // 不限制虚拟引用搜索自己 https://ld246.com/article/1633243424177
  214. ids := model.GetBlockDefIDsByRefText(anchor, excludeIDs)
  215. ret.Data = ids
  216. }
  217. func getBlockBreadcrumb(c *gin.Context) {
  218. ret := gulu.Ret.NewResult()
  219. defer c.JSON(http.StatusOK, ret)
  220. arg, ok := util.JsonArg(c, ret)
  221. if !ok {
  222. return
  223. }
  224. id := arg["id"].(string)
  225. blockPath, err := model.BuildBlockBreadcrumb(id)
  226. if nil != err {
  227. ret.Code = -1
  228. ret.Msg = err.Error()
  229. return
  230. }
  231. ret.Data = blockPath
  232. }
  233. func getBlockInfo(c *gin.Context) {
  234. ret := gulu.Ret.NewResult()
  235. defer c.JSON(http.StatusOK, ret)
  236. arg, ok := util.JsonArg(c, ret)
  237. if !ok {
  238. return
  239. }
  240. id := arg["id"].(string)
  241. block, err := model.GetBlock(id)
  242. if errors.Is(err, filelock.ErrUnableLockFile) {
  243. ret.Code = 2
  244. ret.Data = id
  245. return
  246. }
  247. if nil == block {
  248. ret.Code = -1
  249. ret.Msg = fmt.Sprintf(model.Conf.Language(15), id)
  250. return
  251. }
  252. var rootChildID string
  253. b := block
  254. for i := 0; i < 128; i++ {
  255. parentID := b.ParentID
  256. if "" == parentID {
  257. rootChildID = b.ID
  258. break
  259. }
  260. if b, _ = model.GetBlock(parentID); nil == b {
  261. logging.LogErrorf("not found parent")
  262. break
  263. }
  264. }
  265. root, err := model.GetBlock(block.RootID)
  266. if errors.Is(err, filelock.ErrUnableLockFile) {
  267. ret.Code = 2
  268. ret.Data = id
  269. return
  270. }
  271. rootTitle := root.IAL["title"]
  272. rootTitle = html.UnescapeString(rootTitle)
  273. ret.Data = map[string]string{
  274. "box": block.Box,
  275. "path": block.Path,
  276. "rootID": block.RootID,
  277. "rootTitle": rootTitle,
  278. "rootChildID": rootChildID,
  279. "rootIcon": root.IAL["icon"],
  280. }
  281. }
  282. func getBlockDOM(c *gin.Context) {
  283. ret := gulu.Ret.NewResult()
  284. defer c.JSON(http.StatusOK, ret)
  285. arg, ok := util.JsonArg(c, ret)
  286. if !ok {
  287. return
  288. }
  289. id := arg["id"].(string)
  290. dom := model.GetBlockDOM(id)
  291. ret.Data = map[string]string{
  292. "id": id,
  293. "dom": dom,
  294. }
  295. }
  296. func getBlockKramdown(c *gin.Context) {
  297. ret := gulu.Ret.NewResult()
  298. defer c.JSON(http.StatusOK, ret)
  299. arg, ok := util.JsonArg(c, ret)
  300. if !ok {
  301. return
  302. }
  303. id := arg["id"].(string)
  304. kramdown := model.GetBlockKramdown(id)
  305. ret.Data = map[string]string{
  306. "id": id,
  307. "kramdown": kramdown,
  308. }
  309. }