mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-27 12:05:22 +00:00
Utilise TokenManager and ExpiryManager
This commit is contained in:
parent
153aa4ceaa
commit
667af27bfb
@ -508,7 +508,6 @@ public class ApplicationContext extends Application implements DefaultLifecycleO
|
||||
}
|
||||
|
||||
public void clearAllData(boolean isMigratingToV2KeyPair) {
|
||||
PushNotificationAPI.unregister();
|
||||
if (firebaseInstanceIdJob != null && firebaseInstanceIdJob.isActive()) {
|
||||
firebaseInstanceIdJob.cancel(null);
|
||||
}
|
||||
|
@ -0,0 +1,26 @@
|
||||
package org.thoughtcrime.securesms.notifications
|
||||
|
||||
import android.content.Context
|
||||
import org.session.libsession.utilities.TextSecurePreferences
|
||||
|
||||
class FcmTokenManager(
|
||||
private val context: Context,
|
||||
private val expiryManager: ExpiryManager
|
||||
) {
|
||||
val isUsingFCM get() = TextSecurePreferences.isUsingFCM(context)
|
||||
|
||||
var fcmToken
|
||||
get() = TextSecurePreferences.getFCMToken(context)
|
||||
set(value) {
|
||||
TextSecurePreferences.setFCMToken(context, value)
|
||||
if (value != null) markTime() else clearTime()
|
||||
}
|
||||
|
||||
val requiresUnregister get() = fcmToken != null
|
||||
|
||||
private fun clearTime() = expiryManager.clearTime()
|
||||
private fun markTime() = expiryManager.markTime()
|
||||
private fun isExpired() = expiryManager.isExpired()
|
||||
|
||||
fun isInvalid(): Boolean = fcmToken == null || isExpired()
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package org.thoughtcrime.securesms.notifications
|
||||
|
||||
import android.content.Context
|
||||
import org.session.libsession.utilities.TextSecurePreferences
|
||||
|
||||
class ExpiryManager(
|
||||
private val context: Context,
|
||||
private val interval: Int = 12 * 60 * 60 * 1000
|
||||
) {
|
||||
fun isExpired() = currentTime() > time + interval
|
||||
|
||||
fun markTime() {
|
||||
time = currentTime()
|
||||
}
|
||||
|
||||
fun clearTime() {
|
||||
time = 0
|
||||
}
|
||||
|
||||
private var time
|
||||
get() = TextSecurePreferences.getLastFCMUploadTime(context)
|
||||
set(value) = TextSecurePreferences.setLastFCMUploadTime(context, value)
|
||||
|
||||
private fun currentTime() = System.currentTimeMillis()
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
package org.thoughtcrime.securesms.notifications
|
||||
|
||||
import com.google.android.gms.tasks.Task
|
||||
import com.google.android.gms.tasks.Tasks
|
||||
import com.google.firebase.iid.FirebaseInstanceId
|
||||
import com.google.firebase.iid.InstanceIdResult
|
||||
import kotlinx.coroutines.*
|
||||
@ -9,9 +10,7 @@ import kotlinx.coroutines.*
|
||||
|
||||
fun getFcmInstanceId(body: (Task<InstanceIdResult>)->Unit): Job = MainScope().launch(Dispatchers.IO) {
|
||||
val task = FirebaseInstanceId.getInstance().instanceId
|
||||
while (!task.isComplete && isActive) {
|
||||
// wait for task to complete while we are active
|
||||
}
|
||||
Tasks.await(task)
|
||||
if (!isActive) return@launch // don't 'complete' task if we were canceled
|
||||
body(task)
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
package org.thoughtcrime.securesms.notifications
|
||||
|
||||
import android.content.Context
|
||||
import com.google.android.gms.tasks.Task
|
||||
import com.google.firebase.iid.InstanceIdResult
|
||||
import com.goterl.lazysodium.LazySodiumAndroid
|
||||
import com.goterl.lazysodium.SodiumAndroid
|
||||
import com.goterl.lazysodium.interfaces.AEAD
|
||||
@ -15,12 +13,14 @@ import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.decodeFromStream
|
||||
import nl.komponents.kovenant.Promise
|
||||
import nl.komponents.kovenant.combine.and
|
||||
import nl.komponents.kovenant.functional.map
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationMetadata
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.Response
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.SubscriptionRequest
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.SubscriptionResponse
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.UnsubscribeResponse
|
||||
@ -29,7 +29,6 @@ import org.session.libsession.messaging.utilities.SodiumUtilities
|
||||
import org.session.libsession.snode.OnionRequestAPI
|
||||
import org.session.libsession.snode.SnodeAPI
|
||||
import org.session.libsession.snode.Version
|
||||
import org.session.libsession.utilities.TextSecurePreferences
|
||||
import org.session.libsession.utilities.TextSecurePreferences.Companion.getLocalNumber
|
||||
import org.session.libsession.utilities.bencode.Bencode
|
||||
import org.session.libsession.utilities.bencode.BencodeList
|
||||
@ -37,19 +36,20 @@ import org.session.libsession.utilities.bencode.BencodeString
|
||||
import org.session.libsignal.utilities.Base64
|
||||
import org.session.libsignal.utilities.Log
|
||||
import org.session.libsignal.utilities.Namespace
|
||||
import org.session.libsignal.utilities.emptyPromise
|
||||
import org.session.libsignal.utilities.retryIfNeeded
|
||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
||||
import org.thoughtcrime.securesms.crypto.KeyPairUtilities
|
||||
|
||||
private const val TAG = "FirebasePushManager"
|
||||
|
||||
class FirebasePushManager(private val context: Context, private val prefs: TextSecurePreferences): PushManager {
|
||||
class FirebasePushManager (private val context: Context): PushManager {
|
||||
|
||||
companion object {
|
||||
private const val maxRetryCount = 4
|
||||
private const val tokenExpirationInterval = 12 * 60 * 60 * 1000
|
||||
}
|
||||
|
||||
private val tokenManager = FcmTokenManager(context, ExpiryManager(context))
|
||||
private var firebaseInstanceIdJob: Job? = null
|
||||
private val sodium = LazySodiumAndroid(SodiumAndroid())
|
||||
|
||||
@ -59,12 +59,7 @@ class FirebasePushManager(private val context: Context, private val prefs: TextS
|
||||
val key = sodium.keygen(AEAD.Method.XCHACHA20_POLY1305_IETF)
|
||||
IdentityKeyUtil.save(context, IdentityKeyUtil.NOTIFICATION_KEY, key.asHexString)
|
||||
}
|
||||
return Key.fromHexString(
|
||||
IdentityKeyUtil.retrieve(
|
||||
context,
|
||||
IdentityKeyUtil.NOTIFICATION_KEY
|
||||
)
|
||||
)
|
||||
return Key.fromHexString(IdentityKeyUtil.retrieve(context, IdentityKeyUtil.NOTIFICATION_KEY))
|
||||
}
|
||||
|
||||
fun decrypt(encPayload: ByteArray): ByteArray? {
|
||||
@ -78,8 +73,7 @@ class FirebasePushManager(private val context: Context, private val prefs: TextS
|
||||
val expectedList = (bencoded.decode() as? BencodeList)?.values
|
||||
?: error("Failed to decode bencoded list from payload")
|
||||
|
||||
val metadataJson = (expectedList[0] as? BencodeString)?.value
|
||||
?: error("no metadata")
|
||||
val metadataJson = (expectedList[0] as? BencodeString)?.value ?: error("no metadata")
|
||||
val metadata: PushNotificationMetadata = Json.decodeFromString(String(metadataJson))
|
||||
|
||||
val content: ByteArray? = if (expectedList.size >= 2) (expectedList[1] as? BencodeString)?.value else null
|
||||
@ -94,78 +88,81 @@ class FirebasePushManager(private val context: Context, private val prefs: TextS
|
||||
return content
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
override fun refresh(force: Boolean) {
|
||||
firebaseInstanceIdJob?.apply {
|
||||
if (force) cancel() else if (isActive) return
|
||||
}
|
||||
|
||||
firebaseInstanceIdJob = getFcmInstanceId { refresh(it, force) }
|
||||
}
|
||||
|
||||
private fun refresh(task: Task<InstanceIdResult>, force: Boolean) {
|
||||
Log.d(TAG, "refresh")
|
||||
|
||||
// context in here is Dispatchers.IO
|
||||
if (!task.isSuccessful) {
|
||||
Log.w(TAG, "FirebaseInstanceId.getInstance().getInstanceId() failed." + task.exception
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
val token: String = task.result?.token ?: return
|
||||
val userPublicKey = getLocalNumber(context) ?: return
|
||||
val userEdKey = KeyPairUtilities.getUserED25519KeyPair(context) ?: return
|
||||
|
||||
when {
|
||||
prefs.isUsingFCM() -> register(token, userPublicKey, userEdKey, force)
|
||||
prefs.getFCMToken() != null -> unregister(token, userPublicKey, userEdKey)
|
||||
}
|
||||
}
|
||||
|
||||
private fun unregister(token: String, userPublicKey: String, userEdKey: KeyPair) {
|
||||
val timestamp = SnodeAPI.nowWithOffset / 1000 // get timestamp in ms -> s
|
||||
// if we want to support passing namespace list, here is the place to do it
|
||||
val sigData = "UNSUBSCRIBE${userPublicKey}${timestamp}".encodeToByteArray()
|
||||
val signature = ByteArray(Sign.BYTES)
|
||||
sodium.cryptoSignDetached(signature, sigData, sigData.size.toLong(), userEdKey.secretKey.asBytes)
|
||||
|
||||
val requestParameters = UnsubscriptionRequest(
|
||||
pubkey = userPublicKey,
|
||||
session_ed25519 = userEdKey.publicKey.asHexString,
|
||||
service = "firebase",
|
||||
sig_ts = timestamp,
|
||||
signature = Base64.encodeBytes(signature),
|
||||
service_info = mapOf("token" to token),
|
||||
)
|
||||
|
||||
val url = "${PushNotificationAPI.server}/unsubscribe"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), Json.encodeToString(requestParameters))
|
||||
val request = Request.Builder().url(url).post(body)
|
||||
retryIfNeeded(maxRetryCount) {
|
||||
getResponseBody<UnsubscribeResponse>(request.build()).map { response ->
|
||||
if (response.success == true) {
|
||||
Log.d(TAG, "Unsubscribe FCM success")
|
||||
TextSecurePreferences.setFCMToken(context, null)
|
||||
PushNotificationAPI.unregister()
|
||||
} else {
|
||||
Log.e(TAG, "Couldn't unregister for FCM due to error: ${response.message}")
|
||||
}
|
||||
}.fail { exception ->
|
||||
Log.e(TAG, "Couldn't unregister for FCM due to error: ${exception}.", exception)
|
||||
firebaseInstanceIdJob = getFcmInstanceId { task ->
|
||||
when {
|
||||
task.isSuccessful -> task.result?.token?.let { refresh(it, force).get() }
|
||||
else -> Log.w(TAG, "getFcmInstanceId failed." + task.exception)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun register(token: String, publicKey: String, userEd25519Key: KeyPair, force: Boolean, namespaces: List<Int> = listOf(Namespace.DEFAULT)) {
|
||||
Log.d(TAG, "register token: $token")
|
||||
private fun refresh(token: String, force: Boolean): Promise<*, Exception> {
|
||||
val userPublicKey = getLocalNumber(context) ?: return emptyPromise()
|
||||
val userEdKey = KeyPairUtilities.getUserED25519KeyPair(context) ?: return emptyPromise()
|
||||
|
||||
val oldToken = TextSecurePreferences.getFCMToken(context)
|
||||
val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context)
|
||||
// if (!force && token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) {
|
||||
// Log.d(TAG, "not registering now... not forced or expired")
|
||||
// return
|
||||
// }
|
||||
return when {
|
||||
tokenManager.isUsingFCM -> register(force, token, userPublicKey, userEdKey)
|
||||
tokenManager.requiresUnregister -> unregister(token, userPublicKey, userEdKey)
|
||||
else -> emptyPromise()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register for push notifications if:
|
||||
* force is true
|
||||
* there is no FCM Token
|
||||
* FCM Token has expired
|
||||
*/
|
||||
private fun register(
|
||||
force: Boolean,
|
||||
token: String,
|
||||
publicKey: String,
|
||||
userEd25519Key: KeyPair,
|
||||
namespaces: List<Int> = listOf(Namespace.DEFAULT)
|
||||
): Promise<*, Exception> = if (force || tokenManager.isInvalid()) {
|
||||
register(token, publicKey, userEd25519Key, namespaces)
|
||||
} else emptyPromise()
|
||||
|
||||
/**
|
||||
* Register for push notifications.
|
||||
*/
|
||||
private fun register(
|
||||
token: String,
|
||||
publicKey: String,
|
||||
userEd25519Key: KeyPair,
|
||||
namespaces: List<Int> = listOf(Namespace.DEFAULT)
|
||||
): Promise<*, Exception> = PushNotificationAPI.register(token) and getSubscription(
|
||||
token, publicKey, userEd25519Key, namespaces
|
||||
) fail {
|
||||
Log.e(TAG, "Couldn't register for FCM due to error: $it.", it)
|
||||
} success {
|
||||
tokenManager.fcmToken = token
|
||||
}
|
||||
|
||||
private fun unregister(
|
||||
token: String,
|
||||
userPublicKey: String,
|
||||
userEdKey: KeyPair
|
||||
): Promise<*, Exception> = PushNotificationAPI.unregister() and getUnsubscription(
|
||||
token, userPublicKey, userEdKey
|
||||
) fail {
|
||||
Log.e(TAG, "Couldn't unregister for FCM due to error: ${it}.", it)
|
||||
} success {
|
||||
tokenManager.fcmToken = null
|
||||
}
|
||||
|
||||
private fun getSubscription(
|
||||
token: String,
|
||||
publicKey: String,
|
||||
userEd25519Key: KeyPair,
|
||||
namespaces: List<Int>
|
||||
): Promise<SubscriptionResponse, Exception> {
|
||||
val pnKey = getOrCreateNotificationKey()
|
||||
|
||||
val timestamp = SnodeAPI.nowWithOffset / 1000 // get timestamp in ms -> s
|
||||
@ -183,33 +180,51 @@ class FirebasePushManager(private val context: Context, private val prefs: TextS
|
||||
signature = Base64.encodeBytes(signature),
|
||||
service_info = mapOf("token" to token),
|
||||
enc_key = pnKey.asHexString,
|
||||
)
|
||||
).let(Json::encodeToString)
|
||||
|
||||
val url = "${PushNotificationAPI.server}/subscribe"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), Json.encodeToString(requestParameters))
|
||||
val request = Request.Builder().url(url).post(body)
|
||||
retryIfNeeded(maxRetryCount) {
|
||||
getResponseBody<SubscriptionResponse>(request.build()).map { response ->
|
||||
if (response.isSuccess()) {
|
||||
Log.d(TAG, "Success $token")
|
||||
TextSecurePreferences.setFCMToken(context, token)
|
||||
TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis())
|
||||
PushNotificationAPI.register(token)
|
||||
} else {
|
||||
val (_, message) = response.errorInfo()
|
||||
Log.e(TAG, "Couldn't register for FCM due to error: $message.")
|
||||
}
|
||||
}.fail { exception ->
|
||||
Log.e(TAG, "Couldn't register for FCM due to error: ${exception}.", exception)
|
||||
}
|
||||
}
|
||||
return retryResponseBody("subscribe", requestParameters)
|
||||
}
|
||||
|
||||
private inline fun <reified T> getResponseBody(request: Request): Promise<T, Exception> =
|
||||
OnionRequestAPI.sendOnionRequest(
|
||||
private fun getUnsubscription(
|
||||
token: String,
|
||||
userPublicKey: String,
|
||||
userEdKey: KeyPair
|
||||
): Promise<UnsubscribeResponse, Exception> {
|
||||
val timestamp = SnodeAPI.nowWithOffset / 1000 // get timestamp in ms -> s
|
||||
// if we want to support passing namespace list, here is the place to do it
|
||||
val sigData = "UNSUBSCRIBE${userPublicKey}${timestamp}".encodeToByteArray()
|
||||
val signature = ByteArray(Sign.BYTES)
|
||||
sodium.cryptoSignDetached(signature, sigData, sigData.size.toLong(), userEdKey.secretKey.asBytes)
|
||||
|
||||
val requestParameters = UnsubscriptionRequest(
|
||||
pubkey = userPublicKey,
|
||||
session_ed25519 = userEdKey.publicKey.asHexString,
|
||||
service = "firebase",
|
||||
sig_ts = timestamp,
|
||||
signature = Base64.encodeBytes(signature),
|
||||
service_info = mapOf("token" to token),
|
||||
).let(Json::encodeToString)
|
||||
|
||||
return retryResponseBody("unsubscribe", requestParameters)
|
||||
}
|
||||
|
||||
private inline fun <reified T: Response> retryResponseBody(path: String, requestParameters: String): Promise<T, Exception> =
|
||||
retryIfNeeded(maxRetryCount) { getResponseBody(path, requestParameters) }
|
||||
|
||||
private inline fun <reified T: Response> getResponseBody(path: String, requestParameters: String): Promise<T, Exception> {
|
||||
val url = "${PushNotificationAPI.server}/$path"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), requestParameters)
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
|
||||
return OnionRequestAPI.sendOnionRequest(
|
||||
request,
|
||||
PushNotificationAPI.server,
|
||||
PushNotificationAPI.serverPublicKey,
|
||||
Version.V4
|
||||
).map { response -> Json.decodeFromStream(response.body!!.inputStream()) }
|
||||
).map { response ->
|
||||
response.body!!.inputStream()
|
||||
.let { Json.decodeFromStream<T>(it) }
|
||||
.also { if (it.isFailure()) throw Exception("error: ${it.message}.") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,8 +17,7 @@ object FirebasePushModule {
|
||||
@Singleton
|
||||
fun provideFirebasePushManager(
|
||||
@ApplicationContext context: Context,
|
||||
prefs: TextSecurePreferences,
|
||||
) = FirebasePushManager(context, prefs)
|
||||
) = FirebasePushManager(context)
|
||||
}
|
||||
|
||||
@Module
|
||||
|
@ -10,6 +10,7 @@ import okhttp3.RequestBody
|
||||
import org.session.libsession.messaging.jobs.Job.Companion.MAX_BUFFER_SIZE
|
||||
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI.server
|
||||
import org.session.libsession.messaging.utilities.Data
|
||||
import org.session.libsession.snode.SnodeMessage
|
||||
import org.session.libsession.snode.OnionRequestAPI
|
||||
@ -33,18 +34,21 @@ class NotifyPNServerJob(val message: SnodeMessage) : Job {
|
||||
}
|
||||
|
||||
override fun execute(dispatcherName: String) {
|
||||
val server = PushNotificationAPI.server
|
||||
val parameters = mapOf( "data" to message.data, "send_to" to message.recipient )
|
||||
val url = "${server}/notify"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body)
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
retryIfNeeded(4) {
|
||||
OnionRequestAPI.sendOnionRequest(request.build(), server, PushNotificationAPI.serverPublicKey, Version.V2).map { response ->
|
||||
val code = response.info["code"] as? Int
|
||||
if (code == null || code == 0) {
|
||||
Log.d("Loki", "Couldn't notify PN server due to error: ${response.info["message"] as? String ?: "null"}.")
|
||||
OnionRequestAPI.sendOnionRequest(
|
||||
request,
|
||||
server,
|
||||
PushNotificationAPI.serverPublicKey,
|
||||
Version.V2
|
||||
) success { response ->
|
||||
when (response.info["code"]) {
|
||||
null, 0 -> Log.d("Loki", "Couldn't notify PN server due to error: ${response.info["message"]}.")
|
||||
}
|
||||
}.fail { exception ->
|
||||
} fail { exception ->
|
||||
Log.d("Loki", "Couldn't notify PN server due to error: $exception.")
|
||||
}
|
||||
}.success {
|
||||
|
@ -54,37 +54,39 @@ data class UnsubscriptionRequest(
|
||||
val service_info: Map<String, String>,
|
||||
)
|
||||
|
||||
/** invalid values, missing reuqired arguments etc, details in message */
|
||||
private const val UNPARSEABLE_ERROR = 1
|
||||
/** the "service" value is not active / valid */
|
||||
private const val SERVICE_NOT_AVAILABLE = 2
|
||||
/** something getting wrong internally talking to the backend */
|
||||
private const val SERVICE_TIMEOUT = 3
|
||||
/** other error processing the subscription (details in the message) */
|
||||
private const val GENERIC_ERROR = 4
|
||||
|
||||
@Serializable
|
||||
data class SubscriptionResponse(
|
||||
val error: Int? = null,
|
||||
val message: String? = null,
|
||||
val success: Boolean? = null,
|
||||
override val error: Int? = null,
|
||||
override val message: String? = null,
|
||||
override val success: Boolean? = null,
|
||||
val added: Boolean? = null,
|
||||
val updated: Boolean? = null,
|
||||
) {
|
||||
companion object {
|
||||
/** invalid values, missing reuqired arguments etc, details in message */
|
||||
const val UNPARSEABLE_ERROR = 1
|
||||
/** the "service" value is not active / valid */
|
||||
const val SERVICE_NOT_AVAILABLE = 2
|
||||
/** something getting wrong internally talking to the backend */
|
||||
const val SERVICE_TIMEOUT = 3
|
||||
/** other error processing the subscription (details in the message) */
|
||||
const val GENERIC_ERROR = 4
|
||||
}
|
||||
fun isSuccess() = success == true && error == null
|
||||
fun errorInfo() = if (success != true && error != null) {
|
||||
error to message
|
||||
} else null to null
|
||||
}
|
||||
): Response
|
||||
|
||||
@Serializable
|
||||
data class UnsubscribeResponse(
|
||||
val error: Int? = null,
|
||||
val message: String? = null,
|
||||
val success: Boolean? = null,
|
||||
override val error: Int? = null,
|
||||
override val message: String? = null,
|
||||
override val success: Boolean? = null,
|
||||
val removed: Boolean? = null,
|
||||
)
|
||||
): Response
|
||||
|
||||
interface Response {
|
||||
val error: Int?
|
||||
val message: String?
|
||||
val success: Boolean?
|
||||
fun isSuccess() = success == true && error == null
|
||||
fun isFailure() = !isSuccess()
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class PushNotificationMetadata(
|
||||
|
@ -1,7 +1,10 @@
|
||||
package org.session.libsession.messaging.sending_receiving.notifications
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import nl.komponents.kovenant.Promise
|
||||
import nl.komponents.kovenant.all
|
||||
import nl.komponents.kovenant.functional.map
|
||||
import nl.komponents.kovenant.task
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
@ -11,6 +14,7 @@ import org.session.libsession.snode.Version
|
||||
import org.session.libsession.utilities.TextSecurePreferences
|
||||
import org.session.libsignal.utilities.JsonUtil
|
||||
import org.session.libsignal.utilities.Log
|
||||
import org.session.libsignal.utilities.emptyPromise
|
||||
import org.session.libsignal.utilities.retryIfNeeded
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
@ -29,62 +33,57 @@ object PushNotificationAPI {
|
||||
Unsubscribe("unsubscribe_closed_group");
|
||||
}
|
||||
|
||||
fun register(token: String? = TextSecurePreferences.getLegacyFCMToken(context)) {
|
||||
Log.d(TAG, "register: $token")
|
||||
|
||||
register(token, TextSecurePreferences.getLocalNumber(context))
|
||||
fun register(
|
||||
token: String? = TextSecurePreferences.getLegacyFCMToken(context)
|
||||
): Promise<*, Exception> = all(
|
||||
register(token, TextSecurePreferences.getLocalNumber(context)),
|
||||
subscribeGroups()
|
||||
}
|
||||
)
|
||||
|
||||
fun register(token: String?, publicKey: String?) {
|
||||
Log.d(TAG, "register($token)")
|
||||
|
||||
token ?: return
|
||||
publicKey ?: return
|
||||
val parameters = mapOf("token" to token, "pubKey" to publicKey)
|
||||
val url = "$legacyServer/register"
|
||||
val body =
|
||||
RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body)
|
||||
fun register(token: String?, publicKey: String?): Promise<Unit, Exception> =
|
||||
retryIfNeeded(maxRetryCount) {
|
||||
OnionRequestAPI.sendOnionRequest(request.build(), legacyServer, legacyServerPublicKey, Version.V2)
|
||||
.map { response ->
|
||||
val code = response.info["code"] as? Int
|
||||
if (code != null && code != 0) {
|
||||
TextSecurePreferences.setLegacyFCMToken(context, token)
|
||||
} else {
|
||||
Log.d(TAG, "Couldn't register for FCM due to error: ${response.info["message"] as? String ?: "null"}.")
|
||||
}
|
||||
}.fail { exception ->
|
||||
val parameters = mapOf("token" to token!!, "pubKey" to publicKey!!)
|
||||
val url = "$legacyServer/register"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
|
||||
OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response ->
|
||||
when (response.info["code"]) {
|
||||
null, 0 -> throw Exception("error: ${response.info["message"]}.")
|
||||
else -> TextSecurePreferences.setLegacyFCMToken(context, token)
|
||||
}
|
||||
} fail { exception ->
|
||||
Log.d(TAG, "Couldn't register for FCM due to error: ${exception}.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister push notifications for 1-1 conversations as this is now done in FirebasePushManager.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun unregister() {
|
||||
val token = TextSecurePreferences.getLegacyFCMToken(context) ?: return
|
||||
fun unregister(): Promise<*, Exception> {
|
||||
val token = TextSecurePreferences.getLegacyFCMToken(context) ?: emptyPromise()
|
||||
|
||||
val parameters = mapOf( "token" to token )
|
||||
val url = "$legacyServer/unregister"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
retryIfNeeded(maxRetryCount) {
|
||||
OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response ->
|
||||
TextSecurePreferences.clearLegacyFCMToken(context)
|
||||
when (response.info["code"]) {
|
||||
null, 0 -> Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${response.info["message"]}.")
|
||||
return retryIfNeeded(maxRetryCount) {
|
||||
val parameters = mapOf( "token" to token )
|
||||
val url = "$legacyServer/unregister"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
|
||||
OnionRequestAPI.sendOnionRequest(
|
||||
request,
|
||||
legacyServer,
|
||||
legacyServerPublicKey,
|
||||
Version.V2
|
||||
) success {
|
||||
when (it.info["code"]) {
|
||||
null, 0 -> throw Exception("error: ${it.info["message"]}.")
|
||||
else -> Log.d(TAG, "unregisterV1 success token: $token")
|
||||
}
|
||||
}.fail { exception ->
|
||||
Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${exception}.")
|
||||
TextSecurePreferences.clearLegacyFCMToken(context)
|
||||
} fail {
|
||||
exception -> Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${exception}.")
|
||||
}
|
||||
}
|
||||
|
||||
unsubscribeGroups()
|
||||
}
|
||||
|
||||
// Legacy Closed Groups
|
||||
@ -97,7 +96,7 @@ object PushNotificationAPI {
|
||||
private fun subscribeGroups(
|
||||
closedGroupPublicKeys: Collection<String> = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys(),
|
||||
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
|
||||
) = closedGroupPublicKeys.forEach { performGroupOperation(ClosedGroupOperation.Subscribe, it, publicKey) }
|
||||
) = closedGroupPublicKeys.map { performGroupOperation(ClosedGroupOperation.Subscribe, it, publicKey) }.let(::all)
|
||||
|
||||
fun unsubscribeGroup(
|
||||
closedGroupPublicKey: String,
|
||||
@ -107,27 +106,30 @@ object PushNotificationAPI {
|
||||
private fun unsubscribeGroups(
|
||||
closedGroupPublicKeys: Collection<String> = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys(),
|
||||
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
|
||||
) = closedGroupPublicKeys.forEach { performGroupOperation(ClosedGroupOperation.Unsubscribe, it, publicKey) }
|
||||
) = closedGroupPublicKeys.map { performGroupOperation(ClosedGroupOperation.Unsubscribe, it, publicKey) }.let(::all)
|
||||
|
||||
private fun performGroupOperation(
|
||||
operation: ClosedGroupOperation,
|
||||
closedGroupPublicKey: String,
|
||||
publicKey: String
|
||||
) {
|
||||
if (!TextSecurePreferences.isUsingFCM(context)) return
|
||||
|
||||
): Promise<*, Exception> {
|
||||
val parameters = mapOf( "closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey )
|
||||
val url = "$legacyServer/${operation.rawValue}"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
|
||||
retryIfNeeded(maxRetryCount) {
|
||||
OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response ->
|
||||
when (response.info["code"]) {
|
||||
null, 0 -> Log.d(TAG, "performGroupOperation fail: ${operation.rawValue}: $closedGroupPublicKey due to error: ${response.info["message"]}.")
|
||||
return retryIfNeeded(maxRetryCount) {
|
||||
OnionRequestAPI.sendOnionRequest(
|
||||
request,
|
||||
legacyServer,
|
||||
legacyServerPublicKey,
|
||||
Version.V2
|
||||
) success {
|
||||
when (it.info["code"]) {
|
||||
null, 0 -> throw Exception("${it.info["message"]}")
|
||||
else -> Log.d(TAG, "performGroupOperation success: ${operation.rawValue}")
|
||||
}
|
||||
}.fail { exception ->
|
||||
} fail { exception ->
|
||||
Log.d(TAG, "performGroupOperation fail: ${operation.rawValue}: $closedGroupPublicKey due to error: ${exception}.")
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,12 @@ package org.session.libsignal.utilities
|
||||
|
||||
import nl.komponents.kovenant.Promise
|
||||
import nl.komponents.kovenant.deferred
|
||||
import nl.komponents.kovenant.task
|
||||
import java.util.concurrent.TimeoutException
|
||||
|
||||
fun emptyPromise() = EMPTY_PROMISE
|
||||
private val EMPTY_PROMISE: Promise<*, java.lang.Exception> = task {}
|
||||
|
||||
fun <V, E : Throwable> Promise<V, E>.get(defaultValue: V): V {
|
||||
return try {
|
||||
get()
|
||||
|
@ -28,3 +28,4 @@ fun <V, T : Promise<V, Exception>> retryIfNeeded(maxRetryCount: Int, retryInterv
|
||||
retryIfNeeded()
|
||||
return deferred.promise
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user