fix: patch memo visibility (#1695)

This commit is contained in:
boojack 2023-05-21 11:51:13 +08:00 committed by GitHub
parent ba8e1e5dc2
commit 98b3a371f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -163,54 +163,58 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo").SetInternal(err) return echo.NewHTTPError(http.StatusInternalServerError, "Failed to compose memo").SetInternal(err)
} }
resourceIDList := make([]int, 0) if memoPatch.ResourceIDList != nil {
for _, resource := range memo.ResourceList { resourceIDList := make([]int, 0)
resourceIDList = append(resourceIDList, resource.ID) 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)
} }
} addedResourceIDList, removedResourceIDList := getIDListDiff(resourceIDList, memoPatch.ResourceIDList)
for _, resourceID := range removedResourceIDList { for _, resourceID := range addedResourceIDList {
if err := s.Store.DeleteMemoResource(ctx, &api.MemoResourceDelete{ if _, err := s.Store.UpsertMemoResource(ctx, &api.MemoResourceUpsert{
MemoID: &memo.ID, MemoID: memo.ID,
ResourceID: &resourceID, ResourceID: resourceID,
}); err != nil { }); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete memo resource").SetInternal(err) 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) if memoPatch.RelationList != nil {
for _, memoRelationUpsert := range memoPatch.RelationList { patchMemoRelationList := make([]*api.MemoRelation, 0)
patchMemoRelationList = append(patchMemoRelationList, &api.MemoRelation{ for _, memoRelationUpsert := range memoPatch.RelationList {
MemoID: memo.ID, patchMemoRelationList = append(patchMemoRelationList, &api.MemoRelation{
RelatedMemoID: memoRelationUpsert.RelatedMemoID, MemoID: memo.ID,
Type: memoRelationUpsert.Type, 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)
} }
} addedMemoRelationList, removedMemoRelationList := getMemoRelationListDiff(memo.RelationList, patchMemoRelationList)
for _, memoRelation := range removedMemoRelationList { for _, memoRelation := range addedMemoRelationList {
memoRelationType := store.MemoRelationType(memoRelation.Type) if _, err := s.Store.UpsertMemoRelation(ctx, &store.MemoRelationMessage{
if err := s.Store.DeleteMemoRelation(ctx, &store.DeleteMemoRelationMessage{ MemoID: memo.ID,
MemoID: &memo.ID, RelatedMemoID: memoRelation.RelatedMemoID,
RelatedMemoID: &memoRelation.RelatedMemoID, Type: store.MemoRelationType(memoRelation.Type),
Type: &memoRelationType, }); err != nil {
}); err != nil { return echo.NewHTTPError(http.StatusInternalServerError, "Failed to upsert memo relation").SetInternal(err)
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete 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)
}
} }
} }