ref.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 getBacklink(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. beforeLen := arg["beforeLen"].(float64)
  77. boxID, backlinks, backmentions, linkRefsCount, mentionsCount := model.GetBacklink(id, keyword, mentionKeyword, int(beforeLen))
  78. ret.Data = map[string]interface{}{
  79. "backlinks": backlinks,
  80. "linkRefsCount": linkRefsCount,
  81. "backmentions": backmentions,
  82. "mentionsCount": mentionsCount,
  83. "k": keyword,
  84. "mk": mentionKeyword,
  85. "box": boxID,
  86. }
  87. util.RandomSleep(200, 500)
  88. }
  89. func createBacklink(c *gin.Context) {
  90. ret := gulu.Ret.NewResult()
  91. defer c.JSON(http.StatusOK, ret)
  92. arg, ok := util.JsonArg(c, ret)
  93. if !ok {
  94. return
  95. }
  96. defID := arg["defID"].(string)
  97. refID := arg["refID"].(string)
  98. refText := arg["refText"].(string)
  99. isDynamic := arg["isDynamic"].(bool)
  100. refRootID, err := model.CreateBacklink(defID, refID, refText, isDynamic)
  101. if nil != err {
  102. ret.Code = -1
  103. ret.Msg = err.Error()
  104. return
  105. }
  106. ret.Data = map[string]interface{}{
  107. "defID": defID,
  108. "refID": refID,
  109. "refRootID": refRootID,
  110. "refText": refText,
  111. }
  112. }