diff --git a/src/org/thoughtcrime/securesms/loki/api/LokiPushNotificationManager.kt b/src/org/thoughtcrime/securesms/loki/api/LokiPushNotificationManager.kt index 39e9f57b3c..0f12ecd845 100644 --- a/src/org/thoughtcrime/securesms/loki/api/LokiPushNotificationManager.kt +++ b/src/org/thoughtcrime/securesms/loki/api/LokiPushNotificationManager.kt @@ -1,27 +1,24 @@ package org.thoughtcrime.securesms.loki.api import android.content.Context +import nl.komponents.kovenant.functional.map import okhttp3.* import org.thoughtcrime.securesms.database.DatabaseFactory -import org.thoughtcrime.securesms.jobmanager.Data -import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint -import org.thoughtcrime.securesms.jobs.BaseJob -import org.thoughtcrime.securesms.loki.protocol.ClosedGroupUpdateMessageSendJob import org.thoughtcrime.securesms.util.TextSecurePreferences import org.whispersystems.libsignal.logging.Log import org.whispersystems.signalservice.internal.util.JsonUtil import org.whispersystems.signalservice.loki.api.PushNotificationAcknowledgement -import java.io.IOException -import java.lang.Exception -import java.util.concurrent.TimeUnit +import org.whispersystems.signalservice.loki.api.onionrequests.OnionRequestAPI object LokiPushNotificationManager { - private val connection = OkHttpClient() private val tokenExpirationInterval = 12 * 60 * 60 * 1000 private val server by lazy { PushNotificationAcknowledgement.shared.server } + private val pnServerPublicKey by lazy { + PushNotificationAcknowledgement.pnServerPublicKey + } enum class ClosedGroupOperation { Subscribe, Unsubscribe; @@ -38,34 +35,19 @@ object LokiPushNotificationManager { @JvmStatic fun unregister(token: String, context: Context) { val parameters = mapOf( "token" to token ) - val url = "$server/register" + val url = "$server/unregister" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) - val request = Request.Builder().url(url).post(body).build() - connection.newCall(request).enqueue(object : Callback { - - override fun onResponse(call: Call, response: Response) { - when (response.code()) { - 200 -> { - val bodyAsString = response.body()!!.string() - val json = JsonUtil.fromJson(bodyAsString, Map::class.java) - val code = json?.get("code") as? Int - if (code != null && code != 0) { - TextSecurePreferences.setIsUsingFCM(context, false) - Log.d("Loki", "Successfully unregistered from FCM.") - } else { - Log.d("Loki", "Couldn't disable FCM due to error: ${json?.get("message") as? String ?: "null"}.") - } - } - else -> { - Log.d("Loki", "Couldn't disable FCM.") - } - } + 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"}.") } - - override fun onFailure(call: Call, exception: IOException) { - 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() val userPublicKey = TextSecurePreferences.getLocalNumber(context) @@ -82,34 +64,19 @@ object LokiPushNotificationManager { val parameters = mapOf( "token" to token, "pubKey" to publicKey ) val url = "$server/register" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) - val request = Request.Builder().url(url).post(body).build() - connection.newCall(request).enqueue(object : Callback { - - override fun onResponse(call: Call, response: Response) { - when (response.code()) { - 200 -> { - val bodyAsString = response.body()!!.string() - val json = JsonUtil.fromJson(bodyAsString, Map::class.java) - val code = json?.get("code") as? Int - if (code != null && code != 0) { - TextSecurePreferences.setIsUsingFCM(context, true) - TextSecurePreferences.setFCMToken(context, token) - TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis()) - Log.d("Loki", "Successfully registered for FCM.") - } else { - Log.d("Loki", "Couldn't register for FCM due to error: ${json?.get("message") as? String ?: "null"}.") - } - } - else -> { - Log.d("Loki", "Couldn't register for FCM due.") - } - } + 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"}.") } - - override fun onFailure(call: Call, exception: IOException) { - 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() allClosedGroupPublicKeys.forEach { closedGroup -> @@ -123,28 +90,14 @@ object LokiPushNotificationManager { val parameters = mapOf( "closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey ) val url = "$server/${operation.rawValue}" val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) - val request = Request.Builder().url(url).post(body).build() - connection.newCall(request).enqueue(object : Callback { - - override fun onResponse(call: Call, response: Response) { - when (response.code()) { - 200 -> { - val bodyAsString = response.body()!!.string() - val json = JsonUtil.fromJson(bodyAsString, Map::class.java) - val code = json?.get("code") as? Int - if (code == null || code == 0) { - Log.d("Loki", "Couldn't subscribe/unsubscribe to/from PNs for closed group with ID: $closedGroupPublicKey due to error: ${json?.get("message") as? String ?: "null"}.") - } - } - else -> { - Log.d("Loki", "Couldn't subscribe/unsubscribe to/from PNs for closed group with ID: $closedGroupPublicKey.") - } - } + 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"}.") } - - override fun onFailure(call: Call, exception: IOException) { - Log.d("Loki", "Couldn't subscribe/unsubscribe to/from PNs for closed group with ID: $closedGroupPublicKey due to error: $exception.") - } - }) + }.fail { exception -> + Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${exception}.") + } } } diff --git a/src/org/thoughtcrime/securesms/loki/protocol/ClosedGroupUpdateMessageSendJob.kt b/src/org/thoughtcrime/securesms/loki/protocol/ClosedGroupUpdateMessageSendJob.kt index 57310ee18d..da527c5f87 100644 --- a/src/org/thoughtcrime/securesms/loki/protocol/ClosedGroupUpdateMessageSendJob.kt +++ b/src/org/thoughtcrime/securesms/loki/protocol/ClosedGroupUpdateMessageSendJob.kt @@ -129,7 +129,7 @@ class ClosedGroupUpdateMessageSendJob private constructor(parameters: Parameters // isClosedGroup can always be false as it's only used in the context of legacy closed groups messageSender.sendMessage(0, address, udAccess.get().targetUnidentifiedAccess, Date().time, serializedContentMessage, false, ttl, false, - useFallbackEncryption, false, false) + useFallbackEncryption, false, false, false) } catch (e: Exception) { Log.d("Loki", "Failed to send closed group update message to: $destination due to error: $e.") } diff --git a/src/org/thoughtcrime/securesms/loki/protocol/NullMessageSendJob.kt b/src/org/thoughtcrime/securesms/loki/protocol/NullMessageSendJob.kt index 821a281eb2..7983ecfbbd 100644 --- a/src/org/thoughtcrime/securesms/loki/protocol/NullMessageSendJob.kt +++ b/src/org/thoughtcrime/securesms/loki/protocol/NullMessageSendJob.kt @@ -56,7 +56,7 @@ class NullMessageSendJob private constructor(parameters: Parameters, private val try { messageSender.sendMessage(0, address, udAccess.get().targetUnidentifiedAccess, Date().time, serializedContentMessage, false, ttl, false, - false, false, false) + false, false, false, false) } catch (e: Exception) { Log.d("Loki", "Failed to send null message to: $publicKey due to error: $e.") throw e diff --git a/src/org/thoughtcrime/securesms/loki/protocol/SessionRequestMessageSendJob.kt b/src/org/thoughtcrime/securesms/loki/protocol/SessionRequestMessageSendJob.kt index 155ca1e29b..5e2758d8bd 100644 --- a/src/org/thoughtcrime/securesms/loki/protocol/SessionRequestMessageSendJob.kt +++ b/src/org/thoughtcrime/securesms/loki/protocol/SessionRequestMessageSendJob.kt @@ -72,7 +72,7 @@ class SessionRequestMessageSendJob private constructor(parameters: Parameters, p try { messageSender.sendMessage(0, address, udAccess.get().targetUnidentifiedAccess, Date().time, serializedContentMessage, false, ttl, false, - true, false, false) + true, false, false, false) } catch (e: Exception) { Log.d("Loki", "Failed to send session request to: $publicKey due to error: $e.") throw e