From d77f157b12ab2a6aace32df64b7ffa2d47d4673b Mon Sep 17 00:00:00 2001 From: guqing Date: Tue, 26 Sep 2023 11:09:53 +0800 Subject: [PATCH] refactor: api group for notification in user center --- ...oint.java => ConsoleNotifierEndpoint.java} | 50 +--------- .../endpoint/UserNotificationEndpoint.java | 8 +- .../endpoint/UserNotifierEndpoint.java | 95 +++++++++++++++++++ .../role-template-authenticated.yaml | 6 +- docs/notification/README.md | 11 ++- 5 files changed, 112 insertions(+), 58 deletions(-) rename application/src/main/java/run/halo/app/notification/endpoint/{NotifierEndpoint.java => ConsoleNotifierEndpoint.java} (61%) create mode 100644 application/src/main/java/run/halo/app/notification/endpoint/UserNotifierEndpoint.java diff --git a/application/src/main/java/run/halo/app/notification/endpoint/NotifierEndpoint.java b/application/src/main/java/run/halo/app/notification/endpoint/ConsoleNotifierEndpoint.java similarity index 61% rename from application/src/main/java/run/halo/app/notification/endpoint/NotifierEndpoint.java rename to application/src/main/java/run/halo/app/notification/endpoint/ConsoleNotifierEndpoint.java index f961dbcd61..b00d3976b0 100644 --- a/application/src/main/java/run/halo/app/notification/endpoint/NotifierEndpoint.java +++ b/application/src/main/java/run/halo/app/notification/endpoint/ConsoleNotifierEndpoint.java @@ -28,7 +28,7 @@ */ @Component @RequiredArgsConstructor -public class NotifierEndpoint implements CustomEndpoint { +public class ConsoleNotifierEndpoint implements CustomEndpoint { private final NotifierConfigStore notifierConfigStore; @@ -48,18 +48,6 @@ public RouterFunction endpoint() { ) .response(responseBuilder().implementation(ObjectNode.class)) ) - .GET("/notifiers/{name}/receiverConfig", this::fetchReceiverConfig, - builder -> builder.operationId("FetchReceiverConfig") - .description("Fetch receiver config of notifier") - .tag(tag) - .parameter(parameterBuilder() - .in(ParameterIn.PATH) - .name("name") - .description("Notifier name") - .required(true) - ) - .response(responseBuilder().implementation(ObjectNode.class)) - ) .POST("/notifiers/{name}/senderConfig", this::saveSenderConfig, builder -> builder.operationId("SaveSenderConfig") .description("Save sender config of notifier") @@ -80,51 +68,15 @@ public RouterFunction endpoint() { ) .response(responseBuilder().implementation(Void.class)) ) - .POST("/notifiers/{name}/receiverConfig", this::saveReceiverConfig, - builder -> builder.operationId("SaveReceiverConfig") - .description("Save receiver config of notifier") - .tag(tag) - .parameter(parameterBuilder() - .in(ParameterIn.PATH) - .name("name") - .description("Notifier name") - .required(true) - ) - .requestBody(requestBodyBuilder() - .required(true) - .content(contentBuilder() - .mediaType(MediaType.APPLICATION_JSON_VALUE) - .schema(Builder.schemaBuilder() - .implementation(ObjectNode.class)) - ) - ) - .response(responseBuilder().implementation(Void.class)) - ) .build(); } - private Mono fetchReceiverConfig(ServerRequest request) { - var name = request.pathVariable("name"); - return notifierConfigStore.fetchReceiverConfig(name) - .flatMap(config -> ServerResponse.ok().bodyValue(config)); - } - private Mono fetchSenderConfig(ServerRequest request) { var name = request.pathVariable("name"); return notifierConfigStore.fetchSenderConfig(name) .flatMap(config -> ServerResponse.ok().bodyValue(config)); } - private Mono saveReceiverConfig(ServerRequest request) { - var name = request.pathVariable("name"); - return request.bodyToMono(ObjectNode.class) - .switchIfEmpty(Mono.error( - () -> new ServerWebInputException("Request body must not be empty.")) - ) - .flatMap(jsonNode -> notifierConfigStore.saveReceiverConfig(name, jsonNode)) - .then(ServerResponse.ok().build()); - } - private Mono saveSenderConfig(ServerRequest request) { var name = request.pathVariable("name"); return request.bodyToMono(ObjectNode.class) diff --git a/application/src/main/java/run/halo/app/notification/endpoint/UserNotificationEndpoint.java b/application/src/main/java/run/halo/app/notification/endpoint/UserNotificationEndpoint.java index a68bc7af11..6980d66e4e 100644 --- a/application/src/main/java/run/halo/app/notification/endpoint/UserNotificationEndpoint.java +++ b/application/src/main/java/run/halo/app/notification/endpoint/UserNotificationEndpoint.java @@ -20,6 +20,7 @@ import reactor.core.publisher.Mono; import run.halo.app.core.extension.endpoint.CustomEndpoint; import run.halo.app.core.extension.notification.Notification; +import run.halo.app.extension.GroupVersion; import run.halo.app.extension.ListResult; import run.halo.app.extension.router.QueryParamBuildUtil; import run.halo.app.notification.UserNotificationQuery; @@ -47,7 +48,7 @@ public RouterFunction endpoint() { } Supplier> userspaceScopedApis() { - var tag = "api.console.halo.run/v1alpha1/Notification"; + var tag = "api.notification.halo.run/v1alpha1/Notification"; return () -> SpringdocRouteBuilder.route() .GET("/notifications", this::listNotification, builder -> { @@ -108,6 +109,11 @@ Supplier> userspaceScopedApis() { .build(); } + @Override + public GroupVersion groupVersion() { + return GroupVersion.parseAPIVersion("api.notification.halo.run/v1alpha1"); + } + record MarkSpecifiedRequest(List names) { } diff --git a/application/src/main/java/run/halo/app/notification/endpoint/UserNotifierEndpoint.java b/application/src/main/java/run/halo/app/notification/endpoint/UserNotifierEndpoint.java new file mode 100644 index 0000000000..c60fcbddca --- /dev/null +++ b/application/src/main/java/run/halo/app/notification/endpoint/UserNotifierEndpoint.java @@ -0,0 +1,95 @@ +package run.halo.app.notification.endpoint; + +import static org.springdoc.core.fn.builders.apiresponse.Builder.responseBuilder; +import static org.springdoc.core.fn.builders.content.Builder.contentBuilder; +import static org.springdoc.core.fn.builders.parameter.Builder.parameterBuilder; +import static org.springdoc.core.fn.builders.requestbody.Builder.requestBodyBuilder; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.swagger.v3.oas.annotations.enums.ParameterIn; +import lombok.RequiredArgsConstructor; +import org.springdoc.core.fn.builders.schema.Builder; +import org.springdoc.webflux.core.fn.SpringdocRouteBuilder; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.server.RouterFunction; +import org.springframework.web.reactive.function.server.ServerRequest; +import org.springframework.web.reactive.function.server.ServerResponse; +import org.springframework.web.server.ServerWebInputException; +import reactor.core.publisher.Mono; +import run.halo.app.core.extension.endpoint.CustomEndpoint; +import run.halo.app.extension.GroupVersion; +import run.halo.app.notification.NotifierConfigStore; + +/** + * Notifier endpoint for user center. + * + * @author guqing + * @since 2.10.0 + */ +@Component +@RequiredArgsConstructor +public class UserNotifierEndpoint implements CustomEndpoint { + + private final NotifierConfigStore notifierConfigStore; + + @Override + public RouterFunction endpoint() { + var tag = "api.notification.halo.run/v1alpha1/Notifier"; + return SpringdocRouteBuilder.route() + .GET("/notifiers/{name}/receiverConfig", this::fetchReceiverConfig, + builder -> builder.operationId("FetchReceiverConfig") + .description("Fetch receiver config of notifier") + .tag(tag) + .parameter(parameterBuilder() + .in(ParameterIn.PATH) + .name("name") + .description("Notifier name") + .required(true) + ) + .response(responseBuilder().implementation(ObjectNode.class)) + ) + .POST("/notifiers/{name}/receiverConfig", this::saveReceiverConfig, + builder -> builder.operationId("SaveReceiverConfig") + .description("Save receiver config of notifier") + .tag(tag) + .parameter(parameterBuilder() + .in(ParameterIn.PATH) + .name("name") + .description("Notifier name") + .required(true) + ) + .requestBody(requestBodyBuilder() + .required(true) + .content(contentBuilder() + .mediaType(MediaType.APPLICATION_JSON_VALUE) + .schema(Builder.schemaBuilder() + .implementation(ObjectNode.class)) + ) + ) + .response(responseBuilder().implementation(Void.class)) + ) + .build(); + } + + private Mono fetchReceiverConfig(ServerRequest request) { + var name = request.pathVariable("name"); + return notifierConfigStore.fetchReceiverConfig(name) + .flatMap(config -> ServerResponse.ok().bodyValue(config)); + } + + private Mono saveReceiverConfig(ServerRequest request) { + var name = request.pathVariable("name"); + return request.bodyToMono(ObjectNode.class) + .switchIfEmpty(Mono.error( + () -> new ServerWebInputException("Request body must not be empty.")) + ) + .flatMap(jsonNode -> notifierConfigStore.saveReceiverConfig(name, jsonNode)) + .then(ServerResponse.ok().build()); + } + + @Override + public GroupVersion groupVersion() { + return GroupVersion.parseAPIVersion("api.notification.halo.run/v1alpha1"); + } +} diff --git a/application/src/main/resources/extensions/role-template-authenticated.yaml b/application/src/main/resources/extensions/role-template-authenticated.yaml index 43028dc4a7..854ea0df39 100644 --- a/application/src/main/resources/extensions/role-template-authenticated.yaml +++ b/application/src/main/resources/extensions/role-template-authenticated.yaml @@ -122,12 +122,12 @@ metadata: halo.run/role-template: "true" halo.run/hidden: "true" rules: - - apiGroups: [ "api.console.halo.run" ] + - apiGroups: [ "api.notification.halo.run" ] resources: [ "notifications" ] verbs: [ "get", "list" ] - - apiGroups: [ "api.console.halo.run" ] + - apiGroups: [ "api.notification.halo.run" ] resources: [ "notifications/mark-as-read", "notifications/mark-specified-as-read" ] verbs: [ "update" ] - - apiGroups: [ "api.console.halo.run" ] + - apiGroups: [ "api.notification.halo.run" ] resources: [ "notifiers/receiverConfig" ] verbs: [ "get", "update" ] diff --git a/docs/notification/README.md b/docs/notification/README.md index c6d57b3ee4..4531438116 100644 --- a/docs/notification/README.md +++ b/docs/notification/README.md @@ -174,9 +174,10 @@ spec: 个人中心通知自定义 APIs: 1. 获取个人中心获取用户通知列表的 APIs 规则: - `GET /apis/api.console.halo.run/v1alpha1/userspaces/{username}/notifications` -2. 将通知标记为已读:`PUT /apis/api.console.halo.run/v1alpha1/userspaces/{username}/notifications/mark-as-read` -3. 批量将通知标记为已读:`PUT /apis/api.console.halo.run/v1alpha1/userspaces/{username}/notifications/mark-specified-as-read` + `GET /apis/api.notification.halo.run/v1alpha1/userspaces/{username}/notifications` +2. 将通知标记为已读:`PUT /apis/api.notification.halo.run/v1alpha1/userspaces/{username}/notifications/mark-as-read` +3. +批量将通知标记为已读:`PUT /apis/api.notification.halo.run/v1alpha1/userspaces/{username}/notifications/mark-specified-as-read` #### 通知模板 @@ -252,8 +253,8 @@ spec: 个人中心用户获取和保存对应通知器接收消息配置的 APIs: -1. 获取通知器接收消息配置:`GET /apis/api.console.halo.run/v1alpha1/notifiers/{name}/receiverConfig` -2. 获取通知器接收消息配置:`POST /apis/api.console.halo.run/v1alpha1/notifiers/{name}/receiverConfig` +1. 获取通知器接收消息配置:`GET /apis/api.notification.halo.run/v1alpha1/notifiers/{name}/receiverConfig` +2. 获取通知器接收消息配置:`POST /apis/api.notification.halo.run/v1alpha1/notifiers/{name}/receiverConfig` 通知器扩展点用于实现发送通知的方式: