Migrate DeleteConversationWorker from requery to room

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-07-23 16:04:47 +02:00
parent 5b8c352947
commit 2dfd4731af
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B

View file

@ -21,30 +21,35 @@
package com.nextcloud.talk.jobs;
import android.content.Context;
import com.nextcloud.talk.api.NcApi;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.data.user.model.User;
import com.nextcloud.talk.events.EventStatus;
import com.nextcloud.talk.models.json.generic.GenericOverall;
import com.nextcloud.talk.users.UserManager;
import com.nextcloud.talk.utils.ApiUtils;
import com.nextcloud.talk.utils.UserIdUtils;
import com.nextcloud.talk.utils.bundle.BundleKeys;
import org.greenrobot.eventbus.EventBus;
import java.net.CookieManager;
import javax.inject.Inject;
import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import autodagger.AutoInjector;
import com.nextcloud.talk.api.NcApi;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.events.EventStatus;
import com.nextcloud.talk.models.database.UserEntity;
import com.nextcloud.talk.models.json.generic.GenericOverall;
import com.nextcloud.talk.utils.ApiUtils;
import com.nextcloud.talk.utils.bundle.BundleKeys;
import com.nextcloud.talk.utils.database.user.UserUtils;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.JavaNetCookieJar;
import okhttp3.OkHttpClient;
import org.greenrobot.eventbus.EventBus;
import retrofit2.Retrofit;
import javax.inject.Inject;
import java.net.CookieManager;
@AutoInjector(NextcloudTalkApplication.class)
public class DeleteConversationWorker extends Worker {
@Inject
@ -54,7 +59,7 @@ public class DeleteConversationWorker extends Worker {
OkHttpClient okHttpClient;
@Inject
UserUtils userUtils;
UserManager userManager;
@Inject
EventBus eventBus;
@ -72,46 +77,48 @@ public class DeleteConversationWorker extends Worker {
Data data = getInputData();
long operationUserId = data.getLong(BundleKeys.INSTANCE.getKEY_INTERNAL_USER_ID(), -1);
String conversationToken = data.getString(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN());
UserEntity operationUser = userUtils.getUserWithId(operationUserId);
User operationUser = userManager.getUserWithId(operationUserId).blockingGet();
if (operationUser != null) {
int apiVersion = ApiUtils.getConversationApiVersion(operationUser, new int[] {ApiUtils.APIv4, 1});
int apiVersion = ApiUtils.getConversationApiVersion(operationUser, new int[]{ApiUtils.APIv4, 1});
String credentials = ApiUtils.getCredentials(operationUser.getUsername(), operationUser.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 JavaNetCookieJar(new CookieManager())).build())
.build()
.create(NcApi.class);
EventStatus eventStatus = new EventStatus(operationUser.getId(),
EventStatus.EventType.CONVERSATION_UPDATE, true);
EventStatus eventStatus = new EventStatus(UserIdUtils.INSTANCE.getIdForUser(operationUser),
EventStatus.EventType.CONVERSATION_UPDATE,
true);
ncApi.deleteRoom(credentials, ApiUtils.getUrlForRoom(apiVersion, operationUser.getBaseUrl(),
conversationToken))
.subscribeOn(Schedulers.io())
.blockingSubscribe(new Observer<GenericOverall>() {
Disposable disposable;
conversationToken))
.subscribeOn(Schedulers.io())
.blockingSubscribe(new Observer<GenericOverall>() {
Disposable disposable;
@Override
public void onSubscribe(Disposable d) {
disposable = d;
@Override
public void onSubscribe(Disposable d) {
disposable = d;
}
}
@Override
public void onNext(GenericOverall genericOverall) {
eventBus.postSticky(eventStatus);
}
@Override
public void onNext(GenericOverall genericOverall) {
eventBus.postSticky(eventStatus);
@Override
public void onError(Throwable e) {
// unused atm
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
disposable.dispose();
}
});
@Override
public void onComplete() {
disposable.dispose();
}
});
}
return Result.success();