Change poller

This commit is contained in:
Philipp Heckel 2026-01-15 20:57:14 -05:00
parent 1eeabe21c9
commit 700abcb3ca
5 changed files with 19 additions and 45 deletions

View file

@ -4,6 +4,7 @@ import android.content.Context
import com.google.gson.Gson
import io.heckel.ntfy.db.Notification
import io.heckel.ntfy.db.Repository
import io.heckel.ntfy.db.Subscription
import io.heckel.ntfy.db.User
import io.heckel.ntfy.service.NotAuthorizedException
import io.heckel.ntfy.util.ALL_PRIORITIES
@ -113,11 +114,15 @@ class ApiService(private val context: Context) {
}
}
suspend fun poll(subscriptionId: Long, baseUrl: String, topic: String, user: User?, since: String? = null): List<Notification> {
val sinceVal = since ?: "all"
suspend fun poll(subscription: Subscription): List<Notification> {
val subscriptionId = subscription.id
val baseUrl = subscription.baseUrl
val topic = subscription.topic
val sinceVal = subscription.lastNotificationId ?: "all"
val url = topicUrlJsonPoll(baseUrl, topic, sinceVal)
Log.d(TAG, "Polling topic $url")
val user = repository.getUser(baseUrl)
val customHeaders = repository.getCustomHeaders(baseUrl)
val request = HttpUtil.requestBuilder(url, user, customHeaders).build()
HttpUtil.defaultClient(context, baseUrl).newCall(request).execute().use { response ->

View file

@ -3,9 +3,7 @@ package io.heckel.ntfy.msg
import io.heckel.ntfy.db.Notification
import io.heckel.ntfy.db.Repository
import io.heckel.ntfy.db.Subscription
import io.heckel.ntfy.db.User
import io.heckel.ntfy.util.Log
import io.heckel.ntfy.util.deriveNotificationId
/**
* Polls the server for notifications and updates the repository.
@ -21,21 +19,9 @@ class Poller(
* Returns the list of new notifications that were added.
*
* @param subscription The subscription to poll
* @param user The user for authentication (may be null)
* @param since The message ID to poll since (null for all cached messages)
*/
suspend fun poll(
subscription: Subscription,
user: User?,
since: String? = null
): List<Notification> {
val notifications = api.poll(
subscriptionId = subscription.id,
baseUrl = subscription.baseUrl,
topic = subscription.topic,
user = user,
since = since
)
suspend fun poll(subscription: Subscription): List<Notification> {
val notifications = api.poll(subscription)
return processNotifications(subscription.id, notifications)
}

View file

@ -232,8 +232,7 @@ class DetailActivity : AppCompatActivity(), NotificationFragment.NotificationSet
// Fetch cached messages
try {
val user = repository.getUser(subscription.baseUrl) // May be null
poller.poll(subscription, user)
poller.poll(subscription)
} catch (e: Exception) {
Log.e(TAG, "Unable to fetch notifications: ${e.message}", e)
}
@ -723,16 +722,11 @@ class DetailActivity : AppCompatActivity(), NotificationFragment.NotificationSet
lifecycleScope.launch(Dispatchers.IO) {
try {
val subscription = repository.getSubscription(subscriptionId) ?: return@launch
val user = repository.getUser(subscription.baseUrl) // May be null
val addedNotifications = poller.poll(
subscription = subscription,
user = user,
since = subscription.lastNotificationId
)
val toastMessage = if (addedNotifications.isEmpty()) {
val newNotifications = poller.poll(subscription)
val toastMessage = if (newNotifications.isEmpty()) {
getString(R.string.refresh_message_no_results)
} else {
getString(R.string.refresh_message_result, addedNotifications.size)
getString(R.string.refresh_message_result, newNotifications.size)
}
runOnUiThread {
Toast.makeText(this@DetailActivity, toastMessage, Toast.LENGTH_LONG).show()

View file

@ -691,9 +691,8 @@ class MainActivity : AppCompatActivity(), AddFragment.SubscribeListener, Notific
// Fetch cached messages
lifecycleScope.launch(Dispatchers.IO) {
try {
val user = repository.getUser(subscription.baseUrl) // May be null
val addedNotifications = poller.poll(subscription, user)
addedNotifications.forEach { notification ->
val notifications = poller.poll(subscription)
notifications.forEach { notification ->
if (notification.icon != null) {
DownloadManager.enqueue(this@MainActivity, notification.id, userAction = false, DownloadType.ICON)
}
@ -732,14 +731,9 @@ class MainActivity : AppCompatActivity(), AddFragment.SubscribeListener, Notific
repository.getSubscriptions().forEach { subscription ->
Log.d(TAG, "Polling subscription: $subscription")
try {
val user = repository.getUser(subscription.baseUrl) // May be null
val addedNotifications = poller.poll(
subscription = subscription,
user = user,
since = subscription.lastNotificationId
)
newNotificationsCount += addedNotifications.size
addedNotifications.forEach { notification ->
val newNotifications = poller.poll(subscription)
newNotificationsCount += newNotifications.size
newNotifications.forEach { notification ->
dispatcher?.dispatch(subscription, notification)
}
} catch (e: Exception) {

View file

@ -40,12 +40,7 @@ class PollWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx,
subscriptions.forEach{ subscription ->
try {
val user = repository.getUser(subscription.baseUrl)
val newNotifications = poller.poll(
subscription = subscription,
user = user,
since = subscription.lastNotificationId
)
val newNotifications = poller.poll(subscription)
newNotifications.forEach { notification ->
dispatcher.dispatch(subscription, notification)
}