From 8be088ad56dbf4283e5e1ef3d47b02229e5aa85d Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 23 Jun 2023 10:29:49 +0930 Subject: [PATCH] Reinstate v1 PNServerJob --- .../notifications/FirebasePushManager.kt | 8 +-- .../securesms/notifications/PushManagerV2.kt | 14 +++-- .../messaging/jobs/NotifyPNServerJob.kt | 20 +++--- .../notifications/PushManagerV1.kt | 61 ++++++++----------- .../sending_receiving/notifications/Server.kt | 6 ++ .../libsession/snode/OnionRequestAPI.kt | 5 +- 6 files changed, 59 insertions(+), 55 deletions(-) create mode 100644 libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/Server.kt diff --git a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt index 25fed8373b..89712c2e85 100644 --- a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt +++ b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt @@ -66,7 +66,7 @@ class FirebasePushManager( firebaseInstanceIdJob = getFcmInstanceId { task -> when { - task.isSuccessful -> try { task.result?.token?.let { refresh(it, force).get() } } catch(e: Exception) { Log.d(TAG, "refresh() failed", e) } + task.isSuccessful -> try { task.result?.token?.let { refresh(it, force).get() } } catch(e: Exception) { Log.e(TAG, "refresh() failed", e) } else -> Log.w(TAG, "getFcmInstanceId failed." + task.exception) } } @@ -115,9 +115,9 @@ class FirebasePushManager( ) and pushManagerV2.register( token, publicKey, userEd25519Key, namespaces ) fail { - Log.e(TAG, "Couldn't register for FCM due to error: $it.", it) + Log.e(TAG, "registerBoth failed", it) } success { - Log.d(TAG, "register() success... saving token!!") + Log.d(TAG, "registerBoth success... saving token!!") tokenManager.fcmToken = token } @@ -128,7 +128,7 @@ class FirebasePushManager( ): Promise<*, Exception> = PushManagerV1.unregister() and pushManagerV2.unregister( token, userPublicKey, userEdKey ) fail { - Log.e(TAG, "Couldn't unregister for FCM due to error: $it.", it) + Log.e(TAG, "unregisterBoth failed", it) } success { tokenManager.fcmToken = null } diff --git a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/PushManagerV2.kt b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/PushManagerV2.kt index b63d6be5f0..66c7590e5f 100644 --- a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/PushManagerV2.kt +++ b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/PushManagerV2.kt @@ -19,6 +19,7 @@ import okhttp3.RequestBody import org.session.libsession.messaging.sending_receiving.notifications.PushManagerV1 import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationMetadata import org.session.libsession.messaging.sending_receiving.notifications.Response +import org.session.libsession.messaging.sending_receiving.notifications.Server import org.session.libsession.messaging.sending_receiving.notifications.SubscriptionRequest import org.session.libsession.messaging.sending_receiving.notifications.SubscriptionResponse import org.session.libsession.messaging.sending_receiving.notifications.UnsubscribeResponse @@ -67,7 +68,7 @@ class PushManagerV2(private val context: Context) { ).let(Json::encodeToString) return retryResponseBody("subscribe", requestParameters) success { - Log.d(TAG, "register() success!!") + Log.d(TAG, "registerV2 success") } } @@ -92,7 +93,7 @@ class PushManagerV2(private val context: Context) { ).let(Json::encodeToString) return retryResponseBody("unsubscribe", requestParameters) success { - Log.d(TAG, "unregister() success!!") + Log.d(TAG, "unregisterV2 success") } } @@ -100,14 +101,15 @@ class PushManagerV2(private val context: Context) { retryIfNeeded(FirebasePushManager.maxRetryCount) { getResponseBody(path, requestParameters) } private inline fun getResponseBody(path: String, requestParameters: String): Promise { - val url = "${PushManagerV1.server}/$path" + val server = Server.LATEST + val url = "${server.url}/$path" val body = RequestBody.create(MediaType.get("application/json"), requestParameters) val request = Request.Builder().url(url).post(body).build() return OnionRequestAPI.sendOnionRequest( request, - PushManagerV1.server, - PushManagerV1.serverPublicKey, + server.url, + server.publicKey, Version.V4 ).map { response -> response.body!!.inputStream() @@ -152,4 +154,4 @@ class PushManagerV2(private val context: Context) { return content } -} \ No newline at end of file +} diff --git a/libsession/src/main/java/org/session/libsession/messaging/jobs/NotifyPNServerJob.kt b/libsession/src/main/java/org/session/libsession/messaging/jobs/NotifyPNServerJob.kt index 7ade248fa6..6a0d049ce3 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/jobs/NotifyPNServerJob.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/jobs/NotifyPNServerJob.kt @@ -8,8 +8,7 @@ import okhttp3.Request import okhttp3.RequestBody import org.session.libsession.messaging.jobs.Job.Companion.MAX_BUFFER_SIZE -import org.session.libsession.messaging.sending_receiving.notifications.PushManagerV1 -import org.session.libsession.messaging.sending_receiving.notifications.PushManagerV1.server +import org.session.libsession.messaging.sending_receiving.notifications.Server import org.session.libsession.messaging.utilities.Data import org.session.libsession.snode.SnodeMessage import org.session.libsession.snode.OnionRequestAPI @@ -33,26 +32,27 @@ class NotifyPNServerJob(val message: SnodeMessage) : Job { } override fun execute(dispatcherName: String) { + val server = Server.LEGACY val parameters = mapOf( "data" to message.data, "send_to" to message.recipient ) - val url = "${server}/notify" + val url = "${server.url}/notify" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val request = Request.Builder().url(url).post(body).build() retryIfNeeded(4) { OnionRequestAPI.sendOnionRequest( request, - server, - PushManagerV1.serverPublicKey, + server.url, + server.publicKey, Version.V2 ) success { response -> - when (response.info["code"]) { - null, 0 -> Log.d("Loki", "Couldn't notify PN server due to error: ${response.info["message"]}.") + when (response.code) { + null, 0 -> Log.d("NotifyPNServerJob", "Couldn't notify PN server due to error: ${response.message}.") } } fail { exception -> - Log.d("Loki", "Couldn't notify PN server due to error: $exception.") + Log.d("NotifyPNServerJob", "Couldn't notify PN server due to error: $exception.") } - }.success { + } success { handleSuccess(dispatcherName) - }. fail { + } fail { handleFailure(dispatcherName, it) } } diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushManagerV1.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushManagerV1.kt index 09827ef8fa..e112cd2682 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushManagerV1.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushManagerV1.kt @@ -2,12 +2,12 @@ package org.session.libsession.messaging.sending_receiving.notifications import android.annotation.SuppressLint import nl.komponents.kovenant.Promise -import nl.komponents.kovenant.functional.map import okhttp3.MediaType import okhttp3.Request import okhttp3.RequestBody import org.session.libsession.messaging.MessagingModuleConfiguration import org.session.libsession.snode.OnionRequestAPI +import org.session.libsession.snode.OnionResponse import org.session.libsession.snode.Version import org.session.libsession.utilities.TextSecurePreferences import org.session.libsignal.utilities.JsonUtil @@ -21,12 +21,10 @@ object PushManagerV1 { private const val TAG = "PushManagerV1" val context = MessagingModuleConfiguration.shared.context - const val server = "https://push.getsession.org" - const val serverPublicKey: String = "d7557fe563e2610de876c0ac7341b62f3c82d5eea4b62c702392ea4368f51b3b" - private const val legacyServer = "https://dev.apns.getsession.org" - private const val legacyServerPublicKey = "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049" private const val maxRetryCount = 4 + private val server = Server.LEGACY + fun register( isUsingFCM: Boolean = TextSecurePreferences.isUsingFCM(context), token: String? = TextSecurePreferences.getFCMToken(context), @@ -38,17 +36,14 @@ object PushManagerV1 { } else retryIfNeeded(maxRetryCount) { doRegister(token, publicKey, legacyGroupPublicKeys) } fail { exception -> - Log.d(TAG, "Couldn't register for FCM due to error: ${exception}.") - } success { - Log.d(TAG, "register success") + Log.d(TAG, "Couldn't register for FCM due to error: $exception.") } private fun doRegister(token: String?, publicKey: String?, legacyGroupPublicKeys: Collection): Promise<*, Exception> { - Log.d(TAG, "doRegister() called") + Log.d(TAG, "registerV1 requested") token ?: return emptyPromise() publicKey ?: return emptyPromise() - legacyGroupPublicKeys.takeIf { it.isNotEmpty() } ?: return unregister() val parameters = mapOf( "token" to token, @@ -56,15 +51,16 @@ object PushManagerV1 { "legacyGroupPublicKeys" to legacyGroupPublicKeys ) - val url = "$legacyServer/register_legacy_groups_only" + val url = "${server.url}/register_legacy_groups_only" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val request = Request.Builder().url(url).post(body).build() - return OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response -> - when (response.info["code"]) { - null, 0 -> throw Exception("error: ${response.info["message"]}.") - else -> Log.d(TAG, "doRegister success") + return sendOnionRequest(request) sideEffect { response -> + when (response.code) { + null, 0 -> throw Exception("error: ${response.message}.") } + } success { + Log.d(TAG, "registerV1 success") } } @@ -72,24 +68,19 @@ object PushManagerV1 { * Unregister push notifications for 1-1 conversations as this is now done in FirebasePushManager. */ fun unregister(): Promise<*, Exception> { - Log.d(TAG, "unregister() called") + Log.d(TAG, "unregisterV1 requested") val token = TextSecurePreferences.getFCMToken(context) ?: emptyPromise() return retryIfNeeded(maxRetryCount) { val parameters = mapOf( "token" to token ) - val url = "$legacyServer/unregister" + val url = "${server.url}/unregister" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val request = Request.Builder().url(url).post(body).build() - OnionRequestAPI.sendOnionRequest( - request, - legacyServer, - legacyServerPublicKey, - Version.V2 - ) success { - when (it.info["code"]) { - null, 0 -> throw Exception("error: ${it.info["message"]}.") + sendOnionRequest(request) success { + when (it.code) { + null, 0 -> throw Exception("error: ${it.message}.") else -> Log.d(TAG, "unregisterV1 success") } } @@ -120,21 +111,23 @@ object PushManagerV1 { publicKey: String ): Promise<*, Exception> { val parameters = mapOf( "closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey ) - val url = "$legacyServer/$operation" + val url = "${server.url}/$operation" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val request = Request.Builder().url(url).post(body).build() return retryIfNeeded(maxRetryCount) { - OnionRequestAPI.sendOnionRequest( - request, - legacyServer, - legacyServerPublicKey, - Version.V2 - ) sideEffect { - when (it.info["code"]) { - 0, null -> throw Exception("${it.info["message"]}") + sendOnionRequest(request) sideEffect { + when (it.code) { + 0, null -> throw Exception(it.message) } } } } + + private fun sendOnionRequest(request: Request): Promise = OnionRequestAPI.sendOnionRequest( + request, + server.url, + server.publicKey, + Version.V2 + ) } diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/Server.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/Server.kt new file mode 100644 index 0000000000..6fefd694e0 --- /dev/null +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/Server.kt @@ -0,0 +1,6 @@ +package org.session.libsession.messaging.sending_receiving.notifications + +enum class Server(val url: String, val publicKey: String) { + LATEST("https://push.getsession.org", "d7557fe563e2610de876c0ac7341b62f3c82d5eea4b62c702392ea4368f51b3b"), + LEGACY("https://dev.apns.getsession.org", "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049") +} diff --git a/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt b/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt index 087c8e29d3..0ed3a1afe7 100644 --- a/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt +++ b/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt @@ -684,4 +684,7 @@ enum class Version(val value: String) { data class OnionResponse( val info: Map<*, *>, val body: ByteArray? = null -) +) { + val code: Int? get() = info["code"] as? Int + val message: String? get() = info["message"] as? String +}