* feat: #1036

* revert: BaseCommentRepository.
pull/1332/head
Ryan Wang 2021-03-28 20:22:54 +08:00 committed by GitHub
parent f0ba0fa606
commit f9a629c38f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 150 additions and 446 deletions

View File

@ -1,50 +0,0 @@
//package run.halo.app.controller.admin.api;
//
//import io.swagger.annotations.ApiOperation;
//import org.springframework.web.bind.annotation.*;
//import run.halo.app.service.DataProcessService;
//import run.halo.app.service.ThemeSettingService;
//
///**
// * @author ryanwang
// * @date 2019-12-29
// */
//@RestController
//@RequestMapping("/api/admin/data/process")
//public class DataProcessController {
//
// private final DataProcessService dataProcessService;
//
// private final ThemeSettingService themeSettingService;
//
// public DataProcessController(DataProcessService dataProcessService,
// ThemeSettingService themeSettingService) {
// this.dataProcessService = dataProcessService;
// this.themeSettingService = themeSettingService;
// }
//
// @PutMapping("url/replace")
// @ApiOperation("Replace url in all table.")
// public void replaceUrl(@RequestParam("oldUrl") String oldUrl,
// @RequestParam("newUrl") String newUrl) {
// dataProcessService.replaceAllUrl(oldUrl, newUrl);
// }
//
// @DeleteMapping("themes/settings/inactivated")
// @ApiOperation("Delete inactivated theme settings.")
// public void deleteInactivatedThemeSettings() {
// themeSettingService.deleteInactivated();
// }
//
// @DeleteMapping("tags/unused")
// @ApiOperation("Delete unused tags")
// public void deleteUnusedTags() {
// // TODO
// }
//
// @DeleteMapping("categories/unused")
// @ApiOperation("Delete unused categories")
// public void deleteUnusedCategories() {
// // TODO
// }
//}

View File

@ -46,4 +46,5 @@ public class BaseCommentDTO implements OutputConverter<BaseCommentDTO, BaseComme
private Date createTime;
private String avatar;
}

View File

@ -50,6 +50,11 @@ public enum CommentProperties implements PropertyEnum {
INTERNAL_PLUGIN_JS("comment_internal_plugin_js", String.class,
"//cdn.jsdelivr.net/npm/halo-comment@latest/dist/halo-comment.min.js"),
/**
* Gravatar mirror source base url.
*/
GRAVATAR_SOURCE("gravatar_source", String.class, "//gravatar.com/avatar/"),
COMMENT_BAN_TIME("comment_ban_time", Integer.class, "10"),
COMMENT_RANGE("comment_range", Integer.class, "30");

View File

@ -82,13 +82,4 @@ public interface AttachmentService extends CrudService<Attachment, Integer> {
* @return list of type.
*/
List<AttachmentType> listAllType();
/**
* Replace attachment url in batch.
*
* @param oldUrl old blog url.
* @param newUrl new blog url.
* @return replaced attachments.
*/
List<Attachment> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
}

View File

@ -1,20 +0,0 @@
package run.halo.app.service;
import org.springframework.lang.NonNull;
/**
* Data process service interface.
*
* @author ryanwang
* @date 2019-12-29
*/
public interface DataProcessService {
/**
* Replace all url.
*
* @param oldUrl old url must not be null.
* @param newUrl new url must not be null.
*/
void replaceAllUrl(@NonNull String oldUrl, @NonNull String newUrl);
}

View File

@ -468,15 +468,6 @@ public interface OptionService extends CrudService<Option, Integer> {
*/
Boolean isEnabledAbsolutePath();
/**
* Replace option url in batch.
*
* @param oldUrl old blog url.
* @param newUrl new blog url.
* @return replaced options.
*/
List<OptionDTO> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
/**
* Converts to option output dto.
*

View File

@ -79,13 +79,4 @@ public interface PhotoService extends CrudService<Photo, Integer> {
* @return list of teams
*/
List<String> listAllTeams();
/**
* Replace photo url in batch.
*
* @param oldUrl old blog url.
* @param newUrl new blog url.
* @return replaced photos.
*/
List<PhotoDTO> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
}

View File

@ -56,15 +56,6 @@ public interface ThemeSettingService extends CrudService<ThemeSetting, Integer>
@NonNull
Map<String, Object> listAsMapBy(@NonNull String themeId);
/**
* Replace theme setting url in batch.
*
* @param oldUrl old blog url.
* @param newUrl new blog url.
* @return replaced theme settings.
*/
List<ThemeSetting> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
/**
* Delete unused theme setting.
*/

View File

@ -296,38 +296,4 @@ public interface BaseCommentService<COMMENT extends BaseComment>
@NonNull
List<COMMENT> listChildrenBy(@NonNull Integer targetId, @NonNull Long commentParentId,
@NonNull Sort sort);
/**
* Filters comment ip address.
*
* @param comment comment dto must not be null
*/
@Deprecated
<T extends BaseCommentDTO> T filterIpAddress(@NonNull T comment);
/**
* Filters comment ip address.
*
* @param comments comment dto list
*/
@Deprecated
<T extends BaseCommentDTO> List<T> filterIpAddress(@Nullable List<T> comments);
/**
* Filters comment ip address.
*
* @param commentPage comment page
*/
@Deprecated
<T extends BaseCommentDTO> Page<T> filterIpAddress(@NonNull Page<T> commentPage);
/**
* Replace comment url in batch.
*
* @param oldUrl old blog url.
* @param newUrl new blog url.
* @return replaced comments.
*/
List<BaseCommentDTO> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
}

