Migrate PushUtils from requery to room

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-07-23 16:21:40 +02:00
parent 285b9cff03
commit bcb728a0d1
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B

View File

@ -29,18 +29,15 @@ import android.text.TextUtils;
import android.util.Base64; import android.util.Base64;
import android.util.Log; import android.util.Log;
import com.bluelinelabs.logansquare.LoganSquare;
import com.nextcloud.talk.R; import com.nextcloud.talk.R;
import com.nextcloud.talk.api.NcApi; import com.nextcloud.talk.api.NcApi;
import com.nextcloud.talk.application.NextcloudTalkApplication; import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.data.user.model.User; import com.nextcloud.talk.data.user.model.User;
import com.nextcloud.talk.events.EventStatus; import com.nextcloud.talk.events.EventStatus;
import com.nextcloud.talk.models.SignatureVerification; import com.nextcloud.talk.models.SignatureVerification;
import com.nextcloud.talk.models.database.UserEntity;
import com.nextcloud.talk.models.json.push.PushConfigurationState; import com.nextcloud.talk.models.json.push.PushConfigurationState;
import com.nextcloud.talk.models.json.push.PushRegistrationOverall; import com.nextcloud.talk.models.json.push.PushRegistrationOverall;
import com.nextcloud.talk.users.UserManager; import com.nextcloud.talk.users.UserManager;
import com.nextcloud.talk.utils.database.user.UserUtils;
import com.nextcloud.talk.utils.preferences.AppPreferences; import com.nextcloud.talk.utils.preferences.AppPreferences;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
@ -71,6 +68,7 @@ import javax.inject.Inject;
import autodagger.AutoInjector; import autodagger.AutoInjector;
import io.reactivex.Observer; import io.reactivex.Observer;
import io.reactivex.SingleObserver;
import io.reactivex.annotations.NonNull; import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
@ -79,9 +77,6 @@ import io.reactivex.schedulers.Schedulers;
public class PushUtils { public class PushUtils {
private static final String TAG = "PushUtils"; private static final String TAG = "PushUtils";
@Inject
UserUtils userUtils;
@Inject @Inject
UserManager userManager; UserManager userManager;
@ -238,21 +233,20 @@ public class PushUtils {
+ devicePublicKeyBase64 + devicePublicKeyBase64
+ "\n-----END PUBLIC KEY-----\n"; + "\n-----END PUBLIC KEY-----\n";
if (userUtils.anyUserExists()) { List<User> users = userManager.getUsers().blockingGet();
for (Object userEntityObject : userUtils.getUsers()) {
UserEntity userEntity = (UserEntity) userEntityObject;
if (!userEntity.getScheduledForDeletion()) { for (User user : users) {
if (!user.getScheduledForDeletion()) {
Map<String, String> nextcloudRegisterPushMap = new HashMap<>(); Map<String, String> nextcloudRegisterPushMap = new HashMap<>();
nextcloudRegisterPushMap.put("format", "json"); nextcloudRegisterPushMap.put("format", "json");
nextcloudRegisterPushMap.put("pushTokenHash", pushTokenHash); nextcloudRegisterPushMap.put("pushTokenHash", pushTokenHash);
nextcloudRegisterPushMap.put("devicePublicKey", devicePublicKeyBase64); nextcloudRegisterPushMap.put("devicePublicKey", devicePublicKeyBase64);
nextcloudRegisterPushMap.put("proxyServer", proxyServer); nextcloudRegisterPushMap.put("proxyServer", proxyServer);
registerDeviceWithNextcloud(ncApi, nextcloudRegisterPushMap, token, userEntity); registerDeviceWithNextcloud(ncApi, nextcloudRegisterPushMap, token, user);
}
} }
} }
} }
} else { } else {
Log.e(TAG, "push token was empty when trying to register at nextcloud server"); Log.e(TAG, "push token was empty when trying to register at nextcloud server");
@ -262,12 +256,12 @@ public class PushUtils {
private void registerDeviceWithNextcloud(NcApi ncApi, private void registerDeviceWithNextcloud(NcApi ncApi,
Map<String, String> nextcloudRegisterPushMap, Map<String, String> nextcloudRegisterPushMap,
String token, String token,
UserEntity userEntity) { User user) {
String credentials = ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()); String credentials = ApiUtils.getCredentials(user.getUsername(), user.getToken());
ncApi.registerDeviceForNotificationsWithNextcloud( ncApi.registerDeviceForNotificationsWithNextcloud(
credentials, credentials,
ApiUtils.getUrlNextcloudPush(userEntity.getBaseUrl()), ApiUtils.getUrlNextcloudPush(user.getBaseUrl()),
nextcloudRegisterPushMap) nextcloudRegisterPushMap)
.subscribe(new Observer<PushRegistrationOverall>() { .subscribe(new Observer<PushRegistrationOverall>() {
@Override @Override
@ -288,12 +282,12 @@ public class PushUtils {
proxyMap.put("userPublicKey", proxyMap.put("userPublicKey",
pushRegistrationOverall.getOcs().getData().getPublicKey()); pushRegistrationOverall.getOcs().getData().getPublicKey());
registerDeviceWithPushProxy(ncApi, proxyMap, userEntity); registerDeviceWithPushProxy(ncApi, proxyMap, user);
} }
@Override @Override
public void onError(@NonNull Throwable e) { public void onError(@NonNull Throwable e) {
eventBus.post(new EventStatus(userEntity.getId(), eventBus.post(new EventStatus(user.getId(),
EventStatus.EventType.PUSH_REGISTRATION, false)); EventStatus.EventType.PUSH_REGISTRATION, false));
} }
@ -304,7 +298,7 @@ public class PushUtils {
}); });
} }
private void registerDeviceWithPushProxy(NcApi ncApi, Map<String, String> proxyMap, UserEntity userEntity) { private void registerDeviceWithPushProxy(NcApi ncApi, Map<String, String> proxyMap, User user) {
ncApi.registerDeviceForNotificationsWithPushProxy(ApiUtils.getUrlPushProxy(), proxyMap) ncApi.registerDeviceForNotificationsWithPushProxy(ApiUtils.getUrlPushProxy(), proxyMap)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.subscribe(new Observer<Void>() { .subscribe(new Observer<Void>() {
@ -317,7 +311,7 @@ public class PushUtils {
public void onNext(@NonNull Void aVoid) { public void onNext(@NonNull Void aVoid) {
try { try {
Log.d(TAG, "pushToken successfully registered at pushproxy."); Log.d(TAG, "pushToken successfully registered at pushproxy.");
createOrUpdateUser(proxyMap, userEntity); createOrUpdateUser(proxyMap, user);
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "IOException while updating user", e); Log.e(TAG, "IOException while updating user", e);
} }
@ -325,7 +319,7 @@ public class PushUtils {
@Override @Override
public void onError(@NonNull Throwable e) { public void onError(@NonNull Throwable e) {
eventBus.post(new EventStatus(userEntity.getId(), eventBus.post(new EventStatus(user.getId(),
EventStatus.EventType.PUSH_REGISTRATION, false)); EventStatus.EventType.PUSH_REGISTRATION, false));
} }
@ -336,7 +330,7 @@ public class PushUtils {
}); });
} }
private void createOrUpdateUser(Map<String, String> proxyMap, UserEntity userEntity) throws IOException { private void createOrUpdateUser(Map<String, String> proxyMap, User user) throws IOException {
PushConfigurationState pushConfigurationState = new PushConfigurationState(); PushConfigurationState pushConfigurationState = new PushConfigurationState();
pushConfigurationState.setPushToken(proxyMap.get("pushToken")); pushConfigurationState.setPushToken(proxyMap.get("pushToken"));
pushConfigurationState.setDeviceIdentifier(proxyMap.get("deviceIdentifier")); pushConfigurationState.setDeviceIdentifier(proxyMap.get("deviceIdentifier"));
@ -344,36 +338,25 @@ public class PushUtils {
pushConfigurationState.setUserPublicKey(proxyMap.get("userPublicKey")); pushConfigurationState.setUserPublicKey(proxyMap.get("userPublicKey"));
pushConfigurationState.setUsesRegularPass(Boolean.FALSE); pushConfigurationState.setUsesRegularPass(Boolean.FALSE);
userUtils.createOrUpdateUser(null, user.setPushConfigurationState(pushConfigurationState);
null, userManager.saveUser(user).subscribe(new SingleObserver<Integer>() {
null,
userEntity.getDisplayName(),
LoganSquare.serialize(pushConfigurationState),
null,
null,
userEntity.getId(),
null,
null,
null)
.subscribe(new Observer<UserEntity>() {
@Override @Override
public void onSubscribe(@NonNull Disposable d) { public void onSubscribe(Disposable d) {
// unused atm // unused atm
} }
@Override @Override
public void onNext(@NonNull UserEntity userEntity) { public void onSuccess(Integer integer) {
eventBus.post(new EventStatus(userEntity.getId(), EventStatus.EventType.PUSH_REGISTRATION, true)); eventBus.post(new EventStatus(UserIdUtils.INSTANCE.getIdForUser(user),
EventStatus.EventType.PUSH_REGISTRATION,
true));
} }
@Override @Override
public void onError(@NonNull Throwable e) { public void onError(Throwable e) {
eventBus.post(new EventStatus(userEntity.getId(), EventStatus.EventType.PUSH_REGISTRATION, false)); eventBus.post(new EventStatus(UserIdUtils.INSTANCE.getIdForUser(user),
} EventStatus.EventType.PUSH_REGISTRATION,
false));
@Override
public void onComplete() {
// unused atm
} }
}); });
} }