From 1fdad38b9db0771b9e08690cd8a0f331ae663607 Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 3 Jul 2019 11:59:45 +0200 Subject: [PATCH] Check Push rule on sync only + fix bad room name in notif --- .../android/internal/session/SessionModule.kt | 5 -- .../session/notification/BingRuleWatcher.kt | 62 ------------------- .../notification/ProcessEventForPushTask.kt | 34 +++++++++- .../internal/session/room/DefaultRoom.kt | 2 +- .../internal/session/sync/RoomSyncHandler.kt | 39 +++++++++--- 5 files changed, 62 insertions(+), 80 deletions(-) delete mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/BingRuleWatcher.kt diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt index 1a3abc409c..8f580da11e 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt @@ -35,7 +35,6 @@ import im.vector.matrix.android.internal.di.Unauthenticated import im.vector.matrix.android.internal.network.AccessTokenInterceptor import im.vector.matrix.android.internal.network.RetrofitFactory import im.vector.matrix.android.internal.session.group.GroupSummaryUpdater -import im.vector.matrix.android.internal.session.notification.BingRuleWatcher import im.vector.matrix.android.internal.session.room.EventRelationsAggregationUpdater import im.vector.matrix.android.internal.session.room.prune.EventsPruner import im.vector.matrix.android.internal.session.user.UserEntityUpdater @@ -129,10 +128,6 @@ internal abstract class SessionModule { @IntoSet abstract fun bindEventRelationsAggregationUpdater(groupSummaryUpdater: EventRelationsAggregationUpdater): LiveEntityObserver - @Binds - @IntoSet - abstract fun bindBingRuleWatcher(bingRuleWatcher: BingRuleWatcher): LiveEntityObserver - @Binds @IntoSet abstract fun bindUserEntityUpdater(groupSummaryUpdater: UserEntityUpdater): LiveEntityObserver diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/BingRuleWatcher.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/BingRuleWatcher.kt deleted file mode 100644 index e2c299be72..0000000000 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/BingRuleWatcher.kt +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2019 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package im.vector.matrix.android.internal.session.notification - -import com.zhuinden.monarchy.Monarchy -import im.vector.matrix.android.api.auth.data.SessionParams -import im.vector.matrix.android.api.session.events.model.EventType -import im.vector.matrix.android.internal.database.RealmLiveEntityObserver -import im.vector.matrix.android.internal.database.mapper.asDomain -import im.vector.matrix.android.internal.database.model.EventEntity -import im.vector.matrix.android.internal.database.query.types -import im.vector.matrix.android.internal.session.sync.SyncTokenStore -import im.vector.matrix.android.internal.task.TaskExecutor -import im.vector.matrix.android.internal.task.configureWith -import javax.inject.Inject - - -internal class BingRuleWatcher @Inject constructor(monarchy: Monarchy, - private val task: ProcessEventForPushTask, - private val defaultPushRuleService: DefaultPushRuleService, - private val sessionParams: SessionParams, - private val tokenStore: SyncTokenStore, - private val taskExecutor: TaskExecutor) : - RealmLiveEntityObserver(monarchy) { - - override val query = Monarchy.Query { - - EventEntity.types(it, listOf( - EventType.MESSAGE, - EventType.REDACTION, - EventType.ENCRYPTED) - ) - - } - - override fun processChanges(inserted: List, updated: List, deleted: List) { - if (tokenStore.getLastToken() == null) return //no notif on initial sync - // TODO Use const for "global" - val rules = defaultPushRuleService.getPushRules("global") - inserted.map { it.asDomain() } - .filter { it.senderId != sessionParams.credentials.userId } - .let { events -> - task.configureWith(ProcessEventForPushTask.Params(events, rules)) - .executeBy(taskExecutor) - } - } - - -} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/ProcessEventForPushTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/ProcessEventForPushTask.kt index 2c13970ae7..6989cb4cfc 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/ProcessEventForPushTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/ProcessEventForPushTask.kt @@ -20,15 +20,17 @@ import arrow.core.Try import im.vector.matrix.android.api.auth.data.SessionParams import im.vector.matrix.android.api.pushrules.rest.PushRule import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.api.session.events.model.EventType import im.vector.matrix.android.api.session.room.RoomService import im.vector.matrix.android.internal.session.pushers.DefaultConditionResolver +import im.vector.matrix.android.internal.session.sync.model.RoomsSyncResponse import im.vector.matrix.android.internal.task.Task import timber.log.Timber import javax.inject.Inject internal interface ProcessEventForPushTask : Task { data class Params( - val events: List, + val syncResponse: RoomsSyncResponse, val rules: List ) } @@ -42,9 +44,35 @@ internal class DefaultProcessEventForPushTask @Inject constructor( override suspend fun execute(params: ProcessEventForPushTask.Params): Try { return Try { - params.events.forEach { event -> + val newJoinEvents = params.syncResponse.join + .map { entries -> + entries.value.timeline?.events?.map { it.copy(roomId = entries.key) } + } + .fold(emptyList(), { acc, next -> + acc + (next ?: emptyList()) + }) + val inviteEvents = params.syncResponse.invite + .map { entries -> + entries.value.inviteState?.events?.map { it.copy(roomId = entries.key) } + } + .fold(emptyList(), { acc, next -> + acc + (next ?: emptyList()) + }) + val allEvents = (newJoinEvents + inviteEvents).filter { event -> + when (event.type) { + EventType.MESSAGE, + EventType.REDACTION, + EventType.ENCRYPTED, + EventType.STATE_ROOM_MEMBER -> true + else -> false + } + }.filter { + it.senderId != sessionParams.credentials.userId + } + Timber.v("[PushRules] Found ${allEvents.size} out of ${(newJoinEvents + inviteEvents).size} to check for push rules with ${params.rules.size} rules") + allEvents.forEach { event -> fulfilledBingRule(event, params.rules)?.let { - Timber.v("Rule $it match for event ${event.eventId}") + Timber.v("[PushRules] Rule $it match for event ${event.eventId}") defaultPushRuleService.dispatchBing(event, it) } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoom.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoom.kt index 9c53f5b9ad..b94ba76991 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoom.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoom.kt @@ -73,7 +73,7 @@ internal class DefaultRoom @Inject constructor(override val roomId: String, override fun roomSummary(fetchLastEvent: Boolean): RoomSummary? { return monarchy.fetchAllMappedSync( - { realm -> RoomSummaryEntity.where(realm).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME) }, + { realm -> RoomSummaryEntity.where(realm, roomId).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME) }, { roomSummaryMapper.map(it, fetchLastEvent) } ).firstOrNull() } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt index 4c80fbccff..3afc57f943 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt @@ -32,13 +32,13 @@ import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.query.find import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom import im.vector.matrix.android.internal.database.query.where +import im.vector.matrix.android.internal.session.notification.DefaultPushRuleService +import im.vector.matrix.android.internal.session.notification.ProcessEventForPushTask import im.vector.matrix.android.internal.session.room.RoomSummaryUpdater import im.vector.matrix.android.internal.session.room.timeline.PaginationDirection -import im.vector.matrix.android.internal.session.sync.model.InvitedRoomSync -import im.vector.matrix.android.internal.session.sync.model.RoomSync -import im.vector.matrix.android.internal.session.sync.model.RoomSyncAccountData -import im.vector.matrix.android.internal.session.sync.model.RoomSyncEphemeral -import im.vector.matrix.android.internal.session.sync.model.RoomsSyncResponse +import im.vector.matrix.android.internal.session.sync.model.* +import im.vector.matrix.android.internal.task.TaskExecutor +import im.vector.matrix.android.internal.task.configureWith import io.realm.Realm import io.realm.kotlin.createObject import timber.log.Timber @@ -48,7 +48,11 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch private val readReceiptHandler: ReadReceiptHandler, private val roomSummaryUpdater: RoomSummaryUpdater, private val roomTagHandler: RoomTagHandler, - private val cryptoManager: CryptoManager) { + private val cryptoManager: CryptoManager, + private val tokenStore: SyncTokenStore, + private val pushRuleService: DefaultPushRuleService, + private val processForPushTask: ProcessEventForPushTask, + private val taskExecutor: TaskExecutor) { sealed class HandlingStrategy { data class JOINED(val data: Map) : HandlingStrategy() @@ -62,6 +66,23 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch handleRoomSync(realm, HandlingStrategy.INVITED(roomsSyncResponse.invite)) handleRoomSync(realm, HandlingStrategy.LEFT(roomsSyncResponse.leave)) } + + //handle event for bing rule checks + checkPushRules(roomsSyncResponse) + + } + + private fun checkPushRules(roomsSyncResponse: RoomsSyncResponse) { + Timber.v("[PushRules] --> checkPushRules") + if (tokenStore.getLastToken() == null) { + Timber.v("[PushRules] <-- No push tule check on initial sync") + return + } //nothing on initial sync + + val rules = pushRuleService.getPushRules("global") + processForPushTask.configureWith(ProcessEventForPushTask.Params(roomsSyncResponse, rules)) + .executeBy(taskExecutor) + Timber.v("[PushRules] <-- Push task scheduled") } // PRIVATE METHODS ***************************************************************************** @@ -82,7 +103,7 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch Timber.v("Handle join sync for room $roomId") val roomEntity = RoomEntity.where(realm, roomId).findFirst() - ?: realm.createObject(roomId) + ?: realm.createObject(roomId) if (roomEntity.membership == Membership.INVITE) { roomEntity.chunks.deleteAllFromRealm() @@ -153,7 +174,7 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch InvitedRoomSync): RoomEntity { Timber.v("Handle invited sync for room $roomId") val roomEntity = RoomEntity.where(realm, roomId).findFirst() - ?: realm.createObject(roomId) + ?: realm.createObject(roomId) roomEntity.membership = Membership.INVITE if (roomSync.inviteState != null && roomSync.inviteState.events.isNotEmpty()) { val chunkEntity = handleTimelineEvents(realm, roomId, roomSync.inviteState.events) @@ -167,7 +188,7 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch roomId: String, roomSync: RoomSync): RoomEntity { val roomEntity = RoomEntity.where(realm, roomId).findFirst() - ?: realm.createObject(roomId) + ?: realm.createObject(roomId) roomEntity.membership = Membership.LEAVE roomEntity.chunks.deleteAllFromRealm()