PushUtils: update only pushState when updating users

This is to work around a race condition where this class would asynchronously overwrite other user attributes
with old values after a user switch.

Co-authored-by: Marcel Hibbe <dev@mhibbe.de>
Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-12-14 12:15:21 +01:00 committed by Andy Scherzinger
parent 2e2d4a9ca3
commit a37f947eb9
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
5 changed files with 16 additions and 2 deletions

View File

@ -29,6 +29,7 @@ import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.nextcloud.talk.data.user.model.UserEntity
import com.nextcloud.talk.models.json.push.PushConfigurationState
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
@ -89,6 +90,9 @@ abstract class UsersDao {
)
abstract fun setUserAsActiveWithId(id: Long): Int
@Query("Update User SET pushConfigurationState = :state WHERE id == :id")
abstract fun updatePushState(id: Long, state: PushConfigurationState): Single<Int>
companion object {
const val TAG = "UsersDao"
}

View File

@ -23,6 +23,7 @@
package com.nextcloud.talk.data.user
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.json.push.PushConfigurationState
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
@ -42,4 +43,5 @@ interface UsersRepository {
fun insertUser(user: User): Long
fun setUserAsActiveWithId(id: Long): Single<Boolean>
fun deleteUser(user: User): Int
fun updatePushState(id: Long, state: PushConfigurationState): Single<Int>
}

View File

@ -23,6 +23,7 @@
package com.nextcloud.talk.data.user
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.json.push.PushConfigurationState
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
@ -86,4 +87,8 @@ class UsersRepositoryImpl(private val usersDao: UsersDao) : UsersRepository {
override fun deleteUser(user: User): Int {
return usersDao.deleteUser(UserMapper.toEntity(user))
}
override fun updatePushState(id: Long, state: PushConfigurationState): Single<Int> {
return usersDao.updatePushState(id, state)
}
}

View File

@ -230,6 +230,10 @@ class UserManager internal constructor(private val userRepository: UsersReposito
return user
}
fun updatePushState(id: Long, state: PushConfigurationState): Single<Int> {
return userRepository.updatePushState(id, state)
}
companion object {
const val TAG = "UserManager"
}

View File

@ -338,8 +338,7 @@ public class PushUtils {
pushConfigurationState.setUserPublicKey(proxyMap.get("userPublicKey"));
pushConfigurationState.setUsesRegularPass(Boolean.FALSE);
user.setPushConfigurationState(pushConfigurationState);
userManager.saveUser(user).subscribe(new SingleObserver<Integer>() {
userManager.updatePushState(user.getId(), pushConfigurationState).subscribe(new SingleObserver<Integer>() {
@Override
public void onSubscribe(Disposable d) {
// unused atm