mirror of
https://github.com/nextcloud/talk-android
synced 2025-06-20 12:09:45 +01:00
Migrate CapabilitiesWorker from requery to room
Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
parent
d4b84635f1
commit
1cec9045ee
@ -22,39 +22,42 @@ package com.nextcloud.talk.jobs;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
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.capabilities.CapabilitiesOverall;
|
||||||
|
import com.nextcloud.talk.users.UserManager;
|
||||||
|
import com.nextcloud.talk.utils.ApiUtils;
|
||||||
|
import com.nextcloud.talk.utils.bundle.BundleKeys;
|
||||||
|
|
||||||
|
import org.greenrobot.eventbus.EventBus;
|
||||||
|
|
||||||
|
import java.net.CookieManager;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.work.Data;
|
import androidx.work.Data;
|
||||||
import androidx.work.Worker;
|
import androidx.work.Worker;
|
||||||
import androidx.work.WorkerParameters;
|
import androidx.work.WorkerParameters;
|
||||||
import autodagger.AutoInjector;
|
import autodagger.AutoInjector;
|
||||||
import com.bluelinelabs.logansquare.LoganSquare;
|
|
||||||
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.capabilities.CapabilitiesOverall;
|
|
||||||
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.Observer;
|
||||||
import io.reactivex.disposables.Disposable;
|
import io.reactivex.disposables.Disposable;
|
||||||
import okhttp3.JavaNetCookieJar;
|
import okhttp3.JavaNetCookieJar;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import org.greenrobot.eventbus.EventBus;
|
|
||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.CookieManager;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@AutoInjector(NextcloudTalkApplication.class)
|
@AutoInjector(NextcloudTalkApplication.class)
|
||||||
public class CapabilitiesWorker extends Worker {
|
public class CapabilitiesWorker extends Worker {
|
||||||
public static final String TAG = "CapabilitiesWorker";
|
public static final String TAG = "CapabilitiesWorker";
|
||||||
|
public static final long NO_ID = -1;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
UserUtils userUtils;
|
UserManager userManager;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
Retrofit retrofit;
|
Retrofit retrofit;
|
||||||
@ -69,42 +72,41 @@ public class CapabilitiesWorker extends Worker {
|
|||||||
|
|
||||||
public CapabilitiesWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
|
public CapabilitiesWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
|
||||||
super(context, workerParams);
|
super(context, workerParams);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateUser(CapabilitiesOverall capabilitiesOverall, UserEntity internalUserEntity) {
|
private void updateUser(CapabilitiesOverall capabilitiesOverall, User user) {
|
||||||
|
if (capabilitiesOverall.getOcs() != null && capabilitiesOverall.getOcs().getData() != null &&
|
||||||
|
capabilitiesOverall.getOcs().getData().getCapabilities() != null) {
|
||||||
|
|
||||||
|
user.setCapabilities(capabilitiesOverall.getOcs().getData().getCapabilities());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
userUtils.createOrUpdateUser(null, null,
|
int rowsCount = userManager.updateOrCreateUser(user).blockingGet();
|
||||||
null, null,
|
if (rowsCount > 0) {
|
||||||
null, null, null, internalUserEntity.getId(),
|
eventBus.post(new EventStatus(getIdForUser(user),
|
||||||
LoganSquare.serialize(capabilitiesOverall.getOcs().getData().getCapabilities()), null, null)
|
EventStatus.EventType.CAPABILITIES_FETCH,
|
||||||
.blockingSubscribe(new Observer<UserEntity>() {
|
true));
|
||||||
@Override
|
} else {
|
||||||
public void onSubscribe(Disposable d) {
|
Log.w(TAG, "Error updating user");
|
||||||
|
eventBus.post(new EventStatus(getIdForUser(user),
|
||||||
|
EventStatus.EventType.CAPABILITIES_FETCH,
|
||||||
|
false));
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Error updating user", e);
|
||||||
|
eventBus.post(new EventStatus(getIdForUser(user),
|
||||||
|
EventStatus.EventType.CAPABILITIES_FETCH,
|
||||||
|
false));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private long getIdForUser(User user) {
|
||||||
public void onNext(UserEntity userEntity) {
|
if (user != null && user.getId() != null) {
|
||||||
eventBus.post(new EventStatus(userEntity.getId(),
|
return user.getId();
|
||||||
EventStatus.EventType.CAPABILITIES_FETCH, true));
|
} else {
|
||||||
|
return NO_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(Throwable e) {
|
|
||||||
eventBus.post(new EventStatus(internalUserEntity.getId(),
|
|
||||||
EventStatus.EventType.CAPABILITIES_FETCH, false));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onComplete() {
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(TAG, "Failed to create or update user");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@ -116,45 +118,50 @@ public class CapabilitiesWorker extends Worker {
|
|||||||
|
|
||||||
long internalUserId = data.getLong(BundleKeys.INSTANCE.getKEY_INTERNAL_USER_ID(), -1);
|
long internalUserId = data.getLong(BundleKeys.INSTANCE.getKEY_INTERNAL_USER_ID(), -1);
|
||||||
|
|
||||||
UserEntity userEntity;
|
List<User> userEntityObjectList = new ArrayList<>();
|
||||||
List userEntityObjectList = new ArrayList();
|
boolean userExists = userManager.getUserWithInternalId(internalUserId).isEmpty().blockingGet();
|
||||||
|
|
||||||
if (internalUserId == -1 || (userEntity = userUtils.getUserWithInternalId(internalUserId)) == null) {
|
if (internalUserId == -1 || !userExists) {
|
||||||
userEntityObjectList = userUtils.getUsers();
|
userEntityObjectList = userManager.getUsers().blockingGet();
|
||||||
} else {
|
} else {
|
||||||
userEntityObjectList.add(userEntity);
|
userEntityObjectList.add(userManager.getUserWithInternalId(internalUserId).blockingGet());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Object userEntityObject : userEntityObjectList) {
|
for (User user : userEntityObjectList) {
|
||||||
UserEntity internalUserEntity = (UserEntity) userEntityObject;
|
|
||||||
|
|
||||||
ncApi = retrofit.newBuilder().client(okHttpClient.newBuilder().cookieJar(new
|
ncApi = retrofit
|
||||||
JavaNetCookieJar(new CookieManager())).build()).build().create(NcApi.class);
|
.newBuilder()
|
||||||
|
.client(okHttpClient
|
||||||
|
.newBuilder()
|
||||||
|
.cookieJar(new JavaNetCookieJar(new CookieManager()))
|
||||||
|
.build())
|
||||||
|
.build()
|
||||||
|
.create(NcApi.class);
|
||||||
|
|
||||||
ncApi.getCapabilities(ApiUtils.getCredentials(internalUserEntity.getUsername(),
|
ncApi.getCapabilities(ApiUtils.getCredentials(user.getUsername(), user.getToken()),
|
||||||
internalUserEntity.getToken()), ApiUtils.getUrlForCapabilities(internalUserEntity.getBaseUrl()))
|
ApiUtils.getUrlForCapabilities(user.getBaseUrl()))
|
||||||
.retry(3)
|
.retry(3)
|
||||||
.blockingSubscribe(new Observer<CapabilitiesOverall>() {
|
.blockingSubscribe(new Observer<CapabilitiesOverall>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSubscribe(Disposable d) {
|
public void onSubscribe(Disposable d) {
|
||||||
|
// unused atm
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNext(CapabilitiesOverall capabilitiesOverall) {
|
public void onNext(CapabilitiesOverall capabilitiesOverall) {
|
||||||
updateUser(capabilitiesOverall, internalUserEntity);
|
updateUser(capabilitiesOverall, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(Throwable e) {
|
public void onError(Throwable e) {
|
||||||
eventBus.post(new EventStatus(internalUserEntity.getId(),
|
eventBus.post(new EventStatus(user.getId(),
|
||||||
EventStatus.EventType.CAPABILITIES_FETCH, false));
|
EventStatus.EventType.CAPABILITIES_FETCH,
|
||||||
|
false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onComplete() {
|
public void onComplete() {
|
||||||
|
// unused atm
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user