From 0e39374b7bf1d65da35cb5f8fc3ece0d50bae63f Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Wed, 7 Oct 2020 15:56:55 +1100 Subject: [PATCH] Retry PN server requests if needed --- .../loki/api/LokiPushNotificationManager.kt | 56 +++++++++++-------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/src/org/thoughtcrime/securesms/loki/api/LokiPushNotificationManager.kt b/src/org/thoughtcrime/securesms/loki/api/LokiPushNotificationManager.kt index 1b57b0b6d6..238c7efd6b 100644 --- a/src/org/thoughtcrime/securesms/loki/api/LokiPushNotificationManager.kt +++ b/src/org/thoughtcrime/securesms/loki/api/LokiPushNotificationManager.kt @@ -9,8 +9,10 @@ import org.whispersystems.libsignal.logging.Log import org.whispersystems.signalservice.internal.util.JsonUtil import org.whispersystems.signalservice.loki.api.PushNotificationAPI import org.whispersystems.signalservice.loki.api.onionrequests.OnionRequestAPI +import org.whispersystems.signalservice.loki.utilities.retryIfNeeded object LokiPushNotificationManager { + private val maxRetryCount = 4 private val tokenExpirationInterval = 12 * 60 * 60 * 1000 private val server by lazy { @@ -38,15 +40,17 @@ object LokiPushNotificationManager { val url = "$server/unregister" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val request = Request.Builder().url(url).post(body) - OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json -> - val code = json["code"] as? Int - if (code != null && code != 0) { - TextSecurePreferences.setIsUsingFCM(context, false) - } else { - Log.d("Loki", "Couldn't disable FCM due to error: ${json["message"] as? String ?: "null"}.") + retryIfNeeded(maxRetryCount) { + OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json -> + val code = json["code"] as? Int + if (code != null && code != 0) { + TextSecurePreferences.setIsUsingFCM(context, false) + } else { + Log.d("Loki", "Couldn't disable FCM due to error: ${json["message"] as? String ?: "null"}.") + } + }.fail { exception -> + Log.d("Loki", "Couldn't disable FCM due to error: ${exception}.") } - }.fail { exception -> - Log.d("Loki", "Couldn't disable FCM due to error: ${exception}.") } // Unsubscribe from all closed groups val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys() @@ -65,17 +69,19 @@ object LokiPushNotificationManager { val url = "$server/register" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val request = Request.Builder().url(url).post(body) - OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json -> - val code = json["code"] as? Int - if (code != null && code != 0) { - TextSecurePreferences.setIsUsingFCM(context, true) - TextSecurePreferences.setFCMToken(context, token) - TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis()) - } else { - Log.d("Loki", "Couldn't register for FCM due to error: ${json["message"] as? String ?: "null"}.") + retryIfNeeded(maxRetryCount) { + OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json -> + val code = json["code"] as? Int + if (code != null && code != 0) { + TextSecurePreferences.setIsUsingFCM(context, true) + TextSecurePreferences.setFCMToken(context, token) + TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis()) + } else { + Log.d("Loki", "Couldn't register for FCM due to error: ${json["message"] as? String ?: "null"}.") + } + }.fail { exception -> + Log.d("Loki", "Couldn't register for FCM due to error: ${exception}.") } - }.fail { exception -> - Log.d("Loki", "Couldn't register for FCM due to error: ${exception}.") } // Subscribe to all closed groups val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys() @@ -91,13 +97,15 @@ object LokiPushNotificationManager { val url = "$server/${operation.rawValue}" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val request = Request.Builder().url(url).post(body) - OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json -> - val code = json["code"] as? Int - if (code == null || code == 0) { - Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${json["message"] as? String ?: "null"}.") + retryIfNeeded(maxRetryCount) { + OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json -> + val code = json["code"] as? Int + if (code == null || code == 0) { + Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${json["message"] as? String ?: "null"}.") + } + }.fail { exception -> + Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${exception}.") } - }.fail { exception -> - Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${exception}.") } } }