Let GetProfileInfoTask store result into DB, except when we want to do bulk insertion.

This commit is contained in:
Benoit Marty 2022-10-14 12:07:52 +02:00
parent 0a6d620f27
commit 5a2d74443d
2 changed files with 25 additions and 6 deletions

View File

@ -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)))
}
}
} }
} }
} }

View File

@ -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> {
return userIdsToFetch.mapNotNull { userId ->
tryOrNull { tryOrNull {
val profileJson = getProfileInfoTask.execute(GetProfileInfoTask.Params(it)) val profileJson = getProfileInfoTask.execute(GetProfileInfoTask.Params(
User.fromJson(it, profileJson) userId = userId,
// Bulk insert later, so tell the task not to store the User.
storeInDatabase = false,
))
User.fromJson(userId, profileJson)
}
} }
} }