Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.wespot.comment

import com.wespot.comment.dto.PostCommentReportRequest
import com.wespot.comment.port.`in`.PostCommentReportUseCase
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/v1/post/comment")
Expand All @@ -15,8 +13,11 @@ class PostCommentReportController(
) {

@PostMapping("/{commentId}/report")
fun reportPostComment(@PathVariable commentId: Long): ResponseEntity<Unit> {
postCommentReportUseCase.reportComment(commentId)
fun reportPostComment(
@PathVariable commentId: Long,
@RequestBody(required = false) request: PostCommentReportRequest?
): ResponseEntity<Unit> {
postCommentReportUseCase.reportComment(commentId, request)

return ResponseEntity.status(HttpStatus.CREATED)
.build()
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/kotlin/com/wespot/post/PostReportController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import com.wespot.post.dto.request.PostReportRequest
import com.wespot.post.port.`in`.PostReportUseCase
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/v1/post")
Expand All @@ -16,7 +13,10 @@ class PostReportController(
) {

@PostMapping("/{postId}/report")
fun reportPost(@PathVariable postId: Long, request: PostReportRequest): ResponseEntity<Unit> {
fun reportPost(
@PathVariable postId: Long,
@RequestBody(required = false) request: PostReportRequest?
): ResponseEntity<Unit> {
postReportUseCase.reportPost(postId, request)

return ResponseEntity.status(HttpStatus.CREATED)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.wespot.comment.dto

import com.wespot.report.dto.ReportReasonRequest

data class PostCommentReportRequest(
val reportReasonRequests: List<ReportReasonRequest>,
) {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.wespot.comment.port.`in`

import com.wespot.comment.dto.PostCommentReportRequest

interface PostCommentReportUseCase {

fun reportComment(commentId: Long)
fun reportComment(commentId: Long, request: PostCommentReportRequest?)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.wespot.comment.port.out

import com.wespot.comment.PostCommentReportReason

interface PostCommentReportReasonPort {

fun saveAll(
postCommentReportId: Long,
postCommentReportReasons: List<PostCommentReportReason>
): List<PostCommentReportReason>

fun findAllByPostCommentReportId(postCommentReportId: Long): List<PostCommentReportReason>

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package com.wespot.comment.service

import com.wespot.auth.service.SecurityUtils
import com.wespot.comment.PostCommentReport
import com.wespot.comment.PostCommentReportReason
import com.wespot.comment.dto.PostCommentReportRequest
import com.wespot.comment.port.`in`.PostCommentReportUseCase
import com.wespot.comment.port.out.PostCommentPort
import com.wespot.comment.port.out.PostCommentReportPort
import com.wespot.exception.CustomException
import com.wespot.report.ReportReasonWithCustomReason
import com.wespot.report.port.out.ReportReasonPort
import com.wespot.user.port.out.UserPort
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -14,22 +18,42 @@ import org.springframework.transaction.annotation.Transactional
class PostCommentReportService(
private val userPort: UserPort,
private val postCommentReportPort: PostCommentReportPort,
private val postCommentPort: PostCommentPort
private val postCommentPort: PostCommentPort,
private val reportReasonPort: ReportReasonPort
) : PostCommentReportUseCase {

@Transactional
override fun reportComment(commentId: Long) {
override fun reportComment(commentId: Long, request: PostCommentReportRequest?) {
val loginUser = SecurityUtils.getLoginUser(userPort = userPort)
val postComment = postCommentPort.findById(commentId) ?: throw CustomException(message = "존재하지 않는 댓글입니다.")

postCommentReportPort.findByPostCommentIdAndUserId(postCommentId = commentId, userId = loginUser.id)
?.let {
postCommentReportPort.deleteById(it.id)
val removedReportPostComment = postComment.removeReport()
postCommentPort.save(removedReportPostComment)
}
?: run {
val postCommentReport = PostCommentReport(postCommentId = commentId, userId = loginUser.id)
if (request == null) {
throw CustomException(message = "신고 사유를 선택해주세요.")
}

val postCommentReport =
PostCommentReport(postCommentId = commentId, userId = loginUser.id)
val postCommentReportReasons = request.reportReasonRequests
.map {
PostCommentReportReason(
reportReasonWithCustomReason = ReportReasonWithCustomReason(
reportReason = reportReasonPort.findById(it.reportReasonId) ?: throw CustomException(
message = "존재하지 않는 신고 사유입니다."
),
customReason = it.customReason
)
)
}
postCommentReport.addReportReasons(postCommentReportReason = postCommentReportReasons)
postCommentReportPort.save(postCommentReport)

val addedReportPostComment = postComment.addReport()
postCommentPort.save(addedReportPostComment)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.wespot.notification.NotificationType
import com.wespot.notification.service.NotificationHelper
import com.wespot.post.port.out.PostNotificationPort
import com.wespot.post.port.out.PostPort
import com.wespot.post.port.out.PostProfilePort
import org.springframework.http.HttpStatus
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Component
Expand All @@ -19,14 +20,15 @@ import org.springframework.transaction.event.TransactionalEventListener
@Component
class CommentNotificationListener(
private val postPort: PostPort,
private val postProfilePort: PostProfilePort,
private val postNotificationPort: PostNotificationPort,
private val notificationHelper: NotificationHelper,
) {

@Async
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
fun listenCreatedPostCommentEvent(postCommentCreatedEvent: PostCommentCreatedEvent) { // TODO : 일단, 본인은 알림 못받게 해야하고, 익명 프로필로
fun listenCreatedPostCommentEvent(postCommentCreatedEvent: PostCommentCreatedEvent) {
val postComment = postCommentCreatedEvent.postComment
val post = postPort.findById(postComment.postId) ?: throw CustomException(
status = HttpStatus.BAD_REQUEST,
Expand All @@ -35,14 +37,15 @@ class CommentNotificationListener(
val commentRegister = postComment.user
val postNotifications = postNotificationPort.findAllByPostId(post.id)

val users = listOf(post.user) + postNotifications.map { it.user }.distinct()
val users = postNotifications.map { it.user }.distinct()
val notifications = users.stream()
.filter { postComment.isNotAuthor(it.id) }
.map {
Notification.create(
userId = it.id,
type = NotificationType.COMMENT,
type = NotificationType.POST_COMMENT,
targetId = post.id,
title = "${commentRegister.name} 님이 댓글을 남겼습니다.",
title = "${post.commentProfileName(commentRegister)} 님이 댓글을 남겼습니다.",
body = NotificationUtil.summaryContent(
content = postComment.content.content
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.wespot.post.dto.request

import com.wespot.report.dto.ReportReasonRequest

data class PostReportRequest(
val reportReasonId: Long,
val reportReasonRequests: List<ReportReasonRequest>,
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import com.wespot.post.dto.request.PostReportRequest

interface PostReportUseCase {

fun reportPost(postId: Long, postReportRequest: PostReportRequest)
fun reportPost(postId: Long, postReportRequest: PostReportRequest?)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.wespot.post.port.out

import com.wespot.post.PostReportReason

interface PostReportReasonPort {

fun saveAll(postReportId: Long, postReportReasons: List<PostReportReason>): List<PostReportReason>

fun findByPostReportId(postReportId: Long): List<PostReportReason>

}
36 changes: 26 additions & 10 deletions core/src/main/kotlin/com/wespot/post/service/PostReportService.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.wespot.post.service

import com.wespot.auth.service.SecurityUtils
import com.wespot.exception.CustomException
import com.wespot.post.PostReport
import com.wespot.post.PostReportReason
import com.wespot.post.dto.request.PostReportRequest
import com.wespot.post.port.`in`.PostReportUseCase
import com.wespot.post.port.out.PostPort
import com.wespot.post.port.out.PostReportPort
import com.wespot.report.ReportReasonWithCustomReason
import com.wespot.report.port.out.ReportReasonPort
import com.wespot.user.port.out.UserPort
import org.springframework.stereotype.Service
Expand All @@ -20,25 +23,38 @@ class PostReportService(
) : PostReportUseCase {

@Transactional
override fun reportPost(postId: Long, postReportRequest: PostReportRequest) {
override fun reportPost(postId: Long, postReportRequest: PostReportRequest?) {
val loginUser = SecurityUtils.getLoginUser(userPort = userPort)

val post = postPort.findById(postId)
?: throw IllegalArgumentException("존재하지 않는 게시글입니다.")
val reportReason = reportReasonPort.findById(postReportRequest.reportReasonId)
?: throw IllegalArgumentException("존재하지 않는 신고 사유입니다.")
?: throw CustomException(message = "존재하지 않는 게시글입니다.")

postReportPort.findByPostIdAndUserId(postId, loginUser.id)
?.let {
postReportPort.deleteById(id = it.id)
}
?: run {
val postReport =
PostReport(
postId = post.id,
userId = loginUser.id,
reportReason = reportReason
)
if (postReportRequest == null) {
throw CustomException(message = "신고 사유를 선택해주세요.")
}

val postReport = PostReport(
postId = post.id,
userId = loginUser.id,
)

val postReportReasons = postReportRequest.reportReasonRequests
.map {
PostReportReason(
reportReasonWithCustomReason = ReportReasonWithCustomReason(
reportReason = reportReasonPort.findById(it.reportReasonId)
?: throw CustomException(message = "존재하지 않는 신고 사유입니다."),
customReason = it.customReason
)
)
}

postReport.addReportReasons(postReportReasons)
postReportPort.save(postReport)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wespot.report.dto

data class ReportReasonRequest(
val reportReasonId: Long,
val customReason: String?
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ interface ReportReasonPort {

fun findById(reportReasonId: Long): ReportReason?

fun findAllByIdIn(ids: List<Long>): List<ReportReason>

}
4 changes: 4 additions & 0 deletions domain/src/main/kotlin/com/wespot/comment/PostComment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@ class PostComment(
return user.id == id
}

fun isNotAuthor(id: Long): Boolean {
return !isAuthor(id)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ class PostCommentReport(
val id: Long = 0L,
val postCommentId: Long,
val userId: Long,
val postCommentReportReasons: List<PostCommentReportReason> = mutableListOf(),
val createdAt: LocalDateTime = LocalDateTime.now()
) {

fun addReportReasons(postCommentReportReason: List<PostCommentReportReason>) {
postCommentReportReason.forEach {
this.postCommentReportReasons.plus(it)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.wespot.comment

import com.wespot.report.ReportReasonWithCustomReason
import java.time.LocalDateTime

data class PostCommentReportReason(
val id: Long = 0L,
val postCommentReportId: Long = 0L,
val reportReasonWithCustomReason: ReportReasonWithCustomReason,

val createdAt: LocalDateTime = LocalDateTime.now(),
val updatedAt: LocalDateTime = LocalDateTime.now(),
) {

fun registeredInPostCommentReport(postCommentReportId: Long): PostCommentReportReason {
return copy(postCommentReportId = postCommentReportId)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum class NotificationType(
PROFILE_UPDATE({ it.isEnableMarketingNotification() }, SpecificNotificationType.UPDATE), // 프로필 업데이트 이벤트
UPDATE_REQUIRED({ it.isEnableMarketingNotification() }, SpecificNotificationType.UPDATE), // 업데이트를 아직 안한 유저

COMMENT({ it.isEnableMarketingNotification() }, SpecificNotificationType.POST)
POST_COMMENT({ it.isEnablePostNotification() }, SpecificNotificationType.POST) // 게시글 댓글 알림
;

fun isVote(): Boolean {
Expand Down
11 changes: 11 additions & 0 deletions domain/src/main/kotlin/com/wespot/post/Post.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Post(

companion object {

const val VIEWER_COMMENT_PROFILE_NAME = "익명의 댓쓴이"
const val AUTHOR_COMMENT_PROFILE_NAME = "익명의 글쓴이"

fun of(
category: PostCategory,
user: User,
Expand Down Expand Up @@ -142,4 +145,12 @@ class Post(
return copyAndUpdateField(bookmarkedCount = this.bookmarkedCount + 1)
}

fun commentProfileName(user: User): String {
if (isAuthor(user.id)) {
return AUTHOR_COMMENT_PROFILE_NAME
}

return VIEWER_COMMENT_PROFILE_NAME
}

}
10 changes: 8 additions & 2 deletions domain/src/main/kotlin/com/wespot/post/PostReport.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.wespot.post

import com.wespot.report.ReportReason
import java.time.LocalDateTime

class PostReport(
val id: Long = 0L,
val postId: Long,
val userId: Long,
val reportReason: ReportReason,
val postReportReasons: List<PostReportReason> = mutableListOf(),
val createdAt: LocalDateTime = LocalDateTime.now()
) {

fun addReportReasons(postReportReasons: List<PostReportReason>) {
postReportReasons.forEach {
this.postReportReasons.plus(it)
}
}

}
Loading
Loading