From f196b200246fca8a0d1adb674607715ce53b5c00 Mon Sep 17 00:00:00 2001
From: Klaus <klaus@vividnext.co.kr>
Date: Mon, 27 Nov 2023 20:35:08 +0900
Subject: [PATCH] =?UTF-8?q?=EC=BD=98=ED=85=90=EC=B8=A0=20=EB=8C=93?=
 =?UTF-8?q?=EA=B8=80=EC=9D=98=20=EB=8B=B5=EA=B8=80=20=ED=91=B8=EC=8B=9C=20?=
 =?UTF-8?q?-=20AS-IS=20:=20=EC=9B=90=20=EB=8C=93=EA=B8=80=EC=9D=98=20?=
 =?UTF-8?q?=EA=B8=80=EC=93=B4=EC=9D=B4=EC=97=90=EA=B2=8C=20=EC=95=8C?=
 =?UTF-8?q?=EB=A6=BC(=EC=9B=90=20=EB=8C=93=EA=B8=80=20=EA=B8=80=EC=93=B4?=
 =?UTF-8?q?=EC=9D=B4=EA=B0=80=20=EB=8B=B5=EA=B8=80=EC=9D=84=20=EB=8B=AC?=
 =?UTF-8?q?=EC=95=84=EB=8F=84=20=EC=95=8C=EB=A6=BC)=20-=20To-Be=20:=20?=
 =?UTF-8?q?=EB=8B=B5=EA=B8=80=EC=9D=84=20=EC=93=B4=20=EB=B3=B8=EC=9D=B8?=
 =?UTF-8?q?=EC=9D=84=20=EC=A0=9C=EC=99=B8=ED=95=98=EA=B3=A0=20=EC=9B=90=20?=
 =?UTF-8?q?=EB=8C=93=EA=B8=80=EC=9D=98=20=EB=8B=B5=EA=B8=80=EC=9D=84=20?=
 =?UTF-8?q?=EC=93=B4=20=EB=AA=A8=EB=93=A0=20=EC=9C=A0=EC=A0=80=EC=97=90?=
 =?UTF-8?q?=EA=B2=8C=20=ED=91=B8=EC=8B=9C=20=EC=95=8C=EB=A6=BC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../comment/AudioContentCommentRepository.kt  | 68 +++++++++++++------
 .../comment/AudioContentCommentService.kt     |  3 +-
 ...FindPushTokenByContentCommentIdResponse.kt |  6 --
 ...tIdAndCommentParentIdMyMemberIdResponse.kt |  8 +++
 .../kr/co/vividnext/sodalive/fcm/FcmEvent.kt  | 43 ++++++++++--
 5 files changed, 93 insertions(+), 35 deletions(-)
 delete mode 100644 src/main/kotlin/kr/co/vividnext/sodalive/content/comment/FindPushTokenByContentCommentIdResponse.kt
 create mode 100644 src/main/kotlin/kr/co/vividnext/sodalive/content/comment/FindPushTokenByContentIdAndCommentParentIdMyMemberIdResponse.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 0e791f3..1d6268d 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
@@ -3,6 +3,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 kr.co.vividnext.sodalive.member.QMember.member
 import org.springframework.data.jpa.repository.JpaRepository
 import org.springframework.stereotype.Repository
 import java.time.ZoneId
@@ -30,7 +31,11 @@ interface AudioContentCommentQueryRepository {
         limit: Int
     ): List<GetAudioContentCommentListItem>
 
-    fun findPushTokenByContentCommentId(contentCommentId: Long): FindPushTokenByContentCommentIdResponse?
+    fun findPushTokenByContentIdAndCommentParentIdMyMemberId(
+        contentId: Long,
+        commentParentId: Long?,
+        myMemberId: Long
+    ): List<FindPushTokenByContentIdAndCommentParentIdMyMemberIdResponse>
 }
 
 @Repository
@@ -143,27 +148,48 @@ class AudioContentCommentQueryRepositoryImpl(
             .toList()
     }
 
-    override fun findPushTokenByContentCommentId(contentCommentId: Long): FindPushTokenByContentCommentIdResponse? {
-        val response = queryFactory
-            .selectFrom(audioContentComment)
-            .innerJoin(audioContentComment.audioContent, audioContent)
-            .where(audioContentComment.id.eq(contentCommentId))
-            .fetchFirst()
+    override fun findPushTokenByContentIdAndCommentParentIdMyMemberId(
+        contentId: Long,
+        commentParentId: Long?,
+        myMemberId: Long
+    ): List<FindPushTokenByContentIdAndCommentParentIdMyMemberIdResponse> {
+        var where = audioContent.id.eq(contentId)
+            .and(member.id.ne(myMemberId))
 
-        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
-                )
-            }
+        if (commentParentId != null) {
+            where = where.and(
+                audioContentComment.parent.id.eq(commentParentId)
+                    .or(audioContentComment.id.eq(commentParentId))
+            )
         }
