Merge remote-tracking branch 'origin/v1' into v1

pull/146/head
ruibaby 2019-05-04 12:23:11 +08:00
commit a10fa0e312
11 changed files with 57 additions and 64 deletions

View File

@ -79,7 +79,8 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer {
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
String workDir = FILE_PROTOCOL + haloProperties.getWorkDir(); String workDir = FILE_PROTOCOL + haloProperties.getWorkDir();
registry.addResourceHandler("/static/**") registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/"); .addResourceLocations("classpath:/static/")
.addResourceLocations(workDir + "static/");
registry.addResourceHandler("/**") registry.addResourceHandler("/**")
.addResourceLocations(workDir + "templates/themes/") .addResourceLocations(workDir + "templates/themes/")
.addResourceLocations(workDir + "templates/admin/") .addResourceLocations(workDir + "templates/admin/")

View File

@ -43,8 +43,10 @@ public class JournalCommentController {
} }
@GetMapping("latest") @GetMapping("latest")
public List<JournalCommentWithJournalVO> listLatest(@RequestParam(name = "top", defaultValue = "10") int top) { public List<JournalCommentWithJournalVO> listLatest(@RequestParam(name = "top", defaultValue = "10") int top,
return journalCommentService.convertToWithJournalVo(journalCommentService.pageLatest(top).getContent()); @RequestParam(name = "status", required = false) CommentStatus status) {
List<JournalComment> latestComments = journalCommentService.pageLatest(top, status).getContent();
return journalCommentService.convertToWithJournalVo(latestComments);
} }
@PostMapping @PostMapping

View File

@ -43,16 +43,12 @@ public class PostCommentController {
@GetMapping("latest") @GetMapping("latest")
@ApiOperation("Pages latest comments") @ApiOperation("Pages latest comments")
public List<PostCommentWithPostVO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
List<PostComment> content = postCommentService.pageLatest(top).getContent();
return postCommentService.convertToWithPostVo(content);
}
@GetMapping("latest/{status}")
@ApiOperation("Pages latest comments by status")
public List<PostCommentWithPostVO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top, public List<PostCommentWithPostVO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top,
@PathVariable("status") CommentStatus status) { @RequestParam(name = "status", required = false) CommentStatus status) {
// Get latest comment
List<PostComment> content = postCommentService.pageLatest(top, status).getContent(); List<PostComment> content = postCommentService.pageLatest(top, status).getContent();
// Convert and return
return postCommentService.convertToWithPostVo(content); return postCommentService.convertToWithPostVo(content);
} }

View File

@ -41,8 +41,9 @@ public class SheetCommentController {
} }
@GetMapping("latest") @GetMapping("latest")
public List<SheetCommentWithSheetVO> listLatest(@RequestParam(name = "top", defaultValue = "10") int top) { public List<SheetCommentWithSheetVO> listLatest(@RequestParam(name = "top", defaultValue = "10") int top,
Page<SheetComment> sheetCommentPage = sheetCommentService.pageLatest(top); @RequestParam(name = "status", required = false) CommentStatus status) {
Page<SheetComment> sheetCommentPage = sheetCommentService.pageLatest(top, status);
return sheetCommentService.convertToWithPostVo(sheetCommentPage.getContent()); return sheetCommentService.convertToWithPostVo(sheetCommentPage.getContent());
} }

View File

@ -1,6 +1,5 @@
package run.halo.app.controller.content; package run.halo.app.controller.content;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -13,6 +12,8 @@ import run.halo.app.service.SheetService;
import run.halo.app.service.ThemeService; import run.halo.app.service.ThemeService;
/** /**
* Content sheet controller.
*
* @author ryanwang * @author ryanwang
* @date : 2019-03-21 * @date : 2019-03-21
*/ */
@ -68,10 +69,8 @@ public class ContentSheetController {
model.addAttribute("post", sheetService.convertToDetail(sheet)); model.addAttribute("post", sheetService.convertToDetail(sheet));
model.addAttribute("is_sheet", true); model.addAttribute("is_sheet", true);
if (StrUtil.isNotEmpty(sheet.getTemplate())) { if (themeService.templateExists(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate() + HaloConst.SUFFIX_FTL)) {
if (themeService.templateExists(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate() + HaloConst.SUFFIX_FTL)) { return themeService.render(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate());
return themeService.render(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate());
}
} }
return themeService.render("sheet"); return themeService.render("sheet");
} }

View File

