diff --git a/application/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentService.kt b/application/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentService.kt index 84461e0d..c21cfbea 100644 --- a/application/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentService.kt +++ b/application/src/main/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentService.kt @@ -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 @@ -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) @@ -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, diff --git a/application/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/CommunityPostCommentController.kt b/application/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/CommunityPostCommentController.kt index 5441bd6d..88ee56f6 100644 --- a/application/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/CommunityPostCommentController.kt +++ b/application/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/CommunityPostCommentController.kt @@ -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 { return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(postId, PostType.COMMUNITY))) diff --git a/application/src/main/kotlin/backend/team/ahachul_backend/api/lost/adapter/web/in/LostPostCommentController.kt b/application/src/main/kotlin/backend/team/ahachul_backend/api/lost/adapter/web/in/LostPostCommentController.kt index d8118f7a..3c270c8d 100644 --- a/application/src/main/kotlin/backend/team/ahachul_backend/api/lost/adapter/web/in/LostPostCommentController.kt +++ b/application/src/main/kotlin/backend/team/ahachul_backend/api/lost/adapter/web/in/LostPostCommentController.kt @@ -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 { return CommonResponse.success(commentUseCase.getComments(GetCommentsCommand(lostId, PostType.LOST))) diff --git a/application/src/test/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentServiceTest.kt b/application/src/test/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentServiceTest.kt index d9170803..8cc9fd98 100644 --- a/application/src/test/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentServiceTest.kt +++ b/application/src/test/kotlin/backend/team/ahachul_backend/api/comment/application/service/CommentServiceTest.kt @@ -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 = ArrayList() + private val membersCount: Int = 5 + private fun loginWithMemberId(memberId: Long) { + RequestUtils.setAttribute("memberId", memberId) + } @BeforeEach fun setup() { val member = memberRepository.save( @@ -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 ) @@ -80,6 +109,7 @@ class CommentServiceTest( LostPostEntity( title = "제목", content = "내용", + member = postWriter, subwayLine = subwayLine, lostType = LostType.LOST, category = category @@ -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 자식_코멘트_조회() { diff --git a/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/domain/entity/CommentEntity.kt b/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/domain/entity/CommentEntity.kt index 0776febb..34449823 100644 --- a/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/domain/entity/CommentEntity.kt +++ b/core/src/main/kotlin/backend/team/ahachul_backend/api/comment/domain/entity/CommentEntity.kt @@ -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 }