From 98b3a371f420afb4af215c7567b9eed0d3a3db8e Mon Sep 17 00:00:00 2001 From: boojack Date: Sun, 21 May 2023 11:51:13 +0800 Subject: [PATCH] fix: patch memo visibility (#1695) --- server/memo.go | 90 ++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/server/memo.go b/server/memo.go index f9d5135d..921d4884 100644 --- a/server/memo.go +++ b/server/memo.go @@ -163,54 +163,58 @@ func (s *Server) registerMemoRoutes(g *echo.Group) { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo").SetInternal(err) } - resourceIDList := make([]int, 0) - for _, resource := range memo.ResourceList { - resourceIDList = append(resourceIDList, resource.ID) - } - addedResourceIDList, removedResourceIDList := getIDListDiff(resourceIDList, memoPatch.ResourceIDList) - for _, resourceID := range addedResourceIDList { - 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) + if memoPatch.ResourceIDList != nil { + resourceIDList := make([]int, 0) + for _, resource := range memo.ResourceList { + resourceIDList = append(resourceIDList, resource.ID) } - } - for _, resourceID := range removedResourceIDList { - if err := s.Store.DeleteMemoResource(ctx, &api.MemoResourceDelete{ - MemoID: &memo.ID, - ResourceID: &resourceID, - }); err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete memo resource").SetInternal(err) + addedResourceIDList, removedResourceIDList := getIDListDiff(resourceIDList, memoPatch.ResourceIDList) + for _, resourceID := range addedResourceIDList { + 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) + } + } + for _, resourceID := range removedResourceIDList { + if err := s.Store.DeleteMemoResource(ctx, &api.MemoResourceDelete{ + MemoID: &memo.ID, + ResourceID: &resourceID, + }); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete memo resource").SetInternal(err) + } } } - patchMemoRelationList := make([]*api.MemoRelation, 0) - for _, memoRelationUpsert := range memoPatch.RelationList { - patchMemoRelationList = append(patchMemoRelationList, &api.MemoRelation{ - MemoID: memo.ID, - RelatedMemoID: memoRelationUpsert.RelatedMemoID, - Type: memoRelationUpsert.Type, - }) - } - addedMemoRelationList, removedMemoRelationList := getMemoRelationListDiff(memo.RelationList, patchMemoRelationList) - for _, memoRelation := range addedMemoRelationList { - if _, err := s.Store.UpsertMemoRelation(ctx, &store.MemoRelationMessage{ - MemoID: memo.ID, - RelatedMemoID: memoRelation.RelatedMemoID, - Type: store.MemoRelationType(memoRelation.Type), - }); err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo relation").SetInternal(err) + if memoPatch.RelationList != nil { + patchMemoRelationList := make([]*api.MemoRelation, 0) + for _, memoRelationUpsert := range memoPatch.RelationList { + patchMemoRelationList = append(patchMemoRelationList, &api.MemoRelation{ + MemoID: memo.ID, + RelatedMemoID: memoRelationUpsert.RelatedMemoID, + Type: memoRelationUpsert.Type, + }) } - } - for _, memoRelation := range removedMemoRelationList { - memoRelationType := store.MemoRelationType(memoRelation.Type) - if err := s.Store.DeleteMemoRelation(ctx, &store.DeleteMemoRelationMessage{ - MemoID: &memo.ID, - RelatedMemoID: &memoRelation.RelatedMemoID, - Type: &memoRelationType, - }); err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete memo relation").SetInternal(err) + addedMemoRelationList, removedMemoRelationList := getMemoRelationListDiff(memo.RelationList, patchMemoRelationList) + for _, memoRelation := range addedMemoRelationList { + if _, err := s.Store.UpsertMemoRelation(ctx, &store.MemoRelationMessage{ + MemoID: memo.ID, + RelatedMemoID: memoRelation.RelatedMemoID, + Type: store.MemoRelationType(memoRelation.Type), + }); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo relation").SetInternal(err) + } + } + for _, memoRelation := range removedMemoRelationList { + memoRelationType := store.MemoRelationType(memoRelation.Type) + if err := s.Store.DeleteMemoRelation(ctx, &store.DeleteMemoRelationMessage{ + MemoID: &memo.ID, + RelatedMemoID: &memoRelation.RelatedMemoID, + Type: &memoRelationType, + }); err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete memo relation").SetInternal(err) + } } }