Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed Jun 28, 2024
2 parents 538b507 + f936e13 commit efdc012
Show file tree
Hide file tree
Showing 69 changed files with 1,454 additions and 302 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import run.halo.app.infra.ExternalLinkProcessor;
import run.halo.app.infra.utils.JsonUtils;
import run.halo.app.notification.NotificationReasonEmitter;
import run.halo.app.plugin.ExtensionComponentsFinder;
import run.halo.app.plugin.extensionpoint.ExtensionGetter;

/**
Expand Down Expand Up @@ -233,7 +232,7 @@ public static <T> Map<String, Object> toAttributeMap(T data) {
static class NewReplyReasonPublisher {
private final ExtensionClient client;
private final NotificationReasonEmitter notificationReasonEmitter;
private final ExtensionComponentsFinder extensionComponentsFinder;
private final ExtensionGetter extensionGetter;

public void publishReasonBy(Reply reply, Comment comment) {
boolean isQuoteReply = StringUtils.isNotBlank(reply.getSpec().getQuoteReply());
Expand Down Expand Up @@ -306,16 +305,14 @@ public void publishReasonBy(Reply reply, Comment comment) {

/**
* To be compatible with older versions, it may be empty, so use optional.
* TODO use {@link ExtensionGetter} instead of {@code extensionComponentsFinder}
*/
@SuppressWarnings("unchecked")
Optional<CommentSubject.SubjectDisplay> getCommentSubjectDisplay(Ref ref) {
return extensionComponentsFinder.getExtensions(CommentSubject.class)
.stream()
return extensionGetter.getExtensions(CommentSubject.class)
.filter(commentSubject -> commentSubject.supports(ref))
.findFirst()
.flatMap(commentSubject
-> commentSubject.getSubjectDisplay(ref.getName()).blockOptional());
.next()
.flatMap(subject -> subject.getSubjectDisplay(ref.getName()))
.blockOptional();
}

