From a2e3a5e1b330d2fffaa98b005960e34ddaaf6ee6 Mon Sep 17 00:00:00 2001 From: Evgenii Kozlov Date: Wed, 28 Jun 2023 18:29:58 +0200 Subject: [PATCH] DROID-1443 App | Tech | Log ignored events in debug mode + feature toggle (#108) --- .../anytypeio/anytype/di/main/EventModule.kt | 6 +- .../interactor/MiddlewareEventChannel.kt | 73 ++++++++++--------- .../interactor/MiddlewareEventMapper.kt | 9 ++- .../anytype/MiddlewareEventChannelTest.kt | 9 ++- 4 files changed, 58 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/com/anytypeio/anytype/di/main/EventModule.kt b/app/src/main/java/com/anytypeio/anytype/di/main/EventModule.kt index 7466b81e45..4267a072ef 100644 --- a/app/src/main/java/com/anytypeio/anytype/di/main/EventModule.kt +++ b/app/src/main/java/com/anytypeio/anytype/di/main/EventModule.kt @@ -1,5 +1,6 @@ package com.anytypeio.anytype.di.main +import com.anytypeio.anytype.core_utils.tools.FeatureToggles import com.anytypeio.anytype.data.auth.account.AccountStatusDataChannel import com.anytypeio.anytype.data.auth.account.AccountStatusRemoteChannel import com.anytypeio.anytype.data.auth.event.EventDataChannel @@ -43,8 +44,9 @@ object EventModule { @Provides @Singleton fun provideEventRemoteChannel( - proxy: EventProxy - ): EventRemoteChannel = MiddlewareEventChannel(events = proxy) + proxy: EventProxy, + featureToggles: FeatureToggles + ): EventRemoteChannel = MiddlewareEventChannel(events = proxy, featureToggles = featureToggles) @JvmStatic @Provides diff --git a/middleware/src/main/java/com/anytypeio/anytype/middleware/interactor/MiddlewareEventChannel.kt b/middleware/src/main/java/com/anytypeio/anytype/middleware/interactor/MiddlewareEventChannel.kt index 973031e1e2..ac6f3b1bc8 100644 --- a/middleware/src/main/java/com/anytypeio/anytype/middleware/interactor/MiddlewareEventChannel.kt +++ b/middleware/src/main/java/com/anytypeio/anytype/middleware/interactor/MiddlewareEventChannel.kt @@ -1,56 +1,26 @@ package com.anytypeio.anytype.middleware.interactor import com.anytypeio.anytype.core_models.Event +import com.anytypeio.anytype.core_utils.tools.FeatureToggles import com.anytypeio.anytype.data.auth.event.EventRemoteChannel import com.anytypeio.anytype.middleware.EventProxy import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.map +import timber.log.Timber class MiddlewareEventChannel( - private val events: EventProxy + private val events: EventProxy, + private val featureToggles: FeatureToggles ) : EventRemoteChannel { - private fun filter(msg: anytype.Event.Message): Boolean { - // TODO move to class property, also we should log non filtered events - val events = listOf( - msg.blockAdd, - msg.blockSetText, - msg.blockSetChildrenIds, - msg.blockSetBackgroundColor, - msg.objectDetailsSet, - msg.objectDetailsAmend, - msg.objectDetailsUnset, - msg.blockDelete, - msg.blockSetLink, - msg.blockSetFile, - msg.blockSetFields, - msg.blockSetBookmark, - msg.blockSetAlign, - msg.blockSetDiv, - msg.blockSetRelation, - msg.blockDataviewRelationSet, - msg.blockDataviewRelationDelete, - msg.blockDataviewViewDelete, - msg.blockDataviewViewOrder, - msg.blockDataviewViewSet, - msg.objectRelationsAmend, - msg.objectRelationsRemove, - msg.blockDataviewViewUpdate, - msg.blockDataviewTargetObjectIdSet, - msg.blockDataviewIsCollectionSet, - msg.blockSetWidget - ) - return events.any { it != null } - } - override fun observeEvents( context: String? ): Flow> = events .flow() .filter { event -> context == null || event.contextId == context } .map { event -> - event.messages.filter { filter(it) }.map { message -> Pair(event.contextId, message) } + event.messages.filter { it.isAccepted() }.map { message -> Pair(event.contextId, message) } } .filter { it.isNotEmpty() } .map { events -> processEvents(events) } @@ -58,4 +28,37 @@ class MiddlewareEventChannel( private fun processEvents(events: List>): List { return events.mapNotNull { (context, event) -> event.toCoreModels(context) } } + + private fun anytype.Event.Message.isAccepted() : Boolean = when { + blockAdd != null -> true + blockSetText != null -> true + blockSetChildrenIds != null -> true + blockSetBackgroundColor != null -> true + objectDetailsSet != null -> true + objectDetailsAmend != null -> true + objectDetailsUnset != null -> true + blockDelete != null -> true + blockSetLink != null -> true + blockSetFile != null -> true + blockSetFields != null -> true + blockSetBookmark != null -> true + blockSetAlign != null -> true + blockSetDiv != null -> true + blockSetRelation != null -> true + blockDataviewRelationSet != null -> true + blockDataviewRelationDelete != null -> true + blockDataviewViewDelete != null -> true + blockDataviewViewOrder != null -> true + blockDataviewViewSet != null -> true + objectRelationsAmend != null -> true + objectRelationsRemove != null -> true + blockDataviewViewUpdate != null -> true + blockDataviewTargetObjectIdSet != null -> true + blockDataviewIsCollectionSet != null -> true + blockSetWidget != null -> true + else -> false.also { + if (featureToggles.isLogMiddlewareInteraction && threadStatus == null) + Timber.w("Ignored event: $this") + } + } } \ No newline at end of file diff --git a/middleware/src/main/java/com/anytypeio/anytype/middleware/interactor/MiddlewareEventMapper.kt b/middleware/src/main/java/com/anytypeio/anytype/middleware/interactor/MiddlewareEventMapper.kt index d4b3a5e9b7..69f60c8eef 100644 --- a/middleware/src/main/java/com/anytypeio/anytype/middleware/interactor/MiddlewareEventMapper.kt +++ b/middleware/src/main/java/com/anytypeio/anytype/middleware/interactor/MiddlewareEventMapper.kt @@ -2,11 +2,13 @@ package com.anytypeio.anytype.middleware.interactor import com.anytypeio.anytype.core_models.Block import com.anytypeio.anytype.core_models.Event +import com.anytypeio.anytype.middleware.BuildConfig import com.anytypeio.anytype.middleware.mappers.MWidgetLayout import com.anytypeio.anytype.middleware.mappers.toCoreModel import com.anytypeio.anytype.middleware.mappers.toCoreModels import com.anytypeio.anytype.middleware.mappers.toCoreModelsAlign import com.anytypeio.anytype.middleware.mappers.toCoreModelsBookmarkState +import timber.log.Timber fun anytype.Event.Message.toCoreModels( context: String @@ -289,5 +291,10 @@ fun anytype.Event.Message.toCoreModels( isCollection = event.value_ ) } - else -> null + else -> { + if (BuildConfig.DEBUG) { + Timber.w("Skipped event while mapping: $this") + } + null + } } \ No newline at end of file diff --git a/middleware/src/test/java/com/anytypeio/anytype/MiddlewareEventChannelTest.kt b/middleware/src/test/java/com/anytypeio/anytype/MiddlewareEventChannelTest.kt index 195c4c0680..95d8cf2fb5 100644 --- a/middleware/src/test/java/com/anytypeio/anytype/MiddlewareEventChannelTest.kt +++ b/middleware/src/test/java/com/anytypeio/anytype/MiddlewareEventChannelTest.kt @@ -5,6 +5,7 @@ import com.anytypeio.anytype.core_models.Event import com.anytypeio.anytype.core_models.RelationFormat import com.anytypeio.anytype.core_models.RelationLink import com.anytypeio.anytype.core_models.ThemeColor +import com.anytypeio.anytype.core_utils.tools.FeatureToggles import com.anytypeio.anytype.middleware.EventProxy import com.anytypeio.anytype.middleware.interactor.MiddlewareEventChannel import com.anytypeio.anytype.middleware.mappers.MRelationLink @@ -24,12 +25,18 @@ class MiddlewareEventChannelTest { @Mock lateinit var proxy: EventProxy + @Mock + lateinit var featureToggles: FeatureToggles + private lateinit var channel: MiddlewareEventChannel @Before fun setup() { MockitoAnnotations.openMocks(this) - channel = MiddlewareEventChannel(proxy) + channel = MiddlewareEventChannel( + events = proxy, + featureToggles = featureToggles + ) } @Test