diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/PushRuleService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/PushRuleService.kt index 05396cb219..c0204d181d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/PushRuleService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/PushRuleService.kt @@ -25,14 +25,14 @@ interface PushRuleService { /** * Fetch the push rules from the server */ - fun fetchPushRules(scope: String = "global") + fun fetchPushRules(scope: String = RuleScope.GLOBAL) //TODO get push rule set - fun getPushRules(scope: String = "global"): List + fun getPushRules(scope: String = RuleScope.GLOBAL): List //TODO update rule - fun updatePushRuleEnableStatus(kind: String, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback): Cancelable + fun updatePushRuleEnableStatus(kind: RuleKind, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback): Cancelable fun addPushRuleListener(listener: PushRuleListener) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RuleIds.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RuleIds.kt index 38a64adf27..cf05e9252d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RuleIds.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RuleIds.kt @@ -37,7 +37,7 @@ object RuleIds { // Default Underride Rules const val RULE_ID_CALL = ".m.rule.call" - const val RULE_ID_one_to_one_encrypted_room = ".m.rule.encrypted_room_one_to_one" + const val RULE_ID_ONE_TO_ONE_ENCRYPTED_ROOM = ".m.rule.encrypted_room_one_to_one" const val RULE_ID_ONE_TO_ONE_ROOM = ".m.rule.room_one_to_one" const val RULE_ID_ALL_OTHER_MESSAGES_ROOMS = ".m.rule.message" const val RULE_ID_ENCRYPTED = ".m.rule.encrypted" diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RulesetKey.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RuleScope.kt similarity index 78% rename from matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RulesetKey.kt rename to matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RuleScope.kt index 834bdf2add..19aa2a4d43 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RulesetKey.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RuleScope.kt @@ -15,12 +15,6 @@ */ package im.vector.matrix.android.api.pushrules - -enum class RulesetKey(val value: String) { - CONTENT("content"), - OVERRIDE("override"), - ROOM("room"), - SENDER("sender"), - UNDERRIDE("underride"), - UNKNOWN("") -} \ No newline at end of file +object RuleScope { + const val GLOBAL = "global" +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RuleSetKey.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RuleSetKey.kt new file mode 100644 index 0000000000..e8862ad343 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/RuleSetKey.kt @@ -0,0 +1,33 @@ +/* + * 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.api.pushrules + +/** + * Ref: https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-pushrules + */ +enum class RuleSetKey(val value: String) { + CONTENT("content"), + OVERRIDE("override"), + ROOM("room"), + SENDER("sender"), + UNDERRIDE("underride") +} + +/** + * Ref: https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-pushrules-scope-kind-ruleid + */ +typealias RuleKind = RuleSetKey diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/PushCondition.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/PushCondition.kt index 8f69401653..7b71a344a0 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/PushCondition.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/pushrules/rest/PushCondition.kt @@ -71,7 +71,7 @@ data class PushCondition( this.key?.let { SenderNotificationPermissionCondition(it) } } Condition.Kind.UNRECOGNIZE -> { - Timber.e("Unknwon kind $kind") + Timber.e("Unknown kind $kind") null } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PushRulesEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PushRulesEntity.kt index f504192550..2a3e75e4aa 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PushRulesEntity.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PushRulesEntity.kt @@ -15,17 +15,27 @@ */ package im.vector.matrix.android.internal.database.model +import im.vector.matrix.android.api.pushrules.RuleKind import io.realm.RealmList import io.realm.RealmObject import io.realm.annotations.Index internal open class PushRulesEntity( + // TODO Remove userId @Index var userId: String = "", var scope: String = "", - // "content", etc. - var rulesetKey: String = "", var pushRules: RealmList = RealmList() ) : RealmObject() { + + private var kindStr: String = RuleKind.CONTENT.name + var kind: RuleKind + get() { + return RuleKind.valueOf(kindStr) + } + set(value) { + kindStr = value.name + } + companion object } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PusherEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PusherEntity.kt index a9d33c86bc..b274e8d06c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PusherEntity.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/PusherEntity.kt @@ -29,6 +29,7 @@ import io.realm.annotations.Index // at im.vector.matrix.android.internal.session.pushers.AddHttpPusherWorker$doWork$$inlined$fold$lambda$2.execute(AddHttpPusherWorker.kt:70) // at io.realm.Realm.executeTransaction(Realm.java:1493) internal open class PusherEntity( + // TODO Remove userId @Index var userId: String = "", var pushKey: String = "", var kind: String? = null, diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/PushersQueries.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/PushersQueries.kt index b7f54f17ea..fec179f864 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/PushersQueries.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/query/PushersQueries.kt @@ -15,6 +15,7 @@ */ package im.vector.matrix.android.internal.database.query +import im.vector.matrix.android.api.pushrules.RuleKind import im.vector.matrix.android.internal.database.model.PushRulesEntity import im.vector.matrix.android.internal.database.model.PushRulesEntityFields import im.vector.matrix.android.internal.database.model.PusherEntity @@ -38,9 +39,9 @@ internal fun PusherEntity.Companion.where(realm: Realm, internal fun PushRulesEntity.Companion.where(realm: Realm, userId: String, scope: String, - ruleSetKey: String): RealmQuery { + kind: RuleKind): RealmQuery { return realm.where() .equalTo(PushRulesEntityFields.USER_ID, userId) .equalTo(PushRulesEntityFields.SCOPE, scope) - .equalTo(PushRulesEntityFields.RULESET_KEY, ruleSetKey) + .equalTo(PushRulesEntityFields.KIND_STR, kind.name) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/DefaultPushRuleService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/DefaultPushRuleService.kt index f388bf0bd1..7c101f00b7 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/DefaultPushRuleService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/notification/DefaultPushRuleService.kt @@ -19,6 +19,8 @@ import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.pushrules.Action import im.vector.matrix.android.api.pushrules.PushRuleService +import im.vector.matrix.android.api.pushrules.RuleKind +import im.vector.matrix.android.api.pushrules.RuleSetKey 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.util.Cancelable @@ -53,36 +55,46 @@ internal class DefaultPushRuleService @Inject constructor( } override fun getPushRules(scope: String): List { - var contentRules: List = emptyList() var overrideRules: List = emptyList() var roomRules: List = emptyList() var senderRules: List = emptyList() var underrideRules: List = emptyList() - // TODO Create const for ruleSetKey monarchy.doWithRealm { realm -> - PushRulesEntity.where(realm, userId, scope, "content").findFirst()?.let { re -> - contentRules = re.pushRules.map { PushRulesMapper.mapContentRule(it) } - } - PushRulesEntity.where(realm, userId, scope, "override").findFirst()?.let { re -> - overrideRules = re.pushRules.map { PushRulesMapper.map(it) } - } - PushRulesEntity.where(realm, userId, scope, "room").findFirst()?.let { re -> - roomRules = re.pushRules.map { PushRulesMapper.mapRoomRule(it) } - } - PushRulesEntity.where(realm, userId, scope, "sender").findFirst()?.let { re -> - senderRules = re.pushRules.map { PushRulesMapper.mapSenderRule(it) } - } - PushRulesEntity.where(realm, userId, scope, "underride").findFirst()?.let { re -> - underrideRules = re.pushRules.map { PushRulesMapper.map(it) } - } + // FIXME PushRulesEntity are not always created here... + // FIWME Get the push rules from the sync + PushRulesEntity.where(realm, userId, scope, RuleSetKey.CONTENT) + .findFirst() + ?.let { pushRulesEntity -> + contentRules = pushRulesEntity.pushRules.map { PushRulesMapper.mapContentRule(it) } + } + PushRulesEntity.where(realm, userId, scope, RuleSetKey.OVERRIDE) + .findFirst() + ?.let { pushRulesEntity -> + overrideRules = pushRulesEntity.pushRules.map { PushRulesMapper.map(it) } + } + PushRulesEntity.where(realm, userId, scope, RuleSetKey.ROOM) + .findFirst() + ?.let { pushRulesEntity -> + roomRules = pushRulesEntity.pushRules.map { PushRulesMapper.mapRoomRule(it) } + } + PushRulesEntity.where(realm, userId, scope, RuleSetKey.SENDER) + .findFirst() + ?.let { pushRulesEntity -> + senderRules = pushRulesEntity.pushRules.map { PushRulesMapper.mapSenderRule(it) } + } + PushRulesEntity.where(realm, userId, scope, RuleSetKey.UNDERRIDE) + .findFirst() + ?.let { pushRulesEntity -> + underrideRules = pushRulesEntity.pushRules.map { PushRulesMapper.map(it) } + } } return contentRules + overrideRules + roomRules + senderRules + underrideRules } - override fun updatePushRuleEnableStatus(kind: String, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback): Cancelable { + override fun updatePushRuleEnableStatus(kind: RuleKind, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback): Cancelable { return updatePushRuleEnableStatusTask .configureWith(UpdatePushRuleEnableStatusTask.Params(kind, pushRule, enabled)) { this.callback = callback 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 226995bc07..4982e479f7 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 @@ -102,6 +102,7 @@ internal class DefaultProcessEventForPushTask @Inject constructor( } private fun fulfilledBingRule(event: Event, rules: List): PushRule? { + // TODO This should be injected val conditionResolver = DefaultConditionResolver(event, roomService, userId) rules.filter { it.enabled }.forEach { rule -> val isFullfilled = rule.conditions?.map { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/DefaultConditionResolver.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/DefaultConditionResolver.kt index e983f3b96d..b69bcd3447 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/DefaultConditionResolver.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/DefaultConditionResolver.kt @@ -21,6 +21,7 @@ import im.vector.matrix.android.api.session.room.RoomService import im.vector.matrix.android.internal.di.UserId import timber.log.Timber +// TODO Inject constructor internal class DefaultConditionResolver(private val event: Event, private val roomService: RoomService, @UserId diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/GetPushRulesTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/GetPushRulesTask.kt index 76921fec9f..981bff8ed2 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/GetPushRulesTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/GetPushRulesTask.kt @@ -16,6 +16,7 @@ package im.vector.matrix.android.internal.session.pushers import com.zhuinden.monarchy.Monarchy +import im.vector.matrix.android.api.pushrules.RuleSetKey import im.vector.matrix.android.api.pushrules.rest.GetPushRulesResponse import im.vector.matrix.android.internal.database.mapper.PushRulesMapper import im.vector.matrix.android.internal.database.model.PushRulesEntity @@ -28,12 +29,9 @@ import javax.inject.Inject internal interface GetPushRulesTask : Task { - data class Params(val scope: String) - } - internal class DefaultGetPushRulesTask @Inject constructor(private val pushRulesApi: PushRulesApi, private val monarchy: Monarchy, @UserId @@ -49,17 +47,16 @@ internal class DefaultGetPushRulesTask @Inject constructor(private val pushRules //TODO realm.where(PushRulesEntity::class.java) .equalTo(PusherEntityFields.USER_ID, userId) - .findAll().deleteAllFromRealm() + .findAll() + .deleteAllFromRealm() - val content = PushRulesEntity(userId, scope, "content") + val content = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.CONTENT } response.global.content?.forEach { rule -> - PushRulesMapper.map(rule).also { - content.pushRules.add(it) - } + content.pushRules.add(PushRulesMapper.map(rule)) } realm.insertOrUpdate(content) - val override = PushRulesEntity(userId, scope, "override") + val override = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.OVERRIDE } response.global.override?.forEach { rule -> PushRulesMapper.map(rule).also { override.pushRules.add(it) @@ -67,27 +64,21 @@ internal class DefaultGetPushRulesTask @Inject constructor(private val pushRules } realm.insertOrUpdate(override) - val rooms = PushRulesEntity(userId, scope, "room") + val rooms = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.ROOM } response.global.room?.forEach { rule -> - PushRulesMapper.map(rule).also { - rooms.pushRules.add(it) - } + rooms.pushRules.add(PushRulesMapper.map(rule)) } realm.insertOrUpdate(rooms) - val senders = PushRulesEntity(userId, scope, "sender") + val senders = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.SENDER } response.global.sender?.forEach { rule -> - PushRulesMapper.map(rule).also { - senders.pushRules.add(it) - } + senders.pushRules.add(PushRulesMapper.map(rule)) } realm.insertOrUpdate(senders) - val underrides = PushRulesEntity(userId, scope, "underride") + val underrides = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.UNDERRIDE } response.global.underride?.forEach { rule -> - PushRulesMapper.map(rule).also { - underrides.pushRules.add(it) - } + underrides.pushRules.add(PushRulesMapper.map(rule)) } realm.insertOrUpdate(underrides) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/PushRulesApi.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/PushRulesApi.kt index f191b21d1d..afdf21f3a2 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/PushRulesApi.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/PushRulesApi.kt @@ -15,6 +15,7 @@ */ package im.vector.matrix.android.internal.session.pushers +import im.vector.matrix.android.api.pushrules.RuleKind import im.vector.matrix.android.api.pushrules.rest.GetPushRulesResponse import im.vector.matrix.android.api.pushrules.rest.PushRule import im.vector.matrix.android.internal.network.NetworkConstants @@ -37,7 +38,7 @@ internal interface PushRulesApi { * @param enable the new enable status */ @PUT(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushrules/global/{kind}/{ruleId}/enabled") - fun updateEnableRuleStatus(@Path("kind") kind: String, + fun updateEnableRuleStatus(@Path("kind") kind: RuleKind, @Path("ruleId") ruleId: String, @Body enable: Boolean?) : Call @@ -51,7 +52,7 @@ internal interface PushRulesApi { * @param actions the actions */ @PUT(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushrules/global/{kind}/{ruleId}/actions") - fun updateRuleActions(@Path("kind") kind: String, + fun updateRuleActions(@Path("kind") kind: RuleKind, @Path("ruleId") ruleId: String, @Body actions: Any) : Call @@ -64,7 +65,7 @@ internal interface PushRulesApi { * @param ruleId the ruleId */ @DELETE(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushrules/global/{kind}/{ruleId}") - fun deleteRule(@Path("kind") kind: String, + fun deleteRule(@Path("kind") kind: RuleKind, @Path("ruleId") ruleId: String) : Call @@ -76,7 +77,7 @@ internal interface PushRulesApi { * @param rule the rule to add. */ @PUT(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushrules/global/{kind}/{ruleId}") - fun addRule(@Path("kind") kind: String, + fun addRule(@Path("kind") kind: RuleKind, @Path("ruleId") ruleId: String, @Body rule: PushRule) : Call diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/UpdatePushRuleEnableStatusTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/UpdatePushRuleEnableStatusTask.kt index 828d9e71ae..046d1b75a2 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/UpdatePushRuleEnableStatusTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/UpdatePushRuleEnableStatusTask.kt @@ -15,6 +15,7 @@ */ package im.vector.matrix.android.internal.session.pushers +import im.vector.matrix.android.api.pushrules.RuleKind import im.vector.matrix.android.api.pushrules.rest.PushRule import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.task.Task @@ -22,7 +23,7 @@ import javax.inject.Inject internal interface UpdatePushRuleEnableStatusTask : Task { - data class Params(val kind: String, + data class Params(val kind: RuleKind, val pushRule: PushRule, val enabled: Boolean) } 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 8586e6fa0b..11ebff7048 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 @@ -18,6 +18,7 @@ package im.vector.matrix.android.internal.session.sync import com.zhuinden.monarchy.Monarchy import im.vector.matrix.android.R +import im.vector.matrix.android.api.pushrules.RuleScope 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.events.model.toModel @@ -77,11 +78,11 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch private fun checkPushRules(roomsSyncResponse: RoomsSyncResponse) { Timber.v("[PushRules] --> checkPushRules") if (tokenStore.getLastToken() == null) { - Timber.v("[PushRules] <-- No push tule check on initial sync") + Timber.v("[PushRules] <-- No push rule check on initial sync") return } //nothing on initial sync - val rules = pushRuleService.getPushRules("global") + val rules = pushRuleService.getPushRules(RuleScope.GLOBAL) processForPushTask.configureWith(ProcessEventForPushTask.Params(roomsSyncResponse, rules)) .executeBy(taskExecutor) Timber.v("[PushRules] <-- Push task scheduled") diff --git a/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt b/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt index 3d4df602b7..47e1571e54 100644 --- a/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt +++ b/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt @@ -24,14 +24,13 @@ import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.events.model.toContent import im.vector.matrix.android.api.session.room.Room import im.vector.matrix.android.api.session.room.RoomService -import im.vector.matrix.android.api.session.room.model.EventAnnotationsSummary -import im.vector.matrix.android.api.session.room.model.Membership -import im.vector.matrix.android.api.session.room.model.RoomMember -import im.vector.matrix.android.api.session.room.model.RoomSummary +import im.vector.matrix.android.api.session.room.model.* import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams import im.vector.matrix.android.api.session.room.model.message.MessageTextContent +import im.vector.matrix.android.api.session.room.send.UserDraft import im.vector.matrix.android.api.session.room.timeline.Timeline import im.vector.matrix.android.api.session.room.timeline.TimelineEvent +import im.vector.matrix.android.api.session.room.timeline.TimelineSettings import im.vector.matrix.android.api.util.Cancelable import org.junit.Assert import org.junit.Test @@ -164,10 +163,29 @@ class PushrulesConditionTest { } + @Test + fun test_notice_condition() { + val conditionEqual = EventMatchCondition("content.msgtype", "m.notice") + + Event( + type = "m.room.message", + eventId = "mx0", + content = MessageTextContent("m.notice", "A").toContent(), + originServerTs = 0, + roomId = "2joined").also { + Assert.assertTrue("Notice", conditionEqual.isSatisfied(it)) + } + } + + class MockRoomService() : RoomService { - override fun createRoom(createRoomParams: CreateRoomParams, callback: MatrixCallback) { + override fun createRoom(createRoomParams: CreateRoomParams, callback: MatrixCallback): Cancelable { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + override fun joinRoom(roomId: String, viaServers: List, callback: MatrixCallback): Cancelable { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun getRoom(roomId: String): Room? { @@ -184,6 +202,53 @@ class PushrulesConditionTest { } class MockRoom(override val roomId: String, val _numberOfJoinedMembers: Int) : Room { + override fun resendTextMessage(localEcho: TimelineEvent): Cancelable? { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun resendMediaMessage(localEcho: TimelineEvent): Cancelable? { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun deleteFailedEcho(localEcho: TimelineEvent) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun clearSendingQueue() { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun resendAllFailedMessages() { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun saveDraft(draft: UserDraft) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun deleteDraft() { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun getDraftsLive(): LiveData> { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun getEventReadReceiptsLive(eventId: String): LiveData> { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun getStateEvent(eventType: String): Event? { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun editReply(replyToEdit: TimelineEvent, originalTimelineEvent: TimelineEvent, newBodyText: String, compatibilityBodyText: String): Cancelable { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun fetchEditHistory(eventId: String, callback: MatrixCallback>) { + } + override fun liveTimeLineEvent(eventId: String): LiveData { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } @@ -201,7 +266,7 @@ class PushrulesConditionTest { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } - override fun createTimeline(eventId: String?, allowedTypes: List?): Timeline { + override fun createTimeline(eventId: String?, settings: TimelineSettings): Timeline { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } @@ -245,7 +310,7 @@ class PushrulesConditionTest { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } - override fun loadRoomMembersIfNeeded(): Cancelable { + override fun loadRoomMembersIfNeeded(matrixCallback: MatrixCallback): Cancelable { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } @@ -257,15 +322,15 @@ class PushrulesConditionTest { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } - override fun invite(userId: String, callback: MatrixCallback) { + override fun invite(userId: String, callback: MatrixCallback): Cancelable { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } - override fun join(callback: MatrixCallback) { + override fun join(viaServers: List, callback: MatrixCallback): Cancelable { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } - override fun leave(callback: MatrixCallback) { + override fun leave(callback: MatrixCallback): Cancelable { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } diff --git a/vector/src/main/java/im/vector/riotx/core/extensions/Session.kt b/vector/src/main/java/im/vector/riotx/core/extensions/Session.kt index 95e17a4b7d..43e7935553 100644 --- a/vector/src/main/java/im/vector/riotx/core/extensions/Session.kt +++ b/vector/src/main/java/im/vector/riotx/core/extensions/Session.kt @@ -32,6 +32,9 @@ fun Session.configureAndStart(pushRuleTriggerListener: PushRuleTriggerListener) startSync(isAtLeastStarted) refreshPushers() pushRuleTriggerListener.startWithSession(this) + + // TODO After a clear cache, this is not done. + // TODO Push rule should be parsed from the sync fetchPushRules() // TODO P1 From HomeActivity diff --git a/vector/src/main/java/im/vector/riotx/features/notifications/NotificationAction.kt b/vector/src/main/java/im/vector/riotx/features/notifications/NotificationAction.kt index 869a204cf6..786ff5060f 100644 --- a/vector/src/main/java/im/vector/riotx/features/notifications/NotificationAction.kt +++ b/vector/src/main/java/im/vector/riotx/features/notifications/NotificationAction.kt @@ -28,6 +28,7 @@ data class NotificationAction( var highlight = false var sound: String? = null ruleActions.forEach { + // TODO When if (it.type == Action.Type.NOTIFY) shouldNotify = true if (it.type == Action.Type.DONT_NOTIFY) shouldNotify = false if (it.type == Action.Type.SET_TWEAK) { diff --git a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsNotificationFragment.kt b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsNotificationFragment.kt index 6536c170ee..731c8a991e 100644 --- a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsNotificationFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsNotificationFragment.kt @@ -21,6 +21,7 @@ import androidx.preference.Preference import androidx.preference.SwitchPreference import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.pushrules.RuleIds +import im.vector.matrix.android.api.pushrules.RuleKind import im.vector.riotx.R import im.vector.riotx.core.di.ActiveSessionHolder import im.vector.riotx.core.di.ScreenComponent @@ -45,12 +46,13 @@ class VectorSettingsNotificationPreferenceFragment : VectorSettingsBaseFragment( .find { it.ruleId == RuleIds.RULE_ID_DISABLE_ALL } if (mRuleMaster == null) { + // The home server does not support RULE_ID_DISABLE_ALL, so hide the preference pref.isVisible = false return } - val areNotifEnabledAtAccountLevelt = !mRuleMaster.enabled - (pref as SwitchPreference).isChecked = areNotifEnabledAtAccountLevelt + val areNotifEnabledAtAccountLevel = !mRuleMaster.enabled + (pref as SwitchPreference).isChecked = areNotifEnabledAtAccountLevel } } @@ -114,19 +116,21 @@ class VectorSettingsNotificationPreferenceFragment : VectorSettingsBaseFragment( .find { it.ruleId == RuleIds.RULE_ID_DISABLE_ALL } ?.let { //Trick, we must enable this room to disable notifications - pushRuleService.updatePushRuleEnableStatus("override", it, !switchPref.isChecked, - object : MatrixCallback { + pushRuleService.updatePushRuleEnableStatus(RuleKind.OVERRIDE, + it, + !switchPref.isChecked, + object : MatrixCallback { - override fun onSuccess(data: Unit) { - pushRuleService.fetchPushRules() - } + override fun onSuccess(data: Unit) { + pushRuleService.fetchPushRules() + } - override fun onFailure(failure: Throwable) { - //revert the check box - switchPref.isChecked = !switchPref.isChecked - Toast.makeText(activity, R.string.unknown_error, Toast.LENGTH_SHORT).show() - } - }) + override fun onFailure(failure: Throwable) { + //revert the check box + switchPref.isChecked = !switchPref.isChecked + Toast.makeText(activity, R.string.unknown_error, Toast.LENGTH_SHORT).show() + } + }) } } diff --git a/vector/src/main/java/im/vector/riotx/features/settings/troubleshoot/TestAccountSettings.kt b/vector/src/main/java/im/vector/riotx/features/settings/troubleshoot/TestAccountSettings.kt index 47d1f6358a..24c9dc1c1a 100644 --- a/vector/src/main/java/im/vector/riotx/features/settings/troubleshoot/TestAccountSettings.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/troubleshoot/TestAccountSettings.kt @@ -17,6 +17,7 @@ package im.vector.riotx.features.settings.troubleshoot import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.pushrules.RuleIds +import im.vector.matrix.android.api.pushrules.RuleKind import im.vector.riotx.R import im.vector.riotx.core.di.ActiveSessionHolder import im.vector.riotx.core.resources.StringProvider @@ -45,8 +46,7 @@ class TestAccountSettings @Inject constructor(private val stringProvider: String override fun doFix() { if (manager?.diagStatus == TestStatus.RUNNING) return //wait before all is finished - // TODO Use constant for kind - session.updatePushRuleEnableStatus("override", defaultRule, !defaultRule.enabled, + session.updatePushRuleEnableStatus(RuleKind.OVERRIDE, defaultRule, !defaultRule.enabled, object : MatrixCallback { override fun onSuccess(data: Unit) {