Save last event id per urgency

This commit is contained in:
sim 2024-11-28 14:45:24 +00:00
parent 99db47a16c
commit 3d4b0e26ec
4 changed files with 67 additions and 8 deletions

View File

@ -14,22 +14,52 @@ class AppStore(context: Context) : Store(context) {
.putOrRemove(PREF_DEVICE_ID, value)
.apply()
var lastEventId: String?
var lastHighEventId: String?
get() = sharedPreferences
.getString(PREF_LAST_EVENT_ID, null)
.getString(PREF_LAST_EVENT_ID_HIGH, null)
set(value) = sharedPreferences
.edit()
.putOrRemove(PREF_LAST_EVENT_ID, value)
.putOrRemove(PREF_LAST_EVENT_ID_HIGH, value)
.apply()
var lastNormalEventId: String?
get() = sharedPreferences
.getString(PREF_LAST_EVENT_ID_NORMAL, null)
set(value) = sharedPreferences
.edit()
.putOrRemove(PREF_LAST_EVENT_ID_NORMAL, value)
.apply()
var lastLowEventId: String?
get() = sharedPreferences
.getString(PREF_LAST_EVENT_ID_LOW, null)
set(value) = sharedPreferences
.edit()
.putOrRemove(PREF_LAST_EVENT_ID_LOW, value)
.apply()
var lastVeryLowEventId: String?
get() = sharedPreferences
.getString(PREF_LAST_EVENT_ID_VERY_LOW, null)
set(value) = sharedPreferences
.edit()
.putOrRemove(PREF_LAST_EVENT_ID_VERY_LOW, value)
.apply()
override fun wipe() {
deviceId = null
lastEventId = null
lastHighEventId = null
lastNormalEventId = null
lastLowEventId = null
lastVeryLowEventId = null
account.wipe()
}
companion object {
private const val PREF_DEVICE_ID = "deviceId"
private const val PREF_LAST_EVENT_ID = "lastEventId"
private const val PREF_LAST_EVENT_ID_HIGH = "lastEventId.high"
private const val PREF_LAST_EVENT_ID_NORMAL = "lastEventId.normal"
private const val PREF_LAST_EVENT_ID_LOW = "lastEventId.low"
private const val PREF_LAST_EVENT_ID_VERY_LOW = "lastEventId.very-low"
}
}

View File

@ -0,0 +1,28 @@
package org.unifiedpush.distributor.nextpush
class LastEventId(private val store: AppStore) {
fun save(id: String) {
when {
id.startsWith("high.") -> store.lastHighEventId = id
id.startsWith("normal.") -> store.lastNormalEventId = id
id.startsWith("low.") -> store.lastLowEventId = id
id.startsWith("very-low.") -> store.lastVeryLowEventId = id
}
}
fun get(): String? {
return arrayOf(
store.lastHighEventId,
store.lastNormalEventId,
store.lastLowEventId,
store.lastVeryLowEventId
).filterNotNull().let {
if (it.isEmpty()) {
null
} else {
it.joinToString(",")
}
}
}
}

View File

@ -15,6 +15,7 @@ import okhttp3.Request
import okhttp3.sse.EventSource
import okhttp3.sse.EventSources
import org.unifiedpush.distributor.nextpush.AppStore
import org.unifiedpush.distributor.nextpush.LastEventId
import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount
import org.unifiedpush.distributor.nextpush.account.AccountType
import org.unifiedpush.distributor.nextpush.api.provider.*
@ -95,7 +96,7 @@ class Api(context: Context) {
val request = Request.Builder().url(url)
.get()
.apply {
store.lastEventId?.let {
LastEventId(store).get()?.let {
header("Last-Event-ID", it)
}
}
@ -139,7 +140,6 @@ class Api(context: Context) {
}
})
store.deviceId = null
store.lastEventId = null
}
} catch (e: NoProviderException) {
e.printStackTrace()

View File

@ -16,6 +16,7 @@ import okhttp3.sse.EventSourceListener
import org.unifiedpush.distributor.nextpush.AppCompanion
import org.unifiedpush.distributor.nextpush.AppStore
import org.unifiedpush.distributor.nextpush.Database
import org.unifiedpush.distributor.nextpush.LastEventId
import org.unifiedpush.distributor.nextpush.WakeLock
import org.unifiedpush.distributor.nextpush.api.response.SSEResponse
import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteAppFromSSE
@ -85,7 +86,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
message.token,
Base64.decode(message.message, Base64.DEFAULT)
)
store.lastEventId = id
id?.let { LastEventId(store).save(it) }
}
"deleteApp" -> {