2022-02-03 10:32:03 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-02-04 11:51:48 +03:00
|
|
|
"encoding/json"
|
2022-02-03 10:32:03 +03:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2022-07-26 16:12:20 +03:00
|
|
|
"strings"
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2022-06-27 17:09:06 +03:00
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
g.POST("/memo", func(c echo.Context) error {
|
2022-08-07 05:17:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-07-28 15:09:25 +03:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-02-03 10:32:03 +03:00
|
|
|
memoCreate := &api.MemoCreate{
|
2022-05-02 21:05:43 +03:00
|
|
|
CreatorID: userID,
|
2022-08-20 16:22:36 +03:00
|
|
|
// Private is the default memo visibility.
|
|
|
|
Visibility: api.Privite,
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-02-04 11:51:48 +03:00
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(memoCreate); err != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-08-20 16:22:36 +03:00
|
|
|
if memoCreate.Content == "" {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Memo content shouldn't be empty")
|
|
|
|
}
|
|
|
|
|
2022-08-18 19:00:47 +03:00
|
|
|
userSettingMemoVisibilityKey := api.UserSettingMemoVisibilityKey
|
|
|
|
userMemoVisibilitySetting, err := s.Store.FindUserSetting(ctx, &api.UserSettingFind{
|
|
|
|
UserID: userID,
|
|
|
|
Key: &userSettingMemoVisibilityKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user setting").SetInternal(err)
|
|
|
|
}
|
|
|
|
if userMemoVisibilitySetting != nil {
|
2022-08-19 16:56:22 +03:00
|
|
|
memoVisibility := api.Privite
|
2022-08-20 16:51:28 +03:00
|
|
|
err := json.Unmarshal([]byte(userMemoVisibilitySetting.Value), &memoVisibility)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal user setting value").SetInternal(err)
|
|
|
|
}
|
2022-08-20 16:22:36 +03:00
|
|
|
memoCreate.Visibility = memoVisibility
|
2022-07-15 16:25:07 +03:00
|
|
|
}
|
2022-07-08 17:23:27 +03:00
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
memo, err := s.Store.CreateMemo(ctx, memoCreate)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create memo").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 12:06:04 +03:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
|
2022-02-05 06:43:25 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 17:21:10 +03:00
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
g.PATCH("/memo/:memoId", func(c echo.Context) error {
|
2022-08-07 05:17:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-05-02 21:05:43 +03:00
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
memoPatch := &api.MemoPatch{
|
2022-05-02 21:05:43 +03:00
|
|
|
ID: memoID,
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-02-04 11:51:48 +03:00
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(memoPatch); err != nil {
|
2022-02-03 10:32:03 +03:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch memo request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
memo, err := s.Store.PatchMemo(ctx, memoPatch)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch memo").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 12:06:04 +03:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
|
2022-02-05 06:43:25 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 17:21:10 +03:00
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
g.GET("/memo", func(c echo.Context) error {
|
2022-08-07 05:17:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-07-07 18:11:20 +03:00
|
|
|
memoFind := &api.MemoFind{}
|
|
|
|
|
|
|
|
if userID, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
|
|
|
|
memoFind.CreatorID = &userID
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-05-19 13:32:04 +03:00
|
|
|
|
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-08 18:38:24 +03:00
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
rowStatus := api.RowStatus(c.QueryParam("rowStatus"))
|
2022-03-28 19:01:34 +03:00
|
|
|
if rowStatus != "" {
|
|
|
|
memoFind.RowStatus = &rowStatus
|
2022-02-04 16:24:21 +03:00
|
|
|
}
|
2022-05-19 13:32:04 +03:00
|
|
|
pinnedStr := c.QueryParam("pinned")
|
|
|
|
if pinnedStr != "" {
|
|
|
|
pinned := pinnedStr == "true"
|
|
|
|
memoFind.Pinned = &pinned
|
|
|
|
}
|
2022-06-14 18:09:03 +03:00
|
|
|
tag := c.QueryParam("tag")
|
|
|
|
if tag != "" {
|
2022-06-21 16:58:33 +03:00
|
|
|
contentSearch := "#" + tag + " "
|
|
|
|
memoFind.ContentSearch = &contentSearch
|
|
|
|
}
|
2022-07-26 16:12:20 +03:00
|
|
|
visibilitListStr := c.QueryParam("visibility")
|
|
|
|
if visibilitListStr != "" {
|
|
|
|
visibilityList := []api.Visibility{}
|
|
|
|
for _, visibility := range strings.Split(visibilitListStr, ",") {
|
|
|
|
visibilityList = append(visibilityList, api.Visibility(visibility))
|
|
|
|
}
|
|
|
|
memoFind.VisibilityList = visibilityList
|
|
|
|
}
|
2022-06-21 16:58:33 +03:00
|
|
|
if limit, err := strconv.Atoi(c.QueryParam("limit")); err == nil {
|
|
|
|
memoFind.Limit = limit
|
|
|
|
}
|
|
|
|
if offset, err := strconv.Atoi(c.QueryParam("offset")); err == nil {
|
|
|
|
memoFind.Offset = offset
|
2022-06-14 18:09:03 +03:00
|
|
|
}
|
2022-02-04 16:24:21 +03:00
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
list, err := s.Store.FindMemoList(ctx, memoFind)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 12:06:04 +03:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil {
|
2022-02-05 06:43:25 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo list response").SetInternal(err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 17:21:10 +03:00
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
g.POST("/memo/:memoId/organizer", func(c echo.Context) error {
|
2022-08-07 05:17:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-05-19 13:32:04 +03:00
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:09:25 +03:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-05-19 13:32:04 +03:00
|
|
|
memoOrganizerUpsert := &api.MemoOrganizerUpsert{
|
|
|
|
MemoID: memoID,
|
|
|
|
UserID: userID,
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(memoOrganizerUpsert); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo organizer request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
err = s.Store.UpsertMemoOrganizer(ctx, memoOrganizerUpsert)
|
2022-05-19 13:32:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo organizer").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-08-07 05:17:12 +03:00
|
|
|
memo, err := s.Store.FindMemo(ctx, &api.MemoFind{
|
2022-05-19 13:32:04 +03:00
|
|
|
ID: &memoID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if common.ErrorCode(err) == common.NotFound {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID)).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", memoID)).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
g.GET("/memo/:memoId", func(c echo.Context) error {
|
2022-08-07 05:17:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-05-02 21:05:43 +03:00
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
memoFind := &api.MemoFind{
|
2022-05-02 21:05:43 +03:00
|
|
|
ID: &memoID,
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-08-07 05:17:12 +03:00
|
|
|
memo, err := s.Store.FindMemo(ctx, memoFind)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
if common.ErrorCode(err) == common.NotFound {
|
2022-05-02 21:05:43 +03:00
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID)).SetInternal(err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-02-05 19:25:41 +03:00
|
|
|
|
2022-05-02 21:05:43 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", memoID)).SetInternal(err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 12:06:04 +03:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
|
2022-02-05 06:43:25 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 17:21:10 +03:00
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
g.DELETE("/memo/:memoId", func(c echo.Context) error {
|
2022-08-07 05:17:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-05-02 21:05:43 +03:00
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
memoDelete := &api.MemoDelete{
|
2022-05-17 16:21:13 +03:00
|
|
|
ID: memoID,
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-08-07 05:17:12 +03:00
|
|
|
if err := s.Store.DeleteMemo(ctx, memoDelete); err != nil {
|
2022-09-03 13:54:22 +03:00
|
|
|
if common.ErrorCode(err) == common.NotFound {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID))
|
|
|
|
}
|
2022-05-02 21:05:43 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete memo ID: %v", memoID)).SetInternal(err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2022-07-02 05:47:16 +03:00
|
|
|
return c.JSON(http.StatusOK, true)
|
2022-02-03 10:32:03 +03:00
|
|
|
})
|
2022-06-21 16:58:33 +03:00
|
|
|
|
|
|
|
g.GET("/memo/amount", func(c echo.Context) error {
|
2022-08-07 05:17:12 +03:00
|
|
|
ctx := c.Request().Context()
|
2022-07-28 15:09:25 +03:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-06-21 16:58:33 +03:00
|
|
|
normalRowStatus := api.Normal
|
|
|
|
memoFind := &api.MemoFind{
|
|
|
|
CreatorID: &userID,
|
|
|
|
RowStatus: &normalRowStatus,
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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(len(memoList))); err != nil {
|
2022-06-21 16:58:33 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo amount").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|