+
+        val response = if (commentParentId != null) {
+            queryFactory
+                .select(
+                    QFindPushTokenByContentIdAndCommentParentIdMyMemberIdResponse(
+                        member.pushToken.coalesce(""),
+                        member.container
+                    )
+                )
+                .from(audioContentComment)
+                .innerJoin(audioContentComment.audioContent, audioContent)
+                .innerJoin(audioContentComment.member, member)
+                .where(where)
+                .fetch()
+        } else {
+            queryFactory
+                .select(
+                    QFindPushTokenByContentIdAndCommentParentIdMyMemberIdResponse(
+                        member.pushToken.coalesce(""),
+                        member.container
+                    )
+                )
+                .from(audioContent)
+                .innerJoin(audioContent.member, member)
+                .where(where)
+                .fetch()
+        }
+
+        return response
     }
 }
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 8f25068..a35de3f 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
@@ -57,7 +57,8 @@ class AudioContentCommentService(
                     "콘텐츠에 댓글을 달았습니다.: ${audioContent.title}"
                 },
                 contentId = audioContentId,
-                contentCommentId = audioContentComment.id!!
+                commentParentId = parentId,
+                myMemberId = member.id
             )
         )
     }
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
deleted file mode 100644
index 6fd6d89..0000000
--- a/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/FindPushTokenByContentCommentIdResponse.kt
+++ /dev/null
@@ -1,6 +0,0 @@
-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/content/comment/FindPushTokenByContentIdAndCommentParentIdMyMemberIdResponse.kt b/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/FindPushTokenByContentIdAndCommentParentIdMyMemberIdResponse.kt
new file mode 100644
index 0000000..1876da4
--- /dev/null
+++ b/src/main/kotlin/kr/co/vividnext/sodalive/content/comment/FindPushTokenByContentIdAndCommentParentIdMyMemberIdResponse.kt
@@ -0,0 +1,8 @@
+package kr.co.vividnext.sodalive.content.comment
+
+import com.querydsl.core.annotations.QueryProjection
+
+data class FindPushTokenByContentIdAndCommentParentIdMyMemberIdResponse @QueryProjection constructor(
+    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 8b0c4d9..32a1236 100644
--- a/src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmEvent.kt
+++ b/src/main/kotlin/kr/co/vividnext/sodalive/fcm/FcmEvent.kt
@@ -25,7 +25,8 @@ class FcmEvent(
     val contentId: Long? = null,
     val messageId: Long? = null,
     val creatorId: Long? = null,
-    val contentCommentId: Long? = null
+    val commentParentId: Long? = null,
+    val myMemberId: Long? = null
 )
 
 @Component
@@ -228,17 +229,45 @@ class FcmSendListener(
             }
 
             FcmEventType.CREATE_CONTENT_COMMENT -> {
-                if (fcmEvent.contentCommentId != null && fcmEvent.contentId != null) {
-                    val response = contentCommentRepository.findPushTokenByContentCommentId(
-                        contentCommentId = fcmEvent.contentCommentId
+                if (fcmEvent.myMemberId != null && fcmEvent.contentId != null) {
+                    val response = contentCommentRepository.findPushTokenByContentIdAndCommentParentIdMyMemberId(
+                        contentId = fcmEvent.contentId,
+                        commentParentId = fcmEvent.commentParentId,
+                        myMemberId = fcmEvent.myMemberId
                     )
 
-                    if (response != null) {
+                    val iosPushTokens = response
+                        .asSequence()
+                        .distinct()
+                        .filter { it.pushToken.isNotBlank() }
+                        .filter { it.container == "ios" }
+                        .map { it.pushToken }
+                        .toList()
+
+                    val aosPushTokens = response
+                        .asSequence()
+                        .distinct()
+                        .filter { it.pushToken.isNotBlank() }
+                        .filter { it.container == "aos" }
+                        .map { it.pushToken }
+                        .toList()
+
+                    if (iosPushTokens.isNotEmpty()) {
                         pushService.send(
-                            tokens = listOf(response.pushToken),
+                            tokens = iosPushTokens,
                             title = fcmEvent.title,
                             message = fcmEvent.message,
-                            container = response.container,
+                            container = "ios",
+                            contentId = fcmEvent.contentId
+                        )
+                    }
+
+                    if (aosPushTokens.isNotEmpty()) {
+                        pushService.send(
+                            tokens = aosPushTokens,
+                            title = fcmEvent.title,
+                            message = fcmEvent.message,
+                            container = "aos",
                             contentId = fcmEvent.contentId
                         )
                     }