diff --git a/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java b/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java index eeefc0ce0..f5de0ac22 100644 --- a/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java +++ b/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java @@ -9,6 +9,7 @@ import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; +import run.halo.app.event.user.UserUpdatedEvent; import run.halo.app.handler.theme.config.support.ThemeProperty; import run.halo.app.service.OptionService; import run.halo.app.service.ThemeService; @@ -56,36 +57,43 @@ public class FreemarkerConfigAwareListener { @Async @EventListener @Order(Ordered.HIGHEST_PRECEDENCE + 1) - public void onApplicationStartedEvent(ApplicationStartedEvent applicationStartedEvent) { - try { - configuration.setSharedVariable("theme", themeService.getActivatedTheme()); - configuration.setSharedVariable("options", optionsService.listOptions()); - configuration.setSharedVariable("user", userService.getCurrentUser().orElse(null)); - configuration.setSharedVariable("settings", themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); - log.info("Initialized freemarker configuration"); - } catch (TemplateModelException e) { - log.warn("Failed to configure freemarker", e); - // Ignore this error - } + public void onApplicationStartedEvent(ApplicationStartedEvent applicationStartedEvent) throws TemplateModelException { + log.debug("Received application started event"); + + loadThemeConfig(); + loadOptionsConfig(); + loadUserConfig(); } @Async @EventListener - public void onThemeActivatedEvent(ThemeActivatedEvent themeActivatedEvent) { + public void onThemeActivatedEvent(ThemeActivatedEvent themeActivatedEvent) throws TemplateModelException { log.debug("Received theme activated event"); - try { - ThemeProperty activatedTheme = themeService.getActivatedTheme(); - log.debug("Set shared variable theme: [{}]", activatedTheme); - configuration.setSharedVariable("theme", activatedTheme); - Map options = optionService.listOptions(); - log.debug("Set shared variable options: [{}]", options); - configuration.setSharedVariable("options", options); - log.debug("Set shared variable theme settings: [{}]", options); - configuration.setSharedVariable("settings", themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); - } catch (TemplateModelException e) { - log.warn("Failed to configure freemarker", e); - // Ignore this error - } + loadThemeConfig(); + } + + @Async + @EventListener + public void onUserUpdate(UserUpdatedEvent event) throws TemplateModelException { + log.debug("Received user update event, user id: [{}]", event.getUserId()); + + loadUserConfig(); + } + + + private void loadUserConfig() throws TemplateModelException { + configuration.setSharedVariable("user", userService.getCurrentUser().orElse(null)); + } + + private void loadOptionsConfig() throws TemplateModelException { + Map options = optionService.listOptions(); + configuration.setSharedVariable("options", options); + } + + private void loadThemeConfig() throws TemplateModelException { + ThemeProperty activatedTheme = themeService.getActivatedTheme(); + configuration.setSharedVariable("theme", activatedTheme); + configuration.setSharedVariable("settings", themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); } } diff --git a/src/main/java/run/halo/app/event/user/UserUpdatedEvent.java b/src/main/java/run/halo/app/event/user/UserUpdatedEvent.java new file mode 100644 index 000000000..94856d645 --- /dev/null +++ b/src/main/java/run/halo/app/event/user/UserUpdatedEvent.java @@ -0,0 +1,34 @@ +package run.halo.app.event.user; + +import org.springframework.context.ApplicationEvent; +import org.springframework.lang.NonNull; +import org.springframework.util.Assert; + +/** + * User update event. (creation or updating) + * + * @author johnniang + * @date 19-4-29 + */ +public class UserUpdatedEvent extends ApplicationEvent { + + private final Integer userId; + + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + * @param userId user id must not be null + */ + public UserUpdatedEvent(Object source, @NonNull Integer userId) { + super(source); + Assert.notNull(userId, "User id must not be null"); + + this.userId = userId; + } + + @NonNull + public Integer getUserId() { + return userId; + } +} diff --git a/src/main/java/run/halo/app/listener/StartedListener.java b/src/main/java/run/halo/app/listener/StartedListener.java index a4930accf..9c044b542 100644 --- a/src/main/java/run/halo/app/listener/StartedListener.java +++ b/src/main/java/run/halo/app/listener/StartedListener.java @@ -54,35 +54,35 @@ public class StartedListener implements ApplicationListener users = userService.listAll(); - - if (users.isEmpty()) { - UserParam userParam = new UserParam(); - userParam.setUsername("test"); - userParam.setNickname("developer"); - userParam.setEmail("test@test.com"); - userParam.setPassword("opentest"); - - log.debug("Initializing a test user: [{}]", userParam); - - // Validate the user param - ValidationUtils.validate(userParam, CreateCheck.class); - - User testUser = userService.createBy(userParam); - - log.debug("Initialized a test user: [{}]", testUser); - } - } +// /** +// * Initialize an test user if absent +// */ +// private void initAnTestUserIfAbsent() { +// // Create an user if absent +// List users = userService.listAll(); +// +// if (users.isEmpty()) { +// UserParam userParam = new UserParam(); +// userParam.setUsername("test"); +// userParam.setNickname("developer"); +// userParam.setEmail("test@test.com"); +// userParam.setPassword("opentest"); +// +// log.debug("Initializing a test user: [{}]", userParam); +// +// // Validate the user param +// ValidationUtils.validate(userParam, CreateCheck.class); +// +// User testUser = userService.createBy(userParam); +// +// log.debug("Initialized a test user: [{}]", testUser); +// } +// } private void printStartInfo() { String blogUrl = optionService.getBlogBaseUrl(); diff --git a/src/main/java/run/halo/app/service/PostCommentService.java b/src/main/java/run/halo/app/service/PostCommentService.java index b7f5161d1..a0e55c3b1 100644 --- a/src/main/java/run/halo/app/service/PostCommentService.java +++ b/src/main/java/run/halo/app/service/PostCommentService.java @@ -1,9 +1,11 @@ package run.halo.app.service; import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import run.halo.app.model.entity.PostComment; +import run.halo.app.model.params.CommentQuery; import run.halo.app.model.vo.PostCommentWithPostVO; import run.halo.app.service.base.BaseCommentService; @@ -33,4 +35,7 @@ public interface PostCommentService extends BaseCommentService { */ @NonNull List convertToWithPostVo(@Nullable List postComments); + + @NonNull + Page pageTreeBy(@NonNull CommentQuery commentQuery, @NonNull Pageable pageable); } diff --git a/src/main/java/run/halo/app/service/impl/PostCommentServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostCommentServiceImpl.java index 8ce5d1302..6cbc658bd 100644 --- a/src/main/java/run/halo/app/service/impl/PostCommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostCommentServiceImpl.java @@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -11,6 +12,7 @@ import run.halo.app.exception.NotFoundException; import run.halo.app.model.dto.post.BasePostMinimalDTO; import run.halo.app.model.entity.Post; import run.halo.app.model.entity.PostComment; +import run.halo.app.model.params.CommentQuery; import run.halo.app.model.vo.PostCommentWithPostVO; import run.halo.app.repository.PostCommentRepository; import run.halo.app.repository.PostRepository; @@ -80,6 +82,13 @@ public class PostCommentServiceImpl extends BaseCommentServiceImpl }).collect(Collectors.toList()); } + @Override + public Page pageTreeBy(CommentQuery commentQuery, Pageable pageable) { + Page postCommentPage = pageBy(commentQuery, pageable); + + return null; + } + @Override public void targetMustExist(Integer postId) { if (!postRepository.existsById(postId)) { diff --git a/src/main/java/run/halo/app/service/impl/UserServiceImpl.java b/src/main/java/run/halo/app/service/impl/UserServiceImpl.java index 3cc244ca7..cd694fad2 100644 --- a/src/main/java/run/halo/app/service/impl/UserServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/UserServiceImpl.java @@ -10,6 +10,7 @@ import org.springframework.util.CollectionUtils; import run.halo.app.cache.StringCacheStore; import run.halo.app.cache.lock.CacheLock; import run.halo.app.event.logger.LogEvent; +import run.halo.app.event.user.UserUpdatedEvent; import run.halo.app.exception.BadRequestException; import run.halo.app.exception.NotFoundException; import run.halo.app.model.entity.User; @@ -211,7 +212,11 @@ public class UserServiceImpl extends AbstractCrudService implemen throw new BadRequestException("This blog already exists a blogger"); } - return super.create(user); + User createdUser = super.create(user); + + eventPublisher.publishEvent(new UserUpdatedEvent(this, createdUser.getId())); + + return createdUser; } @Override @@ -220,6 +225,7 @@ public class UserServiceImpl extends AbstractCrudService implemen // Log it eventPublisher.publishEvent(new LogEvent(this, user.getId().toString(), LogType.PROFILE_UPDATED, user.getUsername())); + eventPublisher.publishEvent(new UserUpdatedEvent(this, user.getId())); return updatedUser; }