feat: add API for recycle users own posts

pull/6729/head
guqing 2024-09-29 16:22:44 +08:00
parent 2c849d8361
commit cd90771bb7
4 changed files with 45 additions and 2 deletions

View File

@ -50,4 +50,6 @@ public interface PostService {
Mono<Post> revertToSpecifiedSnapshot(String postName, String snapshotName); Mono<Post> revertToSpecifiedSnapshot(String postName, String snapshotName);
Mono<ContentWrapper> deleteContent(String postName, String snapshotName); Mono<ContentWrapper> deleteContent(String postName, String snapshotName);
Mono<Post> recycleBy(String postName, String username);
} }

View File

@ -379,6 +379,15 @@ public class PostServiceImpl extends AbstractContentService implements PostServi
}); });
} }
@Override
public Mono<Post> recycleBy(String postName, String username) {
return getByUsername(postName, username)
.flatMap(post -> updatePostWithRetry(post, record -> {
record.getSpec().setDeleted(true);
return record;
}));
}
private Mono<Post> updatePostWithRetry(Post post, UnaryOperator<Post> func) { private Mono<Post> updatePostWithRetry(Post post, UnaryOperator<Post> func) {
return client.update(func.apply(post)) return client.update(func.apply(post))
.onErrorResume(OptimisticLockingFailureException.class, .onErrorResume(OptimisticLockingFailureException.class,

View File

@ -124,13 +124,27 @@ public class UcPostEndpoint implements CustomEndpoint {
.operationId("UnpublishMyPost") .operationId("UnpublishMyPost")
.description("Unpublish my post.") .description("Unpublish my post.")
.parameter(namePathParam) .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(), .build(),
builder -> { builder -> {
}) })
.build(); .build();
} }
private Mono<ServerResponse> 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<ServerResponse> getMyPostDraft(ServerRequest request) { private Mono<ServerResponse> getMyPostDraft(ServerRequest request) {
var name = request.pathVariable("name"); var name = request.pathVariable("name");
var patched = request.queryParam("patched").map(Boolean::valueOf).orElse(false); var patched = request.queryParam("patched").map(Boolean::valueOf).orElse(false);

View File

@ -39,7 +39,8 @@ metadata:
# Currently, yaml definition does not support i18n, please see https://github.com/halo-dev/halo/issues/3573 # 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/display-name: "Post Author"
rbac.authorization.halo.run/dependencies: | 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: [ ] rules: [ ]
--- ---
@ -98,6 +99,23 @@ rules:
resources: [ "posts/publish", "posts/unpublish" ] resources: [ "posts/publish", "posts/unpublish" ]
verbs: [ "update" ] 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 apiVersion: v1alpha1
kind: Role kind: Role