2020-05-12 11:46:11 +10:00
|
|
|
package org.thoughtcrime.securesms.loki.api
|
2020-04-15 10:24:30 +10:00
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import okhttp3.*
|
2020-09-04 15:55:32 +10:00
|
|
|
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
|
2020-04-15 10:24:30 +10:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
|
|
|
import org.whispersystems.libsignal.logging.Log
|
|
|
|
import org.whispersystems.signalservice.internal.util.JsonUtil
|
2020-07-08 17:05:26 +10:00
|
|
|
import org.whispersystems.signalservice.loki.api.PushNotificationAcknowledgement
|
2020-04-15 10:24:30 +10:00
|
|
|
import java.io.IOException
|
2020-09-04 15:55:32 +10:00
|
|
|
import java.lang.Exception
|
|
|
|
import java.util.concurrent.TimeUnit
|
2020-04-15 10:24:30 +10:00
|
|
|
|
|
|
|
object LokiPushNotificationManager {
|
|
|
|
private val connection = OkHttpClient()
|
|
|
|
|
2020-04-16 16:56:12 +10:00
|
|
|
private val server by lazy {
|
2020-07-08 17:05:26 +10:00
|
|
|
PushNotificationAcknowledgement.shared.server
|
2020-04-16 16:56:12 +10:00
|
|
|
}
|
|
|
|
|
2020-09-04 15:55:32 +10:00
|
|
|
public const val subscribe = "subscribe_closed_group"
|
|
|
|
public const val unsubscribe = "unsubscribe_closed_group"
|
|
|
|
|
2020-04-21 12:21:07 +10:00
|
|
|
private const val tokenExpirationInterval = 12 * 60 * 60 * 1000
|
2020-04-16 16:56:12 +10:00
|
|
|
|
2020-04-17 12:11:27 +10:00
|
|
|
@JvmStatic
|
|
|
|
fun unregister(token: String, context: Context?) {
|
2020-09-04 15:55:32 +10:00
|
|
|
val parameters = mapOf("token" to token)
|
2020-05-12 11:46:11 +10:00
|
|
|
val url = "$server/register"
|
2020-04-15 10:24:30 +10:00
|
|
|
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)
|
2020-09-04 15:55:32 +10:00
|
|
|
val code = json?.get("code") as? Int
|
2020-04-15 10:24:30 +10:00
|
|
|
if (code != null && code != 0) {
|
2020-04-16 16:56:12 +10:00
|
|
|
TextSecurePreferences.setIsUsingFCM(context, false)
|
2020-04-15 10:24:30 +10:00
|
|
|
} else {
|
2020-04-16 16:56:12 +10:00
|
|
|
Log.d("Loki", "Couldn't disable FCM due to error: ${json?.get("message") as? String ?: "null"}.")
|
2020-04-15 10:24:30 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onFailure(call: Call, exception: IOException) {
|
2020-04-16 16:56:12 +10:00
|
|
|
Log.d("Loki", "Couldn't disable FCM.")
|
2020-04-15 10:24:30 +10:00
|
|
|
}
|
|
|
|
})
|
2020-09-04 15:55:32 +10:00
|
|
|
|
|
|
|
for (closedGroup: String in DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()) {
|
|
|
|
operateClosedGroup(closedGroup, TextSecurePreferences.getLocalNumber(context), context, unsubscribe)
|
|
|
|
}
|
2020-04-15 10:24:30 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
2020-07-15 15:14:37 +10:00
|
|
|
fun register(token: String, publicKey: String, context: Context?, force: Boolean) {
|
2020-04-16 16:56:12 +10:00
|
|
|
val oldToken = TextSecurePreferences.getFCMToken(context)
|
|
|
|
val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context)
|
2020-04-21 12:21:07 +10:00
|
|
|
if (!force && token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) { return }
|
2020-09-04 15:55:32 +10:00
|
|
|
val parameters = mapOf("token" to token, "pubKey" to publicKey)
|
2020-05-12 11:46:11 +10:00
|
|
|
val url = "$server/register"
|
2020-04-15 10:24:30 +10:00
|
|
|
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)
|
2020-09-04 15:55:32 +10:00
|
|
|
val code = json?.get("code") as? Int
|
2020-04-15 10:24:30 +10:00
|
|
|
if (code != null && code != 0) {
|
2020-04-16 16:56:12 +10:00
|
|
|
TextSecurePreferences.setIsUsingFCM(context, true)
|
|
|
|
TextSecurePreferences.setFCMToken(context, token)
|
|
|
|
TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis())
|
2020-04-15 10:24:30 +10:00
|
|
|
} else {
|
2020-04-16 16:56:12 +10:00
|
|
|
Log.d("Loki", "Couldn't register for FCM due to error: ${json?.get("message") as? String ?: "null"}.")
|
2020-04-15 10:24:30 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onFailure(call: Call, exception: IOException) {
|
2020-04-16 16:56:12 +10:00
|
|
|
Log.d("Loki", "Couldn't register for FCM.")
|
2020-04-15 10:24:30 +10:00
|
|
|
}
|
|
|
|
})
|
2020-09-04 15:55:32 +10:00
|
|
|
|
|
|
|
for (closedGroup: String in DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()) {
|
|
|
|
operateClosedGroup(closedGroup, publicKey, context, subscribe)
|
|
|
|
}
|
|
|
|
|
2020-04-15 10:24:30 +10:00
|
|
|
}
|
2020-09-04 15:55:32 +10:00
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun operateClosedGroup(closedGroupPublicKey: String, publicKey: String, context: Context?, operation: 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)
|
|
|
|
val url = "$server/$operation"
|
|
|
|
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 $closedGroupPublicKey due to error: ${json?.get("message") as? String ?: "null"}.")
|
|
|
|
} else {
|
|
|
|
Log.d("Loki", "Subscribe/unsubscribe success.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onFailure(call: Call, exception: IOException) {
|
|
|
|
Log.d("Loki", "Couldn't subscribe/unsubscribe $closedGroupPublicKey.")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ClosedGroupSubscribeJob private constructor(parameters: Parameters, private val closedGroupPublicKey: String) : BaseJob(parameters) {
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val KEY = "ClosedGroupSubscribeJob"
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(closedGroupPublicKey: String) : this(Parameters.Builder()
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setQueue(KEY)
|
|
|
|
.setLifespan(TimeUnit.DAYS.toMillis(1))
|
|
|
|
.setMaxAttempts(1)
|
|
|
|
.build(),
|
|
|
|
closedGroupPublicKey)
|
|
|
|
|
|
|
|
override fun serialize(): Data {
|
|
|
|
val builder = Data.Builder()
|
|
|
|
builder.putString("closedGroupPublicKey", closedGroupPublicKey)
|
|
|
|
return builder.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getFactoryKey(): String { return KEY }
|
|
|
|
|
|
|
|
override fun onCanceled() { }
|
|
|
|
|
|
|
|
public override fun onRun() {
|
|
|
|
LokiPushNotificationManager.operateClosedGroup(closedGroupPublicKey, TextSecurePreferences.getLocalNumber(context), context, LokiPushNotificationManager.subscribe)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onShouldRetry(e: Exception): Boolean { return false }
|
2020-04-15 10:24:30 +10:00
|
|
|
}
|