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"
|
2022-06-21 16:58:33 +03:00
|
|
|
|
2023-01-02 18:18:12 +03:00
|
|
|
"github.com/pkg/errors"
|
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"
|
2023-01-13 17:37:30 +03:00
|
|
|
"golang.org/x/exp/slices"
|
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")
|
|
|
|
}
|
|
|
|
|
2022-12-28 15:22:52 +03:00
|
|
|
tagUpsert := &api.TagUpsert{}
|
2022-12-21 18:59:03 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-12-28 15:22:52 +03:00
|
|
|
tagUpsert.CreatorID = userID
|
2022-12-21 18:59:03 +03:00
|
|
|
tag, err := s.Store.UpsertTag(ctx, tagUpsert)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert tag").SetInternal(err)
|
|
|
|
}
|
2023-01-02 18:18:12 +03:00
|
|
|
if err := s.createTagCreateActivity(c, tag); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
|
|
|
|
}
|
2023-02-17 18:55:56 +03:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(tag.Name))
|
2022-12-21 18:59:03 +03:00
|
|
|
})
|
|
|
|
|
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()
|
2023-01-01 18:26:21 +03:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find tag")
|
2022-12-21 18:59:03 +03:00
|
|
|
}
|
|
|
|
|
2023-01-01 18:26:21 +03:00
|
|
|
tagFind := &api.TagFind{
|
|
|
|
CreatorID: userID,
|
2022-12-21 18:59:03 +03:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2023-02-17 18:55:56 +03:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(tagNameList))
|
2022-12-21 18:59:03 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
g.GET("/tag/suggestion", func(c echo.Context) error {
|
2022-08-07 05:17:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-12-28 15:22:52 +03:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Missing user session")
|
|
|
|
}
|
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{
|
2022-12-28 15:22:52 +03:00
|
|
|
CreatorID: &userID,
|
2022-06-21 16:58:33 +03:00
|
|
|
ContentSearch: &contentSearch,
|
2022-06-21 18:29:07 +03:00
|
|
|
RowStatus: &normalRowStatus,
|
2022-06-21 16:58:33 +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)
|
|
|
|
}
|
|
|
|
|
2023-01-13 17:37:30 +03:00
|
|
|
tagFind := &api.TagFind{
|
|
|
|
CreatorID: userID,
|
|
|
|
}
|
|
|
|
existTagList, 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 existTagList {
|
|
|
|
tagNameList = append(tagNameList, tag.Name)
|
|
|
|
}
|
|
|
|
|
2022-11-12 06:31:16 +03:00
|
|
|
tagMapSet := make(map[string]bool)
|
2022-11-12 04:02:44 +03:00
|
|
|
for _, memo := range memoList {
|
2022-11-12 06:31:16 +03:00
|
|
|
for _, tag := range findTagListFromMemoContent(memo.Content) {
|
2023-01-13 17:37:30 +03:00
|
|
|
if !slices.Contains(tagNameList, tag) {
|
|
|
|
tagMapSet[tag] = true
|
|
|
|
}
|
2022-11-12 06:31:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
2023-02-17 18:55:56 +03:00
|
|
|
return c.JSON(http.StatusOK, composeResponse(tagList))
|
2022-06-21 16:58:33 +03:00
|
|
|
})
|
2022-12-21 18:59:03 +03:00
|
|
|
|
2023-02-10 18:57:02 +03:00
|
|
|
g.POST("/tag/delete", func(c echo.Context) error {
|
2022-12-21 18:59:03 +03:00
|
|
|
ctx := c.Request().Context()
|
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
|
|
|
|
2023-02-10 18:57:02 +03:00
|
|
|
tagDelete := &api.TagDelete{}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(tagDelete); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post tag request").SetInternal(err)
|
2022-12-21 18:59:03 +03:00
|
|
|
}
|
2023-02-10 18:57:02 +03:00
|
|
|
if tagDelete.Name == "" {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Tag name shouldn't be empty")
|
2022-12-21 18:59:03 +03:00
|
|
|
}
|
2023-02-10 18:57:02 +03:00
|
|
|
|
|
|
|
tagDelete.CreatorID = userID
|
2022-12-21 18:59:03 +03:00
|
|
|
if err := s.Store.DeleteTag(ctx, tagDelete); err != nil {
|
|
|
|
if common.ErrorCode(err) == common.NotFound {
|
2023-02-10 18:57:02 +03:00
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Tag name not found: %s", tagDelete.Name))
|
2022-12-21 18:59:03 +03:00
|
|
|
}
|
2023-02-10 18:57:02 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete tag name: %v", tagDelete.Name)).SetInternal(err)
|
2022-12-21 18:59:03 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2023-01-02 18:18:12 +03:00
|
|
|
|
|
|
|
func (s *Server) createTagCreateActivity(c echo.Context, tag *api.Tag) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
payload := api.ActivityTagCreatePayload{
|
|
|
|
TagName: tag.Name,
|
|
|
|
}
|
2023-02-17 18:55:56 +03:00
|
|
|
payloadBytes, err := json.Marshal(payload)
|
2023-01-02 18:18:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal activity payload")
|
|
|
|
}
|
2023-01-07 06:49:58 +03:00
|
|
|
activity, err := s.Store.CreateActivity(ctx, &api.ActivityCreate{
|
2023-01-02 18:18:12 +03:00
|
|
|
CreatorID: tag.CreatorID,
|
|
|
|
Type: api.ActivityTagCreate,
|
|
|
|
Level: api.ActivityInfo,
|
2023-02-17 18:55:56 +03:00
|
|
|
Payload: string(payloadBytes),
|
2023-01-02 18:18:12 +03:00
|
|
|
})
|
2023-01-07 06:49:58 +03:00
|
|
|
if err != nil || activity == nil {
|
|
|
|
return errors.Wrap(err, "failed to create activity")
|
|
|
|
}
|
2023-01-02 18:18:12 +03:00
|
|
|
return err
|
|
|
|
}
|