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.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<String, Object> 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<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();
// Init user in development environment
if (!haloProperties.isProductionEnv()) {
initAnTestUserIfAbsent();
}
// if (!haloProperties.isProductionEnv()) {
// initAnTestUserIfAbsent();
// }
}
/**
* Initialize an test user if absent
*/
private void initAnTestUserIfAbsent() {
// Create an user if absent
List<User> 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<User> 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();

View File

@ -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<PostComment> {
*/
@NonNull
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.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<PostComment>
}).collect(Collectors.toList());
}
@Override
public Page<PostCommentWithPostVO> pageTreeBy(CommentQuery commentQuery, Pageable pageable) {
Page<PostComment> postCommentPage = pageBy(commentQuery, pageable);
return null;
}
@Override
public void targetMustExist(Integer 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.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<User, Integer> 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<User, Integer> 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;
}