@ -52,38 +52,8 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
// save halo version to database // save halo version to database
this.printStartInfo(); this.printStartInfo();
this.initThemes(); this.initThemes();
// Init user in development environment
// 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);
// }
// }
private void printStartInfo() { private void printStartInfo() {
String blogUrl = optionService.getBlogBaseUrl(); String blogUrl = optionService.getBlogBaseUrl();
@ -102,7 +72,7 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
// Whether the blog has initialized // Whether the blog has initialized
Boolean isInstalled = optionService.getByPropertyOrDefault(PrimaryProperties.IS_INSTALLED, Boolean.class, false); Boolean isInstalled = optionService.getByPropertyOrDefault(PrimaryProperties.IS_INSTALLED, Boolean.class, false);
if (isInstalled) { if (haloProperties.isProductionEnv() && isInstalled) {
// Skip // Skip
return; return;
} }

View File

@ -1,6 +1,7 @@
package run.halo.app.service; package run.halo.app.service;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import run.halo.app.handler.theme.config.support.Group; import run.halo.app.handler.theme.config.support.Group;
import run.halo.app.handler.theme.config.support.ThemeProperty; import run.halo.app.handler.theme.config.support.ThemeProperty;
@ -65,6 +66,9 @@ public interface ThemeService {
*/ */
String THEMES_CACHE_KEY = "themes"; String THEMES_CACHE_KEY = "themes";
/**
* Custom sheet prefix.
*/
String CUSTOM_SHEET_PREFIX = "sheet_"; String CUSTOM_SHEET_PREFIX = "sheet_";
/** /**
@ -79,11 +83,11 @@ public interface ThemeService {
/** /**
* Get theme property by theme id. * Get theme property by theme id.
* *
* @param themeId must not be blank * @param themeId theme id
* @return a optional theme property * @return a optional theme property
*/ */
@NonNull @NonNull
Optional<ThemeProperty> getThemeBy(@NonNull String themeId); Optional<ThemeProperty> getThemeBy(@Nullable String themeId);
/** /**
* Gets all themes * Gets all themes
@ -123,15 +127,15 @@ public interface ThemeService {
* @param template template must not be blank * @param template template must not be blank
* @return boolean * @return boolean
*/ */
boolean templateExists(@NonNull String template); boolean templateExists(@Nullable String template);
/** /**
* Checks whether theme exists under template path * Checks whether theme exists under template path
* *
* @param themeId theme name * @param themeId theme id
* @return boolean * @return boolean
*/ */
boolean themeExists(@NonNull String themeId); boolean themeExists(@Nullable String themeId);
/** /**
* Gets theme base path. * Gets theme base path.

View File

@ -59,7 +59,7 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
* @return a page of comments * @return a page of comments
*/ */
@NonNull @NonNull
Page<COMMENT> pageLatest(int top, CommentStatus status); Page<COMMENT> pageLatest(int top, @Nullable CommentStatus status);
/** /**
* Pages comments. * Pages comments.

View File

@ -7,6 +7,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import run.halo.app.cache.StringCacheStore; import run.halo.app.cache.StringCacheStore;
import run.halo.app.exception.BadRequestException; import run.halo.app.exception.BadRequestException;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.dto.StatisticDTO; import run.halo.app.model.dto.StatisticDTO;
import run.halo.app.model.entity.User; import run.halo.app.model.entity.User;
import run.halo.app.model.enums.CommentStatus; import run.halo.app.model.enums.CommentStatus;
@ -78,14 +79,25 @@ public class AdminServiceImpl implements AdminService {
Assert.notNull(loginParam, "Login param must not be null"); Assert.notNull(loginParam, "Login param must not be null");
String username = loginParam.getUsername(); String username = loginParam.getUsername();
User user = Validator.isEmail(username) ?
userService.getByEmailOfNonNull(username) : userService.getByUsernameOfNonNull(username); String mismatchTip = "用户名或者密码不正确";
final User user;
try {
// Get user by username or email
user = Validator.isEmail(username) ?
userService.getByEmailOfNonNull(username) : userService.getByUsernameOfNonNull(username);
} catch (NotFoundException e) {
log.error("Failed to find user by name: " + username, e);
throw new BadRequestException(mismatchTip);
}
userService.mustNotExpire(user); userService.mustNotExpire(user);
if (!userService.passwordMatch(user, loginParam.getPassword())) { if (!userService.passwordMatch(user, loginParam.getPassword())) {
// If the password is mismatch // If the password is mismatch
throw new BadRequestException("用户名或者密码不正确"); throw new BadRequestException(mismatchTip);
} }
if (SecurityContextHolder.getContext().isAuthenticated()) { if (SecurityContextHolder.getContext().isAuthenticated()) {

View File

@ -75,12 +75,16 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
@Override @Override
public Page<COMMENT> pageLatest(int top) { public Page<COMMENT> pageLatest(int top) {
return listAll(ServiceUtils.buildLatestPageable(top)); return pageLatest(top, null);
} }
@Override @Override
public Page<COMMENT> pageLatest(int top,CommentStatus status){ public Page<COMMENT> pageLatest(int top, CommentStatus status) {
return baseCommentRepository.findAllByStatus(status,ServiceUtils.buildLatestPageable(top)); if (status == null) {
return listAll(ServiceUtils.buildLatestPageable(top));
}
return baseCommentRepository.findAllByStatus(status, ServiceUtils.buildLatestPageable(top));
} }
@Override @Override

View File

@ -105,7 +105,9 @@ public class ThemeServiceImpl implements ThemeService {
@Override @Override
public Optional<ThemeProperty> getThemeBy(String themeId) { public Optional<ThemeProperty> getThemeBy(String themeId) {
Assert.hasText(themeId, "Theme id must not be blank"); if (StringUtils.isBlank(themeId)) {
return Optional.empty();
}
// Get all themes // Get all themes
Set<ThemeProperty> themes = getThemes(); Set<ThemeProperty> themes = getThemes();
@ -181,7 +183,9 @@ public class ThemeServiceImpl implements ThemeService {
@Override @Override
public boolean templateExists(String template) { public boolean templateExists(String template) {
Assert.hasText(template, "Template must not be blank"); if (StringUtils.isBlank(template)) {
return false;
}
// Resolve template path // Resolve template path
Path templatePath = Paths.get(getActivatedTheme().getThemePath(), template); Path templatePath = Paths.get(getActivatedTheme().getThemePath(), template);
@ -507,7 +511,7 @@ public class ThemeServiceImpl implements ThemeService {
*/ */
private void setActivatedTheme(@Nullable ThemeProperty activatedTheme) { private void setActivatedTheme(@Nullable ThemeProperty activatedTheme) {
this.activatedTheme = activatedTheme; this.activatedTheme = activatedTheme;
this.activatedThemeId = activatedTheme.getId(); this.activatedThemeId = Optional.ofNullable(activatedTheme).map(ThemeProperty::getId).orElse(null);
} }
/** /**