View File

@ -297,16 +297,6 @@ public interface BasePostService<POST extends BasePost> extends CrudService<POST
@NonNull
List<POST> updateStatusByIds(@NonNull List<Integer> ids, @NonNull PostStatus status);
/**
* Replace post blog url in batch.
*
* @param oldUrl old blog url.
* @param newUrl new blog url.
* @return replaced posts.
*/
@NonNull
List<BasePostDetailDTO> replaceUrl(@NonNull String oldUrl, @NonNull String newUrl);
/**
* Generate description.
*

View File

@ -1,6 +1,5 @@
package run.halo.app.service.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
@ -77,7 +76,7 @@ public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Integ
private Specification<Attachment> buildSpecByQuery(@NonNull AttachmentQuery attachmentQuery) {
Assert.notNull(attachmentQuery, "Attachment query must not be null");
return (Specification<Attachment>) (root, query, criteriaBuilder) -> {
return (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new LinkedList<>();
if (attachmentQuery.getMediaType() != null) {
@ -198,22 +197,6 @@ public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Integ
return attachmentRepository.findAllType();
}
@Override
public List<Attachment> replaceUrl(String oldUrl, String newUrl) {
List<Attachment> attachments = listAll();
List<Attachment> replaced = new ArrayList<>();
attachments.forEach(attachment -> {
if (StringUtils.isNotEmpty(attachment.getPath())) {
attachment.setPath(attachment.getPath().replaceAll(oldUrl, newUrl));
}
if (StringUtils.isNotEmpty(attachment.getThumbPath())) {
attachment.setThumbPath(attachment.getThumbPath().replaceAll(oldUrl, newUrl));
}
replaced.add(attachment);
});
return updateInBatch(replaced);
}
@Override
public Attachment create(Attachment attachment) {
Assert.notNull(attachment, "Attachment must not be null");

View File

@ -13,7 +13,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;
import javax.persistence.criteria.Predicate;
@ -86,18 +85,21 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public List<COMMENT> listBy(Integer postId) {
@NonNull
public List<COMMENT> listBy(@NonNull Integer postId) {
Assert.notNull(postId, "Post id must not be null");
return baseCommentRepository.findAllByPostId(postId);
}
@Override
public Page<COMMENT> pageLatest(int top) {
@NonNull
public Page<COMMENT> pageLatest(@NonNull int top) {
return pageLatest(top, null);
}
@Override
@NonNull
public Page<COMMENT> pageLatest(int top, CommentStatus status) {
if (status == null) {
return listAll(ServiceUtils.buildLatestPageable(top));
@ -107,7 +109,8 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public Page<COMMENT> pageBy(CommentStatus status, Pageable pageable) {
@NonNull
public Page<COMMENT> pageBy(@NonNull CommentStatus status, @NonNull Pageable pageable) {
Assert.notNull(status, "Comment status must not be null");
Assert.notNull(pageable, "Page info must not be null");
@ -117,14 +120,16 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public Page<COMMENT> pageBy(CommentQuery commentQuery, Pageable pageable) {
@NonNull
public Page<COMMENT> pageBy(@NonNull CommentQuery commentQuery, @NonNull Pageable pageable) {
Assert.notNull(pageable, "Page info must not be null");
return baseCommentRepository.findAll(buildSpecByQuery(commentQuery), pageable);
}
@Override
public Page<BaseCommentVO> pageVosAllBy(Integer postId, Pageable pageable) {
@NonNull
public Page<BaseCommentVO> pageVosAllBy(@NonNull Integer postId, @NonNull Pageable pageable) {
Assert.notNull(postId, "Post id must not be null");
Assert.notNull(pageable, "Page info must not be null");
@ -137,7 +142,9 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public Page<BaseCommentVO> pageVosBy(List<COMMENT> comments, Pageable pageable) {
@NonNull
public Page<BaseCommentVO> pageVosBy(@NonNull List<COMMENT> comments,
@NonNull Pageable pageable) {
Assert.notNull(comments, "Comments must not be null");
Assert.notNull(pageable, "Page info must not be null");
@ -170,7 +177,8 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public Page<BaseCommentVO> pageVosBy(Integer postId, Pageable pageable) {
@NonNull
public Page<BaseCommentVO> pageVosBy(@NonNull Integer postId, @NonNull Pageable pageable) {
Assert.notNull(postId, "Post id must not be null");
Assert.notNull(pageable, "Page info must not be null");
@ -184,7 +192,9 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public Page<BaseCommentWithParentVO> pageWithParentVoBy(Integer postId, Pageable pageable) {
@NonNull
public Page<BaseCommentWithParentVO> pageWithParentVoBy(@NonNull Integer postId,
@NonNull Pageable pageable) {
Assert.notNull(postId, "Post id must not be null");
Assert.notNull(pageable, "Page info must not be null");
@ -214,9 +224,11 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
// Convert to comment page
return commentPage.map(comment -> {
// Convert to with parent vo
BaseCommentWithParentVO commentWithParentVO =
BaseCommentWithParentVO commentWithParentVo =
new BaseCommentWithParentVO().convertFrom(comment);
commentWithParentVo.setAvatar(buildAvatarUrl(commentWithParentVo.getGravatarMd5()));
// Get parent comment vo from cache
BaseCommentWithParentVO parentCommentVo = parentCommentVoMap.get(comment.getParentId());
@ -227,19 +239,23 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
if (parentComment != null) {
// Convert to parent comment vo
parentCommentVo = new BaseCommentWithParentVO().convertFrom(parentComment);
parentCommentVo.setAvatar(buildAvatarUrl(parentComment.getGravatarMd5()));
// Cache the parent comment vo
parentCommentVoMap.put(parentComment.getId(), parentCommentVo);
}
}
// Set parent
commentWithParentVO.setParent(parentCommentVo == null ? null : parentCommentVo.clone());
commentWithParentVo.setParent(parentCommentVo == null ? null : parentCommentVo.clone());
return commentWithParentVO;
return commentWithParentVo;
});
}
@Override
@NonNull
public Map<Integer, Long> countByPostIds(Collection<Integer> postIds) {
if (CollectionUtils.isEmpty(postIds)) {
return Collections.emptyMap();
@ -269,18 +285,19 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public long countByPostId(Integer postId) {
public long countByPostId(@NonNull Integer postId) {
Assert.notNull(postId, "Post id must not be null");
return baseCommentRepository.countByPostId(postId);
}
@Override
public long countByStatus(CommentStatus status) {
public long countByStatus(@NonNull CommentStatus status) {
return baseCommentRepository.countByStatus(status);
}
@Override
public COMMENT create(COMMENT comment) {
@NonNull
public COMMENT create(@NonNull COMMENT comment) {
Assert.notNull(comment, "Domain must not be null");
// Check post id
@ -294,7 +311,8 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
// Check user login status and set this field
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
final Authentication authentication =
SecurityContextHolder.getContext().getAuthentication();
// Set some default values
if (comment.getIpAddress() == null) {
@ -342,7 +360,8 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public COMMENT createBy(BaseCommentParam<COMMENT> commentParam) {
@NonNull
public COMMENT createBy(@NonNull BaseCommentParam<COMMENT> commentParam) {
Assert.notNull(commentParam, "Comment param must not be null");
// Check user login status and set this field
@ -374,7 +393,8 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public COMMENT updateStatus(Long commentId, CommentStatus status) {
@NonNull
public COMMENT updateStatus(@NonNull Long commentId, @NonNull CommentStatus status) {
Assert.notNull(commentId, "Comment id must not be null");
Assert.notNull(status, "Comment status must not be null");
@ -389,7 +409,8 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public List<COMMENT> updateStatusByIds(List<Long> ids, CommentStatus status) {
@NonNull
public List<COMMENT> updateStatusByIds(@NonNull List<Long> ids, @NonNull CommentStatus status) {
if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList();
}
@ -399,13 +420,14 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public List<COMMENT> removeByPostId(Integer postId) {
public List<COMMENT> removeByPostId(@NonNull Integer postId) {
Assert.notNull(postId, "Post id must not be null");
return baseCommentRepository.deleteByPostId(postId);
}
@Override
public COMMENT removeById(Long id) {
@NonNull
public COMMENT removeById(@NonNull Long id) {
Assert.notNull(id, "Comment id must not be null");
COMMENT comment = baseCommentRepository.findById(id)
@ -424,7 +446,8 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public List<COMMENT> removeByIds(Collection<Long> ids) {
@NonNull
public List<COMMENT> removeByIds(@NonNull Collection<Long> ids) {
if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList();
}
@ -432,7 +455,8 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public List<BaseCommentDTO> convertTo(List<COMMENT> comments) {
@NonNull
public List<BaseCommentDTO> convertTo(@NonNull List<COMMENT> comments) {
if (CollectionUtils.isEmpty(comments)) {
return Collections.emptyList();
}
@ -442,17 +466,23 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public Page<BaseCommentDTO> convertTo(Page<COMMENT> commentPage) {
@NonNull
public Page<BaseCommentDTO> convertTo(@NonNull Page<COMMENT> commentPage) {
Assert.notNull(commentPage, "Comment page must not be null");
return commentPage.map(this::convertTo);
}
@Override
public BaseCommentDTO convertTo(COMMENT comment) {
@NonNull
public BaseCommentDTO convertTo(@NonNull COMMENT comment) {
Assert.notNull(comment, "Comment must not be null");
return new BaseCommentDTO().convertFrom(comment);
BaseCommentDTO baseCommentDto = new BaseCommentDTO().convertFrom(comment);
baseCommentDto.setAvatar(buildAvatarUrl(comment.getGravatarMd5()));
return baseCommentDto;
}
@NonNull
@ -527,8 +557,10 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public Page<CommentWithHasChildrenVO> pageTopCommentsBy(Integer targetId, CommentStatus status,
Pageable pageable) {
@NonNull
public Page<CommentWithHasChildrenVO> pageTopCommentsBy(@NonNull Integer targetId,
@NonNull CommentStatus status,
@NonNull Pageable pageable) {
Assert.notNull(targetId, "Target id must not be null");
Assert.notNull(status, "Comment status must not be null");
Assert.notNull(pageable, "Page info must not be null");
@ -561,13 +593,15 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
new CommentWithHasChildrenVO().convertFrom(topComment);
comment
.setHasChildren(commentChildrenCountMap.getOrDefault(topComment.getId(), 0L) > 0);
comment.setAvatar(buildAvatarUrl(topComment.getGravatarMd5()));
return comment;
});
}
@Override
public List<COMMENT> listChildrenBy(Integer targetId, Long commentParentId,
CommentStatus status, Sort sort) {
@NonNull
public List<COMMENT> listChildrenBy(@NonNull Integer targetId, @NonNull Long commentParentId,
@NonNull CommentStatus status, @NonNull Sort sort) {
Assert.notNull(targetId, "Target id must not be null");
Assert.notNull(commentParentId, "Comment parent id must not be null");
Assert.notNull(sort, "Sort info must not be null");
@ -592,7 +626,9 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
@Override
public List<COMMENT> listChildrenBy(Integer targetId, Long commentParentId, Sort sort) {
@NonNull
public List<COMMENT> listChildrenBy(@NonNull Integer targetId, @NonNull Long commentParentId,
@NonNull Sort sort) {
Assert.notNull(targetId, "Target id must not be null");
Assert.notNull(commentParentId, "Comment parent id must not be null");
Assert.notNull(sort, "Sort info must not be null");
@ -616,70 +652,6 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
return childrenList;
}
@Override
@Deprecated
public <T extends BaseCommentDTO> T filterIpAddress(@NonNull T comment) {
Assert.notNull(comment, "Base comment dto must not be null");
// Clear ip address
comment.setIpAddress("");
// Handle base comment vo
if (comment instanceof BaseCommentVO) {
BaseCommentVO baseCommentVO = (BaseCommentVO) comment;
Queue<BaseCommentVO> commentQueue = new LinkedList<>();
commentQueue.offer(baseCommentVO);
while (!commentQueue.isEmpty()) {
BaseCommentVO current = commentQueue.poll();
// Clear ip address
current.setIpAddress("");
if (!CollectionUtils.isEmpty(current.getChildren())) {
// Add children
commentQueue.addAll(current.getChildren());
}
}
}
return comment;
}
@Override
@Deprecated
public <T extends BaseCommentDTO> List<T> filterIpAddress(List<T> comments) {
if (CollectionUtils.isEmpty(comments)) {
return Collections.emptyList();
}
comments.forEach(this::filterIpAddress);
return comments;
}
@Override
@Deprecated
public <T extends BaseCommentDTO> Page<T> filterIpAddress(Page<T> commentPage) {
Assert.notNull(commentPage, "Comment page must not be null");
commentPage.forEach(this::filterIpAddress);
return commentPage;
}
@Override
public List<BaseCommentDTO> replaceUrl(String oldUrl, String newUrl) {
List<COMMENT> comments = listAll();
List<COMMENT> replaced = new ArrayList<>();
comments.forEach(comment -> {
if (StringUtils.isNotEmpty(comment.getAuthorUrl())) {
comment.setAuthorUrl(comment.getAuthorUrl().replaceAll(oldUrl, newUrl));
}
replaced.add(comment);
});
List<COMMENT> updated = updateInBatch(replaced);
return convertTo(updated);
}
/**
* Get children comments recursively.
*
@ -761,13 +733,15 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
// Add children
children.forEach(comment -> {
// Convert to comment vo
BaseCommentVO commentVO = new BaseCommentVO().convertFrom(comment);
BaseCommentVO commentVo = new BaseCommentVO().convertFrom(comment);
commentVo.setAvatar(buildAvatarUrl(commentVo.getGravatarMd5()));
if (parentComment.getChildren() == null) {
parentComment.setChildren(new LinkedList<>());
}
parentComment.getChildren().add(commentVO);
parentComment.getChildren().add(commentVo);
});
// Remove children
@ -784,4 +758,18 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
}
}
/**
* Build avatar url by gravatarMd5
*
* @param gravatarMd5 gravatarMd5
* @return avatar url
*/
public String buildAvatarUrl(String gravatarMd5) {
final String gravatarSource =
optionService.getByPropertyOrDefault(CommentProperties.GRAVATAR_SOURCE, String.class);
final String gravatarDefault =
optionService.getByPropertyOrDefault(CommentProperties.GRAVATAR_DEFAULT, String.class);
return gravatarSource + gravatarMd5 + "?d=" + gravatarDefault;
}
}

