2021-11-12 13:41:29 +13:00
|
|
|
package io.heckel.ntfy.work
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import androidx.work.CoroutineWorker
|
|
|
|
import androidx.work.WorkerParameters
|
|
|
|
import io.heckel.ntfy.BuildConfig
|
|
|
|
import io.heckel.ntfy.data.Database
|
|
|
|
import io.heckel.ntfy.data.Repository
|
2022-01-17 18:19:05 +13:00
|
|
|
import io.heckel.ntfy.log.Log
|
2021-11-12 13:41:29 +13:00
|
|
|
import io.heckel.ntfy.msg.ApiService
|
2021-12-30 09:36:47 +13:00
|
|
|
import io.heckel.ntfy.msg.NotificationDispatcher
|
2021-11-12 13:41:29 +13:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.withContext
|
2021-11-16 10:24:31 +13:00
|
|
|
import kotlin.random.Random
|
2021-11-12 13:41:29 +13:00
|
|
|
|
|
|
|
class PollWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx, params) {
|
2021-11-23 09:45:43 +13:00
|
|
|
// IMPORTANT WARNING:
|
|
|
|
// Every time the worker is changed, the periodic work has to be REPLACEd.
|
|
|
|
// This is facilitated in the MainActivity using the VERSION below.
|
2021-11-12 13:41:29 +13:00
|
|
|
|
2022-01-17 18:19:05 +13:00
|
|
|
init {
|
|
|
|
Log.init(ctx) // Init in all entrypoints
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:41:29 +13:00
|
|
|
override suspend fun doWork(): Result {
|
2022-01-17 18:19:05 +13:00
|
|
|
|
2021-11-12 13:41:29 +13:00
|
|
|
return withContext(Dispatchers.IO) {
|
|
|
|
Log.d(TAG, "Polling for new notifications")
|
|
|
|
val database = Database.getInstance(applicationContext)
|
2021-11-23 09:45:43 +13:00
|
|
|
val sharedPrefs = applicationContext.getSharedPreferences(Repository.SHARED_PREFS_ID, Context.MODE_PRIVATE)
|
|
|
|
val repository = Repository.getInstance(sharedPrefs, database.subscriptionDao(), database.notificationDao())
|
2021-12-30 09:36:47 +13:00
|
|
|
val dispatcher = NotificationDispatcher(applicationContext, repository)
|
2021-11-12 13:41:29 +13:00
|
|
|
val api = ApiService()
|
|
|
|
|
2021-12-12 13:40:32 +13:00
|
|
|
repository.getSubscriptions().forEach{ subscription ->
|
|
|
|
try {
|
2022-01-02 13:37:09 +13:00
|
|
|
val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic, since = subscription.lastActive)
|
2021-11-16 10:24:31 +13:00
|
|
|
val newNotifications = repository
|
|
|
|
.onlyNewNotifications(subscription.id, notifications)
|
|
|
|
.map { it.copy(notificationId = Random.nextInt()) }
|
2021-11-12 13:41:29 +13:00
|
|
|
newNotifications.forEach { notification ->
|
2021-12-30 09:36:47 +13:00
|
|
|
if (repository.addNotification(notification)) {
|
|
|
|
dispatcher.dispatch(subscription, notification)
|
2021-12-12 09:09:07 +13:00
|
|
|
}
|
2021-11-12 13:41:29 +13:00
|
|
|
}
|
2021-12-12 13:40:32 +13:00
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.e(TAG, "Failed checking messages: ${e.message}", e)
|
2021-11-12 13:41:29 +13:00
|
|
|
}
|
|
|
|
}
|
2021-12-12 13:40:32 +13:00
|
|
|
Log.d(TAG, "Finished polling for new notifications")
|
|
|
|
return@withContext Result.success()
|
2021-11-12 13:41:29 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
2021-12-12 13:40:32 +13:00
|
|
|
const val VERSION = BuildConfig.VERSION_CODE
|
2021-11-12 13:41:29 +13:00
|
|
|
const val TAG = "NtfyPollWorker"
|
2022-01-02 13:37:09 +13:00
|
|
|
const val WORK_NAME_PERIODIC = "NtfyPollWorkerPeriodic" // Do not change
|
2021-11-12 13:41:29 +13:00
|
|
|
}
|
|
|
|
}
|