Migrate AccountRemovalWorker from requery to room

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-07-23 14:29:41 +02:00
parent 7ea37b9ee3
commit 27017aac9e
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
5 changed files with 103 additions and 125 deletions

View File

@ -33,7 +33,7 @@ class ArbitraryStorageManager(private val arbitraryStoragesRepository: Arbitrary
return arbitraryStoragesRepository.getStorageSetting(accountIdentifier, key, objectString) return arbitraryStoragesRepository.getStorageSetting(accountIdentifier, key, objectString)
} }
suspend fun deleteAllEntriesForAccountIdentifier(accountIdentifier: Long) { fun deleteAllEntriesForAccountIdentifier(accountIdentifier: Long): Int {
return arbitraryStoragesRepository.deleteArbitraryStorage(accountIdentifier) return arbitraryStoragesRepository.deleteArbitraryStorage(accountIdentifier)
} }
} }

View File

@ -42,7 +42,7 @@ abstract class ArbitraryStoragesDao {
): Maybe<ArbitraryStorageEntity> ): Maybe<ArbitraryStorageEntity>
@Query("DELETE FROM ArbitraryStorage WHERE accountIdentifier = :accountIdentifier") @Query("DELETE FROM ArbitraryStorage WHERE accountIdentifier = :accountIdentifier")
abstract fun deleteArbitraryStorage(accountIdentifier: Long) abstract fun deleteArbitraryStorage(accountIdentifier: Long): Int
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun saveArbitraryStorage(arbitraryStorage: ArbitraryStorageEntity): Long abstract fun saveArbitraryStorage(arbitraryStorage: ArbitraryStorageEntity): Long

View File

@ -25,6 +25,6 @@ import io.reactivex.Maybe
interface ArbitraryStoragesRepository { interface ArbitraryStoragesRepository {
fun getStorageSetting(accountIdentifier: Long, key: String, objectString: String): Maybe<ArbitraryStorage> fun getStorageSetting(accountIdentifier: Long, key: String, objectString: String): Maybe<ArbitraryStorage>
fun deleteArbitraryStorage(accountIdentifier: Long) fun deleteArbitraryStorage(accountIdentifier: Long): Int
fun saveArbitraryStorage(arbitraryStorage: ArbitraryStorage): Long fun saveArbitraryStorage(arbitraryStorage: ArbitraryStorage): Long
} }

View File

