Identity: change DB and add sendAttempt

This commit is contained in:
Benoit Marty 2020-05-10 09:25:30 +02:00
parent b44f5d3b4a
commit 7e8e1ab9b7
5 changed files with 18 additions and 12 deletions

View File

@ -67,6 +67,7 @@ internal class DefaultIdentityRequestTokenForBindingTask @Inject constructor(
identityServiceStore.storePendingBinding( identityServiceStore.storePendingBinding(
params.threePid, params.threePid,
clientSecret, clientSecret,
1,
tokenResponse.sid) tokenResponse.sid)
} }
} }

View File

@ -16,14 +16,22 @@
package im.vector.matrix.android.internal.session.identity.db package im.vector.matrix.android.internal.session.identity.db
import im.vector.matrix.android.api.session.identity.ThreePid
import im.vector.matrix.android.api.session.identity.toMedium
import io.realm.RealmObject import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
internal open class IdentityPendingBindingEntity( internal open class IdentityPendingBindingEntity(
var threePidValue: String = "", @PrimaryKey var threePid: String = "",
var medium: String = "", /* Managed by Riot */
var clientSecret: String = "", var clientSecret: String = "",
/* Managed by Riot */
var sendAttempt: Int = 0,
/* Provided by the identity server */
var sid: String = "" var sid: String = ""
) : RealmObject() { ) : RealmObject() {
companion object companion object {
fun ThreePid.toPrimaryKey() = "${toMedium()}_$value"
}
} }

View File

@ -17,26 +17,23 @@
package im.vector.matrix.android.internal.session.identity.db package im.vector.matrix.android.internal.session.identity.db
import im.vector.matrix.android.api.session.identity.ThreePid import im.vector.matrix.android.api.session.identity.ThreePid
import im.vector.matrix.android.api.session.identity.toMedium
import io.realm.Realm import io.realm.Realm
import io.realm.kotlin.createObject import io.realm.kotlin.createObject
import io.realm.kotlin.where import io.realm.kotlin.where
internal fun IdentityPendingBindingEntity.Companion.get(realm: Realm, threePid: ThreePid): IdentityPendingBindingEntity? { internal fun IdentityPendingBindingEntity.Companion.get(realm: Realm, threePid: ThreePid): IdentityPendingBindingEntity? {
return realm.where<IdentityPendingBindingEntity>() return realm.where<IdentityPendingBindingEntity>()
.equalTo(IdentityPendingBindingEntityFields.THREE_PID_VALUE, threePid.value) .equalTo(IdentityPendingBindingEntityFields.THREE_PID, threePid.toPrimaryKey())
.equalTo(IdentityPendingBindingEntityFields.MEDIUM, threePid.toMedium())
.findFirst() .findFirst()
} }
internal fun IdentityPendingBindingEntity.Companion.getOrCreate(realm: Realm, threePid: ThreePid): IdentityPendingBindingEntity { internal fun IdentityPendingBindingEntity.Companion.getOrCreate(realm: Realm, threePid: ThreePid): IdentityPendingBindingEntity {
return get(realm, threePid) ?: realm.createObject() return get(realm, threePid) ?: realm.createObject(threePid.toPrimaryKey())
} }
internal fun IdentityPendingBindingEntity.Companion.delete(realm: Realm, threePid: ThreePid) { internal fun IdentityPendingBindingEntity.Companion.delete(realm: Realm, threePid: ThreePid) {
realm.where<IdentityPendingBindingEntity>() realm.where<IdentityPendingBindingEntity>()
.equalTo(IdentityPendingBindingEntityFields.THREE_PID_VALUE, threePid.value) .equalTo(IdentityPendingBindingEntityFields.THREE_PID, threePid.toPrimaryKey())
.equalTo(IdentityPendingBindingEntityFields.MEDIUM, threePid.toMedium())
.findAll() .findAll()
.deleteAllFromRealm() .deleteAllFromRealm()
} }

View File

@ -34,6 +34,7 @@ internal interface IdentityServiceStore {
*/ */
fun storePendingBinding(threePid: ThreePid, fun storePendingBinding(threePid: ThreePid,
clientSecret: String, clientSecret: String,
sendAttempt: Int,
sid: String) sid: String)
fun getPendingBinding(threePid: ThreePid): IdentityPendingBindingEntity? fun getPendingBinding(threePid: ThreePid): IdentityPendingBindingEntity?

View File

@ -61,13 +61,12 @@ internal class RealmIdentityServiceStore @Inject constructor(
} }
} }
override fun storePendingBinding(threePid: ThreePid, clientSecret: String, sid: String) { override fun storePendingBinding(threePid: ThreePid, clientSecret: String, sendAttempt: Int, sid: String) {
Realm.getInstance(realmConfiguration).use { Realm.getInstance(realmConfiguration).use {
it.executeTransaction { realm -> it.executeTransaction { realm ->
IdentityPendingBindingEntity.getOrCreate(realm, threePid).let { entity -> IdentityPendingBindingEntity.getOrCreate(realm, threePid).let { entity ->
entity.threePidValue = threePid.value
entity.medium = threePid.toMedium()
entity.clientSecret = clientSecret entity.clientSecret = clientSecret
entity.sendAttempt = sendAttempt
entity.sid = sid entity.sid = sid
} }
} }