UsersDao: fix race condition in setUserAsActiveWithId

Make the method return a Single instead, and don't make it a suspend function

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-06-30 16:43:39 +02:00 committed by Andy Scherzinger
parent 205292f9b0
commit f339648875
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
3 changed files with 11 additions and 30 deletions

View file

@ -32,8 +32,6 @@ import androidx.room.Update
import com.nextcloud.talk.data.user.model.UserEntity
import io.reactivex.Maybe
import io.reactivex.Single
import io.reactivex.SingleObserver
import io.reactivex.disposables.Disposable
import java.lang.Boolean.FALSE
import java.lang.Boolean.TRUE
@ -85,36 +83,19 @@ abstract class UsersDao {
abstract fun getUserWithUsernameAndServer(username: String, server: String): Maybe<UserEntity>
@Transaction
open suspend fun setUserAsActiveWithId(id: Long): Boolean {
val users = getUsers()
var result = TRUE
users.subscribe(object : SingleObserver<List<UserEntity>> {
override fun onSuccess(users: List<UserEntity>) {
for (user in users) {
// removed from clause: && UserStatus.ACTIVE == user.status
if (user.id != id) {
user.current = TRUE
updateUser(user)
} // removed from clause: && UserStatus.ACTIVE != user.status
else if (user.id == id) {
user.current = TRUE
open fun setUserAsActiveWithId(id: Long): Single<Boolean> {
return getUsers()
.map { users ->
users.forEach { user ->
user.current = user.id == id
updateUser(user)
}
true
}
}
override fun onSubscribe(d: Disposable) {
// unused atm
}
override fun onError(e: Throwable) {
.onErrorReturn { e ->
Log.e(TAG, "Error setting user active", e)
result = FALSE
false
}
})
return result
}
@Transaction

View file

@ -39,7 +39,7 @@ interface UsersRepository {
fun getUserWithUsernameAndServer(username: String, server: String): Maybe<User>
fun updateUser(user: User): Int
fun insertUser(user: User): Long
suspend fun setUserAsActiveWithId(id: Long): Boolean
fun setUserAsActiveWithId(id: Long): Single<Boolean>
fun deleteUserWithId(id: Long)
suspend fun setAnyUserAsActive(): Boolean
suspend fun markUserForDeletion(id: Long): Boolean

View file

@ -73,7 +73,7 @@ class UsersRepositoryImpl(private val usersDao: UsersDao) : UsersRepository {
return usersDao.saveUser(UserMapper.toEntity(user))
}
override suspend fun setUserAsActiveWithId(id: Long): Boolean {
override fun setUserAsActiveWithId(id: Long): Single<Boolean> {
return usersDao.setUserAsActiveWithId(id)
}