Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split post content to new table and support content version control #1617

Merged
merged 30 commits into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e266a2e
feat: split post content to new table and support content version con…
guqing Jan 6, 2022
352ec3a
feat: Improve post version management
guqing Feb 15, 2022
959e6c1
fix: code styles
guqing Feb 15, 2022
124df87
feat: Add post content and version record deletion
guqing Feb 16, 2022
369ebaf
feat: Add isInProcess attribute for post list and detail api
guqing Feb 16, 2022
cdfb8d6
feat: Add migrate sql script
guqing Feb 16, 2022
678e15a
fix: Add a sql of allow origin_content to null in posts table
guqing Feb 16, 2022
d15239c
refactor: rename migrate script of v6
guqing Feb 16, 2022
ae4dbe5
refactor: Undelete the content field in the Post
guqing Feb 16, 2022
98f16f2
refactor: foreground post content query
guqing Feb 16, 2022
d1b5b99
feat: Assign a value to the source of the post content version record
guqing Feb 16, 2022
f55a5d4
refactor: The return type of content createOrUpdate method
guqing Feb 17, 2022
a338a17
fix: Loss of line breaks after saving
guqing Feb 17, 2022
6294e28
refactor: Set isInProgress on save content api
guqing Feb 17, 2022
3af3978
refactor: Rename method name
guqing Feb 17, 2022
b3a9da6
refactor: Rename isInProcess attribute to inProgress
guqing Feb 18, 2022
1895698
refactor: Rename migrate sql of v6
guqing Feb 18, 2022
3ecdb8e
docs: Add java doc to CotnentPatchLogRepository
guqing Feb 18, 2022
bf4debc
docs: Add java doc
guqing Feb 18, 2022
7eb42c8
fix: rss without displaying content
guqing Feb 18, 2022
ae9498b
feat: Allow the original_content to be null in posts table
guqing Feb 18, 2022
9c5a1bf
feat: Allow the format_content to be null in posts table
guqing Feb 18, 2022
8b36236
feat: Add table index for content_patch_logs
guqing Feb 19, 2022
351d5c8
refactor: Simplified table structure design and adapter code
guqing Feb 19, 2022
3dcbe08
refactor: Specification entitys annotation
guqing Feb 19, 2022
320ba15
refator: Add transactional annotation for creation methods
guqing Feb 19, 2022
40ee930
feat: Add unit test case for content patch log
guqing Feb 19, 2022
af671a8
fix: code style
guqing Feb 19, 2022
4de8e40
refactor: Manually create data instead of script creation for unit te…
guqing Feb 20, 2022
92c751c
refactor: generate summary if not present when query post list
guqing Feb 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.getByIdWithLatestContent(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.getByIdWithLatestContent(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.getByIdWithLatestContent(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.getByIdWithLatestContent(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 @@ -244,8 +244,8 @@ private List<PostDetailVO> buildPosts(@NonNull Pageable pageable) {
Page<Post> postPage = postService.pageBy(PostStatus.PUBLISHED, pageable);
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, ""));
});
Expand All @@ -268,8 +268,8 @@ private List<PostDetailVO> buildCategoryPosts(@NonNull Pageable pageable,
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.setContent(
RegExUtils.replaceAll(postDetailVO.getContent(), XML_INVALID_CHAR, ""));
postDetailVO
.setSummary(RegExUtils.replaceAll(postDetailVO.getSummary(), XML_INVALID_CHAR, ""));
});
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 @@ -15,12 +15,13 @@
import run.halo.app.cache.AbstractStringCacheStore;
import run.halo.app.exception.ForbiddenException;
import run.halo.app.exception.NotFoundException;
import run.halo.app.model.entity.BaseContent;
import run.halo.app.model.entity.BaseContent.PatchedContent;
import run.halo.app.model.entity.Category;
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.getByIdWithLatestContent(post.getId());
} else {
post.setFormatContent(post.getOriginalContent());
post = postService.getById(post.getId());
// Set post content
BaseContent 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.BaseContent;
import run.halo.app.model.entity.BaseContent.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
BaseContent 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.BaseContent.PatchedContent;
import run.halo.app.model.entity.BasePost;

/**
* 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 isInProcess;
guqing marked this conversation as resolved.
Show resolved Hide resolved

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