ref.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. keyword := ""
  43. backlinks := model.GetBackmentionDoc(defID, keyword)
  44. ret.Data = map[string]interface{}{
  45. "backmentions": backlinks,
  46. }
  47. }
  48. func getBacklinkDoc(c *gin.Context) {
  49. ret := gulu.Ret.NewResult()
  50. defer c.JSON(http.StatusOK, ret)
  51. arg, ok := util.JsonArg(c, ret)
  52. if !ok {
  53. return
  54. }
  55. defID := arg["defID"].(string)
  56. refTreeID := arg["refTreeID"].(string)
  57. backlinks := model.GetBacklinkDoc(defID, refTreeID)
  58. ret.Data = map[string]interface{}{
  59. "backlinks": backlinks,
  60. }
  61. }
  62. func getBacklink(c *gin.Context) {
  63. ret := gulu.Ret.NewResult()
  64. defer c.JSON(http.StatusOK, ret)
  65. arg, ok := util.JsonArg(c, ret)
  66. if !ok {
  67. return
  68. }
  69. if nil == arg["id"] {
  70. return
  71. }
  72. id := arg["id"].(string)
  73. keyword := arg["k"].(string)
  74. mentionKeyword := arg["mk"].(string)
  75. beforeLen := arg["beforeLen"].(float64)
  76. boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.BuildTreeBacklink(id, keyword, mentionKeyword, int(beforeLen))
  77. ret.Data = map[string]interface{}{
  78. "backlinks": backlinks,
  79. "linkRefsCount": linkRefsCount,
  80. "backmentions": backmentions,
  81. "mentionsCount": mentionsCount,
  82. "k": keyword,
  83. "mk": mentionKeyword,
  84. "box": boxID,
  85. }
  86. util.RandomSleep(200, 500)
  87. }
  88. func createBacklink(c *gin.Context) {
  89. ret := gulu.Ret.NewResult()
  90. defer c.JSON(http.StatusOK, ret)
  91. arg, ok := util.JsonArg(c, ret)
  92. if !ok {
  93. return
  94. }
  95. defID := arg["defID"].(string)
  96. refID := arg["refID"].(string)
  97. refText := arg["refText"].(string)
  98. isDynamic := arg["isDynamic"].(bool)
  99. refRootID, err := model.CreateBacklink(defID, refID, refText, isDynamic)
  100. if nil != err {
  101. ret.Code = -1
  102. ret.Msg = err.Error()
  103. return
  104. }
  105. ret.Data = map[string]interface{}{
  106. "defID": defID,
  107. "refID": refID,
  108. "refRootID": refRootID,
  109. "refText": refText,
  110. }
  111. }