Skip to content

Commit

Permalink
fix: fcm 버그
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaisqls committed Jul 7, 2023
1 parent 5a5b73d commit 14a4f54
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package team.aliens.dms.domain.notification.service
import team.aliens.dms.common.annotation.Service
import team.aliens.dms.domain.notification.exception.NotificationOfUserNotFoundException
import team.aliens.dms.domain.notification.model.DeviceToken
import team.aliens.dms.domain.notification.model.Topic
import team.aliens.dms.domain.notification.spi.CommandDeviceTokenPort
import team.aliens.dms.domain.notification.spi.CommandNotificationOfUserPort
import team.aliens.dms.domain.notification.spi.NotificationPort
Expand All @@ -18,8 +19,7 @@ class CommandNotificationServiceImpl(
private val notificationPort: NotificationPort,
private val queryTopicSubscriptionPort: QueryTopicSubscriptionPort,
private val queryNotificationOfUserPort: QueryNotificationOfUserPort,
private val commandNotificationOfUserPort: CommandNotificationOfUserPort,
private val notificationServiceImpl: NotificationServiceImpl
private val commandNotificationOfUserPort: CommandNotificationOfUserPort
) : CommandNotificationService {

override fun createOrUpdateDeviceToken(deviceToken: DeviceToken) {
Expand All @@ -34,14 +34,20 @@ class CommandNotificationServiceImpl(
commandDeviceTokenPort.saveDeviceToken(
deviceToken.copy(id = savedDeviceToken.id)
)
notificationServiceImpl.updateSubscribes(
token = deviceToken.token,
updateSubscribes(
deviceToken = deviceToken,
topicsToSubscribe = queryTopicSubscriptionPort.queryTopicSubscriptionsByDeviceTokenId(deviceToken.id)
.map { it.topic to it.isSubscribed }
)
}
}

private fun updateSubscribes(deviceToken: DeviceToken, topicsToSubscribe: List<Pair<Topic, Boolean>>) {
topicsToSubscribe.map { (topic, isSubscribe) ->
notificationPort.subscribeOrUnsubscribeTopic(isSubscribe, deviceToken.token, topic)
}
}

override fun deleteDeviceTokenById(deviceTokenId: UUID) {
commandDeviceTokenPort.deleteDeviceTokenById(deviceTokenId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class NotificationServiceImpl(
override fun updateSubscribes(token: String, topicsToSubscribe: List<Pair<Topic, Boolean>>) {
val deviceToken = this.getDeviceTokenByToken(token)
val topicSubscriptions = topicsToSubscribe.map { (topic, isSubscribe) ->
this.subscribeOrUnsubscribeTopic(isSubscribe, token, topic)
notificationPort.subscribeOrUnsubscribeTopic(isSubscribe, token, topic)
TopicSubscription(
deviceTokenId = deviceToken.id,
topic = topic,
Expand All @@ -63,24 +63,6 @@ class NotificationServiceImpl(
commandTopicSubscriptionPort.saveAllTopicSubscriptions(topicSubscriptions)
}

private fun subscribeOrUnsubscribeTopic(
isSubscribe: Boolean,
token: String,
topic: Topic
) {
if (isSubscribe) {
notificationPort.subscribeTopic(
token = token,
topic = topic
)
} else {
notificationPort.unsubscribeTopic(
token = token,
topic = topic
)
}
}

override fun sendMessage(deviceToken: DeviceToken, notification: Notification) {
notification.runIfSaveRequired {
notificationOfUserPort.saveNotificationOfUser(
Expand Down Expand Up @@ -117,3 +99,21 @@ class NotificationServiceImpl(
)
}
}

fun NotificationPort.subscribeOrUnsubscribeTopic(
isSubscribe: Boolean,
token: String,
topic: Topic
) {
if (isSubscribe) {
subscribeTopic(
token = token,
topic = topic
)
} else {
unsubscribeTopic(
token = token,
topic = topic
)
}
}

0 comments on commit 14a4f54

Please sign in to comment.