tag.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package v1
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "regexp"
  7. "sort"
  8. "github.com/labstack/echo/v4"
  9. "golang.org/x/exp/slices"
  10. "github.com/usememos/memos/store"
  11. )
  12. type Tag struct {
  13. Name string
  14. CreatorID int32
  15. }
  16. type UpsertTagRequest struct {
  17. Name string `json:"name"`
  18. }
  19. type DeleteTagRequest struct {
  20. Name string `json:"name"`
  21. }
  22. func (s *APIV1Service) registerTagRoutes(g *echo.Group) {
  23. g.GET("/tag", s.GetTagList)
  24. g.POST("/tag", s.CreateTag)
  25. g.GET("/tag/suggestion", s.GetTagSuggestion)
  26. g.POST("/tag/delete", s.DeleteTag)
  27. }
  28. // GetTagList godoc
  29. //
  30. // @Summary Get a list of tags
  31. // @Tags tag
  32. // @Produce json
  33. // @Success 200 {object} []string "Tag list"
  34. // @Failure 400 {object} nil "Missing user id to find tag"
  35. // @Failure 500 {object} nil "Failed to find tag list"
  36. // @Router /api/v1/tag [GET]
  37. func (s *APIV1Service) GetTagList(c echo.Context) error {
  38. ctx := c.Request().Context()
  39. userID, ok := c.Get(userIDContextKey).(int32)
  40. if !ok {
  41. return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find tag")
  42. }
  43. list, err := s.Store.ListTags(ctx, &store.FindTag{
  44. CreatorID: userID,
  45. })
  46. if err != nil {
  47. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find tag list").SetInternal(err)
  48. }
  49. tagNameList := []string{}
  50. for _, tag := range list {
  51. tagNameList = append(tagNameList, tag.Name)
  52. }
  53. return c.JSON(http.StatusOK, tagNameList)
  54. }
  55. // CreateTag godoc
  56. //
  57. // @Summary Create a tag
  58. // @Tags tag
  59. // @Accept json
  60. // @Produce json
  61. // @Param body body UpsertTagRequest true "Request object."
  62. // @Success 200 {object} string "Created tag name"
  63. // @Failure 400 {object} nil "Malformatted post tag request | Tag name shouldn't be empty"
  64. // @Failure 401 {object} nil "Missing user in session"
  65. // @Failure 500 {object} nil "Failed to upsert tag | Failed to create activity"
  66. // @Router /api/v1/tag [POST]
  67. func (s *APIV1Service) CreateTag(c echo.Context) error {
  68. ctx := c.Request().Context()
  69. userID, ok := c.Get(userIDContextKey).(int32)
  70. if !ok {
  71. return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
  72. }
  73. tagUpsert := &UpsertTagRequest{}
  74. if err := json.NewDecoder(c.Request().Body).Decode(tagUpsert); err != nil {
  75. return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post tag request").SetInternal(err)
  76. }
  77. if tagUpsert.Name == "" {
  78. return echo.NewHTTPError(http.StatusBadRequest, "Tag name shouldn't be empty")
  79. }
  80. tag, err := s.Store.UpsertTag(ctx, &store.Tag{
  81. Name: tagUpsert.Name,
  82. CreatorID: userID,
  83. })
  84. if err != nil {
  85. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert tag").SetInternal(err)
  86. }
  87. tagMessage := convertTagFromStore(tag)
  88. return c.JSON(http.StatusOK, tagMessage.Name)
  89. }
  90. // DeleteTag godoc
  91. //
  92. // @Summary Delete a tag
  93. // @Tags tag
  94. // @Accept json
  95. // @Produce json
  96. // @Param body body DeleteTagRequest true "Request object."
  97. // @Success 200 {boolean} true "Tag deleted"
  98. // @Failure 400 {object} nil "Malformatted post tag request | Tag name shouldn't be empty"
  99. // @Failure 401 {object} nil "Missing user in session"
  100. // @Failure 500 {object} nil "Failed to delete tag name: %v"
  101. // @Router /api/v1/tag/delete [POST]
  102. func (s *APIV1Service) DeleteTag(c echo.Context) error {
  103. ctx := c.Request().Context()
  104. userID, ok := c.Get(userIDContextKey).(int32)
  105. if !ok {
  106. return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
  107. }
  108. tagDelete := &DeleteTagRequest{}
  109. if err := json.NewDecoder(c.Request().Body).Decode(tagDelete); err != nil {
  110. return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post tag request").SetInternal(err)
  111. }
  112. if tagDelete.Name == "" {
  113. return echo.NewHTTPError(http.StatusBadRequest, "Tag name shouldn't be empty")
  114. }
  115. err := s.Store.DeleteTag(ctx, &store.DeleteTag{
  116. Name: tagDelete.Name,
  117. CreatorID: userID,
  118. })
  119. if err != nil {
  120. return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete tag name: %v", tagDelete.Name)).SetInternal(err)
  121. }
  122. return c.JSON(http.StatusOK, true)
  123. }
  124. // GetTagSuggestion godoc
  125. //
  126. // @Summary Get a list of tags suggested from other memos contents
  127. // @Tags tag
  128. // @Produce json
  129. // @Success 200 {object} []string "Tag list"
  130. // @Failure 400 {object} nil "Missing user session"
  131. // @Failure 500 {object} nil "Failed to find memo list | Failed to find tag list"
  132. // @Router /api/v1/tag/suggestion [GET]
  133. func (s *APIV1Service) GetTagSuggestion(c echo.Context) error {
  134. ctx := c.Request().Context()
  135. userID, ok := c.Get(userIDContextKey).(int32)
  136. if !ok {
  137. return echo.NewHTTPError(http.StatusBadRequest, "Missing user session")
  138. }
  139. normalRowStatus := store.Normal
  140. memoFind := &store.FindMemo{
  141. CreatorID: &userID,
  142. ContentSearch: []string{"#"},
  143. RowStatus: &normalRowStatus,
  144. }
  145. memoMessageList, err := s.Store.ListMemos(ctx, memoFind)
  146. if err != nil {
  147. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
  148. }
  149. list, err := s.Store.ListTags(ctx, &store.FindTag{
  150. CreatorID: userID,
  151. })
  152. if err != nil {
  153. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find tag list").SetInternal(err)
  154. }
  155. tagNameList := []string{}
  156. for _, tag := range list {
  157. tagNameList = append(tagNameList, tag.Name)
  158. }
  159. tagMapSet := make(map[string]bool)
  160. for _, memo := range memoMessageList {
  161. for _, tag := range findTagListFromMemoContent(memo.Content) {
  162. if !slices.Contains(tagNameList, tag) {
  163. tagMapSet[tag] = true
  164. }
  165. }
  166. }
  167. tagList := []string{}
  168. for tag := range tagMapSet {
  169. tagList = append(tagList, tag)
  170. }
  171. sort.Strings(tagList)
  172. return c.JSON(http.StatusOK, tagList)
  173. }
  174. func convertTagFromStore(tag *store.Tag) *Tag {
  175. return &Tag{
  176. Name: tag.Name,
  177. CreatorID: tag.CreatorID,
  178. }
  179. }
  180. var tagRegexp = regexp.MustCompile(`#([^\s#,]+)`)
  181. func findTagListFromMemoContent(memoContent string) []string {
  182. tagMapSet := make(map[string]bool)
  183. matches := tagRegexp.FindAllStringSubmatch(memoContent, -1)
  184. for _, v := range matches {
  185. tagName := v[1]
  186. tagMapSet[tagName] = true
  187. }
  188. tagList := []string{}
  189. for tag := range tagMapSet {
  190. tagList = append(tagList, tag)
  191. }
  192. sort.Strings(tagList)
  193. return tagList
  194. }