Skip to content

Commit

Permalink
refactor: return UI permissions for notification preferences endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Jan 31, 2024
1 parent 5d35ffa commit cd686ad
Showing 1 changed file with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import static org.springdoc.core.fn.builders.parameter.Builder.parameterBuilder;
import static org.springdoc.core.fn.builders.requestbody.Builder.requestBodyBuilder;

import com.fasterxml.jackson.core.type.TypeReference;
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.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
Expand All @@ -25,12 +27,15 @@
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuples;
import run.halo.app.core.extension.Role;
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.MetadataUtil;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.infra.utils.JsonUtils;
import run.halo.app.notification.UserNotificationPreference;
import run.halo.app.notification.UserNotificationPreferenceService;

Expand Down Expand Up @@ -135,10 +140,7 @@ private static <T> Map<String, Integer> toNameIndexMap(List<T> collection,

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())
)
.map(ReasonTypeInfo::from)
.collectList()
.flatMap(reasonTypes -> client.list(NotifierDescriptor.class, null,
Comparators.defaultComparator())
Expand Down Expand Up @@ -188,7 +190,22 @@ static class ReasonTypeNotifierMatrix {
private boolean[][] stateMatrix;
}

record ReasonTypeInfo(String name, String displayName, String description) {
record ReasonTypeInfo(String name, String displayName, String description,
Set<String> uiPermissions) {

public static ReasonTypeInfo from(ReasonType reasonType) {
var uiPermissions = Optional.of(MetadataUtil.nullSafeAnnotations(reasonType))
.map(annotations -> annotations.get(Role.UI_PERMISSIONS_ANNO))
.map(uiPermissionStr -> JsonUtils.jsonToObject(uiPermissionStr,
new TypeReference<Set<String>>() {
})
)
.orElse(Set.of());
return new ReasonTypeInfo(reasonType.getMetadata().getName(),
reasonType.getSpec().getDisplayName(),
reasonType.getSpec().getDescription(),
uiPermissions);
}
}

record NotifierInfo(String name, String displayName, String description) {
Expand Down

0 comments on commit cd686ad

Please sign in to comment.