Complete user update event

pull/146/head
johnniang 2019-04-29 00:45:03 +08:00
parent 0dbe543cff
commit 63d072776e
6 changed files with 115 additions and 53 deletions

View File

@ -9,6 +9,7 @@ import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; 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.handler.theme.config.support.ThemeProperty;
import run.halo.app.service.OptionService; import run.halo.app.service.OptionService;
import run.halo.app.service.ThemeService; import run.halo.app.service.ThemeService;
@ -56,36 +57,43 @@ public class FreemarkerConfigAwareListener {
@Async @Async
@EventListener @EventListener
@Order(Ordered.HIGHEST_PRECEDENCE + 1) @Order(Ordered.HIGHEST_PRECEDENCE + 1)
public void onApplicationStartedEvent(ApplicationStartedEvent applicationStartedEvent) { public void onApplicationStartedEvent(ApplicationStartedEvent applicationStartedEvent) throws TemplateModelException {
try { log.debug("Received application started event");
configuration.setSharedVariable("theme", themeService.getActivatedTheme());
configuration.setSharedVariable("options", optionsService.listOptions()); loadThemeConfig();
configuration.setSharedVariable("user", userService.getCurrentUser().orElse(null)); loadOptionsConfig();
configuration.setSharedVariable("settings", themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); loadUserConfig();
log.info("Initialized freemarker configuration");
} catch (TemplateModelException e) {
log.warn("Failed to configure freemarker", e);
// Ignore this error
}
} }
@Async @Async
@EventListener @EventListener
public void onThemeActivatedEvent(ThemeActivatedEvent themeActivatedEvent) { public void onThemeActivatedEvent(ThemeActivatedEvent themeActivatedEvent) throws TemplateModelException {
log.debug("Received theme activated event"); log.debug("Received theme activated event");
try { loadThemeConfig();
ThemeProperty activatedTheme = themeService.getActivatedTheme(); }
log.debug("Set shared variable theme: [{}]", activatedTheme);
configuration.setSharedVariable("theme", activatedTheme); @Async
Map<String, Object> options = optionService.listOptions(); @EventListener
log.debug("Set shared variable options: [{}]", options); public void onUserUpdate(UserUpdatedEvent event) throws TemplateModelException {
configuration.setSharedVariable("options", options); log.debug("Received user update event, user id: [{}]", event.getUserId());
log.debug("Set shared variable theme settings: [{}]", options);
configuration.setSharedVariable("settings", themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); loadUserConfig();
} catch (TemplateModelException e) { }
log.warn("Failed to configure freemarker", e);
// Ignore this error
} private void loadUserConfig() throws TemplateModelException {
configuration.setSharedVariable("user", userService.getCurrentUser().orElse(null));
}
private void loadOptionsConfig() throws TemplateModelException {
Map<String, Object> 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()));
} }
} }

View File

@ -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;
}
}

View File

@ -54,35 +54,35 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
this.initThemes(); this.initThemes();
// Init user in development environment // Init user in development environment
if (!haloProperties.isProductionEnv()) { // if (!haloProperties.isProductionEnv()) {
initAnTestUserIfAbsent(); // initAnTestUserIfAbsent();
} // }
} }
/** // /**
* Initialize an test user if absent // * Initialize an test user if absent
*/ // */
private void initAnTestUserIfAbsent() { // private void initAnTestUserIfAbsent() {
// Create an user if absent // // Create an user if absent
List<User> users = userService.listAll(); // List<User> users = userService.listAll();
//
if (users.isEmpty()) { // if (users.isEmpty()) {
UserParam userParam = new UserParam(); // UserParam userParam = new UserParam();
userParam.setUsername("test"); // userParam.setUsername("test");
userParam.setNickname("developer"); // userParam.setNickname("developer");
userParam.setEmail("test@test.com"); // userParam.setEmail("test@test.com");
userParam.setPassword("opentest"); // userParam.setPassword("opentest");
//
log.debug("Initializing a test user: [{}]", userParam); // log.debug("Initializing a test user: [{}]", userParam);
//
// Validate the user param // // Validate the user param
ValidationUtils.validate(userParam, CreateCheck.class); // ValidationUtils.validate(userParam, CreateCheck.class);
//
User testUser = userService.createBy(userParam); // User testUser = userService.createBy(userParam);
//
log.debug("Initialized a test user: [{}]", testUser); // log.debug("Initialized a test user: [{}]", testUser);
} // }
} // }
private void printStartInfo() { private void printStartInfo() {
String blogUrl = optionService.getBlogBaseUrl(); String blogUrl = optionService.getBlogBaseUrl();

View File

@ -1,9 +1,11 @@
package run.halo.app.service; package run.halo.app.service;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import run.halo.app.model.entity.PostComment; 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.model.vo.PostCommentWithPostVO;
import run.halo.app.service.base.BaseCommentService; import run.halo.app.service.base.BaseCommentService;
@ -33,4 +35,7 @@ public interface PostCommentService extends BaseCommentService<PostComment> {
*/ */
@NonNull @NonNull
List<PostCommentWithPostVO> convertToWithPostVo(@Nullable List<PostComment> postComments); List<PostCommentWithPostVO> convertToWithPostVo(@Nullable List<PostComment> postComments);
@NonNull
Page<PostCommentWithPostVO> pageTreeBy(@NonNull CommentQuery commentQuery, @NonNull Pageable pageable);
} }

View File

@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; 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.dto.post.BasePostMinimalDTO;
import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.PostComment; 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.model.vo.PostCommentWithPostVO;
import run.halo.app.repository.PostCommentRepository; import run.halo.app.repository.PostCommentRepository;
import run.halo.app.repository.PostRepository; import run.halo.app.repository.PostRepository;
@ -80,6 +82,13 @@ public class PostCommentServiceImpl extends BaseCommentServiceImpl<PostComment>
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }
@Override
public Page<PostCommentWithPostVO> pageTreeBy(CommentQuery commentQuery, Pageable pageable) {
Page<PostComment> postCommentPage = pageBy(commentQuery, pageable);
return null;
}
@Override @Override
public void targetMustExist(Integer postId) { public void targetMustExist(Integer postId) {
if (!postRepository.existsById(postId)) { if (!postRepository.existsById(postId)) {

View File

@ -10,6 +10,7 @@ import org.springframework.util.CollectionUtils;
import run.halo.app.cache.StringCacheStore; import run.halo.app.cache.StringCacheStore;
import run.halo.app.cache.lock.CacheLock; import run.halo.app.cache.lock.CacheLock;
import run.halo.app.event.logger.LogEvent; 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.BadRequestException;
import run.halo.app.exception.NotFoundException; import run.halo.app.exception.NotFoundException;
import run.halo.app.model.entity.User; import run.halo.app.model.entity.User;
@ -211,7 +212,11 @@ public class UserServiceImpl extends AbstractCrudService<User, Integer> implemen
throw new BadRequestException("This blog already exists a blogger"); 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 @Override
@ -220,6 +225,7 @@ public class UserServiceImpl extends AbstractCrudService<User, Integer> implemen
// Log it // Log it
eventPublisher.publishEvent(new LogEvent(this, user.getId().toString(), LogType.PROFILE_UPDATED, user.getUsername())); eventPublisher.publishEvent(new LogEvent(this, user.getId().toString(), LogType.PROFILE_UPDATED, user.getUsername()));
eventPublisher.publishEvent(new UserUpdatedEvent(this, user.getId()));
return updatedUser; return updatedUser;
} }