From cd833dc21d52a0e15fbfcb7c4f208ee516ef6011 Mon Sep 17 00:00:00 2001 From: Klaus Date: Mon, 28 Aug 2023 15:26:27 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=EC=BD=98=ED=85=90=EC=B8=A0=20=EB=8C=93?= =?UTF-8?q?=EA=B8=80=20=EC=95=8C=EB=A6=BC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/AudioContentCommentRepository.kt | 27 ++++++++++++++++ .../comment/AudioContentCommentService.kt | 22 +++++++++++++ ...FindPushTokenByContentCommentIdResponse.kt | 6 ++++ .../kr/co/vividnext/sodalive/fcm/FcmEvent.kt | 31 ++++++++++++++++--- 4 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/kr/co/vividnext/sodalive/content/comment/FindPushTokenByContentCommentIdResponse.kt diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/AudioContentCommentRepository.kt b/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/AudioContentCommentRepository.kt index b58aa2d..153ea4b 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/AudioContentCommentRepository.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/AudioContentCommentRepository.kt @@ -1,6 +1,7 @@ package kr.co.vividnext.sodalive.content.comment import com.querydsl.jpa.impl.JPAQueryFactory +import kr.co.vividnext.sodalive.content.QAudioContent.audioContent import kr.co.vividnext.sodalive.content.comment.QAudioContentComment.audioContentComment import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository @@ -28,6 +29,8 @@ interface AudioContentCommentQueryRepository { offset: Long, limit: Int ): List + + fun findPushTokenByContentCommentId(contentCommentId: Long): FindPushTokenByContentCommentIdResponse? } @Repository @@ -138,4 +141,28 @@ class AudioContentCommentQueryRepositoryImpl( } .toList() } + + override fun findPushTokenByContentCommentId(contentCommentId: Long): FindPushTokenByContentCommentIdResponse? { + val response = queryFactory + .selectFrom(audioContentComment) + .innerJoin(audioContentComment.audioContent, audioContent) + .where(audioContentComment.id.eq(contentCommentId)) + .fetchFirst() + + return if (response == null) { + null + } else { + if (response.parent != null) { + FindPushTokenByContentCommentIdResponse( + pushToken = response.parent!!.member!!.pushToken ?: "", + container = response.parent!!.member!!.container + ) + } else { + FindPushTokenByContentCommentIdResponse( + pushToken = response.audioContent!!.member!!.pushToken ?: "", + container = response.audioContent!!.member!!.container + ) + } + } + } } diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/AudioContentCommentService.kt b/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/AudioContentCommentService.kt index dd91014..fb6f527 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/AudioContentCommentService.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/AudioContentCommentService.kt @@ -2,8 +2,11 @@ package kr.co.vividnext.sodalive.content.comment import kr.co.vividnext.sodalive.common.SodaException import kr.co.vividnext.sodalive.content.AudioContentRepository +import kr.co.vividnext.sodalive.fcm.FcmEvent +import kr.co.vividnext.sodalive.fcm.FcmEventType import kr.co.vividnext.sodalive.member.Member import org.springframework.beans.factory.annotation.Value +import org.springframework.context.ApplicationEventPublisher import org.springframework.data.domain.Pageable import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service @@ -14,6 +17,7 @@ import org.springframework.transaction.annotation.Transactional class AudioContentCommentService( private val repository: AudioContentCommentRepository, private val audioContentRepository: AudioContentRepository, + private val applicationEventPublisher: ApplicationEventPublisher, @Value("\${cloud.aws.cloud-front.host}") private val cloudFrontHost: String @@ -38,6 +42,24 @@ class AudioContentCommentService( } repository.save(audioContentComment) + + applicationEventPublisher.publishEvent( + FcmEvent( + type = FcmEventType.CREATE_CONTENT_COMMENT, + title = if (parent != null) { + parent.member!!.nickname + } else { + member.nickname + }, + message = if (parent != null) { + "댓글에 답글을 달았습니다.: ${audioContent.title}" + } else { + "콘텐츠에 댓글을 달았습니다.: ${audioContent.title}" + }, + contentId = audioContentId, + contentCommentId = audioContentComment.id!! + ) + ) } @Transactional diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/FindPushTokenByContentCommentIdResponse.kt b/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/FindPushTokenByContentCommentIdResponse.kt new file mode 100644 index 0000000..6fd6d89 --- /dev/null +++ b/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/FindPushTokenByContentCommentIdResponse.kt @@ -0,0 +1,6 @@ +package kr.co.vividnext.sodalive.content.comment + +data class FindPushTokenByContentCommentIdResponse( + val pushToken: String, + val container: String +) diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmEvent.kt b/src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmEvent.kt index 3f71cf0..89045ff 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmEvent.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmEvent.kt @@ -1,12 +1,13 @@ package kr.co.vividnext.sodalive.fcm +import kr.co.vividnext.sodalive.content.comment.AudioContentCommentRepository import kr.co.vividnext.sodalive.member.MemberRepository -import org.springframework.context.event.EventListener import org.springframework.scheduling.annotation.Async import org.springframework.stereotype.Component +import org.springframework.transaction.event.TransactionalEventListener enum class FcmEventType { - ALL, INDIVIDUAL, CREATE_LIVE, START_LIVE, UPLOAD_CONTENT, SEND_MESSAGE, CHANGE_NOTICE + ALL, INDIVIDUAL, CREATE_LIVE, START_LIVE, UPLOAD_CONTENT, SEND_MESSAGE, CHANGE_NOTICE, CREATE_CONTENT_COMMENT } class FcmEvent( @@ -19,16 +20,18 @@ class FcmEvent( val roomId: Long? = null, val contentId: Long? = null, val messageId: Long? = null, - val creatorId: Long? = null + val creatorId: Long? = null, + val contentCommentId: Long? = null ) @Component class FcmSendListener( private val pushService: FcmService, - private val memberRepository: MemberRepository + private val memberRepository: MemberRepository, + private val contentCommentRepository: AudioContentCommentRepository ) { @Async - @EventListener + @TransactionalEventListener fun send(fcmEvent: FcmEvent) { when (fcmEvent.type) { FcmEventType.ALL -> { @@ -189,6 +192,24 @@ class FcmSendListener( } } } + + FcmEventType.CREATE_CONTENT_COMMENT -> { + if (fcmEvent.contentCommentId != null && fcmEvent.contentId != null) { + val response = contentCommentRepository.findPushTokenByContentCommentId( + contentCommentId = fcmEvent.contentCommentId + ) + + if (response != null) { + pushService.send( + tokens = listOf(response.pushToken), + title = fcmEvent.title, + message = fcmEvent.message, + container = response.container, + contentId = fcmEvent.contentId + ) + } + } + } } } } From 7bb7e921377b042e7a15ba7a0a5380b70e9232e1 Mon Sep 17 00:00:00 2001 From: Klaus Date: Mon, 28 Aug 2023 16:12:32 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EC=BD=98=ED=85=90=EC=B8=A0=20=EB=8C=93?= =?UTF-8?q?=EA=B8=80=20=EC=95=8C=EB=A6=BC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmEvent.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmEvent.kt b/src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmEvent.kt index 89045ff..295e592 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmEvent.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmEvent.kt @@ -4,6 +4,8 @@ import kr.co.vividnext.sodalive.content.comment.AudioContentCommentRepository import kr.co.vividnext.sodalive.member.MemberRepository import org.springframework.scheduling.annotation.Async import org.springframework.stereotype.Component +import org.springframework.transaction.annotation.Propagation +import org.springframework.transaction.annotation.Transactional import org.springframework.transaction.event.TransactionalEventListener enum class FcmEventType { @@ -32,6 +34,7 @@ class FcmSendListener( ) { @Async @TransactionalEventListener + @Transactional(propagation = Propagation.REQUIRES_NEW) fun send(fcmEvent: FcmEvent) { when (fcmEvent.type) { FcmEventType.ALL -> {