diff --git a/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java b/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java index 07430b10f..d46577754 100644 --- a/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java +++ b/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java @@ -79,7 +79,8 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer { public void addResourceHandlers(ResourceHandlerRegistry registry) { String workDir = FILE_PROTOCOL + haloProperties.getWorkDir(); registry.addResourceHandler("/static/**") - .addResourceLocations("classpath:/static/"); + .addResourceLocations("classpath:/static/") + .addResourceLocations(workDir + "static/"); registry.addResourceHandler("/**") .addResourceLocations(workDir + "templates/themes/") .addResourceLocations(workDir + "templates/admin/") diff --git a/src/main/java/run/halo/app/controller/admin/api/JournalCommentController.java b/src/main/java/run/halo/app/controller/admin/api/JournalCommentController.java index 8224279d7..195688f4b 100644 --- a/src/main/java/run/halo/app/controller/admin/api/JournalCommentController.java +++ b/src/main/java/run/halo/app/controller/admin/api/JournalCommentController.java @@ -43,8 +43,10 @@ public class JournalCommentController { } @GetMapping("latest") - public List listLatest(@RequestParam(name = "top", defaultValue = "10") int top) { - return journalCommentService.convertToWithJournalVo(journalCommentService.pageLatest(top).getContent()); + public List listLatest(@RequestParam(name = "top", defaultValue = "10") int top, + @RequestParam(name = "status", required = false) CommentStatus status) { + List latestComments = journalCommentService.pageLatest(top, status).getContent(); + return journalCommentService.convertToWithJournalVo(latestComments); } @PostMapping diff --git a/src/main/java/run/halo/app/controller/admin/api/PostCommentController.java b/src/main/java/run/halo/app/controller/admin/api/PostCommentController.java index 154c32506..bd83e0c2a 100644 --- a/src/main/java/run/halo/app/controller/admin/api/PostCommentController.java +++ b/src/main/java/run/halo/app/controller/admin/api/PostCommentController.java @@ -43,16 +43,12 @@ public class PostCommentController { @GetMapping("latest") @ApiOperation("Pages latest comments") - public List pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) { - List content = postCommentService.pageLatest(top).getContent(); - return postCommentService.convertToWithPostVo(content); - } - - @GetMapping("latest/{status}") - @ApiOperation("Pages latest comments by status") public List pageLatest(@RequestParam(name = "top", defaultValue = "10") int top, - @PathVariable("status") CommentStatus status) { + @RequestParam(name = "status", required = false) CommentStatus status) { + // Get latest comment List content = postCommentService.pageLatest(top, status).getContent(); + + // Convert and return return postCommentService.convertToWithPostVo(content); } diff --git a/src/main/java/run/halo/app/controller/admin/api/SheetCommentController.java b/src/main/java/run/halo/app/controller/admin/api/SheetCommentController.java index 7ac48883b..4b648c8a1 100644 --- a/src/main/java/run/halo/app/controller/admin/api/SheetCommentController.java +++ b/src/main/java/run/halo/app/controller/admin/api/SheetCommentController.java @@ -41,8 +41,9 @@ public class SheetCommentController { } @GetMapping("latest") - public List listLatest(@RequestParam(name = "top", defaultValue = "10") int top) { - Page sheetCommentPage = sheetCommentService.pageLatest(top); + public List listLatest(@RequestParam(name = "top", defaultValue = "10") int top, + @RequestParam(name = "status", required = false) CommentStatus status) { + Page sheetCommentPage = sheetCommentService.pageLatest(top, status); return sheetCommentService.convertToWithPostVo(sheetCommentPage.getContent()); } diff --git a/src/main/java/run/halo/app/controller/content/ContentSheetController.java b/src/main/java/run/halo/app/controller/content/ContentSheetController.java index 8d9305093..1b6e84a4d 100644 --- a/src/main/java/run/halo/app/controller/content/ContentSheetController.java +++ b/src/main/java/run/halo/app/controller/content/ContentSheetController.java @@ -1,6 +1,5 @@ package run.halo.app.controller.content; -import cn.hutool.core.util.StrUtil; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -13,6 +12,8 @@ import run.halo.app.service.SheetService; import run.halo.app.service.ThemeService; /** + * Content sheet controller. + * * @author ryanwang * @date : 2019-03-21 */ @@ -68,10 +69,8 @@ public class ContentSheetController { model.addAttribute("post", sheetService.convertToDetail(sheet)); model.addAttribute("is_sheet", true); - if (StrUtil.isNotEmpty(sheet.getTemplate())) { - if (themeService.templateExists(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate() + HaloConst.SUFFIX_FTL)) { - return themeService.render(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate()); - } + if (themeService.templateExists(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate() + HaloConst.SUFFIX_FTL)) { + return themeService.render(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate()); } return themeService.render("sheet"); } diff --git a/src/main/java/run/halo/app/listener/StartedListener.java b/src/main/java/run/halo/app/listener/StartedListener.java index 9c044b542..2025ed41e 100644 --- a/src/main/java/run/halo/app/listener/StartedListener.java +++ b/src/main/java/run/halo/app/listener/StartedListener.java @@ -52,38 +52,8 @@ 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); -// } -// } - private void printStartInfo() { String blogUrl = optionService.getBlogBaseUrl(); @@ -102,7 +72,7 @@ public class StartedListener implements ApplicationListener getThemeBy(@NonNull String themeId); + Optional getThemeBy(@Nullable String themeId); /** * Gets all themes @@ -123,15 +127,15 @@ public interface ThemeService { * @param template template must not be blank * @return boolean */ - boolean templateExists(@NonNull String template); + boolean templateExists(@Nullable String template); /** * Checks whether theme exists under template path * - * @param themeId theme name + * @param themeId theme id * @return boolean */ - boolean themeExists(@NonNull String themeId); + boolean themeExists(@Nullable String themeId); /** * Gets theme base path. diff --git a/src/main/java/run/halo/app/service/base/BaseCommentService.java b/src/main/java/run/halo/app/service/base/BaseCommentService.java index 01cb03613..a153bb97f 100644 --- a/src/main/java/run/halo/app/service/base/BaseCommentService.java +++ b/src/main/java/run/halo/app/service/base/BaseCommentService.java @@ -59,7 +59,7 @@ public interface BaseCommentService extends CrudSer * @return a page of comments */ @NonNull - Page pageLatest(int top, CommentStatus status); + Page pageLatest(int top, @Nullable CommentStatus status); /** * Pages comments. diff --git a/src/main/java/run/halo/app/service/impl/AdminServiceImpl.java b/src/main/java/run/halo/app/service/impl/AdminServiceImpl.java index f09259602..e92337c27 100644 --- a/src/main/java/run/halo/app/service/impl/AdminServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/AdminServiceImpl.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Service; import org.springframework.util.Assert; import run.halo.app.cache.StringCacheStore; import run.halo.app.exception.BadRequestException; +import run.halo.app.exception.NotFoundException; import run.halo.app.model.dto.StatisticDTO; import run.halo.app.model.entity.User; 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"); 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); if (!userService.passwordMatch(user, loginParam.getPassword())) { // If the password is mismatch - throw new BadRequestException("用户名或者密码不正确"); + throw new BadRequestException(mismatchTip); } if (SecurityContextHolder.getContext().isAuthenticated()) { diff --git a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java index fc1994a38..52e1b02a0 100644 --- a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java @@ -75,12 +75,16 @@ public abstract class BaseCommentServiceImpl extend @Override public Page pageLatest(int top) { - return listAll(ServiceUtils.buildLatestPageable(top)); + return pageLatest(top, null); } @Override - public Page pageLatest(int top,CommentStatus status){ - return baseCommentRepository.findAllByStatus(status,ServiceUtils.buildLatestPageable(top)); + public Page pageLatest(int top, CommentStatus status) { + if (status == null) { + return listAll(ServiceUtils.buildLatestPageable(top)); + } + + return baseCommentRepository.findAllByStatus(status, ServiceUtils.buildLatestPageable(top)); } @Override diff --git a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java index a967a39c4..f16dad8c6 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java @@ -105,7 +105,9 @@ public class ThemeServiceImpl implements ThemeService { @Override public Optional getThemeBy(String themeId) { - Assert.hasText(themeId, "Theme id must not be blank"); + if (StringUtils.isBlank(themeId)) { + return Optional.empty(); + } // Get all themes Set themes = getThemes(); @@ -181,7 +183,9 @@ public class ThemeServiceImpl implements ThemeService { @Override public boolean templateExists(String template) { - Assert.hasText(template, "Template must not be blank"); + if (StringUtils.isBlank(template)) { + return false; + } // Resolve template path Path templatePath = Paths.get(getActivatedTheme().getThemePath(), template); @@ -507,7 +511,7 @@ public class ThemeServiceImpl implements ThemeService { */ private void setActivatedTheme(@Nullable ThemeProperty activatedTheme) { this.activatedTheme = activatedTheme; - this.activatedThemeId = activatedTheme.getId(); + this.activatedThemeId = Optional.ofNullable(activatedTheme).map(ThemeProperty::getId).orElse(null); } /**