memos/server/tag.go

178 lines
5.2 KiB
Go
Raw Normal View History

2022-06-21 16:58:33 +03:00
package server
import (
"encoding/json"
2022-12-21 18:59:03 +03:00
"fmt"
2022-06-21 16:58:33 +03:00
"net/http"
"regexp"
2022-07-02 10:01:59 +03:00
"sort"
"strconv"
2022-06-21 16:58:33 +03:00
2022-06-27 17:09:06 +03:00
"github.com/usememos/memos/api"
2022-12-21 18:59:03 +03:00
"github.com/usememos/memos/common"
metric "github.com/usememos/memos/plugin/metrics"
2022-06-27 17:09:06 +03:00
2022-06-21 16:58:33 +03:00
"github.com/labstack/echo/v4"
)
func (s *Server) registerTagRoutes(g *echo.Group) {
2022-12-21 18:59:03 +03:00
g.POST("/tag", func(c echo.Context) error {
ctx := c.Request().Context()
userID, ok := c.Get(getUserIDContextKey()).(int)
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}
tagUpsert := &api.TagUpsert{
CreatorID: userID,
}
if err := json.NewDecoder(c.Request().Body).Decode(tagUpsert); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post tag request").SetInternal(err)
}
if tagUpsert.Name == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Tag name shouldn't be empty")
}
tag, err := s.Store.UpsertTag(ctx, tagUpsert)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert tag").SetInternal(err)
}
s.Collector.Collect(ctx, &metric.Metric{
Name: "tag created",
})
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(tag.Name)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode tag response").SetInternal(err)
}
return nil
})
2022-06-21 16:58:33 +03:00
g.GET("/tag", func(c echo.Context) error {
2022-12-21 18:59:03 +03:00
ctx := c.Request().Context()
tagFind := &api.TagFind{}
if userID, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
tagFind.CreatorID = userID
}
if tagFind.CreatorID == 0 {
currentUserID, ok := c.Get(getUserIDContextKey()).(int)
if !ok {
return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find tag")
}
tagFind.CreatorID = currentUserID
}
tagList, err := s.Store.FindTagList(ctx, tagFind)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find tag list").SetInternal(err)
}
tagNameList := []string{}
for _, tag := range tagList {
tagNameList = append(tagNameList, tag.Name)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(tagNameList)); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode tags response").SetInternal(err)
}
return nil
})
g.GET("/tag/suggestion", func(c echo.Context) error {
2022-08-07 05:17:12 +03:00
ctx := c.Request().Context()
2022-06-21 16:58:33 +03:00
contentSearch := "#"
2022-06-21 18:29:07 +03:00
normalRowStatus := api.Normal
2022-06-21 16:58:33 +03:00
memoFind := api.MemoFind{
ContentSearch: &contentSearch,
2022-06-21 18:29:07 +03:00
RowStatus: &normalRowStatus,
2022-06-21 16:58:33 +03:00
}
if userID, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
memoFind.CreatorID = &userID
}
2022-07-27 14:45:37 +03:00
currentUserID, ok := c.Get(getUserIDContextKey()).(int)
if !ok {
if memoFind.CreatorID == nil {
return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find memo")
}
memoFind.VisibilityList = []api.Visibility{api.Public}
} else {
if memoFind.CreatorID == nil {
memoFind.CreatorID = &currentUserID
} else {
memoFind.VisibilityList = []api.Visibility{api.Public, api.Protected}
}
2022-07-09 07:00:26 +03:00
}
2022-08-07 05:17:12 +03:00
memoList, err := s.Store.FindMemoList(ctx, &memoFind)
2022-06-21 16:58:33 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
}
tagMapSet := make(map[string]bool)
2022-11-12 04:02:44 +03:00
for _, memo := range memoList {
for _, tag := range findTagListFromMemoContent(memo.Content) {
tagMapSet[tag] = true
}
}
tagList := []string{}
for tag := range tagMapSet {
tagList = append(tagList, tag)
2022-06-21 16:58:33 +03:00
}
2022-07-02 10:01:59 +03:00
sort.Strings(tagList)
2022-06-21 16:58:33 +03:00
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
2022-06-21 17:29:06 +03:00
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(tagList)); err != nil {
2022-06-21 16:58:33 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode tags response").SetInternal(err)
}
return nil
})
2022-12-21 18:59:03 +03:00
g.DELETE("/tag/:tagName", func(c echo.Context) error {
ctx := c.Request().Context()
userID, ok := c.Get(getUserIDContextKey()).(int)
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}
tagName := c.Param("tagName")
if tagName == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Tag name cannot be empty")
}
tagDelete := &api.TagDelete{
Name: tagName,
CreatorID: userID,
}
if err := s.Store.DeleteTag(ctx, tagDelete); err != nil {
if common.ErrorCode(err) == common.NotFound {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Tag name not found: %s", tagName))
}
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete tag name: %v", tagName)).SetInternal(err)
}
return c.JSON(http.StatusOK, true)
})
2022-06-21 16:58:33 +03:00
}
2022-11-12 04:02:44 +03:00
2022-12-21 18:59:03 +03:00
var tagRegexp = regexp.MustCompile(`#([^\s#]+)`)
2022-11-12 04:02:44 +03:00
func findTagListFromMemoContent(memoContent string) []string {
tagMapSet := make(map[string]bool)
2022-12-21 18:59:03 +03:00
matches := tagRegexp.FindAllStringSubmatch(memoContent, -1)
for _, v := range matches {
tagName := v[1]
tagMapSet[tagName] = true
2022-11-12 04:02:44 +03:00
}
tagList := []string{}
for tag := range tagMapSet {
tagList = append(tagList, tag)
}
sort.Strings(tagList)
return tagList
}