Create enum for Push rules. Also add some TODOs

This commit is contained in:
Benoit Marty 2019-09-20 11:20:51 +02:00
parent acd7a709de
commit 03f8120b7d
20 changed files with 209 additions and 89 deletions

View File

@ -25,14 +25,14 @@ interface PushRuleService {
/** /**
* Fetch the push rules from the server * Fetch the push rules from the server
*/ */
fun fetchPushRules(scope: String = "global") fun fetchPushRules(scope: String = RuleScope.GLOBAL)
//TODO get push rule set //TODO get push rule set
fun getPushRules(scope: String = "global"): List<PushRule> fun getPushRules(scope: String = RuleScope.GLOBAL): List<PushRule>
//TODO update rule //TODO update rule
fun updatePushRuleEnableStatus(kind: String, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback<Unit>): Cancelable fun updatePushRuleEnableStatus(kind: RuleKind, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback<Unit>): Cancelable
fun addPushRuleListener(listener: PushRuleListener) fun addPushRuleListener(listener: PushRuleListener)

View File

@ -37,7 +37,7 @@ object RuleIds {
// Default Underride Rules // Default Underride Rules
const val RULE_ID_CALL = ".m.rule.call" 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_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_ALL_OTHER_MESSAGES_ROOMS = ".m.rule.message"
const val RULE_ID_ENCRYPTED = ".m.rule.encrypted" const val RULE_ID_ENCRYPTED = ".m.rule.encrypted"

View File

@ -15,12 +15,6 @@
*/ */
package im.vector.matrix.android.api.pushrules package im.vector.matrix.android.api.pushrules
object RuleScope {
enum class RulesetKey(val value: String) { const val GLOBAL = "global"
CONTENT("content"), }
OVERRIDE("override"),
ROOM("room"),
SENDER("sender"),
UNDERRIDE("underride"),
UNKNOWN("")
}

View File

@ -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

View File

@ -71,7 +71,7 @@ data class PushCondition(
this.key?.let { SenderNotificationPermissionCondition(it) } this.key?.let { SenderNotificationPermissionCondition(it) }
} }
Condition.Kind.UNRECOGNIZE -> { Condition.Kind.UNRECOGNIZE -> {
Timber.e("Unknwon kind $kind") Timber.e("Unknown kind $kind")
null null
} }
} }

View File

@ -15,17 +15,27 @@
*/ */
package im.vector.matrix.android.internal.database.model package im.vector.matrix.android.internal.database.model
import im.vector.matrix.android.api.pushrules.RuleKind
import io.realm.RealmList import io.realm.RealmList
import io.realm.RealmObject import io.realm.RealmObject
import io.realm.annotations.Index import io.realm.annotations.Index
internal open class PushRulesEntity( internal open class PushRulesEntity(
// TODO Remove userId
@Index var userId: String = "", @Index var userId: String = "",
var scope: String = "", var scope: String = "",
// "content", etc.
var rulesetKey: String = "",
var pushRules: RealmList<PushRuleEntity> = RealmList() var pushRules: RealmList<PushRuleEntity> = RealmList()
) : RealmObject() { ) : RealmObject() {
private var kindStr: String = RuleKind.CONTENT.name
var kind: RuleKind
get() {
return RuleKind.valueOf(kindStr)
}
set(value) {
kindStr = value.name
}
companion object companion object
} }

View File

