ref.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. "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 := ""
  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. backlinks := model.GetBacklinkDoc(defID, refTreeID)
  60. ret.Data = map[string]interface{}{
  61. "backlinks": backlinks,
  62. }
  63. }
  64. func getBacklink2(c *gin.Context) {
  65. ret := gulu.Ret.NewResult()
  66. defer c.JSON(http.StatusOK, ret)
  67. arg, ok := util.JsonArg(c, ret)
  68. if !ok {
  69. return
  70. }
  71. if nil == arg["id"] {
  72. return
  73. }
  74. id := arg["id"].(string)
  75. keyword := arg["k"].(string)
  76. mentionKeyword := arg["mk"].(string)
  77. sortArg := arg["sort"]
  78. sort := util.SortModeUpdatedDESC
  79. if nil != sortArg {
  80. sort, _ = strconv.Atoi(sortArg.(string))
  81. }
  82. mentionSortArg := arg["msort"]
  83. mentionSort := util.SortModeUpdatedDESC
  84. if nil != mentionSortArg {
  85. mentionSort, _ = strconv.Atoi(mentionSortArg.(string))
  86. }
  87. boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.GetBacklink2(id, keyword, mentionKeyword, sort, mentionSort)
  88. ret.Data = map[string]interface{}{
  89. "backlinks": backlinks,
  90. "linkRefsCount": linkRefsCount,
  91. "backmentions": backmentions,
  92. "mentionsCount": mentionsCount,
  93. "k": keyword,
  94. "mk": mentionKeyword,
  95. "box": boxID,
  96. }
  97. }
  98. func getBacklink(c *gin.Context) {
  99. ret := gulu.Ret.NewResult()
  100. defer c.JSON(http.StatusOK, ret)
  101. arg, ok := util.JsonArg(c, ret)
  102. if !ok {
  103. return
  104. }
  105. if nil == arg["id"] {
  106. return
  107. }
  108. id := arg["id"].(string)
  109. keyword := arg["k"].(string)
  110. mentionKeyword := arg["mk"].(string)
  111. beforeLen := 12
  112. if nil != arg["beforeLen"] {
  113. beforeLen = int(arg["beforeLen"].(float64))
  114. }
  115. boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.GetBacklink(id, keyword, mentionKeyword, beforeLen)
  116. ret.Data = map[string]interface{}{
  117. "backlinks": backlinks,
  118. "linkRefsCount": linkRefsCount,
  119. "backmentions": backmentions,
  120. "mentionsCount": mentionsCount,
  121. "k": keyword,
  122. "mk": mentionKeyword,
  123. "box": boxID,
  124. }
  125. util.RandomSleep(200, 500)
  126. }
  127. func createBacklink(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. defID := arg["defID"].(string)
  135. refID := arg["refID"].(string)
  136. refText := arg["refText"].(string)
  137. isDynamic := arg["isDynamic"].(bool)
  138. refRootID, err := model.CreateBacklink(defID, refID, refText, isDynamic)
  139. if nil != err {
  140. ret.Code = -1
  141. ret.Msg = err.Error()
  142. return
  143. }
  144. ret.Data = map[string]interface{}{
  145. "defID": defID,
  146. "refID": refID,
  147. "refRootID": refRootID,
  148. "refText": refText,
  149. }
  150. }