Properly unregister from the ntfy app when the user logs out.
This commit is contained in:
parent
022cd93653
commit
7a7b5d2dd0
15 changed files with 110 additions and 77 deletions
|
|
@ -9,6 +9,7 @@ package io.element.android.libraries.push.impl
|
|||
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.di.SingleIn
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.push.api.GetCurrentPushProvider
|
||||
|
|
@ -17,17 +18,27 @@ import io.element.android.libraries.push.impl.test.TestPush
|
|||
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 io.element.android.libraries.pushstore.api.clientsecret.PushClientSecretStore
|
||||
import io.element.android.libraries.sessionstorage.api.observer.SessionListener
|
||||
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
@ContributesBinding(AppScope::class, boundType = PushService::class)
|
||||
@SingleIn(AppScope::class)
|
||||
class DefaultPushService @Inject constructor(
|
||||
private val testPush: TestPush,
|
||||
private val userPushStoreFactory: UserPushStoreFactory,
|
||||
private val pushProviders: Set<@JvmSuppressWildcards PushProvider>,
|
||||
private val getCurrentPushProvider: GetCurrentPushProvider,
|
||||
) : PushService {
|
||||
private val sessionObserver: SessionObserver,
|
||||
private val pushClientSecretStore: PushClientSecretStore,
|
||||
) : PushService, SessionListener {
|
||||
init {
|
||||
observeSessions()
|
||||
}
|
||||
|
||||
override suspend fun getCurrentPushProvider(): PushProvider? {
|
||||
val currentPushProvider = getCurrentPushProvider.getCurrentPushProvider()
|
||||
return pushProviders.find { it.name == currentPushProvider }
|
||||
|
|
@ -87,4 +98,31 @@ class DefaultPushService @Inject constructor(
|
|||
testPush.execute(config)
|
||||
return true
|
||||
}
|
||||
|
||||
private fun observeSessions() {
|
||||
sessionObserver.addListener(this)
|
||||
}
|
||||
|
||||
override suspend fun onSessionCreated(userId: String) {
|
||||
// Nothing to do
|
||||
}
|
||||
|
||||
/**
|
||||
* The session has been deleted.
|
||||
* In this case, this is not necessary to unregister the pusher from the homeserver,
|
||||
* but we need to do some cleanup locally.
|
||||
* The current push provider may want to take action, and we need to
|
||||
* cleanup the stores.
|
||||
*/
|
||||
override suspend fun onSessionDeleted(userId: String) {
|
||||
val sessionId = SessionId(userId)
|
||||
val userPushStore = userPushStoreFactory.getOrCreate(sessionId)
|
||||
val currentPushProviderName = userPushStore.getPushProviderName()
|
||||
val currentPushProvider = pushProviders.find { it.name == currentPushProviderName }
|
||||
// Cleanup the current push provider. They may need the client secret, so delete the secret after.
|
||||
currentPushProvider?.onSessionDeleted(sessionId)
|
||||
// Now we can safely reset the stores.
|
||||
pushClientSecretStore.resetSecret(sessionId)
|
||||
userPushStore.reset()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue