Improve API, to avoid ignoring errors

This commit is contained in:
Benoit Marty 2024-05-06 23:06:25 +02:00 committed by Benoit Marty
parent d88ef8769e
commit a65c290dd3
18 changed files with 143 additions and 107 deletions

View file

@ -25,6 +25,7 @@ import io.element.android.libraries.push.impl.notifications.DefaultNotificationD
import io.element.android.libraries.pushproviders.api.Distributor
import io.element.android.libraries.pushproviders.api.PushProvider
import io.element.android.libraries.pushstore.api.UserPushStoreFactory
import timber.log.Timber
import javax.inject.Inject
@ContributesBinding(AppScope::class)
@ -53,17 +54,26 @@ class DefaultPushService @Inject constructor(
/**
* Get current push provider, compare with provided one, then unregister and register if different, and store change.
*/
override suspend fun registerWith(matrixClient: MatrixClient, pushProvider: PushProvider, distributor: Distributor) {
override suspend fun registerWith(
matrixClient: MatrixClient,
pushProvider: PushProvider,
distributor: Distributor,
): Result<Unit> {
val userPushStore = userPushStoreFactory.getOrCreate(matrixClient.sessionId)
val currentPushProviderName = userPushStore.getPushProviderName()
val currentDistributorValue = pushProvider.getCurrentDistributor(matrixClient)?.value
if (currentPushProviderName != pushProvider.name || currentDistributorValue != distributor.value) {
// Unregister previous one if any
pushProviders.find { it.name == currentPushProviderName }?.unregister(matrixClient)
?.onFailure {
Timber.w(it, "Failed to unregister previous push provider")
}
}
pushProvider.registerWith(matrixClient, distributor)
// Store new value
userPushStore.setPushProviderName(pushProvider.name)
return pushProvider.registerWith(matrixClient, distributor)
.onSuccess {
// Store new value
userPushStore.setPushProviderName(pushProvider.name)
}
}
override suspend fun testPush(): Boolean {

View file

@ -63,22 +63,26 @@ class PushersManager @Inject constructor(
/**
* Register a pusher to the server if not done yet.
*/
override suspend fun registerPusher(matrixClient: MatrixClient, pushKey: String, gateway: String) {
override suspend fun registerPusher(
matrixClient: MatrixClient,
pushKey: String,
gateway: String,
): Result<Unit> {
val userDataStore = userPushStoreFactory.getOrCreate(matrixClient.sessionId)
if (userDataStore.getCurrentRegisteredPushKey() == pushKey) {
Timber.tag(loggerTag.value)
.d("Unnecessary to register again the same pusher, but do it in case the pusher has been removed from the server")
}
matrixClient.pushersService().setHttpPusher(
createHttpPusher(pushKey, gateway, matrixClient.sessionId)
).fold(
{
return matrixClient.pushersService()
.setHttpPusher(
createHttpPusher(pushKey, gateway, matrixClient.sessionId)
)
.onSuccess {
userDataStore.setCurrentRegisteredPushKey(pushKey)
},
{ throwable ->
}
.onFailure { throwable ->
Timber.tag(loggerTag.value).e(throwable, "Unable to register the pusher")
}
)
}
private suspend fun createHttpPusher(
@ -107,13 +111,25 @@ class PushersManager @Inject constructor(
return "{\"cs\":\"$secretForUser\"}"
}
override suspend fun unregisterPusher(matrixClient: MatrixClient, pushKey: String, gateway: String) {
matrixClient.pushersService().unsetHttpPusher(
unsetHttpPusherData = UnsetHttpPusherData(
pushKey = pushKey,
appId = PushConfig.PUSHER_APP_ID
override suspend fun unregisterPusher(
matrixClient: MatrixClient,
pushKey: String,
gateway: String,
): Result<Unit> {
val userDataStore = userPushStoreFactory.getOrCreate(matrixClient.sessionId)
return matrixClient.pushersService()
.unsetHttpPusher(
unsetHttpPusherData = UnsetHttpPusherData(
pushKey = pushKey,
appId = PushConfig.PUSHER_APP_ID
)
)
)
.onSuccess {
userDataStore.setCurrentRegisteredPushKey(null)
}
.onFailure { throwable ->
Timber.tag(loggerTag.value).e(throwable, "Unable to unregister the pusher")
}
}
companion object {