Revert status to RoomMembersLoadStatusType.NONE) in case of failure

This commit is contained in:
Benoit Marty 2020-12-28 14:52:49 +01:00
parent 9f3176c49c
commit cc01f25d8f
1 changed files with 17 additions and 7 deletions

View File

@ -82,16 +82,19 @@ internal class DefaultLoadRoomMembersTask @Inject constructor(
}
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
}
setRoomMembersLoadStatus(params.roomId, RoomMembersLoadStatusType.LOADING)
val lastToken = syncTokenStore.getLastToken()
val response = executeRequest<RoomMembersResponse>(eventBus) {
apiCall = roomAPI.getMembers(params.roomId, lastToken, null, params.excludeMembership?.value)
val response = try {
executeRequest<RoomMembersResponse>(eventBus) {
apiCall = roomAPI.getMembers(params.roomId, lastToken, null, params.excludeMembership?.value)
}
} catch (throwable: Throwable) {
// Revert status to NONE
setRoomMembersLoadStatus(params.roomId, RoomMembersLoadStatusType.NONE)
throw throwable
}
// This will also set the status to LOADED
insertInDb(response, params.roomId)
}
@ -125,4 +128,11 @@ internal class DefaultLoadRoomMembersTask @Inject constructor(
}
return result ?: RoomMembersLoadStatusType.NONE
}
private suspend fun setRoomMembersLoadStatus(roomId: String, status: RoomMembersLoadStatusType) {
monarchy.awaitTransaction { realm ->
val roomEntity = RoomEntity.where(realm, roomId).findFirst() ?: realm.createObject(roomId)
roomEntity.membersLoadStatus = status
}
}
}