memos/server/memo.go

705 lines
25 KiB
Go
Raw Normal View History

2022-02-03 10:32:03 +03:00
package server
import (
2023-05-25 16:50:37 +03:00
"context"
"encoding/json"
2022-02-03 10:32:03 +03:00
"fmt"
"net/http"
"strconv"
"strings"
2022-09-30 17:58:59 +03:00
"time"
2022-02-03 10:32:03 +03:00
"github.com/pkg/errors"
2022-06-27 17:09:06 +03:00
"github.com/usememos/memos/api"
"github.com/usememos/memos/common"
"github.com/usememos/memos/store"
2022-06-27 17:09:06 +03:00
2022-02-03 10:32:03 +03:00
"github.com/labstack/echo/v4"
)
2023-05-25 16:50:37 +03:00
// maxContentLength means the max memo content bytes is 1MB.
const maxContentLength = 1 << 30
2022-02-03 10:32:03 +03:00
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-09-24 04:34:01 +03:00
2023-05-25 16:50:37 +03:00
createMemoRequest := &api.CreateMemoRequest{}
if err := json.NewDecoder(c.Request().Body).Decode(createMemoRequest); err != nil {
2022-02-03 10:32:03 +03:00
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo request").SetInternal(err)
}
2023-05-25 16:50:37 +03:00
if len(createMemoRequest.Content) > maxContentLength {
return echo.NewHTTPError(http.StatusBadRequest, "Content size overflow, up to 1MB")
}
2023-05-25 16:50:37 +03:00
if createMemoRequest.Visibility == "" {
userMemoVisibilitySetting, err := s.Store.FindUserSetting(ctx, &api.UserSettingFind{
UserID: &userID,
Key: api.UserSettingMemoVisibilityKey,
})
2022-08-20 16:51:28 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user setting").SetInternal(err)
}
if userMemoVisibilitySetting != nil {
2022-11-26 09:23:29 +03:00
memoVisibility := api.Private
err := json.Unmarshal([]byte(userMemoVisibilitySetting.Value), &memoVisibility)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal user setting value").SetInternal(err)
}
2023-05-25 16:50:37 +03:00
createMemoRequest.Visibility = memoVisibility
} else {
// Private is the default memo visibility.
2023-05-25 16:50:37 +03:00
createMemoRequest.Visibility = api.Private
2022-08-20 16:51:28 +03:00
}
}
2023-05-25 16:50:37 +03:00
// Find disable public memos system setting.
disablePublicMemosSystemSetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
Name: api.SystemSettingDisablePublicMemosName,
})
if err != nil && common.ErrorCode(err) != common.NotFound {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting").SetInternal(err)
}
if disablePublicMemosSystemSetting != nil {
disablePublicMemos := false
err = json.Unmarshal([]byte(disablePublicMemosSystemSetting.Value), &disablePublicMemos)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to unmarshal system setting").SetInternal(err)
}
if disablePublicMemos {
user, err := s.Store.FindUser(ctx, &api.UserFind{
ID: &userID,
})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
}
2023-05-25 16:50:37 +03:00
// Enforce normal user to create private memo if public memos are disabled.
if user.Role == "USER" {
2023-05-25 16:50:37 +03:00
createMemoRequest.Visibility = api.Private
}
}
}
2023-05-25 16:50:37 +03:00
createMemoRequest.CreatorID = userID
memoMessage, err := s.Store.CreateMemo(ctx, convertCreateMemoRequestToMemoMessage(createMemoRequest))
2022-02-03 10:32:03 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create memo").SetInternal(err)
}
if err := createMemoCreateActivity(c.Request().Context(), s.Store, memoMessage); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create activity").SetInternal(err)
}
2022-02-03 10:32:03 +03:00
2023-05-25 16:50:37 +03:00
for _, resourceID := range createMemoRequest.ResourceIDList {
2022-10-01 05:57:14 +03:00
if _, err := s.Store.UpsertMemoResource(ctx, &api.MemoResourceUpsert{
2023-05-25 16:50:37 +03:00
MemoID: memoMessage.ID,
2022-10-01 05:57:14 +03:00
ResourceID: resourceID,
}); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo resource").SetInternal(err)
}
}
2023-05-25 16:50:37 +03:00
for _, memoRelationUpsert := range createMemoRequest.RelationList {
if _, err := s.Store.UpsertMemoRelation(ctx, &store.MemoRelationMessage{
2023-05-25 16:50:37 +03:00
MemoID: memoMessage.ID,
RelatedMemoID: memoRelationUpsert.RelatedMemoID,
Type: store.MemoRelationType(memoRelationUpsert.Type),
}); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo relation").SetInternal(err)
}
}
2023-05-25 16:50:37 +03:00
memoMessage, err = s.Store.GetMemo(ctx, &store.FindMemoMessage{
ID: &memoMessage.ID,
})
2022-10-01 05:57:14 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo").SetInternal(err)
}
2023-05-25 19:38:27 +03:00
memoResponse, err := s.composeMemoMessageToMemoResponse(ctx, memoMessage)
2023-05-25 16:50:37 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo response").SetInternal(err)
}
return c.JSON(http.StatusOK, composeResponse(memoResponse))
2022-02-03 10:32:03 +03:00
})
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-09-24 04:34:01 +03:00
userID, ok := c.Get(getUserIDContextKey()).(int)
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}
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)
}
2023-05-25 16:50:37 +03:00
memoMessage, err := s.Store.GetMemo(ctx, &store.FindMemoMessage{
2022-12-28 15:22:52 +03:00
ID: &memoID,
})
if err != nil {
2022-09-24 04:34:01 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo").SetInternal(err)
}
2023-05-25 16:50:37 +03:00
if memoMessage.CreatorID != userID {
2022-12-28 15:22:52 +03:00
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
}
2022-09-24 04:34:01 +03:00
currentTs := time.Now().Unix()
2023-05-25 16:50:37 +03:00
patchMemoRequest := &api.PatchMemoRequest{
ID: memoID,
UpdatedTs: &currentTs,
2022-02-03 10:32:03 +03:00
}
2023-05-25 16:50:37 +03:00
if err := json.NewDecoder(c.Request().Body).Decode(patchMemoRequest); err != nil {
2022-02-03 10:32:03 +03:00
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch memo request").SetInternal(err)
}
2023-05-25 16:50:37 +03:00
if patchMemoRequest.Content != nil && len(*patchMemoRequest.Content) > maxContentLength {
return echo.NewHTTPError(http.StatusBadRequest, "Content size overflow, up to 1MB").SetInternal(err)
}
2023-05-25 16:50:37 +03:00
updateMemoMessage := &store.UpdateMemoMessage{
ID: memoID,
CreatedTs: patchMemoRequest.CreatedTs,
UpdatedTs: patchMemoRequest.UpdatedTs,
Content: patchMemoRequest.Content,
}
if patchMemoRequest.RowStatus != nil {
rowStatus := store.RowStatus(patchMemoRequest.RowStatus.String())
updateMemoMessage.RowStatus = &rowStatus
}
if patchMemoRequest.Visibility != nil {
visibility := store.Visibility(patchMemoRequest.Visibility.String())
updateMemoMessage.Visibility = &visibility
}
err = s.Store.UpdateMemo(ctx, updateMemoMessage)
2022-02-03 10:32:03 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch memo").SetInternal(err)
}
2023-05-25 16:50:37 +03:00
memoMessage, err = s.Store.GetMemo(ctx, &store.FindMemoMessage{ID: &memoID})
if err != nil {
2023-05-25 16:50:37 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo").SetInternal(err)
}
2022-02-03 10:32:03 +03:00
2023-05-25 16:50:37 +03:00
if patchMemoRequest.ResourceIDList != nil {
addedResourceIDList, removedResourceIDList := getIDListDiff(memoMessage.ResourceIDList, patchMemoRequest.ResourceIDList)
2023-05-21 06:51:13 +03:00
for _, resourceID := range addedResourceIDList {
if _, err := s.Store.UpsertMemoResource(ctx, &api.MemoResourceUpsert{
2023-05-25 16:50:37 +03:00
MemoID: memoMessage.ID,
2023-05-21 06:51:13 +03:00
ResourceID: resourceID,
}); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo resource").SetInternal(err)
}
}
for _, resourceID := range removedResourceIDList {
if err := s.Store.DeleteMemoResource(ctx, &api.MemoResourceDelete{
2023-05-25 16:50:37 +03:00
MemoID: &memoMessage.ID,
2023-05-21 06:51:13 +03:00
ResourceID: &resourceID,
}); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete memo resource").SetInternal(err)
}
}
}
2023-05-25 16:50:37 +03:00
if patchMemoRequest.RelationList != nil {
patchMemoRelationList := make([]*store.MemoRelationMessage, 0)
for _, memoRelation := range patchMemoRequest.RelationList {
patchMemoRelationList = append(patchMemoRelationList, &store.MemoRelationMessage{
MemoID: memoMessage.ID,
RelatedMemoID: memoRelation.RelatedMemoID,
Type: store.MemoRelationType(memoRelation.Type),
2023-05-21 06:51:13 +03:00
})
}
2023-05-25 16:50:37 +03:00
addedMemoRelationList, removedMemoRelationList := getMemoRelationListDiff(memoMessage.RelationList, patchMemoRelationList)
2023-05-21 06:51:13 +03:00
for _, memoRelation := range addedMemoRelationList {
2023-05-25 16:50:37 +03:00
if _, err := s.Store.UpsertMemoRelation(ctx, memoRelation); err != nil {
2023-05-21 06:51:13 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo relation").SetInternal(err)
}
}
for _, memoRelation := range removedMemoRelationList {
if err := s.Store.DeleteMemoRelation(ctx, &store.DeleteMemoRelationMessage{
2023-05-25 16:50:37 +03:00
MemoID: &memoMessage.ID,
2023-05-21 06:51:13 +03:00
RelatedMemoID: &memoRelation.RelatedMemoID,
2023-05-25 16:50:37 +03:00
Type: &memoRelation.Type,
2023-05-21 06:51:13 +03:00
}); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete memo relation").SetInternal(err)
}
}
}
2023-05-25 16:50:37 +03:00
memoMessage, err = s.Store.GetMemo(ctx, &store.FindMemoMessage{ID: &memoID})
if err != nil {
2023-05-25 16:50:37 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo").SetInternal(err)
}
2023-05-25 19:38:27 +03:00
memoResponse, err := s.composeMemoMessageToMemoResponse(ctx, memoMessage)
2023-05-25 16:50:37 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo response").SetInternal(err)
}
2023-05-25 16:50:37 +03:00
return c.JSON(http.StatusOK, composeResponse(memoResponse))
2022-02-03 10:32:03 +03:00
})
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()
2023-05-25 16:50:37 +03:00
findMemoMessage := &store.FindMemoMessage{}
if userID, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
2023-05-25 16:50:37 +03:00
findMemoMessage.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 {
2023-05-25 16:50:37 +03:00
if findMemoMessage.CreatorID == nil {
return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find memo")
}
2023-05-25 16:50:37 +03:00
findMemoMessage.VisibilityList = []store.Visibility{store.Public}
} else {
2023-05-25 16:50:37 +03:00
if findMemoMessage.CreatorID == nil {
findMemoMessage.CreatorID = &currentUserID
} else {
2023-05-25 16:50:37 +03:00
findMemoMessage.VisibilityList = []store.Visibility{store.Public, store.Protected}
}
}
2023-05-25 16:50:37 +03:00
rowStatus := store.RowStatus(c.QueryParam("rowStatus"))
2022-03-28 19:01:34 +03:00
if rowStatus != "" {
2023-05-25 16:50:37 +03:00
findMemoMessage.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"
2023-05-25 16:50:37 +03:00
findMemoMessage.Pinned = &pinned
2022-05-19 13:32:04 +03:00
}
contentSearch := []string{}
2022-06-14 18:09:03 +03:00
tag := c.QueryParam("tag")
if tag != "" {
contentSearch = append(contentSearch, "#"+tag)
}
contentSlice := c.QueryParams()["content"]
if len(contentSlice) > 0 {
contentSearch = append(contentSearch, contentSlice...)
2022-06-21 16:58:33 +03:00
}
findMemoMessage.ContentSearch = contentSearch
2022-11-26 09:23:29 +03:00
visibilityListStr := c.QueryParam("visibility")
if visibilityListStr != "" {
2023-05-25 16:50:37 +03:00
visibilityList := []store.Visibility{}
2022-11-26 09:23:29 +03:00
for _, visibility := range strings.Split(visibilityListStr, ",") {
2023-05-25 16:50:37 +03:00
visibilityList = append(visibilityList, store.Visibility(visibility))
}
2023-05-25 16:50:37 +03:00
findMemoMessage.VisibilityList = visibilityList
}
2022-06-21 16:58:33 +03:00
if limit, err := strconv.Atoi(c.QueryParam("limit")); err == nil {
2023-05-25 16:50:37 +03:00
findMemoMessage.Limit = &limit
2022-06-21 16:58:33 +03:00
}
if offset, err := strconv.Atoi(c.QueryParam("offset")); err == nil {
2023-05-25 16:50:37 +03:00
findMemoMessage.Offset = &offset
2022-06-14 18:09:03 +03:00
}
2022-02-04 16:24:21 +03:00
memoDisplayWithUpdatedTs, err := s.getMemoDisplayWithUpdatedTsSettingValue(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get memo display with updated ts setting value").SetInternal(err)
}
if memoDisplayWithUpdatedTs {
findMemoMessage.OrderByUpdatedTs = true
}
2023-05-25 16:50:37 +03:00
memoMessageList, err := s.Store.ListMemos(ctx, findMemoMessage)
2022-02-03 10:32:03 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch memo list").SetInternal(err)
}
2023-05-25 16:50:37 +03:00
memoResponseList := []*api.MemoResponse{}
for _, memoMessage := range memoMessageList {
2023-05-25 19:38:27 +03:00
memoResponse, err := s.composeMemoMessageToMemoResponse(ctx, memoMessage)
2023-05-25 16:50:37 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo response").SetInternal(err)
}
memoResponseList = append(memoResponseList, memoResponse)
}
return c.JSON(http.StatusOK, composeResponse(memoResponseList))
2022-02-03 10:32:03 +03:00
})
2022-02-18 17:21:10 +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-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)
}
2023-05-25 16:50:37 +03:00
memoMessage, err := s.Store.GetMemo(ctx, &store.FindMemoMessage{
2022-05-19 13:32:04 +03:00
ID: &memoID,
2023-05-25 16:50:37 +03:00
})
2022-05-19 13:32:04 +03: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 04:34:01 +03:00
userID, ok := c.Get(getUserIDContextKey()).(int)
2023-05-25 16:50:37 +03:00
if memoMessage.Visibility == store.Private {
if !ok || memoMessage.CreatorID != userID {
2022-09-24 04:34:01 +03:00
return echo.NewHTTPError(http.StatusForbidden, "this memo is private only")
}
2023-05-25 16:50:37 +03:00
} else if memoMessage.Visibility == store.Protected {
if !ok {
return echo.NewHTTPError(http.StatusForbidden, "this memo is protected, missing user in session")
}
}
2023-05-25 19:38:27 +03:00
memoResponse, err := s.composeMemoMessageToMemoResponse(ctx, memoMessage)
2023-05-25 16:50:37 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo response").SetInternal(err)
}
return c.JSON(http.StatusOK, composeResponse(memoResponse))
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-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)
}
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
}
2022-12-28 15:22:52 +03:00
memoOrganizerUpsert := &api.MemoOrganizerUpsert{}
if err := json.NewDecoder(c.Request().Body).Decode(memoOrganizerUpsert); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post memo organizer request").SetInternal(err)
}
2022-12-28 15:22:52 +03:00
memoOrganizerUpsert.MemoID = memoID
memoOrganizerUpsert.UserID = userID
err = s.Store.UpsertMemoOrganizer(ctx, memoOrganizerUpsert)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo organizer").SetInternal(err)
}
2023-05-25 16:50:37 +03:00
memoMessage, err := s.Store.GetMemo(ctx, &store.FindMemoMessage{
ID: &memoID,
})
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-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
}
2023-05-25 19:38:27 +03:00
memoResponse, err := s.composeMemoMessageToMemoResponse(ctx, memoMessage)
2023-05-25 16:50:37 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo response").SetInternal(err)
}
return c.JSON(http.StatusOK, composeResponse(memoResponse))
2022-02-03 10:32:03 +03:00
})
2022-02-18 17:21:10 +03:00
2022-12-28 15:22:52 +03:00
g.GET("/memo/stats", func(c echo.Context) error {
ctx := c.Request().Context()
2023-05-25 16:50:37 +03:00
normalStatus := store.Normal
findMemoMessage := &store.FindMemoMessage{
2022-12-28 15:22:52 +03:00
RowStatus: &normalStatus,
}
if creatorID, err := strconv.Atoi(c.QueryParam("creatorId")); err == nil {
2023-05-25 16:50:37 +03:00
findMemoMessage.CreatorID = &creatorID
2022-12-28 15:22:52 +03:00
}
2023-05-25 16:50:37 +03:00
if findMemoMessage.CreatorID == nil {
2022-12-28 15:22:52 +03:00
return echo.NewHTTPError(http.StatusBadRequest, "Missing user id to find memo")
}
currentUserID, ok := c.Get(getUserIDContextKey()).(int)
if !ok {
2023-05-25 16:50:37 +03:00
findMemoMessage.VisibilityList = []store.Visibility{store.Public}
2022-12-28 15:22:52 +03:00
} else {
2023-05-25 16:50:37 +03:00
if *findMemoMessage.CreatorID != currentUserID {
findMemoMessage.VisibilityList = []store.Visibility{store.Public, store.Protected}
2022-12-28 15:22:52 +03:00
} else {
2023-05-25 16:50:37 +03:00
findMemoMessage.VisibilityList = []store.Visibility{store.Public, store.Protected, store.Private}
2022-12-28 15:22:52 +03:00
}
}
memoDisplayWithUpdatedTs, err := s.getMemoDisplayWithUpdatedTsSettingValue(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get memo display with updated ts setting value").SetInternal(err)
}
if memoDisplayWithUpdatedTs {
findMemoMessage.OrderByUpdatedTs = true
}
memoMessageList, err := s.Store.ListMemos(ctx, findMemoMessage)
2022-09-30 17:58:59 +03:00
if err != nil {
2023-05-25 16:50:37 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
2022-09-30 17:58:59 +03:00
}
memoResponseList := []*api.MemoResponse{}
for _, memoMessage := range memoMessageList {
memoResponse, err := s.composeMemoMessageToMemoResponse(ctx, memoMessage)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo response").SetInternal(err)
}
memoResponseList = append(memoResponseList, memoResponse)
}
2022-09-30 17:58:59 +03:00
displayTsList := []int64{}
for _, memo := range memoResponseList {
displayTsList = append(displayTsList, memo.DisplayTs)
2022-09-30 17:58:59 +03:00
}
return c.JSON(http.StatusOK, composeResponse(displayTsList))
2022-12-28 15:22:52 +03:00
})
2022-09-30 17:58:59 +03:00
2022-12-28 15:22:52 +03:00
g.GET("/memo/all", func(c echo.Context) error {
ctx := c.Request().Context()
2023-05-25 16:50:37 +03:00
findMemoMessage := &store.FindMemoMessage{}
2022-12-28 15:22:52 +03:00
_, ok := c.Get(getUserIDContextKey()).(int)
if !ok {
2023-05-25 16:50:37 +03:00
findMemoMessage.VisibilityList = []store.Visibility{store.Public}
2022-12-28 15:22:52 +03:00
} else {
2023-05-25 16:50:37 +03:00
findMemoMessage.VisibilityList = []store.Visibility{store.Public, store.Protected}
2022-12-28 15:22:52 +03:00
}
pinnedStr := c.QueryParam("pinned")
if pinnedStr != "" {
pinned := pinnedStr == "true"
2023-05-25 16:50:37 +03:00
findMemoMessage.Pinned = &pinned
2022-12-28 15:22:52 +03:00
}
contentSearch := []string{}
2022-12-28 15:22:52 +03:00
tag := c.QueryParam("tag")
if tag != "" {
contentSearch = append(contentSearch, "#"+tag+" ")
}
contentSlice := c.QueryParams()["content"]
if len(contentSlice) > 0 {
contentSearch = append(contentSearch, contentSlice...)
2022-12-28 15:22:52 +03:00
}
findMemoMessage.ContentSearch = contentSearch
2022-12-28 15:22:52 +03:00
visibilityListStr := c.QueryParam("visibility")
if visibilityListStr != "" {
2023-05-25 16:50:37 +03:00
visibilityList := []store.Visibility{}
2022-12-28 15:22:52 +03:00
for _, visibility := range strings.Split(visibilityListStr, ",") {
2023-05-25 16:50:37 +03:00
visibilityList = append(visibilityList, store.Visibility(visibility))
2022-12-28 15:22:52 +03:00
}
2023-05-25 16:50:37 +03:00
findMemoMessage.VisibilityList = visibilityList
2022-12-28 15:22:52 +03:00
}
if limit, err := strconv.Atoi(c.QueryParam("limit")); err == nil {
2023-05-25 16:50:37 +03:00
findMemoMessage.Limit = &limit
2022-12-28 15:22:52 +03:00
}
if offset, err := strconv.Atoi(c.QueryParam("offset")); err == nil {
2023-05-25 16:50:37 +03:00
findMemoMessage.Offset = &offset
2022-12-28 15:22:52 +03:00
}
// Only fetch normal status memos.
2023-05-25 16:50:37 +03:00
normalStatus := store.Normal
findMemoMessage.RowStatus = &normalStatus
2022-12-28 15:22:52 +03:00
memoDisplayWithUpdatedTs, err := s.getMemoDisplayWithUpdatedTsSettingValue(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get memo display with updated ts setting value").SetInternal(err)
}
if memoDisplayWithUpdatedTs {
findMemoMessage.OrderByUpdatedTs = true
}
2023-05-25 16:50:37 +03:00
memoMessageList, err := s.Store.ListMemos(ctx, findMemoMessage)
2022-12-28 15:22:52 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch all memo list").SetInternal(err)
}
2023-05-25 16:50:37 +03:00
memoResponseList := []*api.MemoResponse{}
for _, memoMessage := range memoMessageList {
2023-05-25 19:38:27 +03:00
memoResponse, err := s.composeMemoMessageToMemoResponse(ctx, memoMessage)
2023-05-25 16:50:37 +03:00
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo response").SetInternal(err)
}
memoResponseList = append(memoResponseList, memoResponse)
}
return c.JSON(http.StatusOK, composeResponse(memoResponseList))
2022-09-30 17:58:59 +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-09-24 04:34:01 +03:00
userID, ok := c.Get(getUserIDContextKey()).(int)
if !ok {
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
}
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)
}
2023-05-25 16:50:37 +03:00
memo, err := s.Store.GetMemo(ctx, &store.FindMemoMessage{
2022-12-28 15:22:52 +03:00
ID: &memoID,
})
if err != nil {
2022-09-24 04:34:01 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo").SetInternal(err)
}
2022-12-28 15:22:52 +03:00
if memo.CreatorID != userID {
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized")
}
2022-09-24 04:34:01 +03:00
2023-05-25 16:50:37 +03:00
if err := s.Store.DeleteMemo(ctx, &store.DeleteMemoMessage{
2022-05-17 16:21:13 +03:00
ID: memoID,
2023-05-25 16:50:37 +03:00
}); err != nil {
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
}
return c.JSON(http.StatusOK, true)
2022-02-03 10:32:03 +03:00
})
}
func createMemoCreateActivity(ctx context.Context, store *store.Store, memo *store.MemoMessage) error {
payload := api.ActivityMemoCreatePayload{
Content: memo.Content,
Visibility: memo.Visibility.String(),
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return errors.Wrap(err, "failed to marshal activity payload")
}
activity, err := store.CreateActivity(ctx, &api.ActivityCreate{
CreatorID: memo.CreatorID,
Type: api.ActivityMemoCreate,
Level: api.ActivityInfo,
Payload: string(payloadBytes),
})
2023-01-07 06:49:58 +03:00
if err != nil || activity == nil {
return errors.Wrap(err, "failed to create activity")
}
return err
}
func getIDListDiff(oldList, newList []int) (addedList, removedList []int) {
oldMap := map[int]bool{}
for _, id := range oldList {
oldMap[id] = true
}
newMap := map[int]bool{}
for _, id := range newList {
newMap[id] = true
}
for id := range oldMap {
if !newMap[id] {
removedList = append(removedList, id)
}
}
for id := range newMap {
if !oldMap[id] {
addedList = append(addedList, id)
}
}
return addedList, removedList
}
2023-05-25 16:50:37 +03:00
func getMemoRelationListDiff(oldList, newList []*store.MemoRelationMessage) (addedList, removedList []*store.MemoRelationMessage) {
oldMap := map[string]bool{}
for _, relation := range oldList {
oldMap[fmt.Sprintf("%d-%s", relation.RelatedMemoID, relation.Type)] = true
}
newMap := map[string]bool{}
for _, relation := range newList {
newMap[fmt.Sprintf("%d-%s", relation.RelatedMemoID, relation.Type)] = true
}
for _, relation := range oldList {
key := fmt.Sprintf("%d-%s", relation.RelatedMemoID, relation.Type)
if !newMap[key] {
removedList = append(removedList, relation)
}
}
for _, relation := range newList {
key := fmt.Sprintf("%d-%s", relation.RelatedMemoID, relation.Type)
if !oldMap[key] {
addedList = append(addedList, relation)
}
}
return addedList, removedList
}
2023-05-25 16:50:37 +03:00
func convertCreateMemoRequestToMemoMessage(memoCreate *api.CreateMemoRequest) *store.MemoMessage {
createdTs := time.Now().Unix()
if memoCreate.CreatedTs != nil {
createdTs = *memoCreate.CreatedTs
}
return &store.MemoMessage{
CreatorID: memoCreate.CreatorID,
CreatedTs: createdTs,
Content: memoCreate.Content,
Visibility: store.Visibility(memoCreate.Visibility),
}
}
2023-05-25 19:38:27 +03:00
func (s *Server) composeMemoMessageToMemoResponse(ctx context.Context, memoMessage *store.MemoMessage) (*api.MemoResponse, error) {
memoResponse := &api.MemoResponse{
ID: memoMessage.ID,
RowStatus: api.RowStatus(memoMessage.RowStatus.String()),
CreatorID: memoMessage.CreatorID,
CreatedTs: memoMessage.CreatedTs,
UpdatedTs: memoMessage.UpdatedTs,
Content: memoMessage.Content,
Visibility: api.Visibility(memoMessage.Visibility.String()),
Pinned: memoMessage.Pinned,
2023-05-25 16:50:37 +03:00
}
// Compose creator name.
2023-05-25 16:50:37 +03:00
user, err := s.Store.FindUser(ctx, &api.UserFind{
ID: &memoResponse.CreatorID,
})
if err != nil {
return nil, err
}
if user.Nickname != "" {
memoResponse.CreatorName = user.Nickname
} else {
memoResponse.CreatorName = user.Username
}
// Compose display ts.
memoResponse.DisplayTs = memoResponse.CreatedTs
// Find memo display with updated ts setting.
memoDisplayWithUpdatedTs, err := s.getMemoDisplayWithUpdatedTsSettingValue(ctx)
if err != nil {
return nil, err
}
if memoDisplayWithUpdatedTs {
memoResponse.DisplayTs = memoResponse.UpdatedTs
}
2023-05-25 19:38:27 +03:00
relationList := []*api.MemoRelation{}
for _, relation := range memoMessage.RelationList {
relationList = append(relationList, convertMemoRelationMessageToMemoRelation(relation))
}
memoResponse.RelationList = relationList
resourceList := []*api.Resource{}
for _, resourceID := range memoMessage.ResourceIDList {
2023-05-25 16:50:37 +03:00
resource, err := s.Store.FindResource(ctx, &api.ResourceFind{
ID: &resourceID,
})
if err != nil {
return nil, err
}
2023-05-25 19:38:27 +03:00
resourceList = append(resourceList, resource)
2023-05-25 16:50:37 +03:00
}
2023-05-25 19:38:27 +03:00
memoResponse.ResourceList = resourceList
2023-05-25 16:50:37 +03:00
return memoResponse, nil
}
func (s *Server) getMemoDisplayWithUpdatedTsSettingValue(ctx context.Context) (bool, error) {
memoDisplayWithUpdatedTsSetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
Name: api.SystemSettingMemoDisplayWithUpdatedTsName,
})
if err != nil && common.ErrorCode(err) != common.NotFound {
return false, errors.Wrap(err, "failed to find system setting")
}
memoDisplayWithUpdatedTs := false
if memoDisplayWithUpdatedTsSetting != nil {
err = json.Unmarshal([]byte(memoDisplayWithUpdatedTsSetting.Value), &memoDisplayWithUpdatedTs)
if err != nil {
return false, errors.Wrap(err, "failed to unmarshal system setting value")
}
}
return memoDisplayWithUpdatedTs, nil
}