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

feat: 댓글에 대한 비밀 대댓글을 댓글 작성자가 볼 수 있도록 변경 #168

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions src/main/java/com/mallang/comment/domain/AuthComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ public void write(@Nullable String postPassword) {
post.validateAccess(writer, postPassword);
}

public boolean canSee(Member member) {
if (!secret) {
return true;
}
if (post.isWriter(member)) {
return true;
}
if (writer.equals(member)) {
return true;
}
return parent != null && parent instanceof AuthComment authed && authed.getWriter().equals(member);
}

public void validateUpdate(Member member, @Nullable String postPassword) {
post.validateAccess(member, postPassword);
if (member.equals(writer)) {
Expand Down

This file was deleted.

40 changes: 32 additions & 8 deletions src/main/java/com/mallang/comment/query/CommentQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import com.mallang.auth.domain.Member;
import com.mallang.auth.query.repository.MemberQueryRepository;
import com.mallang.comment.domain.AuthComment;
import com.mallang.comment.domain.Comment;
import com.mallang.comment.domain.UnAuthComment;
import com.mallang.comment.query.repository.CommentQueryRepository;
import com.mallang.comment.query.response.AuthCommentResponse;
import com.mallang.comment.query.response.CommentResponse;
import com.mallang.comment.query.response.UnAuthCommentResponse;
import com.mallang.post.domain.Post;
import com.mallang.post.query.repository.PostQueryRepository;
import jakarta.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -21,7 +26,6 @@ public class CommentQueryService {
private final PostQueryRepository postQueryRepository;
private final MemberQueryRepository memberQueryRepository;
private final CommentQueryRepository commentQueryRepository;
private final CommentDataPostProcessor commentDataPostProcessor;

public List<CommentResponse> findAllByPost(
Long postId,
Expand All @@ -32,14 +36,34 @@ public List<CommentResponse> findAllByPost(
Post post = postQueryRepository.getById(postId, blogName);
Member member = memberQueryRepository.getMemberIfIdNotNull(memberId);
post.validateAccess(member, postPassword);
List<CommentResponse> result = commentQueryRepository.findAllByPost(postId, blogName)
List<Comment> parents = commentQueryRepository.findAllByPost(postId, blogName)
.stream()
.filter(it -> Objects.isNull(it.getParent()))
.map(CommentResponse::from)
.filter(it -> it.getParent() == null)
.toList();
if (post.getWriter().equals(member)) {
return result;
List<CommentResponse> responses = new ArrayList<>();
for (Comment parent : parents) {
List<Comment> children = parent.getChildren();
CommentResponse parentResponse = mapToResponse(parent, member);
parentResponse.setChildren(mapToResponses(children, member));
responses.add(parentResponse);
}
return commentDataPostProcessor.processSecret(result, memberId);
return responses;
}

private List<CommentResponse> mapToResponses(List<Comment> comments, Member member) {
return comments.stream()
.map(it -> mapToResponse(it, member))
.toList();
}

private CommentResponse mapToResponse(Comment comment, Member member) {
if (comment instanceof AuthComment authComment) {
if (authComment.canSee(member)) {
return AuthCommentResponse.from(authComment);
}
return AuthCommentResponse.protectFrom(authComment);
}
return UnAuthCommentResponse.from((UnAuthComment) comment);

}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.mallang.comment.query.response;

import static com.mallang.comment.query.response.AuthCommentResponse.WriterResponse.ANONYMOUS;

import com.mallang.auth.domain.Member;
import com.mallang.comment.domain.AuthComment;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Builder;
import lombok.Getter;

Expand All @@ -21,10 +22,9 @@ public AuthCommentResponse(
LocalDateTime createdDate,
boolean deleted,
WriterResponse writer,
boolean secret,
List<CommentResponse> children
boolean secret
) {
super(id, content, createdDate, deleted, children);
super(id, content, createdDate, deleted);
this.writer = writer;
this.secret = secret;
}
Expand All @@ -35,14 +35,22 @@ public static AuthCommentResponse from(AuthComment comment) {
.content(comment.getContent())
.createdDate(comment.getCreatedDate())
.deleted(comment.isDeleted())
.children(comment.getChildren().stream()
.map(CommentResponse::from)
.toList())
.writer(WriterResponse.from(comment.getWriter()))
.secret(comment.isSecret())
.build();
}

public static AuthCommentResponse protectFrom(AuthComment comment) {
return AuthCommentResponse.builder()
.id(comment.getId())
.content("비밀 댓글입니다.")
.createdDate(comment.getCreatedDate())
.deleted(comment.isDeleted())
.writer(ANONYMOUS)
.secret(true)
.build();
}

public record WriterResponse(
Long memberId,
String nickname,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.mallang.comment.domain.AuthComment;
import com.mallang.comment.domain.Comment;
import com.mallang.comment.domain.UnAuthComment;
import com.mallang.common.execption.MallangLogException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -34,29 +30,22 @@ public abstract sealed class CommentResponse
protected final String content;
protected final LocalDateTime createdDate;
protected final boolean deleted;
protected final List<CommentResponse> children;
protected final List<CommentResponse> children = new ArrayList<>();

protected CommentResponse(
Long id,
String content,
LocalDateTime createdDate,
boolean deleted,
List<CommentResponse> children
boolean deleted
) {
this.id = id;
this.content = content;
this.createdDate = createdDate;
this.deleted = deleted;
this.children = (children == null)
? new ArrayList<>()
: children;
}

public static CommentResponse from(Comment comment) {
return switch (comment) {
case AuthComment authComment -> AuthCommentResponse.from(authComment);
case UnAuthComment unAuthComment -> UnAuthCommentResponse.from(unAuthComment);
default -> throw new MallangLogException("해당 Comment 타입이 CommentResponse 에서 지원되지 않습니다.");
};
public void setChildren(List<CommentResponse> children) {
this.children.clear();
this.children.addAll(children);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mallang.comment.domain.UnAuthComment;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Builder;
import lombok.Getter;

Expand All @@ -18,10 +17,9 @@ public UnAuthCommentResponse(
String content,
LocalDateTime createdDate,
boolean deleted,
WriterResponse writer,
List<CommentResponse> children
WriterResponse writer
) {
super(id, content, createdDate, deleted, children);
super(id, content, createdDate, deleted);
this.writer = writer;
}

Expand All @@ -31,9 +29,6 @@ public static UnAuthCommentResponse from(UnAuthComment comment) {
.content(comment.getContent())
.createdDate(comment.getCreatedDate())
.deleted(comment.isDeleted())
.children(comment.getChildren().stream()
.map(CommentResponse::from)
.toList())
.writer(new WriterResponse(comment.getNickname()))
.build();
}
Expand Down
Loading