ref.go 4.5 KB

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