Skip to content

Commit

Permalink
🐛 비밀 댓글을 볼 수 없는 사용자 에게는 내용을 빈 값으로 응답하도록 수정 (#304)
Browse files Browse the repository at this point in the history
* 🐛 비밀 댓글을 볼 수 없는 사용자 에게는 내용을 빈 값으로 응답하도록 수정

* 🐛 비밀댓글 내용 삭제 로직 리팩토링

* Revert "🐛 비밀댓글 내용 삭제 로직 리팩토링"

This reverts commit 600885c.

* ♻️ 비밀댓글 내용 삭제 로직 리팩토링
  • Loading branch information
hwgyun authored Dec 21, 2024
1 parent f3a18fd commit 4dfcbc4
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import backend.team.ahachul_backend.api.comment.application.port.`in`.CommentUse
import backend.team.ahachul_backend.api.comment.application.port.out.CommentReader
import backend.team.ahachul_backend.api.comment.application.port.out.CommentWriter
import backend.team.ahachul_backend.api.comment.domain.entity.CommentEntity
import backend.team.ahachul_backend.api.comment.domain.model.CommentVisibility
import backend.team.ahachul_backend.api.comment.domain.model.PostType
import backend.team.ahachul_backend.api.community.application.port.out.CommunityPostReader
import backend.team.ahachul_backend.api.lost.application.port.out.LostPostReader
Expand All @@ -29,9 +30,14 @@ class CommentService(
private val lostPostReader: LostPostReader,
private val memberReader: MemberReader,
): CommentUseCase {

override fun getComments(command: GetCommentsCommand): GetCommentsDto.Response {
val postWriterId = when (command.postType) {
PostType.COMMUNITY -> communityPostReader.getCommunityPost(command.postId).createdBy
PostType.LOST -> lostPostReader.getLostPost(command.postId).createdBy
}.toLongOrNull()

val loginMemberId = RequestUtils.getAttribute("memberId")?.toLong()
val isPostWriterEqualToLoginMember = postWriterId != null && loginMemberId == postWriterId

val comments = when (command.postType) {
PostType.COMMUNITY -> commentReader.findAllByCommunityPostId(command.postId)
Expand All @@ -40,7 +46,8 @@ class CommentService(
GetCommentsDto.Comment(
it.id,
it.upperComment?.id,
it.content,
if(it.validateReadPermission(loginMemberId)
|| isPostWriterEqualToLoginMember) it.content else "",
it.status,
it.createdAt,
it.createdBy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CommunityPostCommentController(
private val commentUseCase: CommentUseCase
) {

@Authentication(required = false)
@GetMapping("/v1/community-posts/{postId}/comments")
fun getCommunityPostComments(@PathVariable postId: Long): CommonResponse<GetCommentsDto.Response> {
return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(postId, PostType.COMMUNITY)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class LostPostCommentController(
private val commentUseCase: CommentUseCase
) {

@Authentication(required = false)
@GetMapping("/v1/lost-posts/{lostId}/comments")
fun getLostPostComments(@PathVariable lostId: Long): CommonResponse<GetCommentsDto.Response> {
return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(lostId, PostType.LOST)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ class CommentServiceTest(
private lateinit var category: CategoryEntity
private lateinit var communityPost: CommunityPostEntity
private lateinit var lostPost: LostPostEntity
private val memberIds: MutableList<Long> = ArrayList()
private val membersCount: Int = 5

private fun loginWithMemberId(memberId: Long) {
RequestUtils.setAttribute("memberId", memberId)
}
@BeforeEach
fun setup() {
val member = memberRepository.save(
Expand All @@ -66,12 +71,36 @@ class CommentServiceTest(
)
)
member.id.let { RequestUtils.setAttribute("memberId", it) }
lateinit var postWriter: MemberEntity

for (i in 1..membersCount) {
val member = memberRepository.save(
MemberEntity(
nickname = "nickname${i}",
provider = ProviderType.KAKAO,
providerUserId = "providerUserId${i}",
email = "email${i}",
gender = GenderType.MALE,
ageRange = "20",
status = MemberStatusType.ACTIVE
)
)

if (i == 1) {
postWriter = member
}

memberIds.add(member.id)
}
loginWithMemberId(memberIds[0])

subwayLine = subwayLineRepository.save(SubwayLineEntity(name = "1호선", regionType = RegionType.METROPOLITAN))
category = categoryRepository.save(CategoryEntity(name = "휴대폰"))
communityPost = communityPostRepository.save(
CommunityPostEntity(
title = "제목",
content = "내용",
member = postWriter,
categoryType = CommunityCategoryType.FREE,
subwayLineEntity = subwayLine
)
Expand All @@ -80,6 +109,7 @@ class CommentServiceTest(
LostPostEntity(
title = "제목",
content = "내용",
member = postWriter,
subwayLine = subwayLine,
lostType = LostType.LOST,
category = category
Expand Down Expand Up @@ -261,6 +291,103 @@ class CommentServiceTest(
}
}

@Test
@DisplayName("커뮤니티 코멘트 조회 - 비밀댓글")
fun 커뮤니티_코멘트_조회_비밀댓글() {
// given
val content = "content"

val createParentCommentCommand = CreateCommentCommand(
postId = communityPost.id,
postType = PostType.COMMUNITY,
upperCommentId = null,
content = content,
visibility = CommentVisibility.PRIVATE
)
val createChildCommentCommand = CreateCommentCommand(
postId = communityPost.id,
postType = PostType.COMMUNITY,
upperCommentId = null,
content = content,
visibility = CommentVisibility.PRIVATE
)

loginWithMemberId(memberIds[1])
commentUseCase.createComment(createParentCommentCommand)
loginWithMemberId(memberIds[0])
commentUseCase.createComment(createChildCommentCommand)

val getCommentsCommand = GetCommentsCommand(
postId = communityPost.id,
PostType.COMMUNITY
)

// when
loginWithMemberId(memberIds[0])
val postWriterResult = commentUseCase.getComments(getCommentsCommand)
loginWithMemberId(memberIds[1])
val commentWriterResult = commentUseCase.getComments(getCommentsCommand)
loginWithMemberId(memberIds[2])
val anotherMemberResult = commentUseCase.getComments(getCommentsCommand)

// then
assertThat(postWriterResult.comments[0].parentComment.isPrivate).isTrue()
assertThat(postWriterResult.comments[0].parentComment.content).isEqualTo(content)
assertThat(commentWriterResult.comments[0].parentComment.isPrivate).isTrue()
assertThat(commentWriterResult.comments[0].parentComment.content).isEqualTo(content)
assertThat(anotherMemberResult.comments[0].parentComment.isPrivate).isTrue()
assertThat(anotherMemberResult.comments[0].parentComment.content).isEmpty()
}

@Test
@DisplayName("유실물 코멘트 조회 - 비밀댓글")
fun 유실물_코멘트_조회_비밀댓글() {
// given
val content = "content"

val createParentCommentCommand = CreateCommentCommand(
postId = lostPost.id,
postType = PostType.LOST,
upperCommentId = null,
content = content,
visibility = CommentVisibility.PRIVATE
)
val createChildCommentCommand = CreateCommentCommand(
postId = lostPost.id,
postType = PostType.LOST,
upperCommentId = null,
content = content,
visibility = CommentVisibility.PRIVATE
)

loginWithMemberId(memberIds[1])
commentUseCase.createComment(createParentCommentCommand)
loginWithMemberId(memberIds[0])
commentUseCase.createComment(createChildCommentCommand)

val getCommentsCommand = GetCommentsCommand(
postId = lostPost.id,
PostType.LOST
)

// when
loginWithMemberId(memberIds[0])
val postWriterResult = commentUseCase.getComments(getCommentsCommand)
loginWithMemberId(memberIds[1])
val commentWriterResult = commentUseCase.getComments(getCommentsCommand)
loginWithMemberId(memberIds[2])
val anotherMemberResult = commentUseCase.getComments(getCommentsCommand)

// then
assertThat(postWriterResult.comments[0].parentComment.isPrivate).isTrue()
assertThat(postWriterResult.comments[0].parentComment.content).isEqualTo(content)
assertThat(commentWriterResult.comments[0].parentComment.isPrivate).isTrue()
assertThat(commentWriterResult.comments[0].parentComment.content).isEqualTo(content)
assertThat(anotherMemberResult.comments[0].parentComment.isPrivate).isTrue()
assertThat(anotherMemberResult.comments[0].parentComment.content).isEmpty()
}


@Test
@DisplayName("자식 코멘트 조회")
fun 자식_코멘트_조회() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ class CommentEntity(
}
}

fun validateReadPermission(loginMemberId: Long?) : Boolean {
if (visibility == CommentVisibility.PUBLIC) {
return true
}

return if (upperComment != null) {
upperComment!!.id == loginMemberId
} else {
member.id == loginMemberId
}
}

fun update(content: String) {
this.content = content
}
Expand Down

0 comments on commit 4dfcbc4

Please sign in to comment.