Unverified Commit 857abccc authored by Leonardo Aramaki's avatar Leonardo Aramaki Committed by GitHub

Merge pull request #596 from RocketChat/fix/logout-and-login-same-user

[FIX] Logout and login with the same user
parents 9bcef2c4 b1a1e4a3
...@@ -3,7 +3,8 @@ apply plugin: 'io.fabric' ...@@ -3,7 +3,8 @@ apply plugin: 'io.fabric'
repositories { repositories {
maven { url 'https://maven.fabric.io/public' } maven { url 'https://maven.fabric.io/public' }
maven { url 'https://github.com/uPhyca/stetho-realm/raw/master/maven-repo' } // maven { url 'https://github.com/uPhyca/stetho-realm/raw/master/maven-repo' }
maven { url 'https://github.com/WickeDev/stetho-realm/raw/master/maven-repo' }
} }
apply plugin: 'kotlin-android' apply plugin: 'kotlin-android'
...@@ -100,7 +101,7 @@ ext { ...@@ -100,7 +101,7 @@ ext {
playLibVersion = '11.6.0' playLibVersion = '11.6.0'
stethoVersion = '1.5.0' stethoVersion = '1.5.0'
stethoOkhttp3Version = '1.5.0' stethoOkhttp3Version = '1.5.0'
stethoRealmVersion = '2.1.0' stethoRealmVersion = '2.2.2'
rxbindingVersion = '2.0.0' rxbindingVersion = '2.0.0'
rxlifecycleVersion = '2.1.0' rxlifecycleVersion = '2.1.0'
icepickVersion = '3.2.0' icepickVersion = '3.2.0'
......
...@@ -347,15 +347,12 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -347,15 +347,12 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
@DebugLog @DebugLog
public void onLogout() { public void onLogout() {
presenter.prepareToLogout();
if (new RocketChatCache(getApplicationContext()).getSelectedServerHostname() == null) { if (new RocketChatCache(getApplicationContext()).getSelectedServerHostname() == null) {
finish();
LaunchUtil.showMainActivity(this); LaunchUtil.showMainActivity(this);
} else { } else {
onHostnameUpdated(); onHostnameUpdated();
} }
} }
@DebugLog
public void beforeLogoutCleanUp() {
presenter.beforeLogoutCleanUp();
}
} }
...@@ -40,6 +40,6 @@ public interface MainContract { ...@@ -40,6 +40,6 @@ public interface MainContract {
void loadSignedInServers(String hostname); void loadSignedInServers(String hostname);
void beforeLogoutCleanUp(); void prepareToLogout();
} }
} }
...@@ -28,6 +28,7 @@ import chat.rocket.core.models.Session; ...@@ -28,6 +28,7 @@ import chat.rocket.core.models.Session;
import chat.rocket.core.models.User; import chat.rocket.core.models.User;
import chat.rocket.core.repositories.PublicSettingRepository; import chat.rocket.core.repositories.PublicSettingRepository;
import chat.rocket.core.utils.Pair; import chat.rocket.core.utils.Pair;
import hugo.weaving.DebugLog;
import io.reactivex.Flowable; import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
...@@ -133,8 +134,9 @@ public class MainPresenter extends BasePresenter<MainContract.View> ...@@ -133,8 +134,9 @@ public class MainPresenter extends BasePresenter<MainContract.View>
addSubscription(subscription); addSubscription(subscription);
} }
@DebugLog
@Override @Override
public void beforeLogoutCleanUp() { public void prepareToLogout() {
clearSubscriptions(); clearSubscriptions();
} }
......
...@@ -77,7 +77,7 @@ public class RoomPresenter extends BasePresenter<RoomContract.View> ...@@ -77,7 +77,7 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
success -> { success -> {
if (success) { if (!success) {
connectivityManagerApi.keepAliveServer(); connectivityManagerApi.keepAliveServer();
} }
}, },
...@@ -89,14 +89,13 @@ public class RoomPresenter extends BasePresenter<RoomContract.View> ...@@ -89,14 +89,13 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
@Override @Override
public void loadMoreMessages() { public void loadMoreMessages() {
final Disposable subscription = getSingleRoom() final Disposable subscription = getSingleRoom()
.flatMap(messageInteractor::loadMoreMessages) .flatMap(messageInteractor::loadMoreMessages)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
success -> { success -> {
if (success) { if (!success) {
connectivityManagerApi.keepAliveServer(); connectivityManagerApi.keepAliveServer();
} }
}, },
......
...@@ -25,7 +25,7 @@ public interface SidebarMainContract { ...@@ -25,7 +25,7 @@ public interface SidebarMainContract {
void show(User user); void show(User user);
void onLogoutCleanUp(); void onPreparedToLogOut();
} }
interface Presenter extends BaseContract.Presenter<View> { interface Presenter extends BaseContract.Presenter<View> {
...@@ -48,6 +48,6 @@ public interface SidebarMainContract { ...@@ -48,6 +48,6 @@ public interface SidebarMainContract {
void onLogout(Continuation<Void, Object> continuation); void onLogout(Continuation<Void, Object> continuation);
void beforeLogoutCleanUp(); void prepareToLogOut();
} }
} }
\ No newline at end of file
...@@ -22,7 +22,6 @@ import java.util.ArrayList; ...@@ -22,7 +22,6 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import bolts.Task;
import chat.rocket.android.BuildConfig; import chat.rocket.android.BuildConfig;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
...@@ -51,6 +50,7 @@ import chat.rocket.persistence.realm.repositories.RealmServerInfoRepository; ...@@ -51,6 +50,7 @@ import chat.rocket.persistence.realm.repositories.RealmServerInfoRepository;
import chat.rocket.persistence.realm.repositories.RealmSessionRepository; import chat.rocket.persistence.realm.repositories.RealmSessionRepository;
import chat.rocket.persistence.realm.repositories.RealmSpotlightRepository; import chat.rocket.persistence.realm.repositories.RealmSpotlightRepository;
import chat.rocket.persistence.realm.repositories.RealmUserRepository; import chat.rocket.persistence.realm.repositories.RealmUserRepository;
import hugo.weaving.DebugLog;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
...@@ -64,7 +64,8 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -64,7 +64,8 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
private String hostname; private String hostname;
private static final String HOSTNAME = "hostname"; private static final String HOSTNAME = "hostname";
public SidebarMainFragment() {} public SidebarMainFragment() {
}
/** /**
* build SidebarMainFragment with hostname. * build SidebarMainFragment with hostname.
...@@ -199,7 +200,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -199,7 +200,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
public void filterRoomSidebarList(CharSequence term) { public void filterRoomSidebarList(CharSequence term) {
List<RoomSidebar> filteredRoomSidebarList = new ArrayList<>(); List<RoomSidebar> filteredRoomSidebarList = new ArrayList<>();
for (RoomSidebar roomSidebar: roomSidebarList) { for (RoomSidebar roomSidebar : roomSidebarList) {
if (roomSidebar.getRoomName().contains(term)) { if (roomSidebar.getRoomName().contains(term)) {
filteredRoomSidebarList.add(roomSidebar); filteredRoomSidebarList.add(roomSidebar);
} }
...@@ -323,17 +324,12 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -323,17 +324,12 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
adapter.setRoomListHeaders(roomListHeaders); adapter.setRoomListHeaders(roomListHeaders);
} }
@DebugLog
@Override @Override
public void onLogoutCleanUp() { public void onPreparedToLogOut() {
Activity activity = getActivity(); final Activity activity = getActivity();
if (activity != null && activity instanceof MainActivity) { if (activity != null && activity instanceof MainActivity) {
((MainActivity) activity).onLogout(); ((MainActivity) activity).onLogout();
presenter.onLogout(task -> {
if (task.isFaulted()) {
return Task.forError(task.getError());
}
return null;
});
} }
} }
...@@ -341,12 +337,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -341,12 +337,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> { rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> {
closeUserActionContainer(); closeUserActionContainer();
// Clear relative data and set new hostname if any. // Clear relative data and set new hostname if any.
presenter.beforeLogoutCleanUp(); presenter.prepareToLogOut();
final Activity activity = getActivity();
if (activity != null && activity instanceof MainActivity) {
// Clear subscriptions on MainPresenter.
((MainActivity) activity).beforeLogoutCleanUp();
}
}); });
} }
......
...@@ -18,7 +18,6 @@ import chat.rocket.android.helper.LogIfError; ...@@ -18,7 +18,6 @@ import chat.rocket.android.helper.LogIfError;
import chat.rocket.android.helper.Logger; import chat.rocket.android.helper.Logger;
import chat.rocket.android.helper.TextUtils; import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.service.ConnectivityManager; import chat.rocket.android.service.ConnectivityManager;
import chat.rocket.android.service.ConnectivityManagerApi;
import chat.rocket.android.shared.BasePresenter; import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.interactors.RoomInteractor; import chat.rocket.core.interactors.RoomInteractor;
import chat.rocket.core.models.Room; import chat.rocket.core.models.Room;
...@@ -33,6 +32,7 @@ import chat.rocket.persistence.realm.repositories.RealmSpotlightRepository; ...@@ -33,6 +32,7 @@ import chat.rocket.persistence.realm.repositories.RealmSpotlightRepository;
import io.reactivex.Flowable; import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.realm.Realm;
public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View> implements SidebarMainContract.Presenter { public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View> implements SidebarMainContract.Presenter {
private final String hostname; private final String hostname;
...@@ -150,21 +150,27 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -150,21 +150,27 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
} }
@Override @Override
public void beforeLogoutCleanUp() { public void prepareToLogOut() {
onLogout(task -> {
if (task.isFaulted()) {
return Task.forError(task.getError());
}
clearSubscriptions(); clearSubscriptions();
String currentHostname = rocketChatCache.getSelectedServerHostname(); String currentHostname = rocketChatCache.getSelectedServerHostname();
RealmHelper realmHelper = RealmStore.getOrCreate(currentHostname); RealmHelper realmHelper = RealmStore.getOrCreate(currentHostname);
realmHelper.executeTransaction(realm -> { return realmHelper.executeTransaction(realm -> {
realm.executeTransactionAsync(realmObj -> realmObj.deleteAll());
CookieManager.getInstance().removeAllCookie();
ConnectivityManagerApi connectivityManagerApi = ConnectivityManager.getInstance(RocketChatApplication.getInstance());
connectivityManagerApi.removeServer(currentHostname);
rocketChatCache.removeHostname(currentHostname); rocketChatCache.removeHostname(currentHostname);
rocketChatCache.removeSelectedRoomId(currentHostname); rocketChatCache.removeSelectedRoomId(currentHostname);
rocketChatCache.setSelectedServerHostname(rocketChatCache.getFirstLoggedHostnameIfAny()); rocketChatCache.setSelectedServerHostname(rocketChatCache.getFirstLoggedHostnameIfAny());
view.onLogoutCleanUp(); realm.executeTransactionAsync(Realm::deleteAll);
view.onPreparedToLogOut();
ConnectivityManager.getInstance(RocketChatApplication.getInstance())
.removeServer(hostname);
CookieManager.getInstance().removeAllCookie();
return null; return null;
}); });
});
} }
@Override @Override
...@@ -226,8 +232,8 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -226,8 +232,8 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
} }
private void processUsers(List<User> userList) { private void processUsers(List<User> userList) {
for (User user: userList) { for (User user : userList) {
for(RoomSidebar roomSidebar: roomSidebarList) { for (RoomSidebar roomSidebar : roomSidebarList) {
if (roomSidebar.getRoomName().equals(user.getUsername())) { if (roomSidebar.getRoomName().equals(user.getUsername())) {
roomSidebar.setUserStatus(user.getStatus()); roomSidebar.setUserStatus(user.getStatus());
} }
......
...@@ -279,8 +279,11 @@ import io.reactivex.subjects.BehaviorSubject; ...@@ -279,8 +279,11 @@ import io.reactivex.subjects.BehaviorSubject;
if (serviceInterface != null) { if (serviceInterface != null) {
return serviceInterface.disconnectFromServer(hostname) return serviceInterface.disconnectFromServer(hostname)
// //after disconnection from server, remove HOSTNAME key from HashMap //after disconnection from server, remove HOSTNAME key from HashMap
.doAfterTerminate(() -> serverConnectivityList.remove(hostname)); .doAfterTerminate(() -> {
serverConnectivityList.remove(hostname);
serverConnectivityList.put(hostname, ServerConnectivity.STATE_DISCONNECTED);
});
} else { } else {
return Single.error(new IllegalStateException("not prepared")); return Single.error(new IllegalStateException("not prepared"));
} }
......
...@@ -229,6 +229,8 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -229,6 +229,8 @@ public class RocketChatWebSocketThread extends HandlerThread {
RxWebSocketCallback.Close result = _task.getResult(); RxWebSocketCallback.Close result = _task.getResult();
if (result.code == DDPClient.REASON_NETWORK_ERROR) { if (result.code == DDPClient.REASON_NETWORK_ERROR) {
reconnect(); reconnect();
} else {
unregisterListenersAndClose();
} }
return null; return null;
}); });
...@@ -333,8 +335,8 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -333,8 +335,8 @@ public class RocketChatWebSocketThread extends HandlerThread {
.findAll()); .findAll());
if (sessions != null && sessions.size() > 0) { if (sessions != null && sessions.size() > 0) {
// if we have a session try to resume it. At this point we're probably recovering from // If we have a session try to resume it. At this point we're probably recovering from
// a disconnection state // a disconnection state.
final CompositeDisposable disposables = new CompositeDisposable(); final CompositeDisposable disposables = new CompositeDisposable();
MethodCallHelper methodCall = new MethodCallHelper(realmHelper); MethodCallHelper methodCall = new MethodCallHelper(realmHelper);
disposables.add( disposables.add(
...@@ -351,7 +353,11 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -351,7 +353,11 @@ public class RocketChatWebSocketThread extends HandlerThread {
createObserversAndRegister(); createObserversAndRegister();
disposables.clear(); disposables.clear();
}, },
error -> logErrorAndUnsubscribe(disposables, error) error -> {
logErrorAndUnsubscribe(disposables, error);
connectivityManager.notifyConnectionLost(hostname,
DDPClient.REASON_NETWORK_ERROR);
}
) )
); );
} else { } else {
......
...@@ -19,7 +19,7 @@ class MessageInteractor(private val messageRepository: MessageRepository, ...@@ -19,7 +19,7 @@ class MessageInteractor(private val messageRepository: MessageRepository,
val roomHistoryState = RoomHistoryState.builder() val roomHistoryState = RoomHistoryState.builder()
.setRoomId(room.roomId) .setRoomId(room.roomId)
.setSyncState(SyncState.NOT_SYNCED) .setSyncState(SyncState.NOT_SYNCED)
.setCount(100) .setCount(50)
.setReset(true) .setReset(true)
.setComplete(false) .setComplete(false)
.setTimestamp(0) .setTimestamp(0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment