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(
params.threePid,
clientSecret,
1,
tokenResponse.sid)
}
}

View File

@ -16,14 +16,22 @@
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.annotations.PrimaryKey
internal open class IdentityPendingBindingEntity(
var threePidValue: String = "",
var medium: String = "",
@PrimaryKey var threePid: String = "",
/* Managed by Riot */
var clientSecret: String = "",
/* Managed by Riot */
var sendAttempt: Int = 0,
/* Provided by the identity server */
var sid: String = ""
) : 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
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.kotlin.createObject
import io.realm.kotlin.where
internal fun IdentityPendingBindingEntity.Companion.get(realm: Realm, threePid: ThreePid): IdentityPendingBindingEntity? {
return realm.where<IdentityPendingBindingEntity>()
.equalTo(IdentityPendingBindingEntityFields.THREE_PID_VALUE, threePid.value)
.equalTo(IdentityPendingBindingEntityFields.MEDIUM, threePid.toMedium())
.equalTo(IdentityPendingBindingEntityFields.THREE_PID, threePid.toPrimaryKey())
.findFirst()
}
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) {
realm.where<IdentityPendingBindingEntity>()
.equalTo(IdentityPendingBindingEntityFields.THREE_PID_VALUE, threePid.value)
.equalTo(IdentityPendingBindingEntityFields.MEDIUM, threePid.toMedium())
.equalTo(IdentityPendingBindingEntityFields.THREE_PID, threePid.toPrimaryKey())
.findAll()
.deleteAllFromRealm()
}

View File

@ -34,6 +34,7 @@ internal interface IdentityServiceStore {
*/
fun storePendingBinding(threePid: ThreePid,
clientSecret: String,
sendAttempt: Int,
sid: String)
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 {
it.executeTransaction { realm ->
IdentityPendingBindingEntity.getOrCreate(realm, threePid).let { entity ->
entity.threePidValue = threePid.value
entity.medium = threePid.toMedium()
entity.clientSecret = clientSecret
entity.sendAttempt = sendAttempt
entity.sid = sid
}
}