250 lines
9.7 KiB
Kotlin
250 lines
9.7 KiB
Kotlin
package kr.co.vividnext.sodalive.fcm
|
|
|
|
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 {
|
|
ALL, INDIVIDUAL, CREATE_LIVE, START_LIVE, CANCEL_LIVE, UPLOAD_CONTENT, SEND_MESSAGE, CHANGE_NOTICE,
|
|
CREATE_CONTENT_COMMENT
|
|
}
|
|
|
|
class FcmEvent(
|
|
val type: FcmEventType,
|
|
val title: String,
|
|
val message: String,
|
|
val container: String = "",
|
|
val recipients: List<Long> = listOf(),
|
|
val recipientsMap: Map<String, List<List<String>>>? = null,
|
|
val isAuth: Boolean? = null,
|
|
val roomId: Long? = null,
|
|
val contentId: Long? = null,
|
|
val messageId: Long? = null,
|
|
val creatorId: Long? = null,
|
|
val contentCommentId: Long? = null
|
|
)
|
|
|
|
@Component
|
|
class FcmSendListener(
|
|
private val pushService: FcmService,
|
|
private val memberRepository: MemberRepository,
|
|
private val contentCommentRepository: AudioContentCommentRepository
|
|
) {
|
|
@Async
|
|
@TransactionalEventListener
|
|
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
|
fun send(fcmEvent: FcmEvent) {
|
|
when (fcmEvent.type) {
|
|
FcmEventType.ALL -> {
|
|
if (fcmEvent.container.isNotBlank()) {
|
|
val pushTokens = memberRepository.getAllRecipientPushTokens(
|
|
fcmEvent.isAuth,
|
|
fcmEvent.container
|
|
)
|
|
|
|
for (tokens in pushTokens) {
|
|
pushService.send(
|
|
tokens = tokens,
|
|
title = fcmEvent.title,
|
|
message = fcmEvent.message,
|
|
container = fcmEvent.container
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
FcmEventType.INDIVIDUAL -> {
|
|
if (fcmEvent.recipients.isNotEmpty()) {
|
|
val pushTokens = memberRepository.getIndividualRecipientPushTokens(
|
|
recipients = fcmEvent.recipients,
|
|
isAuth = fcmEvent.isAuth
|
|
)
|
|
|
|
val iosPushTokens = pushTokens["ios"]
|
|
val aosPushToken = pushTokens["aos"]
|
|
|
|
if (iosPushTokens != null) {
|
|
for (tokens in iosPushTokens) {
|
|
pushService.send(
|
|
tokens = tokens,
|
|
title = fcmEvent.title,
|
|
message = fcmEvent.message,
|
|
container = "ios",
|
|
contentId = fcmEvent.contentId
|
|
)
|
|
}
|
|
}
|
|
|
|
if (aosPushToken != null) {
|
|
for (tokens in aosPushToken) {
|
|
pushService.send(
|
|
tokens = tokens,
|
|
title = fcmEvent.title,
|
|
message = fcmEvent.message,
|
|
container = "aos",
|
|
contentId = fcmEvent.contentId
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
FcmEventType.CREATE_LIVE -> {
|
|
if (fcmEvent.container.isNotBlank()) {
|
|
val pushTokens = memberRepository.getCreateLiveRoomNotificationRecipientPushTokens(
|
|
creatorId = fcmEvent.creatorId!!,
|
|
isAuth = fcmEvent.isAuth ?: false,
|
|
container = fcmEvent.container
|
|
)
|
|
|
|
for (tokens in pushTokens) {
|
|
pushService.send(
|
|
tokens = tokens,
|
|
title = fcmEvent.title,
|
|
message = fcmEvent.message,
|
|
container = fcmEvent.container,
|
|
roomId = fcmEvent.roomId
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
FcmEventType.START_LIVE -> {
|
|
if (fcmEvent.container.isNotBlank()) {
|
|
val pushTokens = memberRepository.getStartLiveRoomNotificationRecipientPushTokens(
|
|
creatorId = fcmEvent.creatorId!!,
|
|
roomId = fcmEvent.roomId!!,
|
|
isAuth = fcmEvent.isAuth ?: false,
|
|
container = fcmEvent.container
|
|
)
|
|
|
|
for (tokens in pushTokens) {
|
|
pushService.send(
|
|
tokens = tokens,
|
|
title = fcmEvent.title,
|
|
message = fcmEvent.message,
|
|
container = fcmEvent.container,
|
|
roomId = fcmEvent.roomId
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
FcmEventType.CANCEL_LIVE -> {
|
|
if (fcmEvent.recipientsMap != null) {
|
|
val iosPushTokens = fcmEvent.recipientsMap["ios"]
|
|
val aosPushToken = fcmEvent.recipientsMap["aos"]
|
|
if (iosPushTokens != null) {
|
|
for (tokens in iosPushTokens) {
|
|
pushService.send(
|
|
tokens = tokens,
|
|
title = fcmEvent.title,
|
|
message = fcmEvent.message,
|
|
container = "ios"
|
|
)
|
|
}
|
|
}
|
|
|
|
if (aosPushToken != null) {
|
|
for (tokens in aosPushToken) {
|
|
pushService.send(
|
|
tokens = tokens,
|
|
title = fcmEvent.title,
|
|
message = fcmEvent.message,
|
|
container = "aos"
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
FcmEventType.UPLOAD_CONTENT -> {
|
|
if (fcmEvent.container.isNotBlank()) {
|
|
val pushTokens = memberRepository.getUploadContentNotificationRecipientPushTokens(
|
|
creatorId = fcmEvent.creatorId!!,
|
|
isAuth = fcmEvent.isAuth ?: false,
|
|
container = fcmEvent.container
|
|
)
|
|
|
|
for (tokens in pushTokens) {
|
|
pushService.send(
|
|
tokens = tokens,
|
|
title = fcmEvent.title,
|
|
message = fcmEvent.message,
|
|
container = fcmEvent.container,
|
|
contentId = fcmEvent.contentId
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
FcmEventType.SEND_MESSAGE -> {
|
|
val response = memberRepository.getMessageRecipientPushToken(messageId = fcmEvent.messageId!!)
|
|
|
|
pushService.send(
|
|
tokens = listOf(response.pushToken),
|
|
title = fcmEvent.title,
|
|
message = fcmEvent.message,
|
|
container = response.container,
|
|
messageId = fcmEvent.messageId
|
|
)
|
|
}
|
|
|
|
FcmEventType.CHANGE_NOTICE -> {
|
|
if (fcmEvent.creatorId != null) {
|
|
val pushTokenList = memberRepository.getChangeNoticeRecipientPushTokens(fcmEvent.creatorId)
|
|
|
|
val iosPushTokens = pushTokenList["ios"]
|
|
val aosPushToken = pushTokenList["aos"]
|
|
|
|
if (iosPushTokens != null) {
|
|
for (tokens in iosPushTokens) {
|
|
pushService.send(
|
|
tokens = tokens,
|
|
title = fcmEvent.title,
|
|
message = fcmEvent.message,
|
|
container = "ios",
|
|
creatorId = fcmEvent.creatorId
|
|
)
|
|
}
|
|
}
|
|
|
|
if (aosPushToken != null) {
|
|
for (tokens in aosPushToken) {
|
|
pushService.send(
|
|
tokens = tokens,
|
|
title = fcmEvent.title,
|
|
message = fcmEvent.message,
|
|
container = "aos",
|
|
creatorId = fcmEvent.creatorId
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|