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.identity.DefaultIdentityService
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.permalinks.DefaultPermalinkService
import org.matrix.android.sdk.internal.session.room.EventRelationsAggregationProcessor
@ -365,6 +366,10 @@ internal abstract class SessionModule {
@IntoSet
abstract fun bindEventInsertObserver(observer: EventInsertLiveObserver): SessionLifecycleObserver
@Binds
@IntoSet
abstract fun bindIsMediaAuthenticated(observer: DefaultIsAuthenticatedMediaSupported): SessionLifecycleObserver
@Binds
@IntoSet
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 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.query.get
import org.matrix.android.sdk.internal.di.SessionDatabase
import org.matrix.android.sdk.internal.session.SessionScope
import timber.log.Timber
import javax.inject.Inject
@SessionScope
class DefaultIsAuthenticatedMediaSupported @Inject constructor(
internal class DefaultIsAuthenticatedMediaSupported @Inject constructor(
@SessionDatabase private val monarchy: Monarchy,
) : IsAuthenticatedMediaSupported {
private val canUseAuthenticatedMedia: Boolean by lazy {
canUseAuthenticatedMedia()
}
) :
IsAuthenticatedMediaSupported,
RealmLiveEntityObserver<HomeServerCapabilitiesEntity>(monarchy.realmConfiguration) {
override fun invoke(): Boolean {
return canUseAuthenticatedMedia
}
private fun canUseAuthenticatedMedia(): Boolean {
return Realm.getInstance(monarchy.realmConfiguration).use { realm ->
HomeServerCapabilitiesEntity.get(realm)?.canUseAuthenticatedMedia ?: false
override val query = Monarchy.Query {
it.where(HomeServerCapabilitiesEntity::class.java)
}
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
}
}