boolean doNotEmitReason(Reply currentReply, Reply quoteReply, Comment comment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import run.halo.app.infra.exception.AccessDeniedException;
import run.halo.app.metrics.CounterService;
import run.halo.app.metrics.MeterUtils;
import run.halo.app.plugin.ExtensionComponentsFinder;
import run.halo.app.plugin.extensionpoint.ExtensionGetter;
import run.halo.app.security.authorization.AuthorityUtils;

/**
Expand All @@ -49,22 +49,23 @@ public class CommentServiceImpl implements CommentService {
private final ReactiveExtensionClient client;
private final UserService userService;
private final RoleService roleService;
private final ExtensionComponentsFinder extensionComponentsFinder;
private final ExtensionGetter extensionGetter;

private final SystemConfigurableEnvironmentFetcher environmentFetcher;
private final CounterService counterService;

public CommentServiceImpl(ReactiveExtensionClient client,
UserService userService, ExtensionComponentsFinder extensionComponentsFinder,
UserService userService,
SystemConfigurableEnvironmentFetcher environmentFetcher,
CounterService counterService, RoleService roleService
CounterService counterService, RoleService roleService,
ExtensionGetter extensionGetter
) {
this.client = client;
this.userService = userService;
this.extensionComponentsFinder = extensionComponentsFinder;
this.environmentFetcher = environmentFetcher;
this.counterService = counterService;
this.roleService = roleService;
this.extensionGetter = extensionGetter;
}

@Override
Expand Down Expand Up @@ -247,11 +248,9 @@ private Mono<OwnerInfo> getCommentOwnerInfo(Comment.CommentOwner owner) {

@SuppressWarnings("unchecked")
Mono<Extension> getCommentSubject(Ref ref) {
return extensionComponentsFinder.getExtensions(CommentSubject.class)
.stream()
.filter(commentSubject -> commentSubject.supports(ref))
.findFirst()
.map(commentSubject -> commentSubject.get(ref.getName()))
.orElseGet(Mono::empty);
return extensionGetter.getExtensions(CommentSubject.class)
.filter(subject -> subject.supports(ref))
.next()
.flatMap(subject -> subject.get(ref.getName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@
import run.halo.app.core.extension.service.AttachmentService;
import run.halo.app.extension.ConfigMap;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.plugin.ExtensionComponentsFinder;
import run.halo.app.plugin.extensionpoint.ExtensionGetter;

@Component
public class DefaultAttachmentService implements AttachmentService {

private final ReactiveExtensionClient client;

private final ExtensionComponentsFinder extensionComponentsFinder;
private final ExtensionGetter extensionGetter;

public DefaultAttachmentService(ReactiveExtensionClient client,
ExtensionComponentsFinder extensionComponentsFinder) {
ExtensionGetter extensionGetter) {
this.client = client;
this.extensionComponentsFinder = extensionComponentsFinder;
this.extensionGetter = extensionGetter;
}

@Override
Expand All @@ -61,12 +61,9 @@ public Mono<Attachment> upload(
return client.get(ConfigMap.class, configMapName)
.map(configMap -> new UploadOption(filePart, policy, configMap));
})
.flatMap(uploadContext -> {
var handlers = extensionComponentsFinder.getExtensions(AttachmentHandler.class);
return Flux.fromIterable(handlers)
.concatMap(handler -> handler.upload(uploadContext))
.next();
})
.flatMap(uploadContext -> extensionGetter.getExtensions(AttachmentHandler.class)
.concatMap(handler -> handler.upload(uploadContext))
.next())
.switchIfEmpty(Mono.error(() -> new ServerErrorException(
"No suitable handler found for uploading the attachment.", null)))
.doOnNext(attachment -> {
Expand Down Expand Up @@ -106,32 +103,31 @@ public Mono<Attachment> delete(Attachment attachment) {
return client.get(Policy.class, spec.getPolicyName())
.flatMap(policy -> client.get(ConfigMap.class, policy.getSpec().getConfigMapName())
.map(configMap -> new DeleteOption(attachment, policy, configMap)))
.flatMap(deleteOption -> {
var handlers = extensionComponentsFinder.getExtensions(AttachmentHandler.class);
return Flux.fromIterable(handlers)
.concatMap(handler -> handler.delete(deleteOption))
.next();
});
.flatMap(deleteOption -> extensionGetter.getExtensions(AttachmentHandler.class)
.concatMap(handler -> handler.delete(deleteOption))
.next());
}

@Override
public Mono<URI> getPermalink(Attachment attachment) {
var handlers = extensionComponentsFinder.getExtensions(AttachmentHandler.class);
return client.get(Policy.class, attachment.getSpec().getPolicyName())
.flatMap(policy -> client.get(ConfigMap.class, policy.getSpec().getConfigMapName())
.flatMap(configMap -> Flux.fromIterable(handlers)
.flatMap(configMap -> extensionGetter.getExtensions(AttachmentHandler.class)
.concatMap(handler -> handler.getPermalink(attachment, policy, configMap))
.next()));
.next()
)
);
}

@Override
public Mono<URI> getSharedURL(Attachment attachment, Duration ttl) {
var handlers = extensionComponentsFinder.getExtensions(AttachmentHandler.class);
return client.get(Policy.class, attachment.getSpec().getPolicyName())
.flatMap(policy -> client.get(ConfigMap.class, policy.getSpec().getConfigMapName())
.flatMap(configMap -> Flux.fromIterable(handlers)
.flatMap(configMap -> extensionGetter.getExtensions(AttachmentHandler.class)
.concatMap(handler -> handler.getSharedURL(attachment, policy, configMap, ttl))
.next()));
.next()
)
);
}

private <T> Mono<T> authenticationConsumer(Function<Authentication, Mono<T>> func) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public <E extends Extension> Mono<E> create(E extension) {
})
.map(converter::convertTo)
.flatMap(extStore -> doCreate(extension, extStore.getName(), extStore.getData())
.doOnNext(watchers::onAdd)
.doOnNext(created -> watchers.onAdd(convertToRealExtension(created)))
)
.retryWhen(Retry.backoff(3, Duration.ofMillis(100))
// retry when generateName is set
Expand Down Expand Up @@ -266,7 +266,9 @@ public <E extends Extension> Mono<E> update(E extension) {
var store = this.converter.convertTo(newJsonExt);
var updated = doUpdate(extension, store.getName(), store.getVersion(), store.getData());
if (!onlyStatusChanged) {
updated = updated.doOnNext(ext -> watchers.onUpdate(old, ext));
updated = updated.doOnNext(ext -> watchers.onUpdate(convertToRealExtension(old),
convertToRealExtension(ext))
);
}
return updated;
});
Expand All @@ -293,7 +295,7 @@ public <E extends Extension> Mono<E> delete(E extension) {
var extensionStore = converter.convertTo(extension);
return doUpdate(extension, extensionStore.getName(),
extensionStore.getVersion(), extensionStore.getData()
).doOnNext(watchers::onDelete);
).doOnNext(updated -> watchers.onDelete(convertToRealExtension(extension)));
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import run.halo.app.extension.GroupKind;
import run.halo.app.infra.exception.UserNotFoundException;
import run.halo.app.security.authentication.login.HaloUser;
import run.halo.app.security.authentication.twofactor.TwoFactorUtils;

public class DefaultUserDetailService
implements ReactiveUserDetailsService, ReactiveUserDetailsPasswordService {
Expand Down Expand Up @@ -63,10 +64,9 @@ public Mono<UserDetails> findByUsername(String username) {
.doOnNext(userBuilder::authorities);

return setAuthorities.then(Mono.fromSupplier(() -> {
var twoFactorAuthEnabled =
requireNonNullElse(user.getSpec().getTwoFactorAuthEnabled(), false);
var twoFactorAuthSettings = TwoFactorUtils.getTwoFactorAuthSettings(user);
return new HaloUser.Builder(userBuilder.build())
.twoFactorAuthEnabled(twoFactorAuthEnabled)
.twoFactorAuthEnabled(twoFactorAuthSettings.isAvailable())
.totpEncryptedSecret(user.getSpec().getTotpEncryptedSecret())
.build();
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.session.ReactiveFindByIndexNameSessionRepository;
import org.springframework.session.Session;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
Expand All @@ -31,7 +32,6 @@
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.router.selector.FieldSelector;
import run.halo.app.security.session.ReactiveIndexedSessionRepository;

/**
* Device endpoint for user profile,every user can only manage their own devices.
Expand All @@ -43,7 +43,7 @@
@RequiredArgsConstructor
public class DeviceEndpoint implements CustomEndpoint {
private final ReactiveExtensionClient client;
private final ReactiveIndexedSessionRepository<?> sessionRepository;
private final ReactiveFindByIndexNameSessionRepository<?> sessionRepository;
private final DeviceService deviceService;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package run.halo.app.theme.dialect;

import java.util.Collection;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import static org.thymeleaf.spring6.context.SpringContextUtils.getApplicationContext;

import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IModel;
import org.thymeleaf.model.ITemplateEvent;
import org.thymeleaf.processor.element.AbstractElementModelProcessor;
import org.thymeleaf.processor.element.IElementModelStructureHandler;
import org.thymeleaf.spring6.context.SpringContextUtils;
import org.thymeleaf.templatemode.TemplateMode;
import run.halo.app.plugin.ExtensionComponentsFinder;
import reactor.core.publisher.Flux;
import run.halo.app.plugin.extensionpoint.ExtensionGetter;

/**
* Global head injection processor.
Expand Down Expand Up @@ -71,13 +70,10 @@ protected void doProcess(ITemplateContext context, IModel model,
modelToInsert.remove(0);

// apply processors to modelToInsert
Collection<TemplateHeadProcessor> templateHeadProcessors =
getTemplateHeadProcessors(context);

for (TemplateHeadProcessor processor : templateHeadProcessors) {
processor.process(context, modelToInsert, structureHandler)
.block();
}
getTemplateHeadProcessors(context)
.concatMap(processor -> processor.process(context, modelToInsert, structureHandler))
.then()
.block();

// reset model to insert
model.reset();
Expand All @@ -86,13 +82,12 @@ protected void doProcess(ITemplateContext context, IModel model,
model.add(closeHeadTag);
}

private Collection<TemplateHeadProcessor> getTemplateHeadProcessors(ITemplateContext context) {
ApplicationContext appCtx = SpringContextUtils.getApplicationContext(context);
ExtensionComponentsFinder componentsFinder =
appCtx.getBean(ExtensionComponentsFinder.class);
return componentsFinder.getExtensions(TemplateHeadProcessor.class)
.stream()
.sorted(AnnotationAwareOrderComparator.INSTANCE)
.toList();
private Flux<TemplateHeadProcessor> getTemplateHeadProcessors(ITemplateContext context) {
var extensionGetter = getApplicationContext(context).getBeanProvider(ExtensionGetter.class)
.getIfUnique();
if (extensionGetter == null) {
return Flux.empty();
}
return extensionGetter.getExtensions(TemplateHeadProcessor.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ public interface CategoryFinder {
Flux<CategoryTreeVo> listAsTree(String name);

Mono<CategoryVo> getParentByName(String name);

Flux<CategoryVo> getBreadcrumbs(String name);
}
Loading

0 comments on commit efdc012

Please sign in to comment.