Push distributor: ensure the current UnifiedPush distributor is stored
This commit is contained in:
committed by
Benoit Marty
parent
ae9e489400
commit
3d5fdfc22e
@@ -16,6 +16,14 @@
|
||||
|
||||
package io.element.android.libraries.pushproviders.api
|
||||
|
||||
/**
|
||||
* Firebase does not have the concept of distributor. So for Firebase, there will be one distributor:
|
||||
* Distributor("Firebase", "Firebase").
|
||||
*
|
||||
* For UnifiedPush, for instance, the Distributor can be:
|
||||
* Distributor("io.heckel.ntfy", "ntfy").
|
||||
* But other values are possible.
|
||||
*/
|
||||
data class Distributor(
|
||||
val value: String,
|
||||
val name: String,
|
||||
|
||||
@@ -44,6 +44,11 @@ interface PushProvider {
|
||||
*/
|
||||
suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor)
|
||||
|
||||
/**
|
||||
* Return the current distributor, or null if none.
|
||||
*/
|
||||
suspend fun getCurrentDistributor(matrixClient: MatrixClient): Distributor?
|
||||
|
||||
/**
|
||||
* Unregister the pusher.
|
||||
*/
|
||||
|
||||
@@ -43,7 +43,7 @@ class FirebasePushProvider @Inject constructor(
|
||||
}
|
||||
|
||||
override fun getDistributors(): List<Distributor> {
|
||||
return listOf(Distributor("Firebase", "Firebase"))
|
||||
return listOf(firebaseDistributor)
|
||||
}
|
||||
|
||||
override suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor) {
|
||||
@@ -53,6 +53,8 @@ class FirebasePushProvider @Inject constructor(
|
||||
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 {
|
||||
Timber.tag(loggerTag.value).w("Unable to unregister pusher, Firebase token is not known.")
|
||||
@@ -68,4 +70,8 @@ class FirebasePushProvider @Inject constructor(
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val firebaseDistributor = Distributor("Firebase", "Firebase")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,6 +61,12 @@ class UnifiedPushProvider @Inject constructor(
|
||||
override suspend fun registerWith(matrixClient: MatrixClient, distributor: Distributor) {
|
||||
val clientSecret = pushClientSecret.getSecretForUser(matrixClient.sessionId)
|
||||
registerUnifiedPushUseCase.execute(matrixClient, distributor, clientSecret)
|
||||
unifiedPushStore.setDistributorValue(matrixClient.sessionId, distributor.value)
|
||||
}
|
||||
|
||||
override suspend fun getCurrentDistributor(matrixClient: MatrixClient): Distributor? {
|
||||
val distributorValue = unifiedPushStore.getDistributorValue(matrixClient.sessionId)
|
||||
return getDistributors().find { it.value == distributorValue }
|
||||
}
|
||||
|
||||
override suspend fun unregister(matrixClient: MatrixClient) {
|
||||
|
||||
@@ -21,6 +21,7 @@ import android.content.SharedPreferences
|
||||
import androidx.core.content.edit
|
||||
import io.element.android.libraries.di.ApplicationContext
|
||||
import io.element.android.libraries.di.DefaultPreferences
|
||||
import io.element.android.libraries.matrix.api.core.UserId
|
||||
import javax.inject.Inject
|
||||
|
||||
class UnifiedPushStore @Inject constructor(
|
||||
@@ -71,8 +72,19 @@ class UnifiedPushStore @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun getDistributorValue(userId: UserId): String? {
|
||||
return defaultPrefs.getString(PREFS_DISTRIBUTOR + userId, null)
|
||||
}
|
||||
|
||||
fun setDistributorValue(userId: UserId, value: String) {
|
||||
defaultPrefs.edit {
|
||||
putString(PREFS_DISTRIBUTOR + userId, value)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val PREFS_ENDPOINT_OR_TOKEN = "UP_ENDPOINT_OR_TOKEN"
|
||||
private const val PREFS_PUSH_GATEWAY = "PUSH_GATEWAY"
|
||||
private const val PREFS_DISTRIBUTOR = "DISTRIBUTOR"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user