-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support configuring notifier for different notification reason …
…types (#4680) #### What type of PR is this? /kind feature /area core /area console /milestone 2.10.x #### What this PR does / why we need it: 用户支持为不同的通知事件配置通知方式 <img width="872" alt="image" src="https://github.com/halo-dev/halo/assets/21301288/dad85ba3-96bc-4580-9dae-2b9e66e877fe"> #### Does this PR introduce a user-facing change? ```release-note 用户支持为不同的通知事件配置通知方式 ```
- Loading branch information
Showing
20 changed files
with
1,020 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
...src/main/java/run/halo/app/notification/endpoint/UserNotificationPreferencesEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
package run.halo.app.notification.endpoint; | ||
|
||
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; | ||
import static org.springdoc.core.fn.builders.apiresponse.Builder.responseBuilder; | ||
import static org.springdoc.core.fn.builders.parameter.Builder.parameterBuilder; | ||
import static org.springdoc.core.fn.builders.requestbody.Builder.requestBodyBuilder; | ||
|
||
import io.swagger.v3.oas.annotations.enums.ParameterIn; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.experimental.Accessors; | ||
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.server.RequestPredicates; | ||
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 reactor.core.publisher.Mono; | ||
import reactor.util.function.Tuples; | ||
import run.halo.app.core.extension.endpoint.CustomEndpoint; | ||
import run.halo.app.core.extension.notification.NotifierDescriptor; | ||
import run.halo.app.core.extension.notification.ReasonType; | ||
import run.halo.app.extension.Comparators; | ||
import run.halo.app.extension.GroupVersion; | ||
import run.halo.app.extension.ReactiveExtensionClient; | ||
import run.halo.app.notification.UserNotificationPreference; | ||
import run.halo.app.notification.UserNotificationPreferenceService; | ||
|
||
/** | ||
* Endpoint for user notification preferences. | ||
* | ||
* @author guqing | ||
* @since 2.10.0 | ||
*/ | ||
@Component | ||
@RequiredArgsConstructor | ||
public class UserNotificationPreferencesEndpoint implements CustomEndpoint { | ||
|
||
private final ReactiveExtensionClient client; | ||
private final UserNotificationPreferenceService userNotificationPreferenceService; | ||
|
||
@Override | ||
public RouterFunction<ServerResponse> endpoint() { | ||
return SpringdocRouteBuilder.route() | ||
.nest(RequestPredicates.path("/userspaces/{username}"), userspaceScopedApis(), | ||
builder -> { | ||
}) | ||
.build(); | ||
} | ||
|
||
Supplier<RouterFunction<ServerResponse>> userspaceScopedApis() { | ||
var tag = "api.notification.halo.run/v1alpha1/Notification"; | ||
return () -> SpringdocRouteBuilder.route() | ||
.GET("/notification-preferences", this::listNotificationPreferences, | ||
builder -> builder.operationId("ListUserNotificationPreferences") | ||
.description("List notification preferences for the authenticated user.") | ||
.tag(tag) | ||
.parameter(parameterBuilder() | ||
.in(ParameterIn.PATH) | ||
.name("username") | ||
.description("Username") | ||
.required(true) | ||
) | ||
.response(responseBuilder() | ||
.implementation(ReasonTypeNotifierMatrix.class) | ||
) | ||
) | ||
.POST("/notification-preferences", this::saveNotificationPreferences, | ||
builder -> builder.operationId("SaveUserNotificationPreferences") | ||
.description("Save notification preferences for the authenticated user.") | ||
.tag(tag) | ||
.parameter(parameterBuilder() | ||
.in(ParameterIn.PATH) | ||
.name("username") | ||
.description("Username") | ||
.required(true) | ||
) | ||
.requestBody(requestBodyBuilder() | ||
.implementation(ReasonTypeNotifierCollectionRequest.class) | ||
) | ||
.response(responseBuilder().implementation(ReasonTypeNotifierMatrix.class)) | ||
) | ||
.build(); | ||
} | ||
|
||
private Mono<ServerResponse> saveNotificationPreferences(ServerRequest request) { | ||
var username = request.pathVariable("username"); | ||
return request.bodyToMono(ReasonTypeNotifierCollectionRequest.class) | ||
.flatMap(requestBody -> { | ||
var reasonTypNotifiers = requestBody.reasonTypeNotifiers(); | ||
return userNotificationPreferenceService.getByUser(username) | ||
.flatMap(preference -> { | ||
var reasonTypeNotifierMap = preference.getReasonTypeNotifier(); | ||
reasonTypeNotifierMap.clear(); | ||
reasonTypNotifiers.forEach(reasonTypeNotifierRequest -> { | ||
var reasonType = reasonTypeNotifierRequest.getReasonType(); | ||
var notifiers = reasonTypeNotifierRequest.getNotifiers(); | ||
var notifierSetting = new UserNotificationPreference.NotifierSetting(); | ||
notifierSetting.setNotifiers( | ||
notifiers == null ? Set.of() : Set.copyOf(notifiers)); | ||
reasonTypeNotifierMap.put(reasonType, notifierSetting); | ||
}); | ||
return userNotificationPreferenceService.saveByUser(username, preference); | ||
}); | ||
}) | ||
.then(Mono.defer(() -> listReasonTypeNotifierMatrix(username) | ||
.flatMap(result -> ServerResponse.ok().bodyValue(result))) | ||
); | ||
} | ||
|
||
private Mono<ServerResponse> listNotificationPreferences(ServerRequest request) { | ||
var username = request.pathVariable("username"); | ||
return listReasonTypeNotifierMatrix(username) | ||
.flatMap(matrix -> ServerResponse.ok().bodyValue(matrix)); | ||
} | ||
|
||
@NonNull | ||
private static <T> Map<String, Integer> toNameIndexMap(List<T> collection, | ||
Function<T, String> nameGetter) { | ||
Map<String, Integer> indexMap = new HashMap<>(); | ||
for (int i = 0; i < collection.size(); i++) { | ||
var item = collection.get(i); | ||
indexMap.put(nameGetter.apply(item), i); | ||
} | ||
return indexMap; | ||
} | ||
|
||
Mono<ReasonTypeNotifierMatrix> listReasonTypeNotifierMatrix(String username) { | ||
return client.list(ReasonType.class, null, Comparators.defaultComparator()) | ||
.map(reasonType -> new ReasonTypeInfo(reasonType.getMetadata().getName(), | ||
reasonType.getSpec().getDisplayName(), | ||
reasonType.getSpec().getDescription()) | ||
) | ||
.collectList() | ||
.flatMap(reasonTypes -> client.list(NotifierDescriptor.class, null, | ||
Comparators.defaultComparator()) | ||
.map(notifier -> new NotifierInfo(notifier.getMetadata().getName(), | ||
notifier.getSpec().getDisplayName(), | ||
notifier.getSpec().getDescription()) | ||
) | ||
.collectList() | ||
.map(notifiers -> { | ||
var matrix = new ReasonTypeNotifierMatrix() | ||
.setReasonTypes(reasonTypes) | ||
.setNotifiers(notifiers) | ||
.setStateMatrix(new boolean[reasonTypes.size()][notifiers.size()]); | ||
return Tuples.of(reasonTypes, matrix); | ||
}) | ||
) | ||
.flatMap(tuple2 -> { | ||
var reasonTypes = tuple2.getT1(); | ||
var matrix = tuple2.getT2(); | ||
|
||
var reasonTypeIndexMap = toNameIndexMap(reasonTypes, ReasonTypeInfo::name); | ||
var notifierIndexMap = toNameIndexMap(matrix.getNotifiers(), NotifierInfo::name); | ||
var stateMatrix = matrix.getStateMatrix(); | ||
|
||
return userNotificationPreferenceService.getByUser(username) | ||
.doOnNext(preference -> { | ||
var reasonTypeNotifierMap = preference.getReasonTypeNotifier(); | ||
for (ReasonTypeInfo reasonType : reasonTypes) { | ||
var reasonTypeIndex = reasonTypeIndexMap.get(reasonType.name()); | ||
var notifierNames = | ||
reasonTypeNotifierMap.getNotifiers(reasonType.name()); | ||
for (String notifierName : notifierNames) { | ||
var notifierIndex = notifierIndexMap.get(notifierName); | ||
stateMatrix[reasonTypeIndex][notifierIndex] = true; | ||
} | ||
} | ||
}) | ||
.thenReturn(matrix); | ||
}); | ||
} | ||
|
||
@Data | ||
@Accessors(chain = true) | ||
static class ReasonTypeNotifierMatrix { | ||
private List<ReasonTypeInfo> reasonTypes; | ||
private List<NotifierInfo> notifiers; | ||
private boolean[][] stateMatrix; | ||
} | ||
|
||
record ReasonTypeInfo(String name, String displayName, String description) { | ||
} | ||
|
||
record NotifierInfo(String name, String displayName, String description) { | ||
} | ||
|
||
record ReasonTypeNotifierCollectionRequest( | ||
@Schema(requiredMode = REQUIRED) List<ReasonTypeNotifierRequest> reasonTypeNotifiers) { | ||
} | ||
|
||
@Data | ||
static class ReasonTypeNotifierRequest { | ||
private String reasonType; | ||
private List<String> notifiers; | ||
} | ||
|
||
@Override | ||
public GroupVersion groupVersion() { | ||
return GroupVersion.parseAPIVersion("api.notification.halo.run/v1alpha1"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...test/java/run/halo/app/notification/endpoint/UserNotificationPreferencesEndpointTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package run.halo.app.notification.endpoint; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.core.extension.notification.NotifierDescriptor; | ||
import run.halo.app.core.extension.notification.ReasonType; | ||
import run.halo.app.extension.ReactiveExtensionClient; | ||
import run.halo.app.notification.UserNotificationPreferenceService; | ||
|
||
/** | ||
* Tests for {@link UserNotificationPreferencesEndpoint}. | ||
* | ||
* @author guqing | ||
* @since 2.10.0 | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
class UserNotificationPreferencesEndpointTest { | ||
|
||
@Mock | ||
private ReactiveExtensionClient client; | ||
|
||
@Mock | ||
private UserNotificationPreferenceService userNotificationPreferenceService; | ||
|
||
@InjectMocks | ||
private UserNotificationPreferencesEndpoint userNotificationPreferencesEndpoint; | ||
|
||
private WebTestClient webTestClient; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
webTestClient = WebTestClient | ||
.bindToRouterFunction(userNotificationPreferencesEndpoint.endpoint()) | ||
.build(); | ||
} | ||
|
||
@Test | ||
void listNotificationPreferences() { | ||
when(client.list(eq(ReasonType.class), eq(null), any())).thenReturn(Flux.empty()); | ||
when(client.list(eq(NotifierDescriptor.class), eq(null), any())).thenReturn(Flux.empty()); | ||
when(userNotificationPreferenceService.getByUser(any())).thenReturn(Mono.empty()); | ||
webTestClient.post() | ||
.uri("/userspaces/{username}/notification-preferences", "guqing") | ||
.exchange() | ||
.expectStatus() | ||
.isOk(); | ||
} | ||
|
||
@Test | ||
void saveNotificationPreferences() { | ||
when(client.list(eq(ReasonType.class), eq(null), any())).thenReturn(Flux.empty()); | ||
when(client.list(eq(NotifierDescriptor.class), eq(null), any())).thenReturn(Flux.empty()); | ||
when(userNotificationPreferenceService.getByUser(any())).thenReturn(Mono.empty()); | ||
webTestClient.post() | ||
.uri("/userspaces/{username}/notification-preferences", "guqing") | ||
.exchange() | ||
.expectStatus() | ||
.isOk(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.