ref.go 4.1 KB

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