Skip to content

Commit

Permalink
refactor: hide essential notifications to prevent accidental disabling (
Browse files Browse the repository at this point in the history
#6972)

#### What type of PR is this?
/kind improvement
/area core
/milestone 2.20.x

#### What this PR does / why we need it:
隐藏关键通知项设置以避免用户意外禁用而无法收到通知

#### Which issue(s) this PR fixes:
Fixes #6967

#### Does this PR introduce a user-facing change?
```release-note
隐藏关键通知项设置以避免用户意外禁用而无法收到通知

```
  • Loading branch information
guqing authored Oct 30, 2024
1 parent 2c4e85f commit 0d1a099
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions api/src/main/java/run/halo/app/extension/MetadataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum MetadataUtil {
;

public static final String SYSTEM_FINALIZER = "system-protection";
public static final String HIDDEN_LABEL = "halo.run/hidden";

/**
* Gets extension metadata labels null safe.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
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.ExtensionUtil;
import run.halo.app.extension.GroupVersion;
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.MetadataUtil;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.infra.utils.JsonUtils;
Expand Down Expand Up @@ -140,7 +142,12 @@ private static <T> Map<String, Integer> toNameIndexMap(List<T> collection,
}

Mono<ReasonTypeNotifierMatrix> listReasonTypeNotifierMatrix(String username) {
return client.list(ReasonType.class, null, Comparators.defaultComparator())
var listOptions = ListOptions.builder()
.labelSelector()
.notExists(MetadataUtil.HIDDEN_LABEL)
.end()
.build();
return client.listAll(ReasonType.class, listOptions, ExtensionUtil.defaultSort())
.map(ReasonTypeInfo::from)
.collectList()
.flatMap(reasonTypes -> client.list(NotifierDescriptor.class, null,
Expand Down
4 changes: 2 additions & 2 deletions application/src/main/resources/extensions/notification.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ kind: ReasonType
metadata:
name: email-verification
labels:
halo.run/hide: "true"
halo.run/hidden: "true"
spec:
displayName: "邮箱验证"
description: "当你的邮箱被用于注册账户时,会收到一条带有验证码的邮件,你需要点击邮件中的链接来验证邮箱是否属于你。"
Expand All @@ -191,7 +191,7 @@ kind: ReasonType
metadata:
name: reset-password-by-email
labels:
halo.run/hide: "true"
halo.run/hidden: "true"
spec:
displayName: "根据邮件地址重置密码"
description: "当你通过邮件地址找回密码时,会收到一条带密码重置链接的邮件,你需要点击邮件中的链接来重置密码。"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package run.halo.app.notification.endpoint;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.assertArg;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

Expand All @@ -15,6 +17,7 @@
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.ExtensionUtil;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.notification.UserNotificationPreferenceService;

Expand Down Expand Up @@ -43,11 +46,16 @@ void setUp() {
webTestClient = WebTestClient
.bindToRouterFunction(userNotificationPreferencesEndpoint.endpoint())
.build();

when(client.listAll(eq(ReasonType.class), assertArg(option ->
assertThat(option.toString())
.isEqualTo("labelSelector: (halo.run/hidden NOT_EXISTS)")),
eq(ExtensionUtil.defaultSort()))
).thenReturn(Flux.empty());
}

@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()
Expand All @@ -59,7 +67,6 @@ void listNotificationPreferences() {

@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()
Expand Down

0 comments on commit 0d1a099

Please sign in to comment.