@ -1,27 +1,24 @@
package org.thoughtcrime.securesms.loki.api
package org.thoughtcrime.securesms.loki.api
import android.content.Context
import android.content.Context
import nl.komponents.kovenant.functional.map
import okhttp3.*
import okhttp3.*
import org.thoughtcrime.securesms.database.DatabaseFactory
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.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.libsignal.logging.Log
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.PushNotificationAcknowledgement
import org.whispersystems.signalservice.loki.api.PushNotificationAcknowledgement
import java.io.IOException
import org.whispersystems.signalservice.loki.api.onionrequests.OnionRequestAPI
import java.lang.Exception
import java.util.concurrent.TimeUnit
object LokiPushNotificationManager {
object LokiPushNotificationManager {
private val connection = OkHttpClient ( )
private val tokenExpirationInterval = 12 * 60 * 60 * 1000
private val tokenExpirationInterval = 12 * 60 * 60 * 1000
private val server by lazy {
private val server by lazy {
PushNotificationAcknowledgement . shared . server
PushNotificationAcknowledgement . shared . server
}
}
private val pnServerPublicKey by lazy {
PushNotificationAcknowledgement . pnServerPublicKey
}
enum class ClosedGroupOperation {
enum class ClosedGroupOperation {
Subscribe , Unsubscribe ;
Subscribe , Unsubscribe ;
@ -38,34 +35,19 @@ object LokiPushNotificationManager {
@JvmStatic
@JvmStatic
fun unregister ( token : String , context : Context ) {
fun unregister ( token : String , context : Context ) {
val parameters = mapOf ( " token " to token )
val parameters = mapOf ( " token " to token )
val url = " $server / register"
val url = " $server / un 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 ) . build ( )
val request = Request . Builder ( ) . url ( url ) . post ( body )
connection . newCall ( request ) . enqueue ( object : Callback {
OnionRequestAPI . sendOnionRequest ( request . build ( ) , server , pnServerPublicKey ) . map { json ->
val code = json [ " code " ] as ? Int
override fun onResponse ( call : Call , response : Response ) {
if ( code != null && code != 0 ) {
when ( response . code ( ) ) {
TextSecurePreferences . setIsUsingFCM ( context , false )
200 -> {
} else {
val bodyAsString = response . body ( ) !! . string ( )
Log . d ( " Loki " , " Couldn't disable FCM due to error: ${json["message"] as? String ?: "null"} . " )
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. " )
}
}
}
}
} . fail { exception ->
override fun onFailure ( call : Call , exception : IOException ) {
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 )
@ -82,34 +64,19 @@ object LokiPushNotificationManager {
val parameters = mapOf ( " token " to token , " pubKey " to publicKey )
val parameters = mapOf ( " token " to token , " pubKey " to publicKey )
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 ) . build ( )
val request = Request . Builder ( ) . url ( url ) . post ( body )
connection . newCall ( request ) . enqueue ( object : Callback {
OnionRequestAPI . sendOnionRequest ( request . build ( ) , server , pnServerPublicKey ) . map { json ->
val code = json [ " code " ] as ? Int
override fun onResponse ( call : Call , response : Response ) {
if ( code != null && code != 0 ) {
when ( response . code ( ) ) {
TextSecurePreferences . setIsUsingFCM ( context , true )
200 -> {
TextSecurePreferences . setFCMToken ( context , token )
val bodyAsString = response . body ( ) !! . string ( )
TextSecurePreferences . setLastFCMUploadTime ( context , System . currentTimeMillis ( ) )
val json = JsonUtil . fromJson ( bodyAsString , Map :: class . java )
} else {
val code = json ?. get ( " code " ) as ? Int
Log . d ( " Loki " , " Couldn't register for FCM due to error: ${json["message"] as? String ?: "null"} . " )
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. " )
}
}
}
}
} . fail { exception ->
override fun onFailure ( call : Call , exception : IOException ) {
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 ->
@ -123,28 +90,14 @@ object LokiPushNotificationManager {
val parameters = mapOf ( " closedGroupPublicKey " to closedGroupPublicKey , " pubKey " to publicKey )
val parameters = mapOf ( " closedGroupPublicKey " to closedGroupPublicKey , " pubKey " to publicKey )
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 ) . build ( )
val request = Request . Builder ( ) . url ( url ) . post ( body )
connection . newCall ( request ) . enqueue ( object : Callback {
OnionRequestAPI . sendOnionRequest ( request . build ( ) , server , pnServerPublicKey ) . map { json ->
val code = json [ " code " ] as ? Int
override fun onResponse ( call : Call , response : Response ) {
if ( code == null || code == 0 ) {
when ( response . code ( ) ) {
Log . d ( " Loki " , " Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${json["message"] as? String ?: "null"} . " )
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 . " )
}
}
}
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} . " )
}
}
}
}
}