Let GetProfileInfoTask store result into DB, except when we want to do bulk insertion.
This commit is contained in:
parent
0a6d620f27
commit
5a2d74443d
|
@ -17,26 +17,39 @@
|
||||||
|
|
||||||
package org.matrix.android.sdk.internal.session.profile
|
package org.matrix.android.sdk.internal.session.profile
|
||||||
|
|
||||||
|
import com.zhuinden.monarchy.Monarchy
|
||||||
|
import org.matrix.android.sdk.api.session.user.model.User
|
||||||
import org.matrix.android.sdk.api.util.JsonDict
|
import org.matrix.android.sdk.api.util.JsonDict
|
||||||
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
|
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
|
||||||
import org.matrix.android.sdk.internal.network.executeRequest
|
import org.matrix.android.sdk.internal.network.executeRequest
|
||||||
|
import org.matrix.android.sdk.internal.session.user.UserEntityFactory
|
||||||
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 javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
internal abstract class GetProfileInfoTask : Task<GetProfileInfoTask.Params, JsonDict> {
|
internal abstract class GetProfileInfoTask : Task<GetProfileInfoTask.Params, JsonDict> {
|
||||||
data class Params(
|
data class Params(
|
||||||
val userId: String
|
val userId: String,
|
||||||
|
val storeInDatabase: Boolean = true,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class DefaultGetProfileInfoTask @Inject constructor(
|
internal class DefaultGetProfileInfoTask @Inject constructor(
|
||||||
private val profileAPI: ProfileAPI,
|
private val profileAPI: ProfileAPI,
|
||||||
private val globalErrorReceiver: GlobalErrorReceiver
|
private val globalErrorReceiver: GlobalErrorReceiver,
|
||||||
|
private val monarchy: Monarchy,
|
||||||
) : GetProfileInfoTask() {
|
) : GetProfileInfoTask() {
|
||||||
|
|
||||||
override suspend fun execute(params: Params): JsonDict {
|
override suspend fun execute(params: Params): JsonDict {
|
||||||
return executeRequest(globalErrorReceiver) {
|
return executeRequest(globalErrorReceiver) {
|
||||||
profileAPI.getProfile(params.userId)
|
profileAPI.getProfile(params.userId)
|
||||||
|
}.also { user ->
|
||||||
|
if (params.storeInDatabase) {
|
||||||
|
// Insert into DB
|
||||||
|
monarchy.awaitTransaction {
|
||||||
|
it.insertOrUpdate(UserEntityFactory.create(User.fromJson(params.userId, user)))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,10 +71,16 @@ internal class UpdateUserWorker(context: Context, params: WorkerParameters, sess
|
||||||
?.saveLocally()
|
?.saveLocally()
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun fetchUsers(userIdsToFetch: Collection<String>) = userIdsToFetch.mapNotNull {
|
private suspend fun fetchUsers(userIdsToFetch: Collection<String>): List<User> {
|
||||||
tryOrNull {
|
return userIdsToFetch.mapNotNull { userId ->
|
||||||
val profileJson = getProfileInfoTask.execute(GetProfileInfoTask.Params(it))
|
tryOrNull {
|
||||||
User.fromJson(it, profileJson)
|
val profileJson = getProfileInfoTask.execute(GetProfileInfoTask.Params(
|
||||||
|
userId = userId,
|
||||||
|
// Bulk insert later, so tell the task not to store the User.
|
||||||
|
storeInDatabase = false,
|
||||||
|
))
|
||||||
|
User.fromJson(userId, profileJson)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue