diff --git a/application/src/main/java/run/halo/app/content/PostService.java b/application/src/main/java/run/halo/app/content/PostService.java index cf4c0b44a..d9344a3de 100644 --- a/application/src/main/java/run/halo/app/content/PostService.java +++ b/application/src/main/java/run/halo/app/content/PostService.java @@ -50,4 +50,6 @@ public interface PostService { Mono revertToSpecifiedSnapshot(String postName, String snapshotName); Mono deleteContent(String postName, String snapshotName); + + Mono recycleBy(String postName, String username); } diff --git a/application/src/main/java/run/halo/app/content/impl/PostServiceImpl.java b/application/src/main/java/run/halo/app/content/impl/PostServiceImpl.java index 5e805ac9a..7cdae00cc 100644 --- a/application/src/main/java/run/halo/app/content/impl/PostServiceImpl.java +++ b/application/src/main/java/run/halo/app/content/impl/PostServiceImpl.java @@ -379,6 +379,15 @@ public class PostServiceImpl extends AbstractContentService implements PostServi }); } + @Override + public Mono recycleBy(String postName, String username) { + return getByUsername(postName, username) + .flatMap(post -> updatePostWithRetry(post, record -> { + record.getSpec().setDeleted(true); + return record; + })); + } + private Mono updatePostWithRetry(Post post, UnaryOperator func) { return client.update(func.apply(post)) .onErrorResume(OptimisticLockingFailureException.class, diff --git a/application/src/main/java/run/halo/app/core/endpoint/uc/UcPostEndpoint.java b/application/src/main/java/run/halo/app/core/endpoint/uc/UcPostEndpoint.java index a969e64e8..87ca78019 100644 --- a/application/src/main/java/run/halo/app/core/endpoint/uc/UcPostEndpoint.java +++ b/application/src/main/java/run/halo/app/core/endpoint/uc/UcPostEndpoint.java @@ -124,13 +124,27 @@ public class UcPostEndpoint implements CustomEndpoint { .operationId("UnpublishMyPost") .description("Unpublish my post.") .parameter(namePathParam) - .response(responseBuilder().implementation(Post.class))) + .response(responseBuilder().implementation(Post.class)) + ) + .DELETE("/{name}/recycle", this::recycleMyPost, builder -> builder.tag(tag) + .operationId("RecycleMyPost") + .description("Move my post to recycle bin.") + .parameter(namePathParam) + .response(responseBuilder().implementation(Post.class)) + ) .build(), builder -> { }) .build(); } + private Mono recycleMyPost(ServerRequest request) { + final var name = request.pathVariable("name"); + return getCurrentUser() + .flatMap(username -> postService.recycleBy(name, username)) + .flatMap(post -> ServerResponse.ok().bodyValue(post)); + } + private Mono getMyPostDraft(ServerRequest request) { var name = request.pathVariable("name"); var patched = request.queryParam("patched").map(Boolean::valueOf).orElse(false); diff --git a/application/src/main/resources/extensions/role-template-uc-content.yaml b/application/src/main/resources/extensions/role-template-uc-content.yaml index 93f7bcd6b..575d68e68 100644 --- a/application/src/main/resources/extensions/role-template-uc-content.yaml +++ b/application/src/main/resources/extensions/role-template-uc-content.yaml @@ -39,7 +39,8 @@ metadata: # Currently, yaml definition does not support i18n, please see https://github.com/halo-dev/halo/issues/3573 rbac.authorization.halo.run/display-name: "Post Author" rbac.authorization.halo.run/dependencies: | - [ "role-template-post-contributor", "role-template-post-publisher", "role-template-post-attachment-manager" ] + [ "role-template-post-contributor", "role-template-post-publisher", "role-template-recycle-my-post", + "role-template-post-attachment-manager" ] rules: [ ] --- @@ -98,6 +99,23 @@ rules: resources: [ "posts/publish", "posts/unpublish" ] verbs: [ "update" ] +--- +apiVersion: v1alpha1 +kind: Role +metadata: + name: role-template-recycle-my-post + labels: + halo.run/role-template: "true" + annotations: + rbac.authorization.halo.run/module: "Posts Management" + rbac.authorization.halo.run/display-name: "Recycle My Post" + rbac.authorization.halo.run/ui-permissions: | + [ "uc:posts:recycle" ] +rules: + - apiGroups: [ "uc.api.content.halo.run" ] + resources: [ "posts/recycle" ] + verbs: [ "delete" ] + --- apiVersion: v1alpha1 kind: Role