2023-07-02 18:56:25 +08:00
|
|
|
package v1
|
2022-06-21 21:58:33 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-12-21 23:59:03 +08:00
|
|
|
"fmt"
|
2022-06-21 21:58:33 +08:00
|
|
|
"net/http"
|
|
|
|
"regexp"
|
2022-07-02 15:01:59 +08:00
|
|
|
"sort"
|
2022-06-21 21:58:33 +08:00
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-01-02 23:18:12 +08:00
|
|
|
"github.com/pkg/errors"
|
2023-05-25 21:50:37 +08:00
|
|
|
"github.com/usememos/memos/store"
|
2023-01-13 22:37:30 +08:00
|
|
|
"golang.org/x/exp/slices"
|
2022-06-21 21:58:33 +08:00
|
|
|
)
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
type Tag struct {
|
|
|
|
Name string
|
|
|
|
CreatorID int
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpsertTagRequest struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteTagRequest struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *APIV1Service) registerTagRoutes(g *echo.Group) {
|
2022-12-21 23:59:03 +08: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")
|
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
tagUpsert := &UpsertTagRequest{}
|
2022-12-21 23:59:03 +08: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")
|
|
|
|
}
|
|
|
|
|
2023-07-04 10:05:57 +08:00
|
|
|
tag, err := s.Store.UpsertTag(ctx, &store.Tag{
|
2023-07-02 18:56:25 +08:00
|
|
|
Name: tagUpsert.Name,
|
|
|
|
CreatorID: userID,
|
|
|
|
})
|
2022-12-21 23:59:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert tag").SetInternal(err)
|
|
|
|
}
|
2023-07-02 18:56:25 +08:00
|
|
|
tagMessage := convertTagFromStore(tag)
|
|
|
|
if err := s.createTagCreateActivity(c, tagMessage); err != nil {
|
2023-01-02 23:18:12 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
|
|
|
|
}
|
2023-07-02 18:56:25 +08:00
|
|
|
return c.JSON(http.StatusOK, tagMessage.Name)
|
2022-12-21 23:59:03 +08:00
|
|
|
})
|
|
|
|
|
2022-06-21 21:58:33 +08:00
|
|
|
g.GET("/tag", func(c echo.Context) error {
|
2022-12-21 23:59:03 +08:00
|
|
|
ctx := c.Request().Context()
|
2023-01-01 23:26:21 +08:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find tag")
|
2022-12-21 23:59:03 +08:00
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
list, err := s.Store.ListTags(ctx, &store.FindTag{
|
2023-01-01 23:26:21 +08:00
|
|
|
CreatorID: userID,
|
2023-07-02 18:56:25 +08:00
|
|
|
})
|
2022-12-21 23:59:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find tag list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tagNameList := []string{}
|
2023-07-02 18:56:25 +08:00
|
|
|
for _, tag := range list {
|
2022-12-21 23:59:03 +08:00
|
|
|
tagNameList = append(tagNameList, tag.Name)
|
|
|
|
}
|
2023-07-02 18:56:25 +08:00
|
|
|
return c.JSON(http.StatusOK, tagNameList)
|
2022-12-21 23:59:03 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
g.GET("/tag/suggestion", func(c echo.Context) error {
|
2022-08-07 10:17:12 +08:00
|
|
|
ctx := c.Request().Context()
|
2022-12-28 20:22:52 +08:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Missing user session")
|
|
|
|
}
|
2023-05-25 21:50:37 +08:00
|
|
|
normalRowStatus := store.Normal
|
2023-07-06 21:56:42 +08:00
|
|
|
memoFind := &store.FindMemo{
|
2022-12-28 20:22:52 +08:00
|
|
|
CreatorID: &userID,
|
2023-05-26 18:51:18 +08:00
|
|
|
ContentSearch: []string{"#"},
|
2022-06-21 23:29:07 +08:00
|
|
|
RowStatus: &normalRowStatus,
|
2022-06-21 21:58:33 +08:00
|
|
|
}
|
|
|
|
|
2023-05-25 21:50:37 +08:00
|
|
|
memoMessageList, err := s.Store.ListMemos(ctx, memoFind)
|
2022-06-21 21:58:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
list, err := s.Store.ListTags(ctx, &store.FindTag{
|
2023-01-13 22:37:30 +08:00
|
|
|
CreatorID: userID,
|
2023-07-02 18:56:25 +08:00
|
|
|
})
|
2023-01-13 22:37:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find tag list").SetInternal(err)
|
|
|
|
}
|
|
|
|
tagNameList := []string{}
|
2023-07-02 18:56:25 +08:00
|
|
|
for _, tag := range list {
|
2023-01-13 22:37:30 +08:00
|
|
|
tagNameList = append(tagNameList, tag.Name)
|
|
|
|
}
|
|
|
|
|
2022-11-12 11:31:16 +08:00
|
|
|
tagMapSet := make(map[string]bool)
|
2023-05-25 21:50:37 +08:00
|
|
|
for _, memo := range memoMessageList {
|
2022-11-12 11:31:16 +08:00
|
|
|
for _, tag := range findTagListFromMemoContent(memo.Content) {
|
2023-01-13 22:37:30 +08:00
|
|
|
if !slices.Contains(tagNameList, tag) {
|
|
|
|
tagMapSet[tag] = true
|
|
|
|
}
|
2022-11-12 11:31:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tagList := []string{}
|
|
|
|
for tag := range tagMapSet {
|
|
|
|
tagList = append(tagList, tag)
|
2022-06-21 21:58:33 +08:00
|
|
|
}
|
2022-07-02 15:01:59 +08:00
|
|
|
sort.Strings(tagList)
|
2023-07-02 18:56:25 +08:00
|
|
|
return c.JSON(http.StatusOK, tagList)
|
2022-06-21 21:58:33 +08:00
|
|
|
})
|
2022-12-21 23:59:03 +08:00
|
|
|
|
2023-02-10 23:57:02 +08:00
|
|
|
g.POST("/tag/delete", func(c echo.Context) error {
|
2022-12-21 23:59:03 +08:00
|
|
|
ctx := c.Request().Context()
|
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
tagDelete := &DeleteTagRequest{}
|
2023-02-10 23:57:02 +08:00
|
|
|
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 23:59:03 +08:00
|
|
|
}
|
2023-02-10 23:57:02 +08:00
|
|
|
if tagDelete.Name == "" {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Tag name shouldn't be empty")
|
2022-12-21 23:59:03 +08:00
|
|
|
}
|
2023-02-10 23:57:02 +08:00
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
err := s.Store.DeleteTag(ctx, &store.DeleteTag{
|
|
|
|
Name: tagDelete.Name,
|
|
|
|
CreatorID: userID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-02-10 23:57:02 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete tag name: %v", tagDelete.Name)).SetInternal(err)
|
2022-12-21 23:59:03 +08:00
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, true)
|
|
|
|
})
|
2022-06-21 21:58:33 +08:00
|
|
|
}
|
2022-11-12 09:02:44 +08:00
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
func (s *APIV1Service) createTagCreateActivity(c echo.Context, tag *Tag) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
payload := ActivityTagCreatePayload{
|
|
|
|
TagName: tag.Name,
|
|
|
|
}
|
|
|
|
payloadBytes, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal activity payload")
|
|
|
|
}
|
2023-07-06 21:56:42 +08:00
|
|
|
activity, err := s.Store.CreateActivity(ctx, &store.Activity{
|
2023-07-02 18:56:25 +08:00
|
|
|
CreatorID: tag.CreatorID,
|
|
|
|
Type: ActivityTagCreate.String(),
|
|
|
|
Level: ActivityInfo.String(),
|
|
|
|
Payload: string(payloadBytes),
|
|
|
|
})
|
|
|
|
if err != nil || activity == nil {
|
|
|
|
return errors.Wrap(err, "failed to create activity")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertTagFromStore(tag *store.Tag) *Tag {
|
|
|
|
return &Tag{
|
|
|
|
Name: tag.Name,
|
|
|
|
CreatorID: tag.CreatorID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 21:11:13 +08:00
|
|
|
var tagRegexp = regexp.MustCompile(`#([^\s#,]+)`)
|
2022-12-21 23:59:03 +08:00
|
|
|
|
2022-11-12 09:02:44 +08:00
|
|
|
func findTagListFromMemoContent(memoContent string) []string {
|
|
|
|
tagMapSet := make(map[string]bool)
|
2022-12-21 23:59:03 +08:00
|
|
|
matches := tagRegexp.FindAllStringSubmatch(memoContent, -1)
|
|
|
|
for _, v := range matches {
|
|
|
|
tagName := v[1]
|
|
|
|
tagMapSet[tagName] = true
|
2022-11-12 09:02:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tagList := []string{}
|
|
|
|
for tag := range tagMapSet {
|
|
|
|
tagList = append(tagList, tag)
|
|
|
|
}
|
|
|
|
sort.Strings(tagList)
|
|
|
|
return tagList
|
|
|
|
}
|