@ -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 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) // at io.realm.Realm.executeTransaction(Realm.java:1493)
internal open class PusherEntity( internal open class PusherEntity(
// TODO Remove userId
@Index var userId: String = "", @Index var userId: String = "",
var pushKey: String = "", var pushKey: String = "",
var kind: String? = null, var kind: String? = null,

View File

@ -15,6 +15,7 @@
*/ */
package im.vector.matrix.android.internal.database.query 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.PushRulesEntity
import im.vector.matrix.android.internal.database.model.PushRulesEntityFields import im.vector.matrix.android.internal.database.model.PushRulesEntityFields
import im.vector.matrix.android.internal.database.model.PusherEntity 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, internal fun PushRulesEntity.Companion.where(realm: Realm,
userId: String, userId: String,
scope: String, scope: String,
ruleSetKey: String): RealmQuery<PushRulesEntity> { kind: RuleKind): RealmQuery<PushRulesEntity> {
return realm.where<PushRulesEntity>() return realm.where<PushRulesEntity>()
.equalTo(PushRulesEntityFields.USER_ID, userId) .equalTo(PushRulesEntityFields.USER_ID, userId)
.equalTo(PushRulesEntityFields.SCOPE, scope) .equalTo(PushRulesEntityFields.SCOPE, scope)
.equalTo(PushRulesEntityFields.RULESET_KEY, ruleSetKey) .equalTo(PushRulesEntityFields.KIND_STR, kind.name)
} }

View File

@ -19,6 +19,8 @@ import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.pushrules.Action import im.vector.matrix.android.api.pushrules.Action
import im.vector.matrix.android.api.pushrules.PushRuleService 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.pushrules.rest.PushRule
import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.events.model.Event
import im.vector.matrix.android.api.util.Cancelable import im.vector.matrix.android.api.util.Cancelable
@ -53,36 +55,46 @@ internal class DefaultPushRuleService @Inject constructor(
} }
override fun getPushRules(scope: String): List<PushRule> { override fun getPushRules(scope: String): List<PushRule> {
var contentRules: List<PushRule> = emptyList() var contentRules: List<PushRule> = emptyList()
var overrideRules: List<PushRule> = emptyList() var overrideRules: List<PushRule> = emptyList()
var roomRules: List<PushRule> = emptyList() var roomRules: List<PushRule> = emptyList()
var senderRules: List<PushRule> = emptyList() var senderRules: List<PushRule> = emptyList()
var underrideRules: List<PushRule> = emptyList() var underrideRules: List<PushRule> = emptyList()
// TODO Create const for ruleSetKey
monarchy.doWithRealm { realm -> monarchy.doWithRealm { realm ->
PushRulesEntity.where(realm, userId, scope, "content").findFirst()?.let { re -> // FIXME PushRulesEntity are not always created here...
contentRules = re.pushRules.map { PushRulesMapper.mapContentRule(it) } // FIWME Get the push rules from the sync
} PushRulesEntity.where(realm, userId, scope, RuleSetKey.CONTENT)
PushRulesEntity.where(realm, userId, scope, "override").findFirst()?.let { re -> .findFirst()
overrideRules = re.pushRules.map { PushRulesMapper.map(it) } ?.let { pushRulesEntity ->
} contentRules = pushRulesEntity.pushRules.map { PushRulesMapper.mapContentRule(it) }
PushRulesEntity.where(realm, userId, scope, "room").findFirst()?.let { re -> }
roomRules = re.pushRules.map { PushRulesMapper.mapRoomRule(it) } PushRulesEntity.where(realm, userId, scope, RuleSetKey.OVERRIDE)
} .findFirst()
PushRulesEntity.where(realm, userId, scope, "sender").findFirst()?.let { re -> ?.let { pushRulesEntity ->
senderRules = re.pushRules.map { PushRulesMapper.mapSenderRule(it) } overrideRules = pushRulesEntity.pushRules.map { PushRulesMapper.map(it) }
} }
PushRulesEntity.where(realm, userId, scope, "underride").findFirst()?.let { re -> PushRulesEntity.where(realm, userId, scope, RuleSetKey.ROOM)
underrideRules = re.pushRules.map { PushRulesMapper.map(it) } .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 return contentRules + overrideRules + roomRules + senderRules + underrideRules
} }
override fun updatePushRuleEnableStatus(kind: String, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback<Unit>): Cancelable { override fun updatePushRuleEnableStatus(kind: RuleKind, pushRule: PushRule, enabled: Boolean, callback: MatrixCallback<Unit>): Cancelable {
return updatePushRuleEnableStatusTask return updatePushRuleEnableStatusTask
.configureWith(UpdatePushRuleEnableStatusTask.Params(kind, pushRule, enabled)) { .configureWith(UpdatePushRuleEnableStatusTask.Params(kind, pushRule, enabled)) {
this.callback = callback this.callback = callback

View File

@ -102,6 +102,7 @@ internal class DefaultProcessEventForPushTask @Inject constructor(
} }
private fun fulfilledBingRule(event: Event, rules: List<PushRule>): PushRule? { private fun fulfilledBingRule(event: Event, rules: List<PushRule>): PushRule? {
// TODO This should be injected
val conditionResolver = DefaultConditionResolver(event, roomService, userId) val conditionResolver = DefaultConditionResolver(event, roomService, userId)
rules.filter { it.enabled }.forEach { rule -> rules.filter { it.enabled }.forEach { rule ->
val isFullfilled = rule.conditions?.map { val isFullfilled = rule.conditions?.map {

View File

@ -21,6 +21,7 @@ import im.vector.matrix.android.api.session.room.RoomService
import im.vector.matrix.android.internal.di.UserId import im.vector.matrix.android.internal.di.UserId
import timber.log.Timber import timber.log.Timber
// TODO Inject constructor
internal class DefaultConditionResolver(private val event: Event, internal class DefaultConditionResolver(private val event: Event,
private val roomService: RoomService, private val roomService: RoomService,
@UserId @UserId

View File

@ -16,6 +16,7 @@
package im.vector.matrix.android.internal.session.pushers package im.vector.matrix.android.internal.session.pushers
import com.zhuinden.monarchy.Monarchy 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.api.pushrules.rest.GetPushRulesResponse
import im.vector.matrix.android.internal.database.mapper.PushRulesMapper import im.vector.matrix.android.internal.database.mapper.PushRulesMapper
import im.vector.matrix.android.internal.database.model.PushRulesEntity import im.vector.matrix.android.internal.database.model.PushRulesEntity
@ -28,12 +29,9 @@ import javax.inject.Inject
internal interface GetPushRulesTask : Task<GetPushRulesTask.Params, Unit> { internal interface GetPushRulesTask : Task<GetPushRulesTask.Params, Unit> {
data class Params(val scope: String) data class Params(val scope: String)
} }
internal class DefaultGetPushRulesTask @Inject constructor(private val pushRulesApi: PushRulesApi, internal class DefaultGetPushRulesTask @Inject constructor(private val pushRulesApi: PushRulesApi,
private val monarchy: Monarchy, private val monarchy: Monarchy,
@UserId @UserId
@ -49,17 +47,16 @@ internal class DefaultGetPushRulesTask @Inject constructor(private val pushRules
//TODO //TODO
realm.where(PushRulesEntity::class.java) realm.where(PushRulesEntity::class.java)
.equalTo(PusherEntityFields.USER_ID, userId) .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 -> response.global.content?.forEach { rule ->
PushRulesMapper.map(rule).also { content.pushRules.add(PushRulesMapper.map(rule))
content.pushRules.add(it)
}
} }
realm.insertOrUpdate(content) realm.insertOrUpdate(content)
val override = PushRulesEntity(userId, scope, "override") val override = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.OVERRIDE }
response.global.override?.forEach { rule -> response.global.override?.forEach { rule ->
PushRulesMapper.map(rule).also { PushRulesMapper.map(rule).also {
override.pushRules.add(it) override.pushRules.add(it)
@ -67,27 +64,21 @@ internal class DefaultGetPushRulesTask @Inject constructor(private val pushRules
} }
realm.insertOrUpdate(override) realm.insertOrUpdate(override)
val rooms = PushRulesEntity(userId, scope, "room") val rooms = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.ROOM }
response.global.room?.forEach { rule -> response.global.room?.forEach { rule ->
PushRulesMapper.map(rule).also { rooms.pushRules.add(PushRulesMapper.map(rule))
rooms.pushRules.add(it)
}
} }
realm.insertOrUpdate(rooms) realm.insertOrUpdate(rooms)
val senders = PushRulesEntity(userId, scope, "sender") val senders = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.SENDER }
response.global.sender?.forEach { rule -> response.global.sender?.forEach { rule ->
PushRulesMapper.map(rule).also { senders.pushRules.add(PushRulesMapper.map(rule))
senders.pushRules.add(it)
}
} }
realm.insertOrUpdate(senders) realm.insertOrUpdate(senders)
val underrides = PushRulesEntity(userId, scope, "underride") val underrides = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.UNDERRIDE }
response.global.underride?.forEach { rule -> response.global.underride?.forEach { rule ->
PushRulesMapper.map(rule).also { underrides.pushRules.add(PushRulesMapper.map(rule))
underrides.pushRules.add(it)
}
} }
realm.insertOrUpdate(underrides) realm.insertOrUpdate(underrides)
} }

View File

@ -15,6 +15,7 @@
*/ */
package im.vector.matrix.android.internal.session.pushers 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.GetPushRulesResponse
import im.vector.matrix.android.api.pushrules.rest.PushRule import im.vector.matrix.android.api.pushrules.rest.PushRule
import im.vector.matrix.android.internal.network.NetworkConstants import im.vector.matrix.android.internal.network.NetworkConstants
@ -37,7 +38,7 @@ internal interface PushRulesApi {
* @param enable the new enable status * @param enable the new enable status
*/ */
@PUT(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushrules/global/{kind}/{ruleId}/enabled") @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, @Path("ruleId") ruleId: String,
@Body enable: Boolean?) @Body enable: Boolean?)
: Call<Unit> : Call<Unit>
@ -51,7 +52,7 @@ internal interface PushRulesApi {
* @param actions the actions * @param actions the actions
*/ */
@PUT(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushrules/global/{kind}/{ruleId}/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, @Path("ruleId") ruleId: String,
@Body actions: Any) @Body actions: Any)
: Call<Unit> : Call<Unit>
@ -64,7 +65,7 @@ internal interface PushRulesApi {
* @param ruleId the ruleId * @param ruleId the ruleId
*/ */
@DELETE(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushrules/global/{kind}/{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) @Path("ruleId") ruleId: String)
: Call<Unit> : Call<Unit>
@ -76,7 +77,7 @@ internal interface PushRulesApi {
* @param rule the rule to add. * @param rule the rule to add.
*/ */
@PUT(NetworkConstants.URI_API_PREFIX_PATH_R0 + "pushrules/global/{kind}/{ruleId}") @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, @Path("ruleId") ruleId: String,
@Body rule: PushRule) @Body rule: PushRule)
: Call<Unit> : Call<Unit>

View File

@ -15,6 +15,7 @@
*/ */
package im.vector.matrix.android.internal.session.pushers 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.api.pushrules.rest.PushRule
import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.network.executeRequest
import im.vector.matrix.android.internal.task.Task import im.vector.matrix.android.internal.task.Task
@ -22,7 +23,7 @@ import javax.inject.Inject
internal interface UpdatePushRuleEnableStatusTask : Task<UpdatePushRuleEnableStatusTask.Params, Unit> { internal interface UpdatePushRuleEnableStatusTask : Task<UpdatePushRuleEnableStatusTask.Params, Unit> {
data class Params(val kind: String, data class Params(val kind: RuleKind,
val pushRule: PushRule, val pushRule: PushRule,
val enabled: Boolean) val enabled: Boolean)
} }

View File

@ -18,6 +18,7 @@ package im.vector.matrix.android.internal.session.sync
import com.zhuinden.monarchy.Monarchy import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.R 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.Event
import im.vector.matrix.android.api.session.events.model.EventType import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.events.model.toModel 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) { private fun checkPushRules(roomsSyncResponse: RoomsSyncResponse) {
Timber.v("[PushRules] --> checkPushRules") Timber.v("[PushRules] --> checkPushRules")
if (tokenStore.getLastToken() == null) { 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 return
} //nothing on initial sync } //nothing on initial sync
val rules = pushRuleService.getPushRules("global") val rules = pushRuleService.getPushRules(RuleScope.GLOBAL)
processForPushTask.configureWith(ProcessEventForPushTask.Params(roomsSyncResponse, rules)) processForPushTask.configureWith(ProcessEventForPushTask.Params(roomsSyncResponse, rules))
.executeBy(taskExecutor) .executeBy(taskExecutor)
Timber.v("[PushRules] <-- Push task scheduled") Timber.v("[PushRules] <-- Push task scheduled")

View File

@ -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.events.model.toContent
import im.vector.matrix.android.api.session.room.Room 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.RoomService
import im.vector.matrix.android.api.session.room.model.EventAnnotationsSummary import im.vector.matrix.android.api.session.room.model.*
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.create.CreateRoomParams 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.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.Timeline
import im.vector.matrix.android.api.session.room.timeline.TimelineEvent 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 im.vector.matrix.android.api.util.Cancelable
import org.junit.Assert import org.junit.Assert
import org.junit.Test 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 { class MockRoomService() : RoomService {
override fun createRoom(createRoomParams: CreateRoomParams, callback: MatrixCallback<String>) { override fun createRoom(createRoomParams: CreateRoomParams, callback: MatrixCallback<String>): Cancelable {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun joinRoom(roomId: String, viaServers: List<String>, callback: MatrixCallback<Unit>): Cancelable {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
override fun getRoom(roomId: String): Room? { override fun getRoom(roomId: String): Room? {
@ -184,6 +202,53 @@ class PushrulesConditionTest {
} }
class MockRoom(override val roomId: String, val _numberOfJoinedMembers: Int) : Room { 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<List<UserDraft>> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getEventReadReceiptsLive(eventId: String): LiveData<List<ReadReceipt>> {
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<List<Event>>) {
}
override fun liveTimeLineEvent(eventId: String): LiveData<TimelineEvent> { override fun liveTimeLineEvent(eventId: String): LiveData<TimelineEvent> {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. 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. TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
override fun createTimeline(eventId: String?, allowedTypes: List<String>?): Timeline { override fun createTimeline(eventId: String?, settings: TimelineSettings): Timeline {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. 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. TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
override fun loadRoomMembersIfNeeded(): Cancelable { override fun loadRoomMembersIfNeeded(matrixCallback: MatrixCallback<Unit>): Cancelable {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. 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. TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
override fun invite(userId: String, callback: MatrixCallback<Unit>) { override fun invite(userId: String, callback: MatrixCallback<Unit>): Cancelable {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
override fun join(callback: MatrixCallback<Unit>) { override fun join(viaServers: List<String>, callback: MatrixCallback<Unit>): Cancelable {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
override fun leave(callback: MatrixCallback<Unit>) { override fun leave(callback: MatrixCallback<Unit>): Cancelable {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }

View File

@ -32,6 +32,9 @@ fun Session.configureAndStart(pushRuleTriggerListener: PushRuleTriggerListener)
startSync(isAtLeastStarted) startSync(isAtLeastStarted)
refreshPushers() refreshPushers()
pushRuleTriggerListener.startWithSession(this) pushRuleTriggerListener.startWithSession(this)
// TODO After a clear cache, this is not done.
// TODO Push rule should be parsed from the sync
fetchPushRules() fetchPushRules()
// TODO P1 From HomeActivity // TODO P1 From HomeActivity

View File

@ -28,6 +28,7 @@ data class NotificationAction(
var highlight = false var highlight = false
var sound: String? = null var sound: String? = null
ruleActions.forEach { ruleActions.forEach {
// TODO When
if (it.type == Action.Type.NOTIFY) shouldNotify = true if (it.type == Action.Type.NOTIFY) shouldNotify = true
if (it.type == Action.Type.DONT_NOTIFY) shouldNotify = false if (it.type == Action.Type.DONT_NOTIFY) shouldNotify = false
if (it.type == Action.Type.SET_TWEAK) { if (it.type == Action.Type.SET_TWEAK) {

View File

@ -21,6 +21,7 @@ import androidx.preference.Preference
import androidx.preference.SwitchPreference import androidx.preference.SwitchPreference
import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.pushrules.RuleIds 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.R
import im.vector.riotx.core.di.ActiveSessionHolder import im.vector.riotx.core.di.ActiveSessionHolder
import im.vector.riotx.core.di.ScreenComponent import im.vector.riotx.core.di.ScreenComponent
@ -45,12 +46,13 @@ class VectorSettingsNotificationPreferenceFragment : VectorSettingsBaseFragment(
.find { it.ruleId == RuleIds.RULE_ID_DISABLE_ALL } .find { it.ruleId == RuleIds.RULE_ID_DISABLE_ALL }
if (mRuleMaster == null) { if (mRuleMaster == null) {
// The home server does not support RULE_ID_DISABLE_ALL, so hide the preference
pref.isVisible = false pref.isVisible = false
return return
} }
val areNotifEnabledAtAccountLevelt = !mRuleMaster.enabled val areNotifEnabledAtAccountLevel = !mRuleMaster.enabled
(pref as SwitchPreference).isChecked = areNotifEnabledAtAccountLevelt (pref as SwitchPreference).isChecked = areNotifEnabledAtAccountLevel
} }
} }
@ -114,19 +116,21 @@ class VectorSettingsNotificationPreferenceFragment : VectorSettingsBaseFragment(
.find { it.ruleId == RuleIds.RULE_ID_DISABLE_ALL } .find { it.ruleId == RuleIds.RULE_ID_DISABLE_ALL }
?.let { ?.let {
//Trick, we must enable this room to disable notifications //Trick, we must enable this room to disable notifications
pushRuleService.updatePushRuleEnableStatus("override", it, !switchPref.isChecked, pushRuleService.updatePushRuleEnableStatus(RuleKind.OVERRIDE,
object : MatrixCallback<Unit> { it,
!switchPref.isChecked,
object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) { override fun onSuccess(data: Unit) {
pushRuleService.fetchPushRules() pushRuleService.fetchPushRules()
} }
override fun onFailure(failure: Throwable) { override fun onFailure(failure: Throwable) {
//revert the check box //revert the check box
switchPref.isChecked = !switchPref.isChecked switchPref.isChecked = !switchPref.isChecked
Toast.makeText(activity, R.string.unknown_error, Toast.LENGTH_SHORT).show() Toast.makeText(activity, R.string.unknown_error, Toast.LENGTH_SHORT).show()
} }
}) })
} }
} }

View File

@ -17,6 +17,7 @@ package im.vector.riotx.features.settings.troubleshoot
import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.pushrules.RuleIds 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.R
import im.vector.riotx.core.di.ActiveSessionHolder import im.vector.riotx.core.di.ActiveSessionHolder
import im.vector.riotx.core.resources.StringProvider import im.vector.riotx.core.resources.StringProvider
@ -45,8 +46,7 @@ class TestAccountSettings @Inject constructor(private val stringProvider: String
override fun doFix() { override fun doFix() {
if (manager?.diagStatus == TestStatus.RUNNING) return //wait before all is finished if (manager?.diagStatus == TestStatus.RUNNING) return //wait before all is finished
// TODO Use constant for kind session.updatePushRuleEnableStatus(RuleKind.OVERRIDE, defaultRule, !defaultRule.enabled,
session.updatePushRuleEnableStatus("override", defaultRule, !defaultRule.enabled,
object : MatrixCallback<Unit> { object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) { override fun onSuccess(data: Unit) {