2022-02-03 15:32:03 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-02-04 16:51:48 +08:00
|
|
|
"encoding/json"
|
2022-02-03 15:32:03 +08:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-10-21 19:57:57 +08:00
|
|
|
"sort"
|
2022-02-03 15:32:03 +08:00
|
|
|
"strconv"
|
2022-07-26 21:12:20 +08:00
|
|
|
"strings"
|
2022-09-30 22:58:59 +08:00
|
|
|
"time"
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2022-06-27 22:09:06 +08:00
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
2022-10-29 11:15:39 +08:00
|
|
|
metric "github.com/usememos/memos/plugin/metrics"
|
2022-06-27 22:09:06 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) registerMemoRoutes(g *echo.Group) {
|
|
|
|
g.POST("/memo", func(c echo.Context) error {
|
2022-08-07 10:17:12 +08:00
|
|
|
ctx := c.Request().Context()
|
2022-07-28 20:09:25 +08:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-09-24 09:34:01 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
memoCreate := &api.MemoCreate{
|
2022-05-03 02:05:43 +08:00
|
|
|
CreatorID: userID,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-02-04 16:51:48 +08:00
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(memoCreate); err != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request").SetInternal(err)
|
|
|
|
}
|
2022-08-20 21:22:36 +08:00
|
|
|
if memoCreate.Content == "" {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Memo content shouldn't be empty")
|
|
|
|
}
|
|
|
|
|
2022-10-13 09:01:09 +08:00
|
|
|
if memoCreate.Visibility == "" {
|
|
|
|
userSettingMemoVisibilityKey := api.UserSettingMemoVisibilityKey
|
|
|
|
userMemoVisibilitySetting, err := s.Store.FindUserSetting(ctx, &api.UserSettingFind{
|
|
|
|
UserID: userID,
|
|
|
|
Key: &userSettingMemoVisibilityKey,
|
|
|
|
})
|
2022-08-20 21:51:28 +08:00
|
|
|
if err != nil {
|
2022-10-13 09:01:09 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user setting").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if userMemoVisibilitySetting != nil {
|
|
|
|
memoVisibility := api.Privite
|
|
|
|
err := json.Unmarshal([]byte(userMemoVisibilitySetting.Value), &memoVisibility)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal user setting value").SetInternal(err)
|
|
|
|
}
|
|
|
|
memoCreate.Visibility = memoVisibility
|
|
|
|
} else {
|
|
|
|
// Private is the default memo visibility.
|
|
|
|
memoCreate.Visibility = api.Privite
|
2022-08-20 21:51:28 +08:00
|
|
|
}
|
2022-07-15 21:25:07 +08:00
|
|
|
}
|
2022-07-08 22:23:27 +08:00
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
memo, err := s.Store.CreateMemo(ctx, memoCreate)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create memo").SetInternal(err)
|
|
|
|
}
|
2022-10-29 11:15:39 +08:00
|
|
|
s.Collector.Collect(ctx, &metric.Metric{
|
|
|
|
Name: "memo created",
|
|
|
|
})
|
2022-02-03 15:32:03 +08:00
|
|
|
|
2022-10-01 10:57:14 +08:00
|
|
|
for _, resourceID := range memoCreate.ResourceIDList {
|
|
|
|
if _, err := s.Store.UpsertMemoResource(ctx, &api.MemoResourceUpsert{
|
|
|
|
MemoID: memo.ID,
|
|
|
|
ResourceID: resourceID,
|
|
|
|
}); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo resource").SetInternal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memo, err = s.Store.ComposeMemo(ctx, memo)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 17:06:04 +08:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
|
2022-02-05 11:43:25 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
g.PATCH("/memo/:memoId", func(c echo.Context) error {
|
2022-08-07 10:17:12 +08:00
|
|
|
ctx := c.Request().Context()
|
2022-09-24 09:34:01 +08:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
|
|
|
|
2022-05-03 02:05:43 +08:00
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-09-24 09:34:01 +08:00
|
|
|
memoFind := &api.MemoFind{
|
|
|
|
ID: &memoID,
|
|
|
|
CreatorID: &userID,
|
|
|
|
}
|
|
|
|
if _, err := s.Store.FindMemo(ctx, memoFind); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:57:57 +08:00
|
|
|
currentTs := time.Now().Unix()
|
2022-02-03 15:32:03 +08:00
|
|
|
memoPatch := &api.MemoPatch{
|
2022-10-21 19:57:57 +08:00
|
|
|
ID: memoID,
|
|
|
|
UpdatedTs: ¤tTs,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-02-04 16:51:48 +08:00
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(memoPatch); err != nil {
|
2022-02-03 15:32:03 +08:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch memo request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
memo, err := s.Store.PatchMemo(ctx, memoPatch)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch memo").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-10-21 22:16:05 +08:00
|
|
|
for _, resourceID := range memoPatch.ResourceIDList {
|
|
|
|
if _, err := s.Store.UpsertMemoResource(ctx, &api.MemoResourceUpsert{
|
|
|
|
MemoID: memo.ID,
|
|
|
|
ResourceID: resourceID,
|
|
|
|
}); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo resource").SetInternal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memo, err = s.Store.ComposeMemo(ctx, memo)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 17:06:04 +08:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
|
2022-02-05 11:43:25 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
g.GET("/memo", func(c echo.Context) error {
|
2022-08-07 10:17:12 +08:00
|
|
|
ctx := c.Request().Context()
|
2022-07-07 23:11:20 +08:00
|
|
|
memoFind := &api.MemoFind{}
|
|
|
|
if userID, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
|
|
|
|
memoFind.CreatorID = &userID
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-05-19 18:32:04 +08:00
|
|
|
|
2022-07-27 19:45:37 +08:00
|
|
|
currentUserID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
2022-07-26 21:12:20 +08: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 23:38:24 +08:00
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
rowStatus := api.RowStatus(c.QueryParam("rowStatus"))
|
2022-03-29 00:01:34 +08:00
|
|
|
if rowStatus != "" {
|
|
|
|
memoFind.RowStatus = &rowStatus
|
2022-02-04 21:24:21 +08:00
|
|
|
}
|
2022-05-19 18:32:04 +08:00
|
|
|
pinnedStr := c.QueryParam("pinned")
|
|
|
|
if pinnedStr != "" {
|
|
|
|
pinned := pinnedStr == "true"
|
|
|
|
memoFind.Pinned = &pinned
|
|
|
|
}
|
2022-06-14 23:09:03 +08:00
|
|
|
tag := c.QueryParam("tag")
|
|
|
|
if tag != "" {
|
2022-06-21 21:58:33 +08:00
|
|
|
contentSearch := "#" + tag + " "
|
|
|
|
memoFind.ContentSearch = &contentSearch
|
|
|
|
}
|
2022-07-26 21:12:20 +08: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 21:58:33 +08: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 23:09:03 +08:00
|
|
|
}
|
2022-02-04 21:24:21 +08:00
|
|
|
|
2022-08-07 10:17:12 +08:00
|
|
|
list, err := s.Store.FindMemoList(ctx, memoFind)
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-10-29 20:54:54 +08:00
|
|
|
var pinnedMemoList []*api.Memo
|
|
|
|
var unpinnedMemoList []*api.Memo
|
|
|
|
|
|
|
|
for _, memo := range list {
|
|
|
|
if memo.Pinned {
|
|
|
|
pinnedMemoList = append(pinnedMemoList, memo)
|
|
|
|
} else {
|
|
|
|
unpinnedMemoList = append(unpinnedMemoList, memo)
|
2022-10-27 22:02:42 +08:00
|
|
|
}
|
2022-10-29 20:54:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(pinnedMemoList, func(i, j int) bool {
|
|
|
|
return pinnedMemoList[i].DisplayTs > pinnedMemoList[j].DisplayTs
|
|
|
|
})
|
|
|
|
sort.Slice(unpinnedMemoList, func(i, j int) bool {
|
|
|
|
return unpinnedMemoList[i].DisplayTs > unpinnedMemoList[j].DisplayTs
|
2022-10-27 22:02:42 +08:00
|
|
|
})
|
|
|
|
|
2022-11-01 09:12:00 +08:00
|
|
|
memoList := []*api.Memo{}
|
2022-10-29 20:54:54 +08:00
|
|
|
memoList = append(memoList, pinnedMemoList...)
|
|
|
|
memoList = append(memoList, unpinnedMemoList...)
|
|
|
|
|
2022-10-27 22:02:42 +08:00
|
|
|
if memoFind.Limit != 0 {
|
2022-10-29 20:54:54 +08:00
|
|
|
memoList = memoList[memoFind.Offset:common.Min(len(memoList), memoFind.Offset+memoFind.Limit)]
|
2022-10-27 22:02:42 +08:00
|
|
|
}
|
2022-10-21 19:57:57 +08:00
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-10-29 20:54:54 +08:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memoList)); err != nil {
|
2022-02-05 11:43:25 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo list response").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2022-10-31 20:57:07 +08:00
|
|
|
g.GET("/memo/amount", func(c echo.Context) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
normalRowStatus := api.Normal
|
|
|
|
memoFind := &api.MemoFind{
|
|
|
|
RowStatus: &normalRowStatus,
|
|
|
|
}
|
|
|
|
if userID, err := strconv.Atoi(c.QueryParam("userId")); err == nil {
|
|
|
|
memoFind.CreatorID = &userID
|
|
|
|
}
|
|
|
|
|
|
|
|
memoList, err := s.Store.FindMemoList(ctx, memoFind)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(len(memoList))); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo amount").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-10-21 22:51:41 +08:00
|
|
|
g.GET("/memo/stats", func(c echo.Context) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
normalStatus := api.Normal
|
|
|
|
memoFind := &api.MemoFind{
|
|
|
|
RowStatus: &normalStatus,
|
|
|
|
}
|
2022-11-01 22:06:02 +08:00
|
|
|
if creatorID, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
|
|
|
|
memoFind.CreatorID = &creatorID
|
|
|
|
}
|
|
|
|
if memoFind.CreatorID == nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find memo")
|
2022-10-21 22:51:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
currentUserID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public}
|
|
|
|
} else {
|
2022-11-01 22:06:02 +08:00
|
|
|
if *memoFind.CreatorID != currentUserID {
|
2022-10-21 22:51:41 +08:00
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public, api.Protected}
|
2022-11-01 22:06:02 +08:00
|
|
|
} else {
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public, api.Protected, api.Privite}
|
2022-10-21 22:51:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := s.Store.FindMemoList(ctx, memoFind)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
displayTsList := []int64{}
|
|
|
|
for _, memo := range list {
|
|
|
|
displayTsList = append(displayTsList, memo.DisplayTs)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(displayTsList)); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo stats response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-09-05 21:14:17 +08:00
|
|
|
g.GET("/memo/all", func(c echo.Context) error {
|
|
|
|
ctx := c.Request().Context()
|
|
|
|
memoFind := &api.MemoFind{}
|
|
|
|
|
|
|
|
_, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public}
|
|
|
|
} else {
|
|
|
|
memoFind.VisibilityList = []api.Visibility{api.Public, api.Protected}
|
|
|
|
}
|
|
|
|
|
|
|
|
pinnedStr := c.QueryParam("pinned")
|
|
|
|
if pinnedStr != "" {
|
|
|
|
pinned := pinnedStr == "true"
|
|
|
|
memoFind.Pinned = &pinned
|
|
|
|
}
|
|
|
|
tag := c.QueryParam("tag")
|
|
|
|
if tag != "" {
|
|
|
|
contentSearch := "#" + tag + " "
|
|
|
|
memoFind.ContentSearch = &contentSearch
|
|
|
|
}
|
|
|
|
visibilitListStr := c.QueryParam("visibility")
|
|
|
|
if visibilitListStr != "" {
|
|
|
|
visibilityList := []api.Visibility{}
|
|
|
|
for _, visibility := range strings.Split(visibilitListStr, ",") {
|
|
|
|
visibilityList = append(visibilityList, api.Visibility(visibility))
|
|
|
|
}
|
|
|
|
memoFind.VisibilityList = visibilityList
|
|
|
|
}
|
|
|
|
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-09-09 00:06:05 +08:00
|
|
|
// Only fetch normal status memos.
|
|
|
|
normalStatus := api.Normal
|
|
|
|
memoFind.RowStatus = &normalStatus
|
|
|
|
|
2022-09-05 21:14:17 +08:00
|
|
|
list, err := s.Store.FindMemoList(ctx, memoFind)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch all memo list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:57:57 +08:00
|
|
|
sort.Slice(list, func(i, j int) bool {
|
|
|
|
return list[i].DisplayTs > list[j].DisplayTs
|
|
|
|
})
|
|
|
|
|
2022-11-01 22:57:33 +08:00
|
|
|
if memoFind.Limit != 0 {
|
|
|
|
list = list[memoFind.Offset:common.Min(len(list), memoFind.Offset+memoFind.Limit)]
|
|
|
|
}
|
|
|
|
|
2022-09-05 21:14:17 +08:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(list)); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode all memo list response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-09-21 19:31:02 +08:00
|
|
|
g.GET("/memo/:memoId", func(c echo.Context) error {
|
2022-08-07 10:17:12 +08:00
|
|
|
ctx := c.Request().Context()
|
2022-05-19 18:32:04 +08: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-09-21 19:31:02 +08:00
|
|
|
memoFind := &api.MemoFind{
|
2022-05-19 18:32:04 +08:00
|
|
|
ID: &memoID,
|
2022-09-21 19:31:02 +08:00
|
|
|
}
|
|
|
|
memo, err := s.Store.FindMemo(ctx, memoFind)
|
2022-05-19 18:32:04 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-09-24 09:34:01 +08:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
2022-09-21 19:31:02 +08:00
|
|
|
if memo.Visibility == api.Privite {
|
2022-09-24 09:34:01 +08:00
|
|
|
if !ok || memo.CreatorID != userID {
|
|
|
|
return echo.NewHTTPError(http.StatusForbidden, "this memo is private only")
|
|
|
|
}
|
2022-09-21 19:31:02 +08:00
|
|
|
} else if memo.Visibility == api.Protected {
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusForbidden, "this memo is protected, missing user in session")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:32:04 +08:00
|
|
|
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-09-21 19:31:02 +08:00
|
|
|
g.POST("/memo/:memoId/organizer", func(c echo.Context) error {
|
2022-08-07 10:17:12 +08:00
|
|
|
ctx := c.Request().Context()
|
2022-05-03 02:05:43 +08:00
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:31:02 +08:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-09-21 19:31:02 +08: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.Store.UpsertMemoOrganizer(ctx, memoOrganizerUpsert)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo organizer").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
memo, err := s.Store.FindMemo(ctx, &api.MemoFind{
|
|
|
|
ID: &memoID,
|
|
|
|
})
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
if common.ErrorCode(err) == common.NotFound {
|
2022-05-03 02:05:43 +08:00
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID)).SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-02-06 00:25:41 +08:00
|
|
|
|
2022-05-03 02:05:43 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find memo by ID: %v", memoID)).SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 17:06:04 +08:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(memo)); err != nil {
|
2022-02-05 11:43:25 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode memo response").SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 22:21:10 +08:00
|
|
|
|
2022-09-30 22:58:59 +08:00
|
|
|
g.POST("/memo/:memoId/resource", 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTs := time.Now().Unix()
|
|
|
|
memoResourceUpsert := &api.MemoResourceUpsert{
|
|
|
|
MemoID: memoID,
|
|
|
|
UpdatedTs: ¤tTs,
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(memoResourceUpsert); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo resource request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-10-01 00:06:56 +08:00
|
|
|
if _, err := s.Store.UpsertMemoResource(ctx, memoResourceUpsert); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo resource").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-09-30 22:58:59 +08:00
|
|
|
resourceFind := &api.ResourceFind{
|
|
|
|
ID: &memoResourceUpsert.ResourceID,
|
|
|
|
}
|
|
|
|
resource, err := s.Store.FindResource(ctx, resourceFind)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resource)); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
g.GET("/memo/:memoId/resource", 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceFind := &api.ResourceFind{
|
|
|
|
MemoID: &memoID,
|
|
|
|
}
|
|
|
|
resourceList, err := s.Store.FindResourceList(ctx, resourceFind)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(resourceList)); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode resource list response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
g.DELETE("/memo/:memoId/resource/:resourceId", 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("Memo ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
resourceID, err := strconv.Atoi(c.Param("resourceId"))
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Resource ID is not a number: %s", c.Param("resourceId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
memoResourceDelete := &api.MemoResourceDelete{
|
2022-11-06 12:21:58 +08:00
|
|
|
MemoID: &memoID,
|
2022-09-30 22:58:59 +08:00
|
|
|
ResourceID: &resourceID,
|
|
|
|
}
|
|
|
|
if err := s.Store.DeleteMemoResource(ctx, memoResourceDelete); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch resource list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, true)
|
|
|
|
})
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
g.DELETE("/memo/:memoId", func(c echo.Context) error {
|
2022-08-07 10:17:12 +08:00
|
|
|
ctx := c.Request().Context()
|
2022-09-24 09:34:01 +08:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
|
|
|
|
2022-05-03 02:05:43 +08:00
|
|
|
memoID, err := strconv.Atoi(c.Param("memoId"))
|
2022-02-03 15:32:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("memoId"))).SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-09-24 09:34:01 +08:00
|
|
|
memoFind := &api.MemoFind{
|
|
|
|
ID: &memoID,
|
|
|
|
CreatorID: &userID,
|
|
|
|
}
|
|
|
|
if _, err := s.Store.FindMemo(ctx, memoFind); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-02-03 15:32:03 +08:00
|
|
|
memoDelete := &api.MemoDelete{
|
2022-05-17 21:21:13 +08:00
|
|
|
ID: memoID,
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
2022-08-07 10:17:12 +08:00
|
|
|
if err := s.Store.DeleteMemo(ctx, memoDelete); err != nil {
|
2022-09-03 18:54:22 +08:00
|
|
|
if common.ErrorCode(err) == common.NotFound {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Memo ID not found: %d", memoID))
|
|
|
|
}
|
2022-05-03 02:05:43 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete memo ID: %v", memoID)).SetInternal(err)
|
2022-02-03 15:32:03 +08:00
|
|
|
}
|
|
|
|
|
2022-07-02 10:47:16 +08:00
|
|
|
return c.JSON(http.StatusOK, true)
|
2022-02-03 15:32:03 +08:00
|
|
|
})
|
|
|
|
}
|