session-android/app/src/main/java/org/thoughtcrime/securesms/notifications/PushRegistry.kt

112 lines
3.5 KiB
Kotlin
Raw Normal View History

2023-07-26 03:18:20 +02:00
package org.thoughtcrime.securesms.notifications
import android.content.Context
import com.goterl.lazysodium.utils.KeyPair
2023-07-26 07:06:06 +02:00
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
2023-08-06 14:52:39 +02:00
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
2023-07-26 03:18:20 +02:00
import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.combine.and
2023-08-06 14:52:39 +02:00
import org.session.libsession.messaging.sending_receiving.notifications.PushRegistryV1
2023-07-26 03:18:20 +02:00
import org.session.libsession.utilities.Device
import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsignal.utilities.Log
import org.session.libsignal.utilities.Namespace
import org.session.libsignal.utilities.emptyPromise
import org.thoughtcrime.securesms.crypto.KeyPairUtilities
import javax.inject.Inject
import javax.inject.Singleton
2023-08-06 15:09:49 +02:00
private val TAG = PushRegistry::class.java.name
2023-07-26 03:18:20 +02:00
@Singleton
2023-08-06 14:52:39 +02:00
class PushRegistry @Inject constructor(
2023-07-26 07:06:06 +02:00
@ApplicationContext private val context: Context,
2023-07-26 03:18:20 +02:00
private val device: Device,
private val tokenManager: TokenManager,
2023-08-06 14:52:39 +02:00
private val pushRegistryV2: PushRegistryV2,
private val prefs: TextSecurePreferences,
private val tokenFetcher: TokenFetcher,
2023-07-26 03:18:20 +02:00
) {
2023-08-06 14:52:39 +02:00
2023-08-06 15:09:49 +02:00
private var pushRegistrationJob: Job? = null
2023-08-06 14:52:39 +02:00
fun refresh(force: Boolean): Job {
2023-08-06 14:52:39 +02:00
Log.d(TAG, "refresh() called with: force = $force")
2023-08-06 15:09:49 +02:00
pushRegistrationJob?.apply {
if (force) cancel() else if (isActive) return MainScope().launch {}
2023-08-06 14:52:39 +02:00
}
return MainScope().launch(Dispatchers.IO) {
try {
register(tokenFetcher.fetch()).get()
} catch (e: Exception) {
2023-08-11 10:07:35 +02:00
Log.e(TAG, "register failed", e)
}
}.also { pushRegistrationJob = it }
2023-08-06 14:52:39 +02:00
}
fun register(token: String?): Promise<*, Exception> {
2023-08-11 10:07:35 +02:00
Log.d(TAG, "refresh() called")
if (token?.isNotEmpty() != true) return emptyPromise()
prefs.setPushToken(token)
2023-07-26 03:18:20 +02:00
val userPublicKey = prefs.getLocalNumber() ?: return emptyPromise()
2023-07-26 03:18:20 +02:00
val userEdKey = KeyPairUtilities.getUserED25519KeyPair(context) ?: return emptyPromise()
return when {
prefs.isPushEnabled() -> register(token, userPublicKey, userEdKey)
tokenManager.isRegistered -> unregister(token, userPublicKey, userEdKey)
2023-07-26 03:18:20 +02:00
else -> emptyPromise()
}
}
/**
* Register for push notifications.
*/
private fun register(
token: String,
publicKey: String,
userEd25519Key: KeyPair,
namespaces: List<Int> = listOf(Namespace.DEFAULT())
2023-07-26 07:06:06 +02:00
): Promise<*, Exception> {
2023-08-11 10:07:35 +02:00
Log.d(TAG, "register() called")
2023-08-03 06:44:55 +02:00
2023-08-06 14:52:39 +02:00
val v1 = PushRegistryV1.register(
2023-07-26 07:06:06 +02:00
device = device,
token = token,
publicKey = publicKey
) fail {
Log.e(TAG, "register v1 failed", it)
}
2023-08-06 14:52:39 +02:00
val v2 = pushRegistryV2.register(
2023-07-26 07:18:15 +02:00
device, token, publicKey, userEd25519Key, namespaces
2023-07-26 07:06:06 +02:00
) fail {
Log.e(TAG, "register v2 failed", it)
}
return v1 and v2 success {
Log.d(TAG, "register v1 & v2 success")
tokenManager.register()
2023-07-26 07:06:06 +02:00
}
2023-07-26 03:18:20 +02:00
}
private fun unregister(
token: String,
userPublicKey: String,
userEdKey: KeyPair
2023-08-06 14:52:39 +02:00
): Promise<*, Exception> = PushRegistryV1.unregister() and pushRegistryV2.unregister(
2023-07-26 07:18:15 +02:00
device, token, userPublicKey, userEdKey
2023-07-26 03:18:20 +02:00
) fail {
Log.e(TAG, "unregisterBoth failed", it)
} success {
tokenManager.unregister()
2023-07-26 03:18:20 +02:00
}
}