Remove userId from PushRulesEntity and PusherEntity objects

This commit is contained in:
Benoit Marty 2019-09-20 12:16:13 +02:00 committed by Benoit Marty
parent 1197d4021d
commit 2a726f54a2
11 changed files with 32 additions and 62 deletions

View file

@ -16,9 +16,6 @@
package im.vector.matrix.android.api.session.pushers
data class Pusher(
val userId: String,
val pushKey: String,
val kind: String,
val appId: String,

View file

@ -26,7 +26,6 @@ internal object PushersMapper {
fun map(pushEntity: PusherEntity): Pusher {
return Pusher(
userId = pushEntity.userId,
pushKey = pushEntity.pushKey,
kind = pushEntity.kind ?: "",
appId = pushEntity.appId,
@ -39,9 +38,8 @@ internal object PushersMapper {
)
}
fun map(pusher: JsonPusher, userId: String): PusherEntity {
fun map(pusher: JsonPusher): PusherEntity {
return PusherEntity(
userId = userId,
pushKey = pusher.pushKey,
kind = pusher.kind,
appId = pusher.appId,
@ -58,6 +56,6 @@ internal fun PusherEntity.asDomain(): Pusher {
return PushersMapper.map(this)
}
internal fun JsonPusher.toEntity(userId: String): PusherEntity {
return PushersMapper.map(this, userId)
internal fun JsonPusher.toEntity(): PusherEntity {
return PushersMapper.map(this)
}

View file

@ -18,12 +18,9 @@ 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 = "",
var pushRules: RealmList<PushRuleEntity> = RealmList()
) : RealmObject() {

View file

@ -17,7 +17,6 @@ package im.vector.matrix.android.internal.database.model
import im.vector.matrix.android.api.session.pushers.PusherState
import io.realm.RealmObject
import io.realm.annotations.Index
//TODO
// at java.lang.Thread.run(Thread.java:764)
@ -29,8 +28,6 @@ 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,
var appId: String = "",

View file

@ -25,10 +25,8 @@ import io.realm.RealmQuery
import io.realm.kotlin.where
internal fun PusherEntity.Companion.where(realm: Realm,
userId: String,
pushKey: String? = null): RealmQuery<PusherEntity> {
return realm.where<PusherEntity>()
.equalTo(PusherEntityFields.USER_ID, userId)
.apply {
if (pushKey != null) {
equalTo(PusherEntityFields.PUSH_KEY, pushKey)
@ -37,11 +35,9 @@ internal fun PusherEntity.Companion.where(realm: Realm,
}
internal fun PushRulesEntity.Companion.where(realm: Realm,
userId: String,
scope: String,
kind: RuleKind): RealmQuery<PushRulesEntity> {
return realm.where<PushRulesEntity>()
.equalTo(PushRulesEntityFields.USER_ID, userId)
.equalTo(PushRulesEntityFields.SCOPE, scope)
.equalTo(PushRulesEntityFields.KIND_STR, kind.name)
}

View file

@ -27,7 +27,6 @@ import im.vector.matrix.android.api.util.Cancelable
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.query.where
import im.vector.matrix.android.internal.di.UserId
import im.vector.matrix.android.internal.session.SessionScope
import im.vector.matrix.android.internal.session.pushers.GetPushRulesTask
import im.vector.matrix.android.internal.session.pushers.UpdatePushRuleEnableStatusTask
@ -37,13 +36,10 @@ import timber.log.Timber
import javax.inject.Inject
@SessionScope
internal class DefaultPushRuleService @Inject constructor(
@UserId
private val userId: String,
private val getPushRulesTask: GetPushRulesTask,
private val updatePushRuleEnableStatusTask: UpdatePushRuleEnableStatusTask,
private val taskExecutor: TaskExecutor,
private val monarchy: Monarchy
internal class DefaultPushRuleService @Inject constructor(private val getPushRulesTask: GetPushRulesTask,
private val updatePushRuleEnableStatusTask: UpdatePushRuleEnableStatusTask,
private val taskExecutor: TaskExecutor,
private val monarchy: Monarchy
) : PushRuleService {
private var listeners = ArrayList<PushRuleService.PushRuleListener>()
@ -64,27 +60,27 @@ internal class DefaultPushRuleService @Inject constructor(
monarchy.doWithRealm { realm ->
// FIXME PushRulesEntity are not always created here...
// FIWME Get the push rules from the sync
PushRulesEntity.where(realm, userId, scope, RuleSetKey.CONTENT)
PushRulesEntity.where(realm, scope, RuleSetKey.CONTENT)
.findFirst()
?.let { pushRulesEntity ->
contentRules = pushRulesEntity.pushRules.map { PushRulesMapper.mapContentRule(it) }
}
PushRulesEntity.where(realm, userId, scope, RuleSetKey.OVERRIDE)
PushRulesEntity.where(realm, scope, RuleSetKey.OVERRIDE)
.findFirst()
?.let { pushRulesEntity ->
overrideRules = pushRulesEntity.pushRules.map { PushRulesMapper.map(it) }
}
PushRulesEntity.where(realm, userId, scope, RuleSetKey.ROOM)
PushRulesEntity.where(realm, scope, RuleSetKey.ROOM)
.findFirst()
?.let { pushRulesEntity ->
roomRules = pushRulesEntity.pushRules.map { PushRulesMapper.mapRoomRule(it) }
}
PushRulesEntity.where(realm, userId, scope, RuleSetKey.SENDER)
PushRulesEntity.where(realm, scope, RuleSetKey.SENDER)
.findFirst()
?.let { pushRulesEntity ->
senderRules = pushRulesEntity.pushRules.map { PushRulesMapper.mapSenderRule(it) }
}
PushRulesEntity.where(realm, userId, scope, RuleSetKey.UNDERRIDE)
PushRulesEntity.where(realm, scope, RuleSetKey.UNDERRIDE)
.findFirst()
?.let { pushRulesEntity ->
underrideRules = pushRulesEntity.pushRules.map { PushRulesMapper.map(it) }

View file

@ -57,14 +57,14 @@ internal class AddHttpPusherWorker(context: Context, params: WorkerParameters)
return Result.failure()
}
return try {
setPusher(pusher, params.userId)
setPusher(pusher)
Result.success()
} catch (exception: Throwable) {
when (exception) {
is Failure.NetworkConnection -> Result.retry()
else -> {
monarchy.awaitTransaction { realm ->
PusherEntity.where(realm, params.userId, pusher.pushKey).findFirst()?.let {
PusherEntity.where(realm, pusher.pushKey).findFirst()?.let {
//update it
it.state = PusherState.FAILED_TO_REGISTER
}
@ -76,12 +76,12 @@ internal class AddHttpPusherWorker(context: Context, params: WorkerParameters)
}
}
private suspend fun setPusher(pusher: JsonPusher, userId: String) {
private suspend fun setPusher(pusher: JsonPusher) {
executeRequest<Unit> {
apiCall = pushersAPI.setPusher(pusher)
}
monarchy.awaitTransaction { realm ->
val echo = PusherEntity.where(realm, userId, pusher.pushKey).findFirst()
val echo = PusherEntity.where(realm, pusher.pushKey).findFirst()
if (echo != null) {
//update it
echo.appDisplayName = pusher.appDisplayName
@ -93,7 +93,7 @@ internal class AddHttpPusherWorker(context: Context, params: WorkerParameters)
echo.data?.url = pusher.data?.url
echo.state = PusherState.REGISTERED
} else {
pusher.toEntity(userId).also {
pusher.toEntity().also {
it.state = PusherState.REGISTERED
realm.insertOrUpdate(it)
}

View file

@ -82,7 +82,7 @@ internal class DefaultPusherService @Inject constructor(private val context: Con
}
override fun removeHttpPusher(pushkey: String, appId: String, callback: MatrixCallback<Unit>) {
val params = RemovePusherTask.Params(userId, pushkey, appId)
val params = RemovePusherTask.Params(pushkey, appId)
removePusherTask
.configureWith(params) {
this.callback = callback
@ -93,12 +93,12 @@ internal class DefaultPusherService @Inject constructor(private val context: Con
override fun livePushers(): LiveData<List<Pusher>> {
return monarchy.findAllMappedWithChanges(
{ realm -> PusherEntity.where(realm, userId) },
{ realm -> PusherEntity.where(realm) },
{ it.asDomain() }
)
}
override fun pushers(): List<Pusher> {
return monarchy.fetchAllCopiedSync { PusherEntity.where(it, userId) }.map { it.asDomain() }
return monarchy.fetchAllCopiedSync { PusherEntity.where(it) }.map { it.asDomain() }
}
}

View file

@ -20,8 +20,6 @@ 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
import im.vector.matrix.android.internal.database.model.PusherEntityFields
import im.vector.matrix.android.internal.di.UserId
import im.vector.matrix.android.internal.network.executeRequest
import im.vector.matrix.android.internal.task.Task
import im.vector.matrix.android.internal.util.awaitTransaction
@ -33,9 +31,7 @@ internal interface GetPushRulesTask : Task<GetPushRulesTask.Params, Unit> {
}
internal class DefaultGetPushRulesTask @Inject constructor(private val pushRulesApi: PushRulesApi,
private val monarchy: Monarchy,
@UserId
private val userId: String) : GetPushRulesTask {
private val monarchy: Monarchy) : GetPushRulesTask {
override suspend fun execute(params: GetPushRulesTask.Params) {
val response = executeRequest<GetPushRulesResponse> {
@ -46,17 +42,16 @@ internal class DefaultGetPushRulesTask @Inject constructor(private val pushRules
//clear existings?
//TODO
realm.where(PushRulesEntity::class.java)
.equalTo(PusherEntityFields.USER_ID, userId)
.findAll()
.deleteAllFromRealm()
val content = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.CONTENT }
val content = PushRulesEntity(scope).apply { kind = RuleSetKey.CONTENT }
response.global.content?.forEach { rule ->
content.pushRules.add(PushRulesMapper.map(rule))
}
realm.insertOrUpdate(content)
val override = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.OVERRIDE }
val override = PushRulesEntity(scope).apply { kind = RuleSetKey.OVERRIDE }
response.global.override?.forEach { rule ->
PushRulesMapper.map(rule).also {
override.pushRules.add(it)
@ -64,19 +59,19 @@ internal class DefaultGetPushRulesTask @Inject constructor(private val pushRules
}
realm.insertOrUpdate(override)
val rooms = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.ROOM }
val rooms = PushRulesEntity(scope).apply { kind = RuleSetKey.ROOM }
response.global.room?.forEach { rule ->
rooms.pushRules.add(PushRulesMapper.map(rule))
}
realm.insertOrUpdate(rooms)
val senders = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.SENDER }
val senders = PushRulesEntity(scope).apply { kind = RuleSetKey.SENDER }
response.global.sender?.forEach { rule ->
senders.pushRules.add(PushRulesMapper.map(rule))
}
realm.insertOrUpdate(senders)
val underrides = PushRulesEntity(userId, scope).apply { kind = RuleSetKey.UNDERRIDE }
val underrides = PushRulesEntity(scope).apply { kind = RuleSetKey.UNDERRIDE }
response.global.underride?.forEach { rule ->
underrides.pushRules.add(PushRulesMapper.map(rule))
}

View file

@ -19,8 +19,6 @@ import com.zhuinden.monarchy.Monarchy
import im.vector.matrix.android.api.session.pushers.PusherState
import im.vector.matrix.android.internal.database.mapper.toEntity
import im.vector.matrix.android.internal.database.model.PusherEntity
import im.vector.matrix.android.internal.database.model.PusherEntityFields
import im.vector.matrix.android.internal.di.UserId
import im.vector.matrix.android.internal.network.executeRequest
import im.vector.matrix.android.internal.task.Task
import im.vector.matrix.android.internal.util.awaitTransaction
@ -29,9 +27,7 @@ import javax.inject.Inject
internal interface GetPushersTask : Task<Unit, Unit>
internal class DefaultGetPusherTask @Inject constructor(private val pushersAPI: PushersAPI,
private val monarchy: Monarchy,
@UserId
private val userId: String) : GetPushersTask {
private val monarchy: Monarchy) : GetPushersTask {
override suspend fun execute(params: Unit) {
val response = executeRequest<GetPushersResponse> {
@ -40,10 +36,9 @@ internal class DefaultGetPusherTask @Inject constructor(private val pushersAPI:
monarchy.awaitTransaction { realm ->
//clear existings?
realm.where(PusherEntity::class.java)
.equalTo(PusherEntityFields.USER_ID, userId)
.findAll().deleteAllFromRealm()
response.pushers?.forEach { jsonPusher ->
jsonPusher.toEntity(userId).also {
jsonPusher.toEntity().also {
it.state = PusherState.REGISTERED
realm.insertOrUpdate(it)
}

View file

@ -28,8 +28,7 @@ import io.realm.Realm
import javax.inject.Inject
internal interface RemovePusherTask : Task<RemovePusherTask.Params, Unit> {
data class Params(val userId: String,
val pushKey: String,
data class Params(val pushKey: String,
val pushAppId: String)
}
@ -40,12 +39,12 @@ internal class DefaultRemovePusherTask @Inject constructor(
override suspend fun execute(params: RemovePusherTask.Params) {
monarchy.awaitTransaction { realm ->
val existingEntity = PusherEntity.where(realm, params.userId, params.pushKey).findFirst()
val existingEntity = PusherEntity.where(realm, params.pushKey).findFirst()
existingEntity?.state = PusherState.UNREGISTERING
}
val existing = Realm.getInstance(monarchy.realmConfiguration).use { realm ->
PusherEntity.where(realm, params.userId, params.pushKey).findFirst()?.asDomain()
PusherEntity.where(realm, params.pushKey).findFirst()?.asDomain()
} ?: throw Exception("No existing pusher")
val deleteBody = JsonPusher(
@ -64,7 +63,7 @@ internal class DefaultRemovePusherTask @Inject constructor(
apiCall = pushersAPI.setPusher(deleteBody)
}
monarchy.awaitTransaction {
PusherEntity.where(it, params.userId, params.pushKey).findFirst()?.deleteFromRealm()
PusherEntity.where(it, params.pushKey).findFirst()?.deleteFromRealm()
}
}
}