ref.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. }
  35. func getBackmentionDoc(c *gin.Context) {
  36. ret := gulu.Ret.NewResult()
  37. defer c.JSON(http.StatusOK, ret)
  38. arg, ok := util.JsonArg(c, ret)
  39. if !ok {
  40. return
  41. }
  42. defID := arg["defID"].(string)
  43. refTreeID := arg["refTreeID"].(string)
  44. keyword := arg["keyword"].(string)
  45. containChildren := false
  46. if val, ok := arg["containChildren"]; ok {
  47. containChildren = val.(bool)
  48. }
  49. backlinks := model.GetBackmentionDoc(defID, refTreeID, keyword, containChildren)
  50. ret.Data = map[string]interface{}{
  51. "backmentions": backlinks,
  52. }
  53. }
  54. func getBacklinkDoc(c *gin.Context) {
  55. ret := gulu.Ret.NewResult()
  56. defer c.JSON(http.StatusOK, ret)
  57. arg, ok := util.JsonArg(c, ret)
  58. if !ok {
  59. return
  60. }
  61. defID := arg["defID"].(string)
  62. refTreeID := arg["refTreeID"].(string)
  63. keyword := arg["keyword"].(string)
  64. containChildren := false
  65. if val, ok := arg["containChildren"]; ok {
  66. containChildren = val.(bool)
  67. }
  68. backlinks := model.GetBacklinkDoc(defID, refTreeID, keyword, containChildren)
  69. ret.Data = map[string]interface{}{
  70. "backlinks": backlinks,
  71. }
  72. }
  73. func getBacklink2(c *gin.Context) {
  74. ret := gulu.Ret.NewResult()
  75. defer c.JSON(http.StatusOK, ret)
  76. arg, ok := util.JsonArg(c, ret)
  77. if !ok {
  78. return
  79. }
  80. if nil == arg["id"] {
  81. return
  82. }
  83. id := arg["id"].(string)
  84. keyword := arg["k"].(string)
  85. mentionKeyword := arg["mk"].(string)
  86. sortArg := arg["sort"]
  87. sort := util.SortModeUpdatedDESC
  88. if nil != sortArg {
  89. sort, _ = strconv.Atoi(sortArg.(string))
  90. }
  91. mentionSortArg := arg["mSort"]
  92. mentionSort := util.SortModeUpdatedDESC
  93. if nil != mentionSortArg {
  94. mentionSort, _ = strconv.Atoi(mentionSortArg.(string))
  95. }
  96. containChildren := false
  97. if val, ok := arg["containChildren"]; ok {
  98. containChildren = val.(bool)
  99. }
  100. boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.GetBacklink2(id, keyword, mentionKeyword, sort, mentionSort, containChildren)
  101. ret.Data = map[string]interface{}{
  102. "backlinks": backlinks,
  103. "linkRefsCount": linkRefsCount,
  104. "backmentions": backmentions,
  105. "mentionsCount": mentionsCount,
  106. "k": keyword,
  107. "mk": mentionKeyword,
  108. "box": boxID,
  109. }
  110. }
  111. func getBacklink(c *gin.Context) {
  112. ret := gulu.Ret.NewResult()
  113. defer c.JSON(http.StatusOK, ret)
  114. arg, ok := util.JsonArg(c, ret)
  115. if !ok {
  116. return
  117. }
  118. if nil == arg["id"] {
  119. return
  120. }
  121. id := arg["id"].(string)
  122. keyword := arg["k"].(string)
  123. mentionKeyword := arg["mk"].(string)
  124. beforeLen := 12
  125. if nil != arg["beforeLen"] {
  126. beforeLen = int(arg["beforeLen"].(float64))
  127. }
  128. containChildren := false
  129. if val, ok := arg["containChildren"]; ok {
  130. containChildren = val.(bool)
  131. }
  132. boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.GetBacklink(id, keyword, mentionKeyword, beforeLen, containChildren)
  133. ret.Data = map[string]interface{}{
  134. "backlinks": backlinks,
  135. "linkRefsCount": linkRefsCount,
  136. "backmentions": backmentions,
  137. "mentionsCount": mentionsCount,
  138. "k": keyword,
  139. "mk": mentionKeyword,
  140. "box": boxID,
  141. }
  142. util.RandomSleep(200, 500)
  143. }