Skip to content

Commit

Permalink
refactor: api group for notification in user center
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Sep 26, 2023
1 parent 418660b commit d77f157
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
@Component
@RequiredArgsConstructor
public class NotifierEndpoint implements CustomEndpoint {
public class ConsoleNotifierEndpoint implements CustomEndpoint {

private final NotifierConfigStore notifierConfigStore;

Expand All @@ -48,18 +48,6 @@ public RouterFunction<ServerResponse> 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")
Expand All @@ -80,51 +68,15 @@ public RouterFunction<ServerResponse> 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<ServerResponse> fetchReceiverConfig(ServerRequest request) {
var name = request.pathVariable("name");
return notifierConfigStore.fetchReceiverConfig(name)
.flatMap(config -> ServerResponse.ok().bodyValue(config));
}

private Mono<ServerResponse> fetchSenderConfig(ServerRequest request) {
var name = request.pathVariable("name");
return notifierConfigStore.fetchSenderConfig(name)
.flatMap(config -> ServerResponse.ok().bodyValue(config));
}

private Mono<ServerResponse> 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<ServerResponse> saveSenderConfig(ServerRequest request) {
var name = request.pathVariable("name");
return request.bodyToMono(ObjectNode.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,7 +48,7 @@ public RouterFunction<ServerResponse> endpoint() {
}

Supplier<RouterFunction<ServerResponse>> 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 -> {
Expand Down Expand Up @@ -108,6 +109,11 @@ Supplier<RouterFunction<ServerResponse>> userspaceScopedApis() {
.build();
}

@Override
public GroupVersion groupVersion() {
return GroupVersion.parseAPIVersion("api.notification.halo.run/v1alpha1");
}

record MarkSpecifiedRequest(List<String> names) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<ServerResponse> 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<ServerResponse> fetchReceiverConfig(ServerRequest request) {
var name = request.pathVariable("name");
return notifierConfigStore.fetchReceiverConfig(name)
.flatMap(config -> ServerResponse.ok().bodyValue(config));
}

private Mono<ServerResponse> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
11 changes: 6 additions & 5 deletions docs/notification/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

#### 通知模板

Expand Down Expand Up @@ -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`

通知器扩展点用于实现发送通知的方式:

Expand Down

0 comments on commit d77f157

Please sign in to comment.