Use the new RoomMembersLoadStatusType.LOADING value

This commit is contained in:
Benoit Marty 2020-12-18 16:38:27 +01:00
parent b0ba62aa31
commit ca4b91a98f
1 changed files with 39 additions and 9 deletions

View File

@ -17,12 +17,19 @@
package org.matrix.android.sdk.internal.session.room.membership package org.matrix.android.sdk.internal.session.room.membership
import com.zhuinden.monarchy.Monarchy import com.zhuinden.monarchy.Monarchy
import io.realm.Realm
import io.realm.kotlin.createObject
import kotlinx.coroutines.TimeoutCancellationException
import org.greenrobot.eventbus.EventBus
import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.send.SendState import org.matrix.android.sdk.api.session.room.send.SendState
import org.matrix.android.sdk.internal.database.awaitNotEmptyResult
import org.matrix.android.sdk.internal.database.mapper.toEntity import org.matrix.android.sdk.internal.database.mapper.toEntity
import org.matrix.android.sdk.internal.database.model.CurrentStateEventEntity import org.matrix.android.sdk.internal.database.model.CurrentStateEventEntity
import org.matrix.android.sdk.internal.database.model.EventInsertType import org.matrix.android.sdk.internal.database.model.EventInsertType
import org.matrix.android.sdk.internal.database.model.RoomEntity import org.matrix.android.sdk.internal.database.model.RoomEntity
import org.matrix.android.sdk.internal.database.model.RoomEntityFields
import org.matrix.android.sdk.internal.database.model.RoomMembersLoadStatusType
import org.matrix.android.sdk.internal.database.query.copyToRealmOrIgnore import org.matrix.android.sdk.internal.database.query.copyToRealmOrIgnore
import org.matrix.android.sdk.internal.database.query.getOrCreate import org.matrix.android.sdk.internal.database.query.getOrCreate
import org.matrix.android.sdk.internal.database.query.where import org.matrix.android.sdk.internal.database.query.where
@ -33,10 +40,7 @@ import org.matrix.android.sdk.internal.session.room.summary.RoomSummaryUpdater
import org.matrix.android.sdk.internal.session.sync.SyncTokenStore import org.matrix.android.sdk.internal.session.sync.SyncTokenStore
import org.matrix.android.sdk.internal.task.Task import org.matrix.android.sdk.internal.task.Task
import org.matrix.android.sdk.internal.util.awaitTransaction import org.matrix.android.sdk.internal.util.awaitTransaction
import io.realm.Realm import java.util.concurrent.TimeUnit
import io.realm.kotlin.createObject
import org.greenrobot.eventbus.EventBus
import org.matrix.android.sdk.internal.database.model.RoomMembersLoadStatusType
import javax.inject.Inject import javax.inject.Inject
internal interface LoadRoomMembersTask : Task<LoadRoomMembersTask.Params, Unit> { internal interface LoadRoomMembersTask : Task<LoadRoomMembersTask.Params, Unit> {
@ -57,9 +61,33 @@ internal class DefaultLoadRoomMembersTask @Inject constructor(
) : LoadRoomMembersTask { ) : LoadRoomMembersTask {
override suspend fun execute(params: LoadRoomMembersTask.Params) { override suspend fun execute(params: LoadRoomMembersTask.Params) {
if (areAllMembersAlreadyLoaded(params.roomId)) { when (getRoomMembersLoadStatus(params.roomId)) {
return RoomMembersLoadStatusType.NONE -> doRequest(params)
RoomMembersLoadStatusType.LOADING -> waitPreviousRequestToFinish(params)
RoomMembersLoadStatusType.LOADED -> Unit
} }
}
private suspend fun waitPreviousRequestToFinish(params: LoadRoomMembersTask.Params) {
try {
awaitNotEmptyResult(monarchy.realmConfiguration, TimeUnit.MINUTES.toMillis(1L)) { realm ->
realm.where(RoomEntity::class.java)
.equalTo(RoomEntityFields.ROOM_ID, params.roomId)
.equalTo(RoomEntityFields.MEMBERS_LOAD_STATUS_STR, RoomMembersLoadStatusType.LOADED.name)
}
} catch (exception: TimeoutCancellationException) {
// Timeout, do the request anyway (?)
doRequest(params)
}
}
private suspend fun doRequest(params: LoadRoomMembersTask.Params) {
monarchy.awaitTransaction { realm ->
val roomEntity = RoomEntity.where(realm, params.roomId).findFirst()
?: realm.createObject(params.roomId)
roomEntity.membersLoadStatus = RoomMembersLoadStatusType.LOADING
}
val lastToken = syncTokenStore.getLastToken() val lastToken = syncTokenStore.getLastToken()
val response = executeRequest<RoomMembersResponse>(eventBus) { val response = executeRequest<RoomMembersResponse>(eventBus) {
apiCall = roomAPI.getMembers(params.roomId, lastToken, null, params.excludeMembership?.value) apiCall = roomAPI.getMembers(params.roomId, lastToken, null, params.excludeMembership?.value)
@ -90,9 +118,11 @@ internal class DefaultLoadRoomMembersTask @Inject constructor(
} }
} }
private fun areAllMembersAlreadyLoaded(roomId: String): Boolean { private fun getRoomMembersLoadStatus(roomId: String): RoomMembersLoadStatusType {
return Realm.getInstance(monarchy.realmConfiguration).use { var result: RoomMembersLoadStatusType?
RoomEntity.where(it, roomId).findFirst()?.membersLoadStatus == RoomMembersLoadStatusType.LOADED Realm.getInstance(monarchy.realmConfiguration).use {
result = RoomEntity.where(it, roomId).findFirst()?.membersLoadStatus
} }
return result ?: RoomMembersLoadStatusType.NONE
} }
} }