Skip to content

Commit

Permalink
Support post content version control (halo-dev#1617)
Browse files Browse the repository at this point in the history
* feat: split post content to new table and support content version control

* feat: Improve post version management

* feat: Add post content and version record deletion

* feat: Add isInProcess attribute for post list and detail api

* feat: Add migrate sql script

* fix: Add a sql of allow origin_content to null in posts table

* feat: Assign a value to the source of the post content version record
  • Loading branch information
guqing authored Feb 20, 2022
1 parent 1ee7b58 commit 923eb17
Show file tree
Hide file tree
Showing 31 changed files with 1,697 additions and 156 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ ext {
huaweiObsVersion = '3.21.8.1'
templateInheritanceVersion = "0.4.RELEASE"
jsoupVersion = '1.14.3'
diffUtilsVersion = '4.11'
}

dependencies {
Expand Down Expand Up @@ -121,6 +122,7 @@ dependencies {
implementation "net.sf.image4j:image4j:$image4jVersion"
implementation "org.flywaydb:flyway-core:$flywayVersion"
implementation "com.google.zxing:core:$zxingVersion"
implementation "io.github.java-diff-utils:java-diff-utils:$diffUtilsVersion"

implementation "org.iq80.leveldb:leveldb:$levelDbVersion"
runtimeOnly "com.h2database:h2:$h2Version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import run.halo.app.model.dto.post.BasePostMinimalDTO;
import run.halo.app.model.dto.post.BasePostSimpleDTO;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostPermalinkType;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.params.PostContentParam;
import run.halo.app.model.params.PostParam;
Expand Down Expand Up @@ -103,7 +102,7 @@ public Page<? extends BasePostSimpleDTO> pageByStatus(
@GetMapping("{postId:\\d+}")
@ApiOperation("Gets a post")
public PostDetailVO getBy(@PathVariable("postId") Integer postId) {
Post post = postService.getById(postId);
Post post = postService.getWithLatestContentById(postId);
return postService.convertToDetailVo(post, true);
}

Expand Down Expand Up @@ -131,7 +130,7 @@ public PostDetailVO updateBy(@Valid @RequestBody PostParam postParam,
@RequestParam(value = "autoSave", required = false, defaultValue = "false") Boolean autoSave
) {
// Get the post info
Post postToUpdate = postService.getById(postId);
Post postToUpdate = postService.getWithLatestContentById(postId);

postParam.update(postToUpdate);
return postService.updateBy(postToUpdate, postParam.getTagIds(), postParam.getCategoryIds(),
Expand Down Expand Up @@ -161,9 +160,9 @@ public BasePostDetailDTO updateDraftBy(
@PathVariable("postId") Integer postId,
@RequestBody PostContentParam contentParam) {
// Update draft content
Post post = postService.updateDraftContent(contentParam.getContent(), postId);

return new BasePostDetailDTO().convertFrom(post);
Post post = postService.updateDraftContent(contentParam.getContent(),
contentParam.getContent(), postId);
return postService.convertToDetail(post);
}

@DeleteMapping("{postId:\\d+}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public SheetController(SheetService sheetService,
@GetMapping("{sheetId:\\d+}")
@ApiOperation("Gets a sheet")
public SheetDetailVO getBy(@PathVariable("sheetId") Integer sheetId) {
Sheet sheet = sheetService.getById(sheetId);
Sheet sheet = sheetService.getWithLatestContentById(sheetId);
return sheetService.convertToDetailVo(sheet);
}

Expand Down Expand Up @@ -98,7 +98,7 @@ public SheetDetailVO updateBy(
@RequestBody @Valid SheetParam sheetParam,
@RequestParam(value = "autoSave", required = false, defaultValue = "false")
Boolean autoSave) {
Sheet sheetToUpdate = sheetService.getById(sheetId);
Sheet sheetToUpdate = sheetService.getWithLatestContentById(sheetId);

sheetParam.update(sheetToUpdate);

Expand Down Expand Up @@ -127,9 +127,9 @@ public BasePostDetailDTO updateDraftBy(
@PathVariable("sheetId") Integer sheetId,
@RequestBody PostContentParam contentParam) {
// Update draft content
Sheet sheet = sheetService.updateDraftContent(contentParam.getContent(), sheetId);

return new BasePostDetailDTO().convertFrom(sheet);
Sheet sheet = sheetService.updateDraftContent(contentParam.getContent(),
contentParam.getContent(), sheetId);
return sheetService.convertToDetail(sheet);
}

@DeleteMapping("{sheetId:\\d+}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import run.halo.app.model.dto.CategoryDTO;
import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Content;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.PostDetailVO;
Expand All @@ -40,6 +41,7 @@

/**
* @author ryanwang
* @author guqing
* @date 2019-03-21
*/
@Slf4j
Expand Down Expand Up @@ -242,14 +244,35 @@ private List<PostDetailVO> buildPosts(@NonNull Pageable pageable) {
Assert.notNull(pageable, "Pageable must not be null");

Page<Post> postPage = postService.pageBy(PostStatus.PUBLISHED, pageable);
Page<PostDetailVO> posts = convertToDetailPageVo(postPage);
return posts.getContent();
}

/**
* Converts to a page of detail vo.
* Notes: this method will escape the XML tag characters in the post content and summary.
*
* @param postPage post page must not be null
* @return a page of post detail vo that content and summary escaped.
*/
@NonNull
private Page<PostDetailVO> convertToDetailPageVo(Page<Post> postPage) {
Assert.notNull(postPage, "The postPage must not be null.");

// Populate post content
postPage.getContent().forEach(post -> {
Content postContent = postService.getContentById(post.getId());
post.setContent(Content.PatchedContent.of(postContent));
});

Page<PostDetailVO> posts = postService.convertToDetailVo(postPage);
posts.getContent().forEach(postDetailVO -> {
postDetailVO.setFormatContent(
RegExUtils.replaceAll(postDetailVO.getFormatContent(), XML_INVALID_CHAR, ""));
postDetailVO.setContent(
RegExUtils.replaceAll(postDetailVO.getContent(), XML_INVALID_CHAR, ""));
postDetailVO
.setSummary(RegExUtils.replaceAll(postDetailVO.getSummary(), XML_INVALID_CHAR, ""));
});
return posts.getContent();
return posts;
}

/**
Expand All @@ -266,13 +289,7 @@ private List<PostDetailVO> buildCategoryPosts(@NonNull Pageable pageable,

Page<Post> postPage =
postCategoryService.pagePostBy(category.getId(), PostStatus.PUBLISHED, pageable);
Page<PostDetailVO> posts = postService.convertToDetailVo(postPage);
posts.getContent().forEach(postDetailVO -> {
postDetailVO.setFormatContent(
RegExUtils.replaceAll(postDetailVO.getFormatContent(), XML_INVALID_CHAR, ""));
postDetailVO
.setSummary(RegExUtils.replaceAll(postDetailVO.getSummary(), XML_INVALID_CHAR, ""));
});
Page<PostDetailVO> posts = convertToDetailPageVo(postPage);
return posts.getContent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public PostDetailVO getBy(@PathVariable("postId") Integer postId,

if (formatDisabled) {
// Clear the format content
postDetailVO.setFormatContent(null);
postDetailVO.setContent(null);
}

if (sourceDisabled) {
Expand All @@ -133,7 +133,7 @@ public PostDetailVO getBy(@RequestParam("slug") String slug,

if (formatDisabled) {
// Clear the format content
postDetailVO.setFormatContent(null);
postDetailVO.setContent(null);
}

if (sourceDisabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public SheetDetailVO getBy(@PathVariable("sheetId") Integer sheetId,

if (formatDisabled) {
// Clear the format content
sheetDetailVO.setFormatContent(null);
sheetDetailVO.setContent(null);
}

if (sourceDisabled) {
Expand All @@ -102,7 +102,7 @@ public SheetDetailVO getBy(@RequestParam("slug") String slug,

if (formatDisabled) {
// Clear the format content
sheetDetailVO.setFormatContent(null);
sheetDetailVO.setContent(null);
}

if (sourceDisabled) {
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/run/halo/app/controller/content/model/PostModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
import run.halo.app.exception.ForbiddenException;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Content;
import run.halo.app.model.entity.Content.PatchedContent;
import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.PostMeta;
import run.halo.app.model.entity.Tag;
import run.halo.app.model.enums.EncryptTypeEnum;
import run.halo.app.model.enums.PostEditorType;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.ArchiveYearVO;
import run.halo.app.model.vo.PostListVO;
Expand All @@ -33,12 +34,12 @@
import run.halo.app.service.PostTagService;
import run.halo.app.service.TagService;
import run.halo.app.service.ThemeService;
import run.halo.app.utils.MarkdownUtils;

/**
* Post Model
*
* @author ryanwang
* @author guqing
* @date 2020-01-07
*/
@Component
Expand Down Expand Up @@ -116,12 +117,13 @@ public String content(Post post, String token, Model model) {
return "common/template/" + POST_PASSWORD_TEMPLATE;
}

post = postService.getById(post.getId());

if (post.getEditorType().equals(PostEditorType.MARKDOWN)) {
post.setFormatContent(MarkdownUtils.renderHtml(post.getOriginalContent()));
if (StringUtils.isNotBlank(token)) {
post = postService.getWithLatestContentById(post.getId());
} else {
post.setFormatContent(post.getOriginalContent());
post = postService.getById(post.getId());
// Set post content
Content postContent = postService.getContentById(post.getId());
post.setContent(PatchedContent.of(postContent));
}

postService.publishVisitEvent(post.getId());
Expand All @@ -148,7 +150,7 @@ public String content(Post post, String token, Model model) {
model.addAttribute("meta_description", post.getMetaDescription());
} else {
model.addAttribute("meta_description",
postService.generateDescription(post.getFormatContent()));
postService.generateDescription(post.getContent().getContent()));
}

model.addAttribute("is_post", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.springframework.ui.Model;
import run.halo.app.cache.AbstractStringCacheStore;
import run.halo.app.exception.ForbiddenException;
import run.halo.app.model.entity.Content;
import run.halo.app.model.entity.Content.PatchedContent;
import run.halo.app.model.entity.Sheet;
import run.halo.app.model.entity.SheetMeta;
import run.halo.app.model.enums.PostEditorType;
Expand Down Expand Up @@ -61,6 +63,9 @@ public String content(Sheet sheet, String token, Model model) {

if (StringUtils.isEmpty(token)) {
sheet = sheetService.getBy(PostStatus.PUBLISHED, sheet.getSlug());
//Set sheet content
Content content = sheetService.getContentById(sheet.getId());
sheet.setContent(PatchedContent.of(content));
} else {
// verify token
String cachedToken = cacheStore.getAny(token, String.class)
Expand All @@ -69,11 +74,14 @@ public String content(Sheet sheet, String token, Model model) {
throw new ForbiddenException("您没有该页面的访问权限");
}
// render markdown to html when preview sheet
PatchedContent sheetContent = sheetService.getLatestContentById(sheet.getId());
if (sheet.getEditorType().equals(PostEditorType.MARKDOWN)) {
sheet.setFormatContent(MarkdownUtils.renderHtml(sheet.getOriginalContent()));
sheetContent.setContent(
MarkdownUtils.renderHtml(sheetContent.getOriginalContent()));
} else {
sheet.setFormatContent(sheet.getOriginalContent());
sheetContent.setContent(sheetContent.getOriginalContent());
}
sheet.setContent(sheetContent);
}

sheetService.publishVisitEvent(sheet.getId());
Expand All @@ -94,7 +102,7 @@ public String content(Sheet sheet, String token, Model model) {
model.addAttribute("meta_description", sheet.getMetaDescription());
} else {
model.addAttribute("meta_description",
sheetService.generateDescription(sheet.getFormatContent()));
sheetService.generateDescription(sheet.getContent().getContent()));
}

// sheet and post all can use
Expand Down
28 changes: 27 additions & 1 deletion src/main/java/run/halo/app/model/dto/post/BasePostDetailDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.lang.NonNull;
import run.halo.app.model.entity.BasePost;
import run.halo.app.model.entity.Content.PatchedContent;

/**
* Base post detail output dto.
*
* @author johnniang
* @author guqing
*/
@Data
@ToString
Expand All @@ -16,7 +20,29 @@ public class BasePostDetailDTO extends BasePostSimpleDTO {

private String originalContent;

private String formatContent;
private String content;

private Long commentCount;

@Override
@NonNull
@SuppressWarnings("unchecked")
public <T extends BasePostMinimalDTO> T convertFrom(@NonNull BasePost domain) {
BasePostDetailDTO postDetailDTO = super.convertFrom(domain);
PatchedContent content = domain.getContent();
postDetailDTO.setContent(content.getContent());
postDetailDTO.setOriginalContent(content.getOriginalContent());
return (T) postDetailDTO;
}

/**
* Compatible with the formatContent attribute existing in the old version
* it will be removed in v2.0
*
* @return formatted post content
*/
@Deprecated(since = "1.5.0", forRemoval = true)
public String getFormatContent() {
return this.content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class BasePostSimpleDTO extends BasePostMinimalDTO {

private Long wordCount;

private Boolean inProgress;

public boolean isTopped() {
return this.topPriority != null && this.topPriority > 0;
}
Expand Down
Loading

0 comments on commit 923eb17

Please sign in to comment.