session-android/src/org/thoughtcrime/securesms/loki/api/LokiPushNotificationManager.kt

141 lines
6.5 KiB
Kotlin
Raw Normal View History

2020-05-12 03:46:11 +02:00
package org.thoughtcrime.securesms.loki.api
import android.content.Context
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
2020-07-08 09:05:26 +02:00
import org.whispersystems.signalservice.loki.api.PushNotificationAcknowledgement
import java.io.IOException
import java.lang.Exception
import java.util.concurrent.TimeUnit
object LokiPushNotificationManager {
private val connection = OkHttpClient()
2020-09-15 08:57:50 +02:00
private val tokenExpirationInterval = 12 * 60 * 60 * 1000
2020-04-16 08:56:12 +02:00
private val server by lazy {
2020-07-08 09:05:26 +02:00
PushNotificationAcknowledgement.shared.server
2020-04-16 08:56:12 +02:00
}
2020-09-15 08:57:50 +02:00
enum class ClosedGroupOperation {
Subscribe, Unsubscribe;
2020-09-15 08:57:50 +02:00
val rawValue: String
get() {
return when (this) {
Subscribe -> "subscribe_closed_group"
Unsubscribe -> "unsubscribe_closed_group"
}
}
}
2020-04-16 08:56:12 +02:00
2020-04-17 04:11:27 +02:00
@JvmStatic
2020-09-15 08:57:50 +02:00
fun unregister(token: String, context: Context) {
val parameters = mapOf( "token" to token )
2020-05-12 03:46:11 +02:00
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) {
2020-04-16 08:56:12 +02:00
TextSecurePreferences.setIsUsingFCM(context, false)
} else {
2020-04-16 08:56:12 +02:00
Log.d("Loki", "Couldn't disable FCM due to error: ${json?.get("message") as? String ?: "null"}.")
}
}
}
}
override fun onFailure(call: Call, exception: IOException) {
2020-04-16 08:56:12 +02:00
Log.d("Loki", "Couldn't disable FCM.")
}
})
2020-09-15 08:57:50 +02:00
// Unsubscribe from all closed groups
val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()
val userPublicKey = TextSecurePreferences.getLocalNumber(context)
allClosedGroupPublicKeys.forEach { closedGroup ->
performOperation(context, ClosedGroupOperation.Unsubscribe, closedGroup, userPublicKey)
}
}
@JvmStatic
2020-09-15 08:57:50 +02:00
fun register(token: String, publicKey: String, context: Context, force: Boolean) {
2020-04-16 08:56:12 +02:00
val oldToken = TextSecurePreferences.getFCMToken(context)
val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context)
2020-04-21 04:21:07 +02:00
if (!force && token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) { return }
2020-09-15 08:57:50 +02:00
val parameters = mapOf( "token" to token, "pubKey" to publicKey )
2020-05-12 03:46:11 +02:00
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) {
2020-04-16 08:56:12 +02:00
TextSecurePreferences.setIsUsingFCM(context, true)
TextSecurePreferences.setFCMToken(context, token)
TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis())
} else {
2020-04-16 08:56:12 +02:00
Log.d("Loki", "Couldn't register for FCM due to error: ${json?.get("message") as? String ?: "null"}.")
}
}
}
}
override fun onFailure(call: Call, exception: IOException) {
2020-04-16 08:56:12 +02:00
Log.d("Loki", "Couldn't register for FCM.")
}
})
2020-09-15 08:57:50 +02:00
// Subscribe to all closed groups
val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()
allClosedGroupPublicKeys.forEach { closedGroup ->
performOperation(context, ClosedGroupOperation.Subscribe, closedGroup, publicKey)
}
}
@JvmStatic
2020-09-15 08:57:50 +02:00
fun performOperation(context: Context, operation: ClosedGroupOperation, closedGroupPublicKey: String, publicKey: String) {
Log.d("Loki", "Start to notify PN server of closed group.")
if (!TextSecurePreferences.isUsingFCM(context)) { return }
val parameters = mapOf("closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey)
2020-09-15 08:57:50 +02:00
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) {
2020-09-15 08:57:50 +02:00
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"}.")
}
}
}
}
override fun onFailure(call: Call, exception: IOException) {
2020-09-15 08:57:50 +02:00
Log.d("Loki", "Couldn't subscribe/unsubscribe to/from PNs for closed group with ID: $closedGroupPublicKey due to error: $exception.")
}
})
}
}