From 67a39da5342c1419ac895e640ba47b5a511e0a55 Mon Sep 17 00:00:00 2001 From: sim Date: Mon, 4 Nov 2024 14:54:09 +0000 Subject: [PATCH] Add Last-Event-ID header --- .../org/unifiedpush/distributor/nextpush/AppStore.kt | 10 ++++++++++ .../org/unifiedpush/distributor/nextpush/api/Api.kt | 6 ++++++ .../distributor/nextpush/api/SSEListener.kt | 4 ++++ 3 files changed, 20 insertions(+) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/AppStore.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/AppStore.kt index 11c3fad..7148fec 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/AppStore.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/AppStore.kt @@ -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" } } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt index f75aed8..047bdb0 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt @@ -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() diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt index ca443b9..38ab3c1 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt @@ -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" -> {