-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushService.kt
136 lines (113 loc) · 4.5 KB
/
PushService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.wafflestudio.snutt.notification.service
import com.wafflestudio.snutt.common.push.PushClient
import com.wafflestudio.snutt.common.push.dto.PushMessage
import com.wafflestudio.snutt.common.push.dto.TargetedPushMessageWithToken
import com.wafflestudio.snutt.notification.data.PushCategory
import com.wafflestudio.snutt.notification.repository.PushOptOutRepository
import org.springframework.stereotype.Service
/**
* 유저 기기에 푸시를 보내는 서비스입니다.
*/
interface PushService {
suspend fun sendPush(
pushMessage: PushMessage,
userId: String,
)
suspend fun sendPushes(
pushMessage: PushMessage,
userIds: List<String>,
)
suspend fun sendGlobalPush(pushMessage: PushMessage)
suspend fun sendTargetPushes(userToPushMessage: Map<String, PushMessage>)
suspend fun sendCategoricalPush(
pushMessage: PushMessage,
userId: String,
pushCategory: PushCategory,
)
suspend fun sendCategoricalPushes(
pushMessage: PushMessage,
userIds: List<String>,
pushCategory: PushCategory,
)
suspend fun sendCategoricalTargetPushes(
userToPushMessage: Map<String, PushMessage>,
pushCategory: PushCategory,
)
}
@Service
class PushServiceImpl internal constructor(
private val deviceService: DeviceService,
private val pushClient: PushClient,
private val pushOptOutRepository: PushOptOutRepository,
) : PushService {
override suspend fun sendPush(
pushMessage: PushMessage,
userId: String,
) {
val userDevices = deviceService.getUserDevices(userId).ifEmpty { return }
val targetedPushMessageWithTokens = userDevices.map { TargetedPushMessageWithToken(it.fcmRegistrationId, pushMessage) }
pushClient.sendMessages(targetedPushMessageWithTokens)
}
override suspend fun sendPushes(
pushMessage: PushMessage,
userIds: List<String>,
) {
val userIdToDevices = deviceService.getUsersDevices(userIds).ifEmpty { return }
val targetedPushMessageWithTokens =
userIdToDevices.values.flatMap { userDevices ->
userDevices.map { TargetedPushMessageWithToken(it.fcmRegistrationId, pushMessage) }
}
pushClient.sendMessages(targetedPushMessageWithTokens)
}
override suspend fun sendGlobalPush(pushMessage: PushMessage) = pushClient.sendGlobalMessage(pushMessage)
override suspend fun sendTargetPushes(userToPushMessage: Map<String, PushMessage>) =
userToPushMessage.entries.flatMap { (userId, pushMessage) ->
deviceService.getUserDevices(userId).map { it.fcmRegistrationId to pushMessage }
}.map { (fcmRegistrationId, message) -> TargetedPushMessageWithToken(fcmRegistrationId, message) }
.let { pushClient.sendMessages(it) }
override suspend fun sendCategoricalPush(
pushMessage: PushMessage,
userId: String,
pushCategory: PushCategory,
) {
if (pushCategory == PushCategory.NORMAL || !pushOptOutRepository.existsByUserIdAndPushCategory(userId, pushCategory)) {
sendPush(pushMessage, userId)
}
}
override suspend fun sendCategoricalPushes(
pushMessage: PushMessage,
userIds: List<String>,
pushCategory: PushCategory,
) {
if (pushCategory == PushCategory.NORMAL) {
sendPushes(pushMessage, userIds)
}
val filteredUserIds =
pushOptOutRepository
.findByUserIdInAndPushCategory(userIds, pushCategory)
.map { it.userId }
.toSet()
.let { optOutUserIds -> userIds.filterNot { it in optOutUserIds } }
if (filteredUserIds.isNotEmpty()) {
sendPushes(pushMessage, filteredUserIds)
}
}
override suspend fun sendCategoricalTargetPushes(
userToPushMessage: Map<String, PushMessage>,
pushCategory: PushCategory,
) {
if (pushCategory == PushCategory.NORMAL) {
sendTargetPushes(userToPushMessage)
}
val userIds = userToPushMessage.keys.toList()
val filteredUserToPushMessage =
pushOptOutRepository
.findByUserIdInAndPushCategory(userIds, pushCategory)
.map { it.userId }
.toSet()
.let { optOutUserIds -> userToPushMessage.filterKeys { it !in optOutUserIds } }
if (filteredUserToPushMessage.isNotEmpty()) {
sendTargetPushes(filteredUserToPushMessage)
}
}
}