View File

@ -456,26 +456,6 @@ public abstract class BasePostServiceImpl<POST extends BasePost>
}).collect(Collectors.toList());
}
@Override
public List<BasePostDetailDTO> replaceUrl(String oldUrl, String newUrl) {
List<POST> posts = listAll();
List<POST> replaced = new ArrayList<>();
posts.forEach(post -> {
if (StringUtils.isNotEmpty(post.getThumbnail())) {
post.setThumbnail(post.getThumbnail().replaceAll(oldUrl, newUrl));
}
if (StringUtils.isNotEmpty(post.getOriginalContent())) {
post.setOriginalContent(post.getOriginalContent().replaceAll(oldUrl, newUrl));
}
if (StringUtils.isNotEmpty(post.getFormatContent())) {
post.setFormatContent(post.getFormatContent().replaceAll(oldUrl, newUrl));
}
replaced.add(post);
});
List<POST> updated = updateInBatch(replaced);
return updated.stream().map(this::convertToDetail).collect(Collectors.toList());
}
@Override
public String generateDescription(String content) {
Assert.notNull(content, "html content must not be null");

View File

@ -1,74 +0,0 @@
package run.halo.app.service.impl;
import org.springframework.stereotype.Service;
import run.halo.app.service.AttachmentService;
import run.halo.app.service.DataProcessService;
import run.halo.app.service.JournalCommentService;
import run.halo.app.service.OptionService;
import run.halo.app.service.PhotoService;
import run.halo.app.service.PostCommentService;
import run.halo.app.service.PostService;
import run.halo.app.service.SheetCommentService;
import run.halo.app.service.SheetService;
import run.halo.app.service.ThemeSettingService;
/**
* DataProcessService implementation.
*
* @author ryanwang
* @date 2019-12-29
*/
@Service
public class DataProcessServiceImpl implements DataProcessService {
private final PostService postService;
private final SheetService sheetService;
private final PostCommentService postCommentService;
private final SheetCommentService sheetCommentService;
private final JournalCommentService journalCommentService;
private final AttachmentService attachmentService;
private final OptionService optionService;
private final PhotoService photoService;
private final ThemeSettingService themeSettingService;
public DataProcessServiceImpl(PostService postService,
SheetService sheetService,
PostCommentService postCommentService,
SheetCommentService sheetCommentService,
JournalCommentService journalCommentService,
AttachmentService attachmentService,
OptionService optionService,
PhotoService photoService,
ThemeSettingService themeSettingService) {
this.postService = postService;
this.sheetService = sheetService;
this.postCommentService = postCommentService;
this.sheetCommentService = sheetCommentService;
this.journalCommentService = journalCommentService;
this.attachmentService = attachmentService;
this.optionService = optionService;
this.photoService = photoService;
this.themeSettingService = themeSettingService;
}
@Override
public void replaceAllUrl(String oldUrl, String newUrl) {
postService.replaceUrl(oldUrl, newUrl);
sheetService.replaceUrl(oldUrl, newUrl);
postCommentService.replaceUrl(oldUrl, newUrl);
sheetCommentService.replaceUrl(oldUrl, newUrl);
journalCommentService.replaceUrl(oldUrl, newUrl);
attachmentService.replaceUrl(oldUrl, newUrl);
optionService.replaceUrl(oldUrl, newUrl);
photoService.replaceUrl(oldUrl, newUrl);
themeSettingService.replaceUrl(oldUrl, newUrl);
}
}

View File

@ -9,6 +9,7 @@ import java.util.stream.Collectors;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import run.halo.app.exception.NotFoundException;
@ -33,8 +34,6 @@ import run.halo.app.utils.ServiceUtils;
public class JournalCommentServiceImpl extends BaseCommentServiceImpl<JournalComment>
implements JournalCommentService {
private final JournalCommentRepository journalCommentRepository;
private final JournalRepository journalRepository;
public JournalCommentServiceImpl(JournalCommentRepository journalCommentRepository,
@ -42,18 +41,18 @@ public class JournalCommentServiceImpl extends BaseCommentServiceImpl<JournalCom
UserService userService,
ApplicationEventPublisher eventPublisher, JournalRepository journalRepository) {
super(journalCommentRepository, optionService, userService, eventPublisher);
this.journalCommentRepository = journalCommentRepository;
this.journalRepository = journalRepository;
}
@Override
public void validateTarget(Integer journalId) {
public void validateTarget(@NonNull Integer journalId) {
if (!journalRepository.existsById(journalId)) {
throw new NotFoundException("查询不到该日志信息").setErrorData(journalId);
}
}
@Override
@NonNull
public List<JournalCommentWithJournalVO> convertToWithJournalVo(
List<JournalComment> journalComments) {
@ -76,22 +75,24 @@ public class JournalCommentServiceImpl extends BaseCommentServiceImpl<JournalCom
new JournalCommentWithJournalVO().convertFrom(journalComment);
journalCmtWithJournalVo.setJournal(
new JournalDTO().convertFrom(journalMap.get(journalComment.getPostId())));
journalCmtWithJournalVo.setAvatar(buildAvatarUrl(journalComment.getGravatarMd5()));
return journalCmtWithJournalVo;
})
.collect(Collectors.toList());
}
@Override
@NonNull
public Page<JournalCommentWithJournalVO> convertToWithJournalVo(
Page<JournalComment> journalCommentPage) {
@NonNull Page<JournalComment> journalCommentPage) {
Assert.notNull(journalCommentPage, "Journal comment page must not be null");
// Convert the list
List<JournalCommentWithJournalVO> journalCmtWithJournalVOS =
List<JournalCommentWithJournalVO> journalCmtWithJournalVos =
convertToWithJournalVo(journalCommentPage.getContent());
// Build and return
return new PageImpl<>(journalCmtWithJournalVOS, journalCommentPage.getPageable(),
return new PageImpl<>(journalCmtWithJournalVos, journalCommentPage.getPageable(),
journalCommentPage.getTotalElements());
}
}

View File

@ -2,7 +2,6 @@ package run.halo.app.service.impl;
import com.qiniu.common.Zone;
import com.qiniu.storage.Region;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
@ -12,7 +11,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.persistence.criteria.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@ -275,7 +273,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer>
private Specification<Option> buildSpecByQuery(@NonNull OptionQuery optionQuery) {
Assert.notNull(optionQuery, "Option query must not be null");
return (Specification<Option>) (root, query, criteriaBuilder) -> {
return (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new LinkedList<>();
if (optionQuery.getType() != null) {
@ -616,21 +614,6 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer>
true);
}
@Override
public List<OptionDTO> replaceUrl(String oldUrl, String newUrl) {
List<Option> options = listAll();
List<Option> replaced = new ArrayList<>();
options.forEach(option -> {
if (StringUtils.isNotEmpty(option.getValue())) {
option.setValue(option.getValue().replaceAll(oldUrl, newUrl));
}
replaced.add(option);
});
List<Option> updated = updateInBatch(replaced);
eventPublisher.publishEvent(new OptionUpdatedEvent(this));
return updated.stream().map(this::convertToDto).collect(Collectors.toList());
}
@Override
public OptionSimpleDTO convertToDto(Option option) {
Assert.notNull(option, "Option must not be null");

View File

@ -1,6 +1,5 @@
package run.halo.app.service.impl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -117,29 +116,11 @@ public class PhotoServiceImpl extends AbstractCrudService<Photo, Integer> implem
return photoRepository.findAllTeams();
}
@Override
public List<PhotoDTO> replaceUrl(String oldUrl, String newUrl) {
List<Photo> photos = listAll();
List<Photo> replaced = new ArrayList<>();
photos.forEach(photo -> {
if (StringUtils.isNotEmpty(photo.getThumbnail())) {
photo.setThumbnail(photo.getThumbnail().replace(oldUrl, newUrl));
}
if (StringUtils.isNotEmpty(photo.getUrl())) {
photo.setUrl(photo.getUrl().replaceAll(oldUrl, newUrl));
}
replaced.add(photo);
});
List<Photo> updated = updateInBatch(replaced);
return updated.stream().map(photo -> (PhotoDTO) new PhotoDTO().convertFrom(photo))
.collect(Collectors.toList());
}
@NonNull
private Specification<Photo> buildSpecByQuery(@NonNull PhotoQuery photoQuery) {
Assert.notNull(photoQuery, "Photo query must not be null");
return (Specification<Photo>) (root, query, criteriaBuilder) -> {
return (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new LinkedList<>();
if (photoQuery.getTeam() != null) {

View File

@ -12,6 +12,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
@ -62,7 +63,8 @@ public class PostCommentServiceImpl extends BaseCommentServiceImpl<PostComment>
}
@Override
public Page<PostCommentWithPostVO> convertToWithPostVo(Page<PostComment> commentPage) {
@NonNull
public Page<PostCommentWithPostVO> convertToWithPostVo(@NonNull Page<PostComment> commentPage) {
Assert.notNull(commentPage, "PostComment page must not be null");
return new PageImpl<>(convertToWithPostVo(commentPage.getContent()),
@ -71,19 +73,24 @@ public class PostCommentServiceImpl extends BaseCommentServiceImpl<PostComment>
}
@Override
public PostCommentWithPostVO convertToWithPostVo(PostComment comment) {
@NonNull
public PostCommentWithPostVO convertToWithPostVo(@NonNull PostComment comment) {
Assert.notNull(comment, "PostComment must not be null");
PostCommentWithPostVO postCommentWithPostVO =
PostCommentWithPostVO postCommentWithPostVo =
new PostCommentWithPostVO().convertFrom(comment);
BasePostMinimalDTO basePostMinimalDTO =
new BasePostMinimalDTO().convertFrom(postRepository.getOne(comment.getPostId()));
BasePostMinimalDTO basePostMinimalDto =
new BasePostMinimalDTO().convertFrom(postRepository.getById(comment.getPostId()));
postCommentWithPostVO.setPost(buildPostFullPath(basePostMinimalDTO));
return postCommentWithPostVO;
postCommentWithPostVo.setPost(buildPostFullPath(basePostMinimalDto));
postCommentWithPostVo.setAvatar(buildAvatarUrl(comment.getGravatarMd5()));
return postCommentWithPostVo;
}
@Override
@NonNull
public List<PostCommentWithPostVO> convertToWithPostVo(List<PostComment> postComments) {
if (CollectionUtils.isEmpty(postComments)) {
return Collections.emptyList();
@ -100,15 +107,17 @@ public class PostCommentServiceImpl extends BaseCommentServiceImpl<PostComment>
.filter(comment -> postMap.containsKey(comment.getPostId()))
.map(comment -> {
// Convert to vo
PostCommentWithPostVO postCommentWithPostVO =
PostCommentWithPostVO postCommentWithPostVo =
new PostCommentWithPostVO().convertFrom(comment);
BasePostMinimalDTO basePostMinimalDTO =
BasePostMinimalDTO basePostMinimalDto =
new BasePostMinimalDTO().convertFrom(postMap.get(comment.getPostId()));
postCommentWithPostVO.setPost(buildPostFullPath(basePostMinimalDTO));
postCommentWithPostVo.setPost(buildPostFullPath(basePostMinimalDto));
return postCommentWithPostVO;
postCommentWithPostVo.setAvatar(buildAvatarUrl(comment.getGravatarMd5()));
return postCommentWithPostVo;
}).collect(Collectors.toList());
}
@ -177,7 +186,7 @@ public class PostCommentServiceImpl extends BaseCommentServiceImpl<PostComment>
}
@Override
public void validateTarget(Integer postId) {
public void validateTarget(@NonNull Integer postId) {
Post post = postRepository.findById(postId)
.orElseThrow(() -> new NotFoundException("查询不到该文章的信息").setErrorData(postId));

View File

@ -10,6 +10,7 @@ import java.util.stream.Collectors;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
@ -50,7 +51,7 @@ public class SheetCommentServiceImpl extends BaseCommentServiceImpl<SheetComment
}
@Override
public void validateTarget(Integer sheetId) {
public void validateTarget(@NonNull Integer sheetId) {
Sheet sheet = sheetRepository.findById(sheetId)
.orElseThrow(() -> new NotFoundException("查询不到该页面的信息").setErrorData(sheetId));
@ -60,19 +61,24 @@ public class SheetCommentServiceImpl extends BaseCommentServiceImpl<SheetComment
}
@Override
public SheetCommentWithSheetVO convertToWithSheetVo(SheetComment comment) {
@NonNull
public SheetCommentWithSheetVO convertToWithSheetVo(@NonNull SheetComment comment) {
Assert.notNull(comment, "SheetComment must not be null");
SheetCommentWithSheetVO sheetCommentWithSheetVO =
SheetCommentWithSheetVO sheetCommentWithSheetVo =
new SheetCommentWithSheetVO().convertFrom(comment);
BasePostMinimalDTO basePostMinimalDTO =
BasePostMinimalDTO basePostMinimalDto =
new BasePostMinimalDTO().convertFrom(sheetRepository.getOne(comment.getPostId()));
sheetCommentWithSheetVO.setSheet(buildSheetFullPath(basePostMinimalDTO));
return sheetCommentWithSheetVO;
sheetCommentWithSheetVo.setSheet(buildSheetFullPath(basePostMinimalDto));
sheetCommentWithSheetVo.setAvatar(buildAvatarUrl(comment.getGravatarMd5()));
return sheetCommentWithSheetVo;
}
@Override
@NonNull
public List<SheetCommentWithSheetVO> convertToWithSheetVo(List<SheetComment> sheetComments) {
if (CollectionUtils.isEmpty(sheetComments)) {
return Collections.emptyList();
@ -86,27 +92,32 @@ public class SheetCommentServiceImpl extends BaseCommentServiceImpl<SheetComment
return sheetComments.stream()
.filter(comment -> sheetMap.containsKey(comment.getPostId()))
.map(comment -> {
SheetCommentWithSheetVO sheetCmtWithPostVO =
SheetCommentWithSheetVO sheetCmtWithPostVo =
new SheetCommentWithSheetVO().convertFrom(comment);
BasePostMinimalDTO postMinimalDTO =
BasePostMinimalDTO postMinimalDto =
new BasePostMinimalDTO().convertFrom(sheetMap.get(comment.getPostId()));
sheetCmtWithPostVO.setSheet(buildSheetFullPath(postMinimalDTO));
return sheetCmtWithPostVO;
sheetCmtWithPostVo.setSheet(buildSheetFullPath(postMinimalDto));
sheetCmtWithPostVo.setAvatar(buildAvatarUrl(comment.getGravatarMd5()));
return sheetCmtWithPostVo;
})
.collect(Collectors.toList());
}
@Override
public Page<SheetCommentWithSheetVO> convertToWithSheetVo(Page<SheetComment> sheetCommentPage) {
@NonNull
public Page<SheetCommentWithSheetVO> convertToWithSheetVo(
@NonNull Page<SheetComment> sheetCommentPage) {
Assert.notNull(sheetCommentPage, "Sheet comment page must not be null");
return new PageImpl<>(convertToWithSheetVo(sheetCommentPage.getContent()),
sheetCommentPage.getPageable(), sheetCommentPage.getTotalElements());
}
private BasePostMinimalDTO buildSheetFullPath(BasePostMinimalDTO basePostMinimalDTO) {
private BasePostMinimalDTO buildSheetFullPath(BasePostMinimalDTO basePostMinimalDto) {
StringBuilder fullPath = new StringBuilder();
SheetPermalinkType permalinkType = optionService.getSheetPermalinkType();
@ -119,16 +130,16 @@ public class SheetCommentServiceImpl extends BaseCommentServiceImpl<SheetComment
fullPath.append(URL_SEPARATOR)
.append(optionService.getSheetPrefix())
.append(URL_SEPARATOR)
.append(basePostMinimalDTO.getSlug())
.append(basePostMinimalDto.getSlug())
.append(optionService.getPathSuffix());
} else if (permalinkType.equals(SheetPermalinkType.ROOT)) {
fullPath.append(URL_SEPARATOR)
.append(basePostMinimalDTO.getSlug())
.append(basePostMinimalDto.getSlug())
.append(optionService.getPathSuffix());
}
basePostMinimalDTO.setFullPath(fullPath.toString());
return basePostMinimalDTO;
basePostMinimalDto.setFullPath(fullPath.toString());
return basePostMinimalDto;
}
}

View File

@ -2,7 +2,6 @@ package run.halo.app.service.impl;
import freemarker.template.Configuration;
import freemarker.template.TemplateModelException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
@ -175,19 +174,6 @@ public class ThemeSettingServiceImpl extends AbstractCrudService<ThemeSetting, I
return result;
}
@Override
public List<ThemeSetting> replaceUrl(String oldUrl, String newUrl) {
List<ThemeSetting> themeSettings = listAll();
List<ThemeSetting> replaced = new ArrayList<>();
themeSettings.forEach(themeSetting -> {
if (StringUtils.isNotEmpty(themeSetting.getValue())) {
themeSetting.setValue(themeSetting.getValue().replaceAll(oldUrl, newUrl));
}
replaced.add(themeSetting);
});
return updateInBatch(replaced);
}
@Override
@Transactional
public void deleteInactivated() {