Add Last-Event-ID header

This commit is contained in:
sim 2024-11-04 14:54:09 +00:00
parent 9541ed6aed
commit 67a39da534
3 changed files with 20 additions and 0 deletions

View File

@ -14,12 +14,22 @@ class AppStore(context: Context) : Store(context) {
.putOrRemove(PREF_DEVICE_ID, value)
.apply()
var lastEventId: String?
get() = sharedPreferences
.getString(PREF_LAST_EVENT_ID, null)
set(value) = sharedPreferences
.edit()
.putOrRemove(PREF_LAST_EVENT_ID, value)
.apply()
override fun wipe() {
deviceId = null
lastEventId = null
account.wipe()
}
companion object {
private const val PREF_DEVICE_ID = "deviceId"
private const val PREF_LAST_EVENT_ID = "lastEventId"
}
}

View File

@ -92,6 +92,11 @@ class Api(context: Context) {
val request = Request.Builder().url(url)
.get()
.apply {
store.lastEventId?.let {
header("Last-Event-ID", it)
}
}
.build()
syncSource.getAndSet(
@ -132,6 +137,7 @@ class Api(context: Context) {
}
})
store.deviceId = null
store.lastEventId = null
}
} catch (e: NoProviderException) {
e.printStackTrace()

View File

@ -8,6 +8,7 @@ import okhttp3.Response
import okhttp3.sse.EventSource
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.WakeLock
import org.unifiedpush.distributor.nextpush.api.response.SSEResponse
@ -30,6 +31,8 @@ import kotlin.concurrent.schedule
class SSEListener(val context: Context) : EventSourceListener() {
val store = AppStore(context)
override var timeout: Long? = AppCompanion.keepalive.get().toLong() + TIMEOUT_TOLERANCE
override fun onOpen(eventSource: EventSource, response: Response) {
@ -82,6 +85,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
message.token,
Base64.decode(message.message, Base64.DEFAULT)
)
store.lastEventId = id
}
"deleteApp" -> {