Authenticated media : listen db.

This commit is contained in:
ganfra 2024-07-18 17:52:53 +02:00
parent 9e27705b6e
commit fa0860ac0f
2 changed files with 30 additions and 9 deletions

View File

@ -83,6 +83,7 @@ import org.matrix.android.sdk.internal.session.events.DefaultEventService
import org.matrix.android.sdk.internal.session.homeserver.DefaultHomeServerCapabilitiesService import org.matrix.android.sdk.internal.session.homeserver.DefaultHomeServerCapabilitiesService
import org.matrix.android.sdk.internal.session.identity.DefaultIdentityService import org.matrix.android.sdk.internal.session.identity.DefaultIdentityService
import org.matrix.android.sdk.internal.session.integrationmanager.IntegrationManager import org.matrix.android.sdk.internal.session.integrationmanager.IntegrationManager
import org.matrix.android.sdk.internal.session.media.DefaultIsAuthenticatedMediaSupported
import org.matrix.android.sdk.internal.session.openid.DefaultOpenIdService import org.matrix.android.sdk.internal.session.openid.DefaultOpenIdService
import org.matrix.android.sdk.internal.session.permalinks.DefaultPermalinkService import org.matrix.android.sdk.internal.session.permalinks.DefaultPermalinkService
import org.matrix.android.sdk.internal.session.room.EventRelationsAggregationProcessor import org.matrix.android.sdk.internal.session.room.EventRelationsAggregationProcessor
@ -365,6 +366,10 @@ internal abstract class SessionModule {
@IntoSet @IntoSet
abstract fun bindEventInsertObserver(observer: EventInsertLiveObserver): SessionLifecycleObserver abstract fun bindEventInsertObserver(observer: EventInsertLiveObserver): SessionLifecycleObserver
@Binds
@IntoSet
abstract fun bindIsMediaAuthenticated(observer: DefaultIsAuthenticatedMediaSupported): SessionLifecycleObserver
@Binds @Binds
@IntoSet @IntoSet
abstract fun bindIntegrationManager(manager: IntegrationManager): SessionLifecycleObserver abstract fun bindIntegrationManager(manager: IntegrationManager): SessionLifecycleObserver

View File

@ -18,28 +18,44 @@ package org.matrix.android.sdk.internal.session.media
import com.zhuinden.monarchy.Monarchy import com.zhuinden.monarchy.Monarchy
import io.realm.Realm import io.realm.Realm
import io.realm.RealmResults
import org.matrix.android.sdk.internal.database.RealmLiveEntityObserver
import org.matrix.android.sdk.internal.database.model.HomeServerCapabilitiesEntity import org.matrix.android.sdk.internal.database.model.HomeServerCapabilitiesEntity
import org.matrix.android.sdk.internal.database.query.get
import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.di.SessionDatabase
import org.matrix.android.sdk.internal.session.SessionScope import org.matrix.android.sdk.internal.session.SessionScope
import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@SessionScope @SessionScope
class DefaultIsAuthenticatedMediaSupported @Inject constructor( internal class DefaultIsAuthenticatedMediaSupported @Inject constructor(
@SessionDatabase private val monarchy: Monarchy, @SessionDatabase private val monarchy: Monarchy,
) : IsAuthenticatedMediaSupported { ) :
IsAuthenticatedMediaSupported,
private val canUseAuthenticatedMedia: Boolean by lazy { RealmLiveEntityObserver<HomeServerCapabilitiesEntity>(monarchy.realmConfiguration) {
canUseAuthenticatedMedia()
}
override fun invoke(): Boolean { override fun invoke(): Boolean {
return canUseAuthenticatedMedia return canUseAuthenticatedMedia
} }
private fun canUseAuthenticatedMedia(): Boolean { override val query = Monarchy.Query {
return Realm.getInstance(monarchy.realmConfiguration).use { realm -> it.where(HomeServerCapabilitiesEntity::class.java)
HomeServerCapabilitiesEntity.get(realm)?.canUseAuthenticatedMedia ?: false
} }
override fun onChange(results: RealmResults<HomeServerCapabilitiesEntity>) {
Timber.d("HomeServerCapabilitiesEntity updated.")
canUseAuthenticatedMedia = results.canUseAuthenticatedMedia()
Timber.d("canUseAuthenticatedMedia: $canUseAuthenticatedMedia")
}
private var canUseAuthenticatedMedia = getInitialValue()
private fun getInitialValue(): Boolean {
return Realm.getInstance(monarchy.realmConfiguration).use { realm ->
query.createQuery(realm).findAll().canUseAuthenticatedMedia()
}
}
private fun RealmResults<HomeServerCapabilitiesEntity>.canUseAuthenticatedMedia(): Boolean {
return firstOrNull()?.canUseAuthenticatedMedia ?: false
} }
} }