-
-
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: add author pages for theme-side
- Loading branch information
Showing
10 changed files
with
420 additions
and
8 deletions.
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
95 changes: 89 additions & 6 deletions
95
src/main/java/run/halo/app/core/extension/reconciler/UserReconciler.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package run.halo.app.theme.finders.vo; | ||
|
||
import java.util.List; | ||
import lombok.Builder; | ||
import lombok.Value; | ||
import org.apache.commons.lang3.ObjectUtils; | ||
import run.halo.app.core.extension.User; | ||
import run.halo.app.extension.MetadataOperator; | ||
import run.halo.app.infra.utils.JsonUtils; | ||
|
||
@Value | ||
@Builder | ||
public class UserVo { | ||
MetadataOperator metadata; | ||
|
||
User.UserSpec spec; | ||
|
||
User.UserStatus status; | ||
|
||
/** | ||
* Converts to {@link UserVo} from {@link User}. | ||
* | ||
* @param user user extension | ||
* @return user value object. | ||
*/ | ||
public static UserVo from(User user) { | ||
User.UserStatus statusCopy = | ||
JsonUtils.deepCopy(ObjectUtils.defaultIfNull(user.getStatus(), new User.UserStatus())); | ||
statusCopy.setLoginHistories(List.of()); | ||
statusCopy.setLastLoginAt(null); | ||
|
||
User.UserSpec userSpecCopy = JsonUtils.deepCopy(user.getSpec()); | ||
userSpecCopy.setPassword("[PROTECTED]"); | ||
return UserVo.builder() | ||
.metadata(user.getMetadata()) | ||
.spec(userSpecCopy) | ||
.status(statusCopy) | ||
.build(); | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
src/main/java/run/halo/app/theme/router/strategy/AuthorRouteStrategy.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,49 @@ | ||
package run.halo.app.theme.router.strategy; | ||
|
||
import java.util.Map; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.server.HandlerFunction; | ||
import org.springframework.web.reactive.function.server.ServerResponse; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.core.extension.User; | ||
import run.halo.app.extension.GroupVersionKind; | ||
import run.halo.app.extension.ReactiveExtensionClient; | ||
import run.halo.app.infra.SystemSetting; | ||
import run.halo.app.theme.DefaultTemplateEnum; | ||
import run.halo.app.theme.finders.vo.UserVo; | ||
|
||
/** | ||
* Author route strategy. | ||
* | ||
* @author guqing | ||
* @since 2.0.1 | ||
*/ | ||
@Component | ||
@AllArgsConstructor | ||
public class AuthorRouteStrategy implements DetailsPageRouteHandlerStrategy { | ||
|
||
private final ReactiveExtensionClient client; | ||
|
||
@Override | ||
public HandlerFunction<ServerResponse> getHandler(SystemSetting.ThemeRouteRules routeRules, | ||
String name) { | ||
return request -> ServerResponse.ok() | ||
.render(DefaultTemplateEnum.AUTHOR.getValue(), | ||
Map.of("name", name, | ||
"author", getByName(name), | ||
ModelConst.TEMPLATE_ID, DefaultTemplateEnum.AUTHOR.getValue() | ||
) | ||
); | ||
} | ||
|
||
private Mono<UserVo> getByName(String name) { | ||
return client.fetch(User.class, name) | ||
.map(UserVo::from); | ||
} | ||
|
||
@Override | ||
public boolean supports(GroupVersionKind gvk) { | ||
return GroupVersionKind.fromExtension(User.class).equals(gvk); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/test/java/run/halo/app/core/extension/reconciler/UserReconcilerTest.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,81 @@ | ||
package run.halo.app.core.extension.reconciler; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import run.halo.app.core.extension.User; | ||
import run.halo.app.extension.ExtensionClient; | ||
import run.halo.app.extension.Metadata; | ||
import run.halo.app.extension.controller.Reconciler; | ||
import run.halo.app.infra.AnonymousUserConst; | ||
import run.halo.app.infra.ExternalUrlSupplier; | ||
import run.halo.app.theme.router.PermalinkIndexUpdateCommand; | ||
|
||
/** | ||
* Tests for {@link UserReconciler}. | ||
* | ||
* @author guqing | ||
* @since 2.0.1 | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
class UserReconcilerTest { | ||
@Mock | ||
private ApplicationEventPublisher eventPublisher; | ||
|
||
@Mock | ||
private ExternalUrlSupplier externalUrlSupplier; | ||
|
||
@Mock | ||
private ExtensionClient client; | ||
|
||
@InjectMocks | ||
private UserReconciler userReconciler; | ||
|
||
@Test | ||
void permalinkForFakeUser() throws URISyntaxException { | ||
when(externalUrlSupplier.get()).thenReturn(new URI("http://localhost:8090")); | ||
|
||
when(client.fetch(eq(User.class), eq("fake-user"))) | ||
.thenReturn(Optional.of(user("fake-user"))); | ||
userReconciler.reconcile(new Reconciler.Request("fake-user")); | ||
verify(client, times(1)).update(any(User.class)); | ||
verify(eventPublisher, times(1)).publishEvent(any(PermalinkIndexUpdateCommand.class)); | ||
|
||
ArgumentCaptor<User> captor = ArgumentCaptor.forClass(User.class); | ||
verify(client, times(1)).update(captor.capture()); | ||
assertThat(captor.getValue().getStatus().getPermalink()) | ||
.isEqualTo("http://localhost:8090/authors/fake-user"); | ||
} | ||
|
||
@Test | ||
void permalinkForAnonymousUser() { | ||
when(client.fetch(eq(User.class), eq(AnonymousUserConst.PRINCIPAL))) | ||
.thenReturn(Optional.of(user(AnonymousUserConst.PRINCIPAL))); | ||
userReconciler.reconcile(new Reconciler.Request(AnonymousUserConst.PRINCIPAL)); | ||
verify(client, times(0)).update(any(User.class)); | ||
verify(eventPublisher, times(0)).publishEvent(any(PermalinkIndexUpdateCommand.class)); | ||
} | ||
|
||
User user(String name) { | ||
User user = new User(); | ||
user.setMetadata(new Metadata()); | ||
user.getMetadata().setName(name); | ||
user.getMetadata().setFinalizers(Set.of("user-protection")); | ||
return user; | ||
} | ||
} |
Oops, something went wrong.