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,15 +40,17 @@ 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)
OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json -> retryIfNeeded(maxRetryCount) {
val code = json["code"] as? Int OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json ->
if (code != null && code != 0) { val code = json["code"] as? Int
TextSecurePreferences.setIsUsingFCM(context, false) if (code != null && code != 0) {
} else { TextSecurePreferences.setIsUsingFCM(context, false)
Log.d("Loki", "Couldn't disable FCM due to error: ${json["message"] as? String ?: "null"}.") } 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 // Unsubscribe from all closed groups
val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys() val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()
@ -65,17 +69,19 @@ 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)
OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json -> retryIfNeeded(maxRetryCount) {
val code = json["code"] as? Int OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json ->
if (code != null && code != 0) { val code = json["code"] as? Int
TextSecurePreferences.setIsUsingFCM(context, true) if (code != null && code != 0) {
TextSecurePreferences.setFCMToken(context, token) TextSecurePreferences.setIsUsingFCM(context, true)
TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis()) TextSecurePreferences.setFCMToken(context, token)
} else { TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis())
Log.d("Loki", "Couldn't register for FCM due to error: ${json["message"] as? String ?: "null"}.") } 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 // Subscribe to all closed groups
val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys() val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()
@ -91,13 +97,15 @@ 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)
OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json -> retryIfNeeded(maxRetryCount) {
val code = json["code"] as? Int OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey).map { json ->
if (code == null || code == 0) { val code = json["code"] as? Int
Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${json["message"] as? String ?: "null"}.") 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}.")
} }
} }
} }