Retry PN server requests if needed

This commit is contained in:
Niels Andriesse 2020-10-07 15:56:55 +11:00
parent cd177ee61e
commit 0e39374b7b

View File

@ -9,8 +9,10 @@ import org.whispersystems.libsignal.logging.Log
import org.whispersystems.signalservice.internal.util.JsonUtil import org.whispersystems.signalservice.internal.util.JsonUtil
import org.whispersystems.signalservice.loki.api.PushNotificationAPI import org.whispersystems.signalservice.loki.api.PushNotificationAPI
import org.whispersystems.signalservice.loki.api.onionrequests.OnionRequestAPI import org.whispersystems.signalservice.loki.api.onionrequests.OnionRequestAPI
import org.whispersystems.signalservice.loki.utilities.retryIfNeeded
object LokiPushNotificationManager { object LokiPushNotificationManager {
private val maxRetryCount = 4
private val tokenExpirationInterval = 12 * 60 * 60 * 1000 private val tokenExpirationInterval = 12 * 60 * 60 * 1000
private val server by lazy { private val server by lazy {
@ -38,6 +40,7 @@ object LokiPushNotificationManager {
val url = "$server/unregister" val url = "$server/unregister"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
val request = Request.Builder().url(url).post(body) val request = Request.Builder().url(url).post(body)
retryIfNeeded(maxRetryCount) {
OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json -> OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json ->
val code = json["code"] as? Int val code = json["code"] as? Int
if (code != null && code != 0) { if (code != null && code != 0) {
@ -48,6 +51,7 @@ object LokiPushNotificationManager {
}.fail { exception -> }.fail { exception ->
Log.d("Loki", "Couldn't disable FCM due to error: ${exception}.") Log.d("Loki", "Couldn't disable FCM due to error: ${exception}.")
} }
}
// Unsubscribe from all closed groups // Unsubscribe from all closed groups
val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys() val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()
val userPublicKey = TextSecurePreferences.getLocalNumber(context) val userPublicKey = TextSecurePreferences.getLocalNumber(context)
@ -65,6 +69,7 @@ object LokiPushNotificationManager {
val url = "$server/register" val url = "$server/register"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
val request = Request.Builder().url(url).post(body) val request = Request.Builder().url(url).post(body)
retryIfNeeded(maxRetryCount) {
OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json -> OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json ->
val code = json["code"] as? Int val code = json["code"] as? Int
if (code != null && code != 0) { if (code != null && code != 0) {
@ -77,6 +82,7 @@ object LokiPushNotificationManager {
}.fail { exception -> }.fail { exception ->
Log.d("Loki", "Couldn't register for FCM due to error: ${exception}.") Log.d("Loki", "Couldn't register for FCM due to error: ${exception}.")
} }
}
// Subscribe to all closed groups // Subscribe to all closed groups
val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys() val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()
allClosedGroupPublicKeys.forEach { closedGroup -> allClosedGroupPublicKeys.forEach { closedGroup ->
@ -91,6 +97,7 @@ object LokiPushNotificationManager {
val url = "$server/${operation.rawValue}" val url = "$server/${operation.rawValue}"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
val request = Request.Builder().url(url).post(body) val request = Request.Builder().url(url).post(body)
retryIfNeeded(maxRetryCount) {
OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json -> OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json ->
val code = json["code"] as? Int val code = json["code"] as? Int
if (code == null || code == 0) { if (code == null || code == 0) {
@ -101,3 +108,4 @@ object LokiPushNotificationManager {
} }
} }
} }
}