ref.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. backlinks := model.GetBackmentionDoc(defID, refTreeID, keyword)
  46. ret.Data = map[string]interface{}{
  47. "backmentions": backlinks,
  48. }
  49. }
  50. func getBacklinkDoc(c *gin.Context) {
  51. ret := gulu.Ret.NewResult()
  52. defer c.JSON(http.StatusOK, ret)
  53. arg, ok := util.JsonArg(c, ret)
  54. if !ok {
  55. return
  56. }
  57. defID := arg["defID"].(string)
  58. refTreeID := arg["refTreeID"].(string)
  59. keyword := arg["keyword"].(string)
  60. backlinks := model.GetBacklinkDoc(defID, refTreeID, keyword)
  61. ret.Data = map[string]interface{}{
  62. "backlinks": backlinks,
  63. }
  64. }
  65. func getBacklink2(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. if nil == arg["id"] {
  73. return
  74. }
  75. id := arg["id"].(string)
  76. keyword := arg["k"].(string)
  77. mentionKeyword := arg["mk"].(string)
  78. sortArg := arg["sort"]
  79. sort := util.SortModeUpdatedDESC
  80. if nil != sortArg {
  81. sort, _ = strconv.Atoi(sortArg.(string))
  82. }
  83. mentionSortArg := arg["mSort"]
  84. mentionSort := util.SortModeUpdatedDESC
  85. if nil != mentionSortArg {
  86. mentionSort, _ = strconv.Atoi(mentionSortArg.(string))
  87. }
  88. boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.GetBacklink2(id, keyword, mentionKeyword, sort, mentionSort)
  89. ret.Data = map[string]interface{}{
  90. "backlinks": backlinks,
  91. "linkRefsCount": linkRefsCount,
  92. "backmentions": backmentions,
  93. "mentionsCount": mentionsCount,
  94. "k": keyword,
  95. "mk": mentionKeyword,
  96. "box": boxID,
  97. }
  98. }
  99. func getBacklink(c *gin.Context) {
  100. ret := gulu.Ret.NewResult()
  101. defer c.JSON(http.StatusOK, ret)
  102. arg, ok := util.JsonArg(c, ret)
  103. if !ok {
  104. return
  105. }
  106. if nil == arg["id"] {
  107. return
  108. }
  109. id := arg["id"].(string)
  110. keyword := arg["k"].(string)
  111. mentionKeyword := arg["mk"].(string)
  112. beforeLen := 12
  113. if nil != arg["beforeLen"] {
  114. beforeLen = int(arg["beforeLen"].(float64))
  115. }
  116. boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.GetBacklink(id, keyword, mentionKeyword, beforeLen)
  117. ret.Data = map[string]interface{}{
  118. "backlinks": backlinks,
  119. "linkRefsCount": linkRefsCount,
  120. "backmentions": backmentions,
  121. "mentionsCount": mentionsCount,
  122. "k": keyword,
  123. "mk": mentionKeyword,
  124. "box": boxID,
  125. }
  126. util.RandomSleep(200, 500)
  127. }