2023-07-06 16:56:42 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2023-07-31 15:55:40 +03:00
|
|
|
"github.com/usememos/memos/api/auth"
|
2023-07-06 16:56:42 +03:00
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MemoOrganizer struct {
|
|
|
|
MemoID int `json:"memoId"`
|
|
|
|
UserID int `json:"userId"`
|
|
|
|
Pinned bool `json:"pinned"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpsertMemoOrganizerRequest struct {
|
|
|
|
Pinned bool `json:"pinned"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *APIV1Service) registerMemoOrganizerRoutes(g *echo.Group) {
|
|
|
|
g.POST("/memo/:memoId/organizer", func(c echo.Context) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-07-30 18:49:10 +03:00
|
|
|
userID, ok := c.Get(auth.UserIDContextKey).(int)
|
2023-07-06 16:56:42 +03:00
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
|
|
|
|
|
|
|
memo, err := s.Store.GetMemo(ctx, &store.FindMemo{
|
|
|
|
ID: &memoID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo").SetInternal(err)
|
|
|
|
}
|
2023-07-06 17:53:38 +03:00
|
|
|
if memo == nil {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo not found: %v", memoID))
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
if memo.CreatorID != userID {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &UpsertMemoOrganizerRequest{}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(request); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo organizer request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
upsert := &store.MemoOrganizer{
|
|
|
|
MemoID: memoID,
|
|
|
|
UserID: userID,
|
|
|
|
Pinned: request.Pinned,
|
|
|
|
}
|
2023-07-06 17:53:38 +03:00
|
|
|
_, err = s.Store.UpsertMemoOrganizer(ctx, upsert)
|
2023-07-06 16:56:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo organizer").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
memo, err = s.Store.GetMemo(ctx, &store.FindMemo{
|
|
|
|
ID: &memoID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", memoID)).SetInternal(err)
|
|
|
|
}
|
2023-07-06 17:53:38 +03:00
|
|
|
if memo == nil {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo not found: %v", memoID))
|
|
|
|
}
|
2023-07-06 16:56:42 +03:00
|
|
|
|
|
|
|
memoResponse, err := s.convertMemoFromStore(ctx, memo)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, memoResponse)
|
|
|
|
})
|
|
|
|
}
|