2021-07-09 05:25:57 +02:00
|
|
|
package org.thoughtcrime.securesms.notifications
|
2020-04-15 02:24:30 +02:00
|
|
|
|
|
|
|
import android.content.Context
|
2020-09-18 01:54:59 +02:00
|
|
|
import nl.komponents.kovenant.functional.map
|
2021-10-04 09:51:19 +02:00
|
|
|
import okhttp3.MediaType
|
|
|
|
import okhttp3.Request
|
|
|
|
import okhttp3.RequestBody
|
2021-04-23 08:09:47 +02:00
|
|
|
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
|
|
|
import org.session.libsession.snode.OnionRequestAPI
|
2021-01-15 05:36:30 +01:00
|
|
|
import org.session.libsession.utilities.TextSecurePreferences
|
2021-02-01 01:35:53 +01:00
|
|
|
import org.session.libsignal.utilities.JsonUtil
|
2021-10-04 09:51:19 +02:00
|
|
|
import org.session.libsignal.utilities.Log
|
2021-05-18 01:34:45 +02:00
|
|
|
import org.session.libsignal.utilities.retryIfNeeded
|
2021-10-04 09:51:19 +02:00
|
|
|
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
|
2020-04-15 02:24:30 +02:00
|
|
|
|
|
|
|
object LokiPushNotificationManager {
|
2020-10-07 06:56:55 +02:00
|
|
|
private val maxRetryCount = 4
|
2020-09-15 08:57:50 +02:00
|
|
|
private val tokenExpirationInterval = 12 * 60 * 60 * 1000
|
2020-04-15 02:24:30 +02:00
|
|
|
|
2020-04-16 08:56:12 +02:00
|
|
|
private val server by lazy {
|
2021-04-23 08:09:47 +02:00
|
|
|
PushNotificationAPI.server
|
2020-04-16 08:56:12 +02:00
|
|
|
}
|
2020-09-18 03:07:44 +02:00
|
|
|
private val pnServerPublicKey by lazy {
|
2021-04-23 08:09:47 +02:00
|
|
|
PushNotificationAPI.serverPublicKey
|
2020-09-18 01:54:59 +02:00
|
|
|
}
|
2020-04-16 08:56:12 +02:00
|
|
|
|
2020-09-15 08:57:50 +02:00
|
|
|
enum class ClosedGroupOperation {
|
|
|
|
Subscribe, Unsubscribe;
|
2020-09-04 07:55:32 +02:00
|
|
|
|
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-09-18 01:54:59 +02:00
|
|
|
val url = "$server/unregister"
|
2020-04-15 02:24:30 +02:00
|
|
|
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
2020-09-18 01:54:59 +02:00
|
|
|
val request = Request.Builder().url(url).post(body)
|
2020-10-07 06:56:55 +02:00
|
|
|
retryIfNeeded(maxRetryCount) {
|
2020-11-10 05:51:05 +01:00
|
|
|
OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey, "/loki/v2/lsrpc").map { json ->
|
2020-10-07 06:56:55 +02:00
|
|
|
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"}.")
|
|
|
|
}
|
|
|
|
}.fail { exception ->
|
|
|
|
Log.d("Loki", "Couldn't disable FCM due to error: ${exception}.")
|
2020-04-15 02:24:30 +02:00
|
|
|
}
|
2020-09-18 01:54:59 +02:00
|
|
|
}
|
2020-09-15 08:57:50 +02:00
|
|
|
// Unsubscribe from all closed groups
|
2021-10-04 09:51:19 +02:00
|
|
|
val allClosedGroupPublicKeys = DatabaseComponent.get(context).lokiAPIDatabase().getAllClosedGroupPublicKeys()
|
2021-01-15 05:36:30 +01:00
|
|
|
val userPublicKey = TextSecurePreferences.getLocalNumber(context)!!
|
2020-09-15 08:57:50 +02:00
|
|
|
allClosedGroupPublicKeys.forEach { closedGroup ->
|
|
|
|
performOperation(context, ClosedGroupOperation.Unsubscribe, closedGroup, userPublicKey)
|
2020-09-04 07:55:32 +02:00
|
|
|
}
|
2020-04-15 02:24:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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"
|
2020-04-15 02:24:30 +02:00
|
|
|
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
2020-09-18 01:54:59 +02:00
|
|
|
val request = Request.Builder().url(url).post(body)
|
2020-10-07 06:56:55 +02:00
|
|
|
retryIfNeeded(maxRetryCount) {
|
2020-11-10 05:51:05 +01:00
|
|
|
OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey, "/loki/v2/lsrpc").map { json ->
|
2020-10-07 06:56:55 +02:00
|
|
|
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"}.")
|
|
|
|
}
|
|
|
|
}.fail { exception ->
|
|
|
|
Log.d("Loki", "Couldn't register for FCM due to error: ${exception}.")
|
2020-04-15 02:24:30 +02:00
|
|
|
}
|
2020-09-18 01:54:59 +02:00
|
|
|
}
|
2020-09-15 08:57:50 +02:00
|
|
|
// Subscribe to all closed groups
|
2021-10-04 09:51:19 +02:00
|
|
|
val allClosedGroupPublicKeys = DatabaseComponent.get(context).lokiAPIDatabase().getAllClosedGroupPublicKeys()
|
2020-09-15 08:57:50 +02:00
|
|
|
allClosedGroupPublicKeys.forEach { closedGroup ->
|
|
|
|
performOperation(context, ClosedGroupOperation.Subscribe, closedGroup, publicKey)
|
2020-09-04 07:55:32 +02:00
|
|
|
}
|
2020-04-15 02:24:30 +02:00
|
|
|
}
|
2020-09-04 07:55:32 +02:00
|
|
|
|
|
|
|
@JvmStatic
|
2020-09-15 08:57:50 +02:00
|
|
|
fun performOperation(context: Context, operation: ClosedGroupOperation, closedGroupPublicKey: String, publicKey: String) {
|
2020-09-04 07:55:32 +02:00
|
|
|
if (!TextSecurePreferences.isUsingFCM(context)) { return }
|
2020-09-15 09:00:18 +02:00
|
|
|
val parameters = mapOf( "closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey )
|
2020-09-15 08:57:50 +02:00
|
|
|
val url = "$server/${operation.rawValue}"
|
2020-09-04 07:55:32 +02:00
|
|
|
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
2020-09-18 01:54:59 +02:00
|
|
|
val request = Request.Builder().url(url).post(body)
|
2020-10-07 06:56:55 +02:00
|
|
|
retryIfNeeded(maxRetryCount) {
|
2020-11-10 05:51:05 +01:00
|
|
|
OnionRequestAPI.sendOnionRequest(request.build(), server, pnServerPublicKey, "/loki/v2/lsrpc").map { json ->
|
2020-10-07 06:56:55 +02:00
|
|
|
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"}.")
|
|
|
|
}
|
|
|
|
}.fail { exception ->
|
|
|
|
Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${exception}.")
|
2020-09-04 07:55:32 +02:00
|
|
|
}
|
2020-09-18 01:54:59 +02:00
|
|
|
}
|
2020-09-04 07:55:32 +02:00
|
|
|
}
|
|
|
|
}
|