tag.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package server
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "regexp"
  7. "sort"
  8. "strconv"
  9. "github.com/usememos/memos/api"
  10. "github.com/usememos/memos/common"
  11. metric "github.com/usememos/memos/plugin/metrics"
  12. "github.com/labstack/echo/v4"
  13. )
  14. func (s *Server) registerTagRoutes(g *echo.Group) {
  15. g.POST("/tag", func(c echo.Context) error {
  16. ctx := c.Request().Context()
  17. userID, ok := c.Get(getUserIDContextKey()).(int)
  18. if !ok {
  19. return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
  20. }
  21. tagUpsert := &api.TagUpsert{}
  22. if err := json.NewDecoder(c.Request().Body).Decode(tagUpsert); err != nil {
  23. return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post tag request").SetInternal(err)
  24. }
  25. if tagUpsert.Name == "" {
  26. return echo.NewHTTPError(http.StatusBadRequest, "Tag name shouldn't be empty")
  27. }
  28. tagUpsert.CreatorID = userID
  29. tag, err := s.Store.UpsertTag(ctx, tagUpsert)
  30. if err != nil {
  31. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert tag").SetInternal(err)
  32. }
  33. s.Collector.Collect(ctx, &metric.Metric{
  34. Name: "tag created",
  35. })
  36. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
  37. if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(tag.Name)); err != nil {
  38. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode tag response").SetInternal(err)
  39. }
  40. return nil
  41. })
  42. g.GET("/tag", func(c echo.Context) error {
  43. ctx := c.Request().Context()
  44. tagFind := &api.TagFind{}
  45. if userID, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
  46. tagFind.CreatorID = userID
  47. }
  48. if tagFind.CreatorID == 0 {
  49. currentUserID, ok := c.Get(getUserIDContextKey()).(int)
  50. if !ok {
  51. return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find tag")
  52. }
  53. tagFind.CreatorID = currentUserID
  54. }
  55. tagList, err := s.Store.FindTagList(ctx, tagFind)
  56. if err != nil {
  57. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find tag list").SetInternal(err)
  58. }
  59. tagNameList := []string{}
  60. for _, tag := range tagList {
  61. tagNameList = append(tagNameList, tag.Name)
  62. }
  63. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
  64. if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(tagNameList)); err != nil {
  65. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode tags response").SetInternal(err)
  66. }
  67. return nil
  68. })
  69. g.GET("/tag/suggestion", func(c echo.Context) error {
  70. ctx := c.Request().Context()
  71. userID, ok := c.Get(getUserIDContextKey()).(int)
  72. if !ok {
  73. return echo.NewHTTPError(http.StatusBadRequest, "Missing user session")
  74. }
  75. contentSearch := "#"
  76. normalRowStatus := api.Normal
  77. memoFind := api.MemoFind{
  78. CreatorID: &userID,
  79. ContentSearch: &contentSearch,
  80. RowStatus: &normalRowStatus,
  81. }
  82. memoList, err := s.Store.FindMemoList(ctx, &memoFind)
  83. if err != nil {
  84. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
  85. }
  86. tagMapSet := make(map[string]bool)
  87. for _, memo := range memoList {
  88. for _, tag := range findTagListFromMemoContent(memo.Content) {
  89. tagMapSet[tag] = true
  90. }
  91. }
  92. tagList := []string{}
  93. for tag := range tagMapSet {
  94. tagList = append(tagList, tag)
  95. }
  96. sort.Strings(tagList)
  97. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
  98. if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(tagList)); err != nil {
  99. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode tags response").SetInternal(err)
  100. }
  101. return nil
  102. })
  103. g.DELETE("/tag/:tagName", func(c echo.Context) error {
  104. ctx := c.Request().Context()
  105. userID, ok := c.Get(getUserIDContextKey()).(int)
  106. if !ok {
  107. return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
  108. }
  109. tagName := c.Param("tagName")
  110. if tagName == "" {
  111. return echo.NewHTTPError(http.StatusBadRequest, "Tag name cannot be empty")
  112. }
  113. tagDelete := &api.TagDelete{
  114. Name: tagName,
  115. CreatorID: userID,
  116. }
  117. if err := s.Store.DeleteTag(ctx, tagDelete); err != nil {
  118. if common.ErrorCode(err) == common.NotFound {
  119. return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Tag name not found: %s", tagName))
  120. }
  121. return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete tag name: %v", tagName)).SetInternal(err)
  122. }
  123. return c.JSON(http.StatusOK, true)
  124. })
  125. }
  126. var tagRegexp = regexp.MustCompile(`#([^\s#]+)`)
  127. func findTagListFromMemoContent(memoContent string) []string {
  128. tagMapSet := make(map[string]bool)
  129. matches := tagRegexp.FindAllStringSubmatch(memoContent, -1)
  130. for _, v := range matches {
  131. tagName := v[1]
  132. tagMapSet[tagName] = true
  133. }
  134. tagList := []string{}
  135. for tag := range tagMapSet {
  136. tagList = append(tagList, tag)
  137. }
  138. sort.Strings(tagList)
  139. return tagList
  140. }