Improve API, to avoid ignoring errors
This commit is contained in:
committed by
Benoit Marty
parent
591df14450
commit
4bd01b6f4f
@@ -42,7 +42,7 @@ interface PushProvider {
|
||||
/**
|
||||
* Register the pusher to the homeserver.
|
||||
*/
|
||||
suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor)
|
||||
suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor): Result<Unit>
|
||||
|
||||
/**
|
||||
* Return the current distributor, or null if none.
|
||||
@@ -52,7 +52,7 @@ interface PushProvider {
|
||||
/**
|
||||
* Unregister the pusher.
|
||||
*/
|
||||
suspend fun unregister(matrixClient: MatrixClient)
|
||||
suspend fun unregister(matrixClient: MatrixClient): Result<Unit>
|
||||
|
||||
suspend fun getCurrentUserPushConfig(): CurrentUserPushConfig?
|
||||
}
|
||||
|
||||
@@ -19,6 +19,6 @@ package io.element.android.libraries.pushproviders.api
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
|
||||
interface PusherSubscriber {
|
||||
suspend fun registerPusher(matrixClient: MatrixClient, pushKey: String, gateway: String)
|
||||
suspend fun unregisterPusher(matrixClient: MatrixClient, pushKey: String, gateway: String)
|
||||
suspend fun registerPusher(matrixClient: MatrixClient, pushKey: String, gateway: String): Result<Unit>
|
||||
suspend fun unregisterPusher(matrixClient: MatrixClient, pushKey: String, gateway: String): Result<Unit>
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package io.element.android.libraries.pushproviders.firebase
|
||||
|
||||
import io.element.android.libraries.core.extensions.flatMap
|
||||
import io.element.android.libraries.core.log.logger.LoggerTag
|
||||
import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
@@ -46,9 +47,17 @@ class FirebaseNewTokenHandler @Inject constructor(
|
||||
.forEach { userId ->
|
||||
val userDataStore = userPushStoreFactory.getOrCreate(userId)
|
||||
if (userDataStore.getPushProviderName() == FirebaseConfig.NAME) {
|
||||
matrixAuthenticationService.restoreSession(userId).getOrNull()?.use { client ->
|
||||
pusherSubscriber.registerPusher(client, firebaseToken, FirebaseConfig.PUSHER_HTTP_URL)
|
||||
}
|
||||
matrixAuthenticationService
|
||||
.restoreSession(userId)
|
||||
.onFailure {
|
||||
Timber.tag(loggerTag.value).e(it, "Failed to restore session $userId")
|
||||
}
|
||||
.flatMap { client ->
|
||||
pusherSubscriber.registerPusher(client, firebaseToken, FirebaseConfig.PUSHER_HTTP_URL)
|
||||
}
|
||||
.onFailure {
|
||||
Timber.tag(loggerTag.value).e(it, "Failed to register pusher for session $userId")
|
||||
}
|
||||
} else {
|
||||
Timber.tag(loggerTag.value).d("This session is not using Firebase pusher")
|
||||
}
|
||||
|
||||
@@ -46,20 +46,28 @@ class FirebasePushProvider @Inject constructor(
|
||||
return listOf(firebaseDistributor)
|
||||
}
|
||||
|
||||
override suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor) {
|
||||
val pushKey = firebaseStore.getFcmToken() ?: return Unit.also {
|
||||
override suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor): Result<Unit> {
|
||||
val pushKey = firebaseStore.getFcmToken() ?: return Result.failure<Unit>(
|
||||
IllegalStateException(
|
||||
"Unable to register pusher, Firebase token is not known."
|
||||
)
|
||||
).also {
|
||||
Timber.tag(loggerTag.value).w("Unable to register pusher, Firebase token is not known.")
|
||||
}
|
||||
pusherSubscriber.registerPusher(matrixClient, pushKey, FirebaseConfig.PUSHER_HTTP_URL)
|
||||
return pusherSubscriber.registerPusher(matrixClient, pushKey, FirebaseConfig.PUSHER_HTTP_URL)
|
||||
}
|
||||
|
||||
override suspend fun getCurrentDistributor(matrixClient: MatrixClient) = firebaseDistributor
|
||||
|
||||
override suspend fun unregister(matrixClient: MatrixClient) {
|
||||
val pushKey = firebaseStore.getFcmToken() ?: return Unit.also {
|
||||
override suspend fun unregister(matrixClient: MatrixClient): Result<Unit> {
|
||||
val pushKey = firebaseStore.getFcmToken() ?: return Result.failure<Unit>(
|
||||
IllegalStateException(
|
||||
"Unable to unregister pusher, Firebase token is not known."
|
||||
)
|
||||
).also {
|
||||
Timber.tag(loggerTag.value).w("Unable to unregister pusher, Firebase token is not known.")
|
||||
}
|
||||
pusherSubscriber.unregisterPusher(matrixClient, pushKey, FirebaseConfig.PUSHER_HTTP_URL)
|
||||
return pusherSubscriber.unregisterPusher(matrixClient, pushKey, FirebaseConfig.PUSHER_HTTP_URL)
|
||||
}
|
||||
|
||||
override suspend fun getCurrentUserPushConfig(): CurrentUserPushConfig? {
|
||||
|
||||
@@ -31,12 +31,12 @@ class FakePushProvider(
|
||||
|
||||
override fun getDistributors(): List<Distributor> = distributors
|
||||
|
||||
override suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor) {
|
||||
// No-op
|
||||
override suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor): Result<Unit> {
|
||||
return Result.success(Unit)
|
||||
}
|
||||
|
||||
override suspend fun unregister(matrixClient: MatrixClient) {
|
||||
// No-op
|
||||
override suspend fun unregister(matrixClient: MatrixClient): Result<Unit> {
|
||||
return Result.success(Unit)
|
||||
}
|
||||
|
||||
override suspend fun getCurrentUserPushConfig(): CurrentUserPushConfig? {
|
||||
|
||||
@@ -18,55 +18,18 @@ package io.element.android.libraries.pushproviders.unifiedpush
|
||||
|
||||
import android.content.Context
|
||||
import io.element.android.libraries.di.ApplicationContext
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
import io.element.android.libraries.pushproviders.api.Distributor
|
||||
import io.element.android.libraries.pushproviders.api.PusherSubscriber
|
||||
import org.unifiedpush.android.connector.UnifiedPush
|
||||
import javax.inject.Inject
|
||||
|
||||
class RegisterUnifiedPushUseCase @Inject constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
private val pusherSubscriber: PusherSubscriber,
|
||||
private val unifiedPushStore: UnifiedPushStore,
|
||||
) {
|
||||
sealed interface RegisterUnifiedPushResult {
|
||||
data object Success : RegisterUnifiedPushResult
|
||||
data object NeedToAskUserForDistributor : RegisterUnifiedPushResult
|
||||
data object Error : RegisterUnifiedPushResult
|
||||
}
|
||||
|
||||
suspend fun execute(matrixClient: MatrixClient, distributor: Distributor, clientSecret: String): RegisterUnifiedPushResult {
|
||||
val distributorValue = distributor.value
|
||||
if (distributorValue.isNotEmpty()) {
|
||||
saveAndRegisterApp(distributorValue, clientSecret)
|
||||
val endpoint = unifiedPushStore.getEndpoint(clientSecret) ?: return RegisterUnifiedPushResult.Error
|
||||
val gateway = unifiedPushStore.getPushGateway(clientSecret) ?: return RegisterUnifiedPushResult.Error
|
||||
pusherSubscriber.registerPusher(matrixClient, endpoint, gateway)
|
||||
return RegisterUnifiedPushResult.Success
|
||||
}
|
||||
|
||||
// TODO Below should never happen?
|
||||
if (UnifiedPush.getDistributor(context).isNotEmpty()) {
|
||||
registerApp(clientSecret)
|
||||
return RegisterUnifiedPushResult.Success
|
||||
}
|
||||
|
||||
val distributors = UnifiedPush.getDistributors(context)
|
||||
|
||||
return if (distributors.size == 1) {
|
||||
saveAndRegisterApp(distributors.first(), clientSecret)
|
||||
RegisterUnifiedPushResult.Success
|
||||
} else {
|
||||
RegisterUnifiedPushResult.NeedToAskUserForDistributor
|
||||
}
|
||||
}
|
||||
|
||||
private fun saveAndRegisterApp(distributor: String, clientSecret: String) {
|
||||
UnifiedPush.saveDistributor(context, distributor)
|
||||
registerApp(clientSecret)
|
||||
}
|
||||
|
||||
private fun registerApp(clientSecret: String) {
|
||||
fun execute(distributor: Distributor, clientSecret: String): Result<Unit> {
|
||||
UnifiedPush.saveDistributor(context, distributor.value)
|
||||
// This will trigger the callback
|
||||
// VectorUnifiedPushMessagingReceiver.onNewEndpoint
|
||||
UnifiedPush.registerApp(context = context, instance = clientSecret)
|
||||
return Result.success(Unit)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package io.element.android.libraries.pushproviders.unifiedpush
|
||||
|
||||
import io.element.android.libraries.core.extensions.flatMap
|
||||
import io.element.android.libraries.core.log.logger.LoggerTag
|
||||
import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService
|
||||
import io.element.android.libraries.pushproviders.api.PusherSubscriber
|
||||
@@ -35,18 +36,25 @@ class UnifiedPushNewGatewayHandler @Inject constructor(
|
||||
private val pushClientSecret: PushClientSecret,
|
||||
private val matrixAuthenticationService: MatrixAuthenticationService,
|
||||
) {
|
||||
suspend fun handle(endpoint: String, pushGateway: String, clientSecret: String) {
|
||||
suspend fun handle(endpoint: String, pushGateway: String, clientSecret: String): Result<Unit> {
|
||||
// Register the pusher for the session with this client secret, if is it using UnifiedPush.
|
||||
val userId = pushClientSecret.getUserIdFromSecret(clientSecret) ?: return Unit.also {
|
||||
val userId = pushClientSecret.getUserIdFromSecret(clientSecret) ?: return Result.failure<Unit>(
|
||||
IllegalStateException("Unable to retrieve session")
|
||||
).also {
|
||||
Timber.w("Unable to retrieve session")
|
||||
}
|
||||
val userDataStore = userPushStoreFactory.getOrCreate(userId)
|
||||
if (userDataStore.getPushProviderName() == UnifiedPushConfig.NAME) {
|
||||
matrixAuthenticationService.restoreSession(userId).getOrNull()?.use { client ->
|
||||
pusherSubscriber.registerPusher(client, endpoint, pushGateway)
|
||||
}
|
||||
return if (userDataStore.getPushProviderName() == UnifiedPushConfig.NAME) {
|
||||
matrixAuthenticationService
|
||||
.restoreSession(userId)
|
||||
.flatMap { client ->
|
||||
pusherSubscriber.registerPusher(client, endpoint, pushGateway)
|
||||
}
|
||||
} else {
|
||||
Timber.tag(loggerTag.value).d("This session is not using UnifiedPush pusher")
|
||||
Result.failure(
|
||||
IllegalStateException("This session is not using UnifiedPush pusher")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,10 +58,12 @@ class UnifiedPushProvider @Inject constructor(
|
||||
return unifiedPushDistributorProvider.getDistributors()
|
||||
}
|
||||
|
||||
override suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor) {
|
||||
override suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor): Result<Unit> {
|
||||
val clientSecret = pushClientSecret.getSecretForUser(matrixClient.sessionId)
|
||||
registerUnifiedPushUseCase.execute(matrixClient, distributor, clientSecret)
|
||||
unifiedPushStore.setDistributorValue(matrixClient.sessionId, distributor.value)
|
||||
return registerUnifiedPushUseCase.execute(distributor, clientSecret)
|
||||
.onSuccess {
|
||||
unifiedPushStore.setDistributorValue(matrixClient.sessionId, distributor.value)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getCurrentDistributor(matrixClient: MatrixClient): Distributor? {
|
||||
@@ -69,9 +71,9 @@ class UnifiedPushProvider @Inject constructor(
|
||||
return getDistributors().find { it.value == distributorValue }
|
||||
}
|
||||
|
||||
override suspend fun unregister(matrixClient: MatrixClient) {
|
||||
override suspend fun unregister(matrixClient: MatrixClient): Result<Unit> {
|
||||
val clientSecret = pushClientSecret.getSecretForUser(matrixClient.sessionId)
|
||||
unRegisterUnifiedPushUseCase.execute(matrixClient, clientSecret)
|
||||
return unRegisterUnifiedPushUseCase.execute(matrixClient, clientSecret)
|
||||
}
|
||||
|
||||
override suspend fun getCurrentUserPushConfig(): CurrentUserPushConfig? {
|
||||
|
||||
@@ -21,7 +21,6 @@ import io.element.android.libraries.di.ApplicationContext
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
import io.element.android.libraries.pushproviders.api.PusherSubscriber
|
||||
import org.unifiedpush.android.connector.UnifiedPush
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
class UnregisterUnifiedPushUseCase @Inject constructor(
|
||||
@@ -29,18 +28,17 @@ class UnregisterUnifiedPushUseCase @Inject constructor(
|
||||
private val unifiedPushStore: UnifiedPushStore,
|
||||
private val pusherSubscriber: PusherSubscriber,
|
||||
) {
|
||||
suspend fun execute(matrixClient: MatrixClient, clientSecret: String) {
|
||||
suspend fun execute(matrixClient: MatrixClient, clientSecret: String): Result<Unit> {
|
||||
val endpoint = unifiedPushStore.getEndpoint(clientSecret)
|
||||
val gateway = unifiedPushStore.getPushGateway(clientSecret)
|
||||
if (endpoint != null && gateway != null) {
|
||||
try {
|
||||
pusherSubscriber.unregisterPusher(matrixClient, endpoint, gateway)
|
||||
} catch (e: Exception) {
|
||||
Timber.d(e, "Probably unregistering a non existing pusher")
|
||||
}
|
||||
if (endpoint == null || gateway == null) {
|
||||
return Result.failure(IllegalStateException("No endpoint or gateway found for client secret"))
|
||||
}
|
||||
unifiedPushStore.storeUpEndpoint(null, clientSecret)
|
||||
unifiedPushStore.storePushGateway(null, clientSecret)
|
||||
UnifiedPush.unregisterApp(context)
|
||||
return pusherSubscriber.unregisterPusher(matrixClient, endpoint, gateway)
|
||||
.onSuccess {
|
||||
unifiedPushStore.storeUpEndpoint(null, clientSecret)
|
||||
unifiedPushStore.storePushGateway(null, clientSecret)
|
||||
UnifiedPush.unregisterApp(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,12 +73,17 @@ class VectorUnifiedPushMessagingReceiver : MessagingReceiver() {
|
||||
// If the endpoint has changed
|
||||
// or the gateway has changed
|
||||
if (unifiedPushStore.getEndpoint(instance) != endpoint) {
|
||||
unifiedPushStore.storeUpEndpoint(endpoint, instance)
|
||||
coroutineScope.launch {
|
||||
val gateway = unifiedPushGatewayResolver.getGateway(endpoint)
|
||||
unifiedPushStore.storePushGateway(gateway, instance)
|
||||
gateway?.let { pushGateway ->
|
||||
newGatewayHandler.handle(endpoint, pushGateway, instance)
|
||||
.onFailure {
|
||||
Timber.tag(loggerTag.value).e("Failed to handle new gateway")
|
||||
}
|
||||
.onSuccess {
|
||||
unifiedPushStore.storeUpEndpoint(endpoint, instance)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user