mirror of
https://github.com/usememos/memos.git
synced 2025-01-01 10:01:54 +08:00
feat: delete memo comments when memo is deleted (#3491)
* Added logic to delete memo relation and memo comments recursively when a memo is deleted * Fixed logic to only target comment types and added removing of broken references * Fixed linting errors --------- Co-authored-by: Martin MacDonald <martinmacdonald@Martins-MacBook-Pro.local>
This commit is contained in:
parent
08d37acffa
commit
760dee4159
1 changed files with 23 additions and 0 deletions
|
@ -352,6 +352,11 @@ func (s *APIV1Service) DeleteMemo(ctx context.Context, request *v1pb.DeleteMemoR
|
|||
return nil, status.Errorf(codes.Internal, "failed to delete memo")
|
||||
}
|
||||
|
||||
// Delete memo relation
|
||||
if err := s.Store.DeleteMemoRelation(ctx, &store.DeleteMemoRelation{MemoID: &id}); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "failed to delete memo relations")
|
||||
}
|
||||
|
||||
// Delete related resources.
|
||||
resources, err := s.Store.ListResources(ctx, &store.FindResource{MemoID: &id})
|
||||
if err != nil {
|
||||
|
@ -363,6 +368,24 @@ func (s *APIV1Service) DeleteMemo(ctx context.Context, request *v1pb.DeleteMemoR
|
|||
}
|
||||
}
|
||||
|
||||
// Delete memo comments
|
||||
commentType := store.MemoRelationComment
|
||||
relations, err := s.Store.ListMemoRelations(ctx, &store.FindMemoRelation{RelatedMemoID: &id, Type: &commentType})
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "failed to list memo comments")
|
||||
}
|
||||
for _, relation := range relations {
|
||||
if _, err := s.DeleteMemo(ctx, &v1pb.DeleteMemoRequest{Name: fmt.Sprintf("%s%d", MemoNamePrefix, relation.MemoID)}); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "failed to delete memo comment")
|
||||
}
|
||||
}
|
||||
|
||||
// Delete memo references
|
||||
referenceType := store.MemoRelationReference
|
||||
if err := s.Store.DeleteMemoRelation(ctx, &store.DeleteMemoRelation{RelatedMemoID: &id, Type: &referenceType}); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "failed to delete memo references")
|
||||
}
|
||||
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue