Skip to content

Commit

Permalink
refactor: installation data. (halo-dev#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby authored Feb 27, 2020
1 parent 136b1fb commit 949a3da
Showing 1 changed file with 67 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@
import run.halo.app.exception.BadRequestException;
import run.halo.app.model.entity.BaseComment;
import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.PostComment;
import run.halo.app.model.entity.User;
import run.halo.app.model.enums.LogType;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.params.*;
import run.halo.app.model.properties.BlogProperties;
import run.halo.app.model.properties.OtherProperties;
import run.halo.app.model.properties.PrimaryProperties;
import run.halo.app.model.properties.PropertyEnum;
import run.halo.app.model.support.BaseResponse;
import run.halo.app.model.support.CreateCheck;
import run.halo.app.model.vo.PostDetailVO;
import run.halo.app.service.*;
import run.halo.app.utils.ValidationUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* Installation controller.
Expand All @@ -52,6 +51,8 @@ public class InstallController {

private final PostService postService;

private final SheetService sheetService;

private final PostCommentService postCommentService;

private final OptionService optionService;
Expand All @@ -63,13 +64,15 @@ public class InstallController {
public InstallController(UserService userService,
CategoryService categoryService,
PostService postService,
SheetService sheetService,
PostCommentService postCommentService,
OptionService optionService,
MenuService menuService,
ApplicationEventPublisher eventPublisher) {
this.userService = userService;
this.categoryService = categoryService;
this.postService = postService;
this.sheetService = sheetService;
this.postCommentService = postCommentService;
this.optionService = optionService;
this.menuService = menuService;
Expand Down Expand Up @@ -101,7 +104,10 @@ public BaseResponse<String> installBlog(@RequestBody InstallParam installParam)
Category category = createDefaultCategoryIfAbsent();

// Create default post
Post post = createDefaultPostIfAbsent(category);
PostDetailVO post = createDefaultPostIfAbsent(category);

// Create default sheet
createDefaultSheet();

// Create default postComment
createDefaultComment(post);
Expand Down Expand Up @@ -133,30 +139,42 @@ private void createDefaultMenu() {

MenuParam menuArchive = new MenuParam();

menuArchive.setName("归档");
menuArchive.setName("文章归档");
menuArchive.setUrl("/archives");
menuArchive.setPriority(2);
menuService.create(menuArchive.convertTo());

MenuParam menuCategory = new MenuParam();
menuCategory.setName("默认分类");
menuCategory.setUrl("/categories/default");
menuCategory.setPriority(3);
menuService.create(menuCategory.convertTo());

MenuParam menuSheet = new MenuParam();
menuSheet.setName("关于页面");
menuSheet.setUrl("/s/about");
menuSheet.setPriority(4);
menuService.create(menuSheet.convertTo());
}


@Nullable
private BaseComment createDefaultComment(@Nullable Post post) {
private BaseComment createDefaultComment(@Nullable PostDetailVO post) {
if (post == null) {
return null;
}

PostCommentParam commentParam = new PostCommentParam();
commentParam.setAuthor("Halo Bot");
commentParam.setAuthorUrl("https://halo.run");
commentParam.setContent("欢迎使用 Halo,这是你的第一条评论。");
commentParam.setEmail("halo@halo.run");
commentParam.setPostId(post.getId());
return postCommentService.create(commentParam.convertTo());
PostComment comment = new PostComment();
comment.setAuthor("Halo");
comment.setAuthorUrl("https://halo.run");
comment.setContent("欢迎使用 Halo,这是你的第一条评论,头像来自 [Gravatar](https://cn.gravatar.com),你也可以通过注册 [Gravatar](https://cn.gravatar.com) 来显示自己的头像。");
comment.setEmail("hi@halo.run");
comment.setPostId(post.getId());
return postCommentService.create(comment);
}

@Nullable
private Post createDefaultPostIfAbsent(@Nullable Category category) {
private PostDetailVO createDefaultPostIfAbsent(@Nullable Category category) {
long publishedCount = postService.countByStatus(PostStatus.PUBLISHED);
if (publishedCount > 0) {
return null;
Expand All @@ -166,16 +184,42 @@ private Post createDefaultPostIfAbsent(@Nullable Category category) {
postParam.setSlug("hello-halo");
postParam.setTitle("Hello Halo");
postParam.setStatus(PostStatus.PUBLISHED);
postParam.setOriginalContent("## Hello Halo!\n" +
postParam.setOriginalContent("## Hello Halo\n" +
"\n" +
"如果你看到了这一篇文章,那么证明你已经安装成功了,感谢使用 [Halo](https://halo.run) 进行创作,希望能够使用愉快。\n" +
"\n" +
"## 相关链接\n" +
"\n" +
"- 官网:[https://halo.run](https://halo.run)\n" +
"- 社区:[https://bbs.halo.run](https://bbs.halo.run)\n" +
"- 主题仓库:[https://halo.run/s/themes](https://halo.run/s/themes)\n" +
"- 开源地址:[https://github.com/halo-dev/halo](https://github.com/halo-dev/halo)\n" +
"\n" +
"感谢使用 [Halo](https://github.com/halo-dev/halo) 进行创作,请删除该文章开始吧!");
"在使用过程中,有任何问题都可以通过以上链接找寻答案,或者联系我们。\n" +
"\n" +
"> 这是一篇自动生成的文章,请删除这篇文章之后开始你的创作吧!\n" +
"\n");

Set<Integer> categoryIds = new HashSet<>();
if (category != null) {
Set<Integer> categoryIds = new HashSet<>();
categoryIds.add(category.getId());
postParam.setCategoryIds(categoryIds);
}
return postService.createOrUpdateBy(postParam.convertTo());
return postService.createBy(postParam.convertTo(), Collections.emptySet(), categoryIds, false);
}

@Nullable
private void createDefaultSheet() {
SheetParam sheetParam = new SheetParam();
sheetParam.setSlug("about");
sheetParam.setTitle("关于页面");
sheetParam.setStatus(PostStatus.PUBLISHED);
sheetParam.setOriginalContent("## 关于页面\n" +
"\n" +
"这是一个自定义页面,你可以在后台的 `页面` -> `所有页面` -> `自定义页面` 找到它,你可以用于新建关于页面、留言板页面等等。发挥你自己的想象力!\n" +
"\n" +
"> 这是一篇自动生成的页面,你可以在后台删除它。");
sheetService.createBy(sheetParam.convertTo(), false);
}

@Nullable
Expand All @@ -186,9 +230,9 @@ private Category createDefaultCategoryIfAbsent() {
}

CategoryParam category = new CategoryParam();
category.setName("未分类");
category.setName("默认分类");
category.setSlug("default");
category.setDescription("未分类");
category.setDescription("这是你的默认分类,如不需要,删除即可。");
ValidationUtils.validate(category);
return categoryService.create(category.convertTo());
}
Expand Down Expand Up @@ -220,6 +264,7 @@ private void initSettings(InstallParam installParam) {
properties.put(BlogProperties.BLOG_URL, StringUtils.isBlank(installParam.getUrl()) ?
optionService.getBlogBaseUrl() : installParam.getUrl());
properties.put(PrimaryProperties.BIRTHDAY, String.valueOf(System.currentTimeMillis()));
properties.put(OtherProperties.GLOBAL_ABSOLUTE_PATH_ENABLED, Boolean.FALSE.toString());

// Create properties
optionService.saveProperties(properties);
Expand Down

0 comments on commit 949a3da

Please sign in to comment.