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 com.nextcloud.talk.data.user.model.UserEntity
import io.reactivex.Maybe import io.reactivex.Maybe
import io.reactivex.Single import io.reactivex.Single
import io.reactivex.SingleObserver
import io.reactivex.disposables.Disposable
import java.lang.Boolean.FALSE import java.lang.Boolean.FALSE
import java.lang.Boolean.TRUE import java.lang.Boolean.TRUE
@ -85,36 +83,19 @@ abstract class UsersDao {
abstract fun getUserWithUsernameAndServer(username: String, server: String): Maybe<UserEntity> abstract fun getUserWithUsernameAndServer(username: String, server: String): Maybe<UserEntity>
@Transaction @Transaction
open suspend fun setUserAsActiveWithId(id: Long): Boolean { open fun setUserAsActiveWithId(id: Long): Single<Boolean> {
val users = getUsers() return getUsers()
var result = TRUE .map { users ->
users.forEach { user ->
users.subscribe(object : SingleObserver<List<UserEntity>> { user.current = user.id == id
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
updateUser(user) updateUser(user)
} }
true
} }
} .onErrorReturn { e ->
override fun onSubscribe(d: Disposable) {
// unused atm
}
override fun onError(e: Throwable) {
Log.e(TAG, "Error setting user active", e) Log.e(TAG, "Error setting user active", e)
result = FALSE false
} }
})
return result
} }
@Transaction @Transaction

View file

@ -39,7 +39,7 @@ interface UsersRepository {
fun getUserWithUsernameAndServer(username: String, server: String): Maybe<User> fun getUserWithUsernameAndServer(username: String, server: String): Maybe<User>
fun updateUser(user: User): Int fun updateUser(user: User): Int
fun insertUser(user: User): Long fun insertUser(user: User): Long
suspend fun setUserAsActiveWithId(id: Long): Boolean fun setUserAsActiveWithId(id: Long): Single<Boolean>
fun deleteUserWithId(id: Long) fun deleteUserWithId(id: Long)
suspend fun setAnyUserAsActive(): Boolean suspend fun setAnyUserAsActive(): Boolean
suspend fun markUserForDeletion(id: Long): 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)) return usersDao.saveUser(UserMapper.toEntity(user))
} }
override suspend fun setUserAsActiveWithId(id: Long): Boolean { override fun setUserAsActiveWithId(id: Long): Single<Boolean> {
return usersDao.setUserAsActiveWithId(id) return usersDao.setUserAsActiveWithId(id)
} }