2021-12-29 20:33:17 +01:00
|
|
|
package io.heckel.ntfy.msg
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import io.heckel.ntfy.data.Notification
|
2021-12-29 21:36:47 +01:00
|
|
|
import io.heckel.ntfy.data.Repository
|
2021-12-29 20:33:17 +01:00
|
|
|
import io.heckel.ntfy.data.Subscription
|
2021-12-29 21:36:47 +01:00
|
|
|
import io.heckel.ntfy.up.Distributor
|
2021-12-30 01:05:32 +01:00
|
|
|
import io.heckel.ntfy.util.safeLet
|
2021-12-29 21:36:47 +01:00
|
|
|
|
|
|
|
class NotificationDispatcher(val context: Context, val repository: Repository) {
|
|
|
|
private val notifier = NotificationService(context)
|
|
|
|
private val broadcaster = BroadcastService(context)
|
|
|
|
private val distributor = Distributor(context)
|
|
|
|
|
|
|
|
fun init() {
|
|
|
|
notifier.createNotificationChannels()
|
|
|
|
}
|
2021-12-29 20:33:17 +01:00
|
|
|
|
|
|
|
fun dispatch(subscription: Subscription, notification: Notification) {
|
2021-12-29 21:36:47 +01:00
|
|
|
val muted = checkMuted(subscription)
|
|
|
|
val notify = checkNotify(subscription, notification, muted)
|
2021-12-30 01:05:32 +01:00
|
|
|
val broadcast = subscription.upAppId == null
|
|
|
|
val distribute = subscription.upAppId != null
|
2021-12-29 21:36:47 +01:00
|
|
|
if (notify) {
|
|
|
|
notifier.send(subscription, notification)
|
|
|
|
}
|
|
|
|
if (broadcast) {
|
|
|
|
broadcaster.send(subscription, notification, muted)
|
|
|
|
}
|
|
|
|
if (distribute) {
|
2021-12-30 01:05:32 +01:00
|
|
|
safeLet(subscription.upAppId, subscription.upConnectorToken) { appId, connectorToken ->
|
|
|
|
distributor.sendMessage(appId, connectorToken, notification.message)
|
|
|
|
}
|
2021-12-29 21:36:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun checkNotify(subscription: Subscription, notification: Notification, muted: Boolean): Boolean {
|
2021-12-30 17:00:27 +01:00
|
|
|
if (subscription.upAppId != null) {
|
2021-12-29 21:36:47 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
val detailsVisible = repository.detailViewSubscriptionId.get() == notification.subscriptionId
|
|
|
|
return !detailsVisible && !muted
|
|
|
|
}
|
2021-12-29 20:33:17 +01:00
|
|
|
|
2021-12-29 21:36:47 +01:00
|
|
|
private fun checkMuted(subscription: Subscription): Boolean {
|
|
|
|
if (repository.isGlobalMuted()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return subscription.mutedUntil == 1L || (subscription.mutedUntil > 1L && subscription.mutedUntil > System.currentTimeMillis()/1000)
|
2021-12-29 20:33:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val TAG = "NtfyNotificationDispatcher"
|
|
|
|
}
|
|
|
|
}
|