@ -35,8 +35,8 @@ class ArbitraryStoragesRepositoryImpl(private val arbitraryStoragesDao: Arbitrar
.map { ArbitraryStorageMapper.toModel(it) } .map { ArbitraryStorageMapper.toModel(it) }
} }
override fun deleteArbitraryStorage(accountIdentifier: Long) { override fun deleteArbitraryStorage(accountIdentifier: Long): Int {
arbitraryStoragesDao.deleteArbitraryStorage(accountIdentifier) return arbitraryStoragesDao.deleteArbitraryStorage(accountIdentifier)
} }
override fun saveArbitraryStorage(arbitraryStorage: ArbitraryStorage): Long { override fun saveArbitraryStorage(arbitraryStorage: ArbitraryStorage): Long {

View File

@ -2,6 +2,8 @@
* Nextcloud Talk application * Nextcloud Talk application
* *
* @author Mario Danic * @author Mario Danic
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
* Copyright (C) 2017 Mario Danic <mario@lovelyhq.com> * Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -20,54 +22,52 @@
package com.nextcloud.talk.jobs; package com.nextcloud.talk.jobs;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import autodagger.AutoInjector;
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.models.database.UserEntity; import com.nextcloud.talk.arbitrarystorage.ArbitraryStorageManager;
import com.nextcloud.talk.data.user.model.User;
import com.nextcloud.talk.models.json.generic.GenericMeta; import com.nextcloud.talk.models.json.generic.GenericMeta;
import com.nextcloud.talk.models.json.generic.GenericOverall; import com.nextcloud.talk.models.json.generic.GenericOverall;
import com.nextcloud.talk.models.json.push.PushConfigurationState; import com.nextcloud.talk.models.json.push.PushConfigurationState;
import com.nextcloud.talk.users.UserManager;
import com.nextcloud.talk.utils.ApiUtils; import com.nextcloud.talk.utils.ApiUtils;
import com.nextcloud.talk.utils.database.arbitrarystorage.ArbitraryStorageUtils;
import com.nextcloud.talk.utils.database.user.UserUtils;
import com.nextcloud.talk.webrtc.WebSocketConnectionHelper; import com.nextcloud.talk.webrtc.WebSocketConnectionHelper;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import io.reactivex.CompletableObserver; import java.net.CookieManager;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.zip.CRC32;
import javax.inject.Inject;
import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import autodagger.AutoInjector;
import io.reactivex.Observer; import io.reactivex.Observer;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import okhttp3.JavaNetCookieJar; import okhttp3.JavaNetCookieJar;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import javax.inject.Inject;
import java.io.IOException;
import java.net.CookieManager;
import java.util.HashMap;
import java.util.Objects;
import java.util.zip.CRC32;
@AutoInjector(NextcloudTalkApplication.class) @AutoInjector(NextcloudTalkApplication.class)
public class AccountRemovalWorker extends Worker { public class AccountRemovalWorker extends Worker {
public static final String TAG = "AccountRemovalWorker"; public static final String TAG = "AccountRemovalWorker";
@Inject @Inject
UserUtils userUtils; UserManager userManager;
@Inject @Inject
ArbitraryStorageUtils arbitraryStorageUtils; ArbitraryStorageManager arbitraryStorageManager;
@Inject @Inject
Retrofit retrofit; Retrofit retrofit;
@ -86,95 +86,95 @@ public class AccountRemovalWorker extends Worker {
public Result doWork() { public Result doWork() {
NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this); NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this);
PushConfigurationState pushConfigurationState; List<User> users = userManager.getUsersScheduledForDeletion().blockingGet();
String credentials; for (User user : users) {
for (Object userEntityObject : userUtils.getUsersScheduledForDeletion()) { if (user.getPushConfigurationState() != null) {
UserEntity userEntity = (UserEntity) userEntityObject; PushConfigurationState finalPushConfigurationState = user.getPushConfigurationState();
try {
if (!TextUtils.isEmpty(userEntity.getPushConfigurationState())) {
pushConfigurationState = LoganSquare.parse(userEntity.getPushConfigurationState(),
PushConfigurationState.class);
PushConfigurationState finalPushConfigurationState = pushConfigurationState;
credentials = ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()); ncApi = retrofit
.newBuilder()
.client(okHttpClient
.newBuilder()
.cookieJar(new JavaNetCookieJar(new CookieManager()))
.build())
.build()
.create(NcApi.class);
ncApi = retrofit.newBuilder().client(okHttpClient.newBuilder().cookieJar(new ncApi.unregisterDeviceForNotificationsWithNextcloud(
JavaNetCookieJar(new CookieManager())).build()).build().create(NcApi.class); ApiUtils.getCredentials(user.getUsername(), user.getToken()),
ApiUtils.getUrlNextcloudPush(user.getBaseUrl()))
.blockingSubscribe(new Observer<GenericOverall>() {
@Override
public void onSubscribe(@NotNull Disposable d) {
// unused atm
}
ncApi.unregisterDeviceForNotificationsWithNextcloud(credentials, ApiUtils.getUrlNextcloudPush(userEntity @Override
.getBaseUrl())) public void onNext(@NotNull GenericOverall genericOverall) {
.blockingSubscribe(new Observer<GenericOverall>() { GenericMeta meta = Objects.requireNonNull(genericOverall.getOcs()).getMeta();
@Override int statusCode = Objects.requireNonNull(meta).getStatusCode();
public void onSubscribe(@NotNull Disposable d) {
// unused atm
}
@Override if (statusCode == 200 || statusCode == 202) {
public void onNext(@NotNull GenericOverall genericOverall) { HashMap<String, String> queryMap = new HashMap<>();
GenericMeta meta = Objects.requireNonNull(genericOverall.getOcs()).getMeta(); queryMap.put("deviceIdentifier",
int statusCode = Objects.requireNonNull(meta).getStatusCode(); finalPushConfigurationState.getDeviceIdentifier());
queryMap.put("userPublicKey", finalPushConfigurationState.getUserPublicKey());
queryMap.put("deviceIdentifierSignature",
finalPushConfigurationState.getDeviceIdentifierSignature());
unregisterDeviceForNotificationWithProxy(queryMap, user);
}
}
if (statusCode == 200 || statusCode == 202) { @Override
HashMap<String, String> queryMap = new HashMap<>(); public void onError(@NotNull Throwable e) {
queryMap.put("deviceIdentifier", Log.e(TAG, "error while trying to unregister Device For Notifications", e);
finalPushConfigurationState.getDeviceIdentifier()); }
queryMap.put("userPublicKey", finalPushConfigurationState.getUserPublicKey());
queryMap.put("deviceIdentifierSignature",
finalPushConfigurationState.getDeviceIdentifierSignature());
unregisterDeviceForNotificationWithProxy(queryMap, userEntity);
}
}
@Override @Override
public void onError(@NotNull Throwable e) { public void onComplete() {
Log.e(TAG, "error while trying to unregister Device For Notifications", e); // unused atm
} }
});
@Override } else {
public void onComplete() { deleteUser(user);
// unused atm
}
});
} else {
deleteUser(userEntity);
}
} catch (IOException e) {
Log.d(TAG, "Something went wrong while removing job at parsing PushConfigurationState");
deleteUser(userEntity);
} }
} }
return Result.success(); return Result.success();
} }
private void unregisterDeviceForNotificationWithProxy(HashMap<String, String> queryMap, UserEntity userEntity) { private void unregisterDeviceForNotificationWithProxy(HashMap<String, String> queryMap, User user) {
ncApi.unregisterDeviceForNotificationsWithProxy ncApi.unregisterDeviceForNotificationsWithProxy
(ApiUtils.getUrlPushProxy(), queryMap) (ApiUtils.getUrlPushProxy(), queryMap)
.subscribe(new Observer<Void>() { .subscribe(new Observer<Void>() {
@Override @Override
public void onSubscribe(Disposable d) { public void onSubscribe(Disposable d) {
// unused atm
} }
@Override @Override
public void onNext(Void aVoid) { public void onNext(Void aVoid) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String groupName = String.format(getApplicationContext().getResources() String groupName = String.format(
.getString(R.string getApplicationContext()
.nc_notification_channel), userEntity.getUserId(), userEntity.getBaseUrl()); .getResources()
.getString(R.string.nc_notification_channel), user.getUserId(), user.getBaseUrl());
CRC32 crc32 = new CRC32(); CRC32 crc32 = new CRC32();
crc32.update(groupName.getBytes()); crc32.update(groupName.getBytes());
NotificationManager notificationManager = NotificationManager notificationManager =
(NotificationManager) getApplicationContext().getSystemService (NotificationManager) getApplicationContext()
(Context.NOTIFICATION_SERVICE); .getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) { if (notificationManager != null) {
notificationManager.deleteNotificationChannelGroup(Long notificationManager.deleteNotificationChannelGroup(
.toString(crc32.getValue())); Long.toString(crc32.getValue()));
} }
} }
WebSocketConnectionHelper.deleteExternalSignalingInstanceForUserEntity(userEntity.getId());
deleteAllEntriesForAccountIdentifier(userEntity); if (user.getId() != null) {
WebSocketConnectionHelper.deleteExternalSignalingInstanceForUserEntity(user.getId());
}
deleteAllEntriesForAccountIdentifier(user);
} }
@Override @Override
@ -184,53 +184,31 @@ public class AccountRemovalWorker extends Worker {
@Override @Override
public void onComplete() { public void onComplete() {
// unused atm
} }
}); });
} }
private void deleteAllEntriesForAccountIdentifier(UserEntity userEntity) { private void deleteAllEntriesForAccountIdentifier(User user) {
arbitraryStorageUtils.deleteAllEntriesForAccountIdentifier(userEntity.getId()).subscribe(new Observer() { if (user.getId() != null) {
@Override try {
public void onSubscribe(Disposable d) { arbitraryStorageManager.deleteAllEntriesForAccountIdentifier(user.getId());
deleteUser(user);
} } catch (Throwable e) {
@Override
public void onNext(Object o) {
deleteUser(userEntity);
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "error while trying to delete All Entries For Account Identifier", e); Log.e(TAG, "error while trying to delete All Entries For Account Identifier", e);
} }
}
@Override
public void onComplete() {
}
});
} }
private void deleteUser(UserEntity userEntity) { private void deleteUser(User user) {
String username = userEntity.getUsername(); if (user.getId() != null) {
userUtils.deleteUser(userEntity.getId()) String username = user.getUsername();
.subscribe(new CompletableObserver() { try {
@Override userManager.deleteUser(user.getId());
public void onSubscribe(Disposable d) { Log.d(TAG, "deleted user: " + username);
} catch (Throwable e) {
} Log.e(TAG, "error while trying to delete user", e);
}
@Override }
public void onComplete() {
Log.d(TAG, "deleted user: " + username);
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "error while trying to delete user", e);
}
});
} }
} }