2022-06-21 16:58:33 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
2022-07-02 10:01:59 +03:00
|
|
|
"sort"
|
2022-07-07 15:22:36 +03:00
|
|
|
"strconv"
|
2022-06-21 16:58:33 +03:00
|
|
|
|
2022-06-27 17:09:06 +03:00
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
|
2022-06-21 16:58:33 +03:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2022-09-05 15:15:34 +03:00
|
|
|
var tagRegexp = regexp.MustCompile(`#([^\s#]+?) `)
|
|
|
|
|
2022-06-21 16:58:33 +03:00
|
|
|
func (s *Server) registerTagRoutes(g *echo.Group) {
|
|
|
|
g.GET("/tag", 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
|
|
|
}
|
|
|
|
|
2022-07-07 18:11:20 +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 {
|
2022-07-26 16:12:20 +03:00
|
|
|
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 = ¤tUserID
|
|
|
|
} 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)
|
|
|
|
|
|
|
|
for _, memo := range memoList {
|
2022-09-05 15:15:34 +03:00
|
|
|
for _, rawTag := range tagRegexp.FindAllString(memo.Content, -1) {
|
|
|
|
tag := tagRegexp.ReplaceAllString(rawTag, "$1")
|
2022-06-21 16:58:33 +03:00
|
|
|
tagMapSet[tag] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tagList := []string{}
|
|
|
|
for tag := range tagMapSet {
|
|
|
|
tagList = append(tagList, tag)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|