Commit 2ae619c8 authored by Yusuke Iwaki's avatar Yusuke Iwaki

implement logout.

parent 9666e58b
...@@ -63,6 +63,7 @@ public class ServerConfigActivity extends AbstractFragmentActivity { ...@@ -63,6 +63,7 @@ public class ServerConfigActivity extends AbstractFragmentActivity {
private void onRenderServerConfigSession(Session session) { private void onRenderServerConfigSession(Session session) {
if (session == null) { if (session == null) {
showFragment(new LoginFragment());
return; return;
} }
......
...@@ -59,6 +59,9 @@ public class MethodCallHelper { ...@@ -59,6 +59,9 @@ public class MethodCallHelper {
} }
String errMessage = new JSONObject(errMessageJson).getString("message"); String errMessage = new JSONObject(errMessageJson).getString("message");
return Task.forError(new Exception(errMessage)); return Task.forError(new Exception(errMessage));
} else if (exception instanceof DDPClientCallback.RPC.Error) {
String errMessage = ((DDPClientCallback.RPC.Error) exception).error.getString("message");
return Task.forError(new Exception(errMessage));
} else if (exception instanceof DDPClientCallback.RPC.Timeout) { } else if (exception instanceof DDPClientCallback.RPC.Timeout) {
return Task.forError(new MethodCall.Timeout()); return Task.forError(new MethodCall.Timeout());
} else { } else {
...@@ -158,14 +161,24 @@ public class MethodCallHelper { ...@@ -158,14 +161,24 @@ public class MethodCallHelper {
.put("resume", token) .put("resume", token)
)).onSuccessTask(CONVERT_TO_JSON_OBJECT) )).onSuccessTask(CONVERT_TO_JSON_OBJECT)
.onSuccessTask(task -> Task.forResult(task.getResult().getString("token"))) .onSuccessTask(task -> Task.forResult(task.getResult().getString("token")))
.onSuccessTask(this::saveToken); .onSuccessTask(this::saveToken)
.continueWithTask(task -> {
if (task.isFaulted()) {
Session.logError(realmHelper, task.getError());
}
return task;
});
} }
/** /**
* Logout. * Logout.
*/ */
public Task<String> logout() { public Task<Void> logout() {
return call("logout", TIMEOUT_MS); return call("logout", TIMEOUT_MS).onSuccessTask(task ->
realmHelper.executeTransaction(realm -> {
realm.delete(Session.class);
return null;
}));
} }
/** /**
......
...@@ -51,7 +51,6 @@ public class RetryLoginFragment extends AbstractServerConfigFragment { ...@@ -51,7 +51,6 @@ public class RetryLoginFragment extends AbstractServerConfigFragment {
if (task.isFaulted()) { if (task.isFaulted()) {
view.setEnabled(true); view.setEnabled(true);
waitingView.setVisibility(View.GONE); waitingView.setVisibility(View.GONE);
Session.logError(RealmStore.get(serverConfigId), task.getError());
} }
return null; return null;
}); });
......
...@@ -31,6 +31,7 @@ public class SidebarMainFragment extends AbstractFragment { ...@@ -31,6 +31,7 @@ public class SidebarMainFragment extends AbstractFragment {
private String hostname; private String hostname;
private RealmListObserver<RoomSubscription> roomsObserver; private RealmListObserver<RoomSubscription> roomsObserver;
private RealmObjectObserver<User> currentUserObserver; private RealmObjectObserver<User> currentUserObserver;
private MethodCallHelper methodCallHelper;
public SidebarMainFragment() { public SidebarMainFragment() {
} }
...@@ -68,6 +69,8 @@ public class SidebarMainFragment extends AbstractFragment { ...@@ -68,6 +69,8 @@ public class SidebarMainFragment extends AbstractFragment {
currentUserObserver = realmHelper currentUserObserver = realmHelper
.createObjectObserver(realm -> realm.where(User.class).isNotEmpty("emails")) .createObjectObserver(realm -> realm.where(User.class).isNotEmpty("emails"))
.setOnUpdateListener(this::onRenderCurrentUser); .setOnUpdateListener(this::onRenderCurrentUser);
methodCallHelper = new MethodCallHelper(serverConfigId);
} }
} }
} }
...@@ -87,6 +90,7 @@ public class SidebarMainFragment extends AbstractFragment { ...@@ -87,6 +90,7 @@ public class SidebarMainFragment extends AbstractFragment {
setupUserActionToggle(); setupUserActionToggle();
setupUserStatusButtons(); setupUserStatusButtons();
setupLogoutButton();
roomListManager = new RoomListManager( roomListManager = new RoomListManager(
(LinearLayout) rootView.findViewById(R.id.channels_container), (LinearLayout) rootView.findViewById(R.id.channels_container),
...@@ -111,24 +115,19 @@ public class SidebarMainFragment extends AbstractFragment { ...@@ -111,24 +115,19 @@ public class SidebarMainFragment extends AbstractFragment {
} }
private void setupUserStatusButtons() { private void setupUserStatusButtons() {
rootView.findViewById(R.id.btn_status_online).setOnClickListener(view -> { rootView.findViewById(R.id.btn_status_online).setOnClickListener(view ->
updateCurrentUserStatus(User.STATUS_ONLINE); updateCurrentUserStatus(User.STATUS_ONLINE));
}); rootView.findViewById(R.id.btn_status_away).setOnClickListener(view ->
rootView.findViewById(R.id.btn_status_away).setOnClickListener(view -> { updateCurrentUserStatus(User.STATUS_AWAY));
updateCurrentUserStatus(User.STATUS_AWAY); rootView.findViewById(R.id.btn_status_busy).setOnClickListener(view ->
}); updateCurrentUserStatus(User.STATUS_BUSY));
rootView.findViewById(R.id.btn_status_busy).setOnClickListener(view -> { rootView.findViewById(R.id.btn_status_invisible).setOnClickListener(view ->
updateCurrentUserStatus(User.STATUS_BUSY); updateCurrentUserStatus(User.STATUS_OFFLINE));
});
rootView.findViewById(R.id.btn_status_invisible).setOnClickListener(view -> {
updateCurrentUserStatus(User.STATUS_OFFLINE);
});
} }
private void updateCurrentUserStatus(String status) { private void updateCurrentUserStatus(String status) {
if (!TextUtils.isEmpty(serverConfigId)) { if (methodCallHelper != null) {
new MethodCallHelper(serverConfigId).setUserStatus(status) methodCallHelper.setUserStatus(status).continueWith(new LogcatIfError());
.continueWith(new LogcatIfError());
} }
} }
...@@ -141,6 +140,14 @@ public class SidebarMainFragment extends AbstractFragment { ...@@ -141,6 +140,14 @@ public class SidebarMainFragment extends AbstractFragment {
} }
} }
private void setupLogoutButton() {
rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> {
if (methodCallHelper != null) {
methodCallHelper.logout().continueWith(new LogcatIfError());
}
});
}
@Override public void onResume() { @Override public void onResume() {
super.onResume(); super.onResume();
if (roomsObserver != null) { if (roomsObserver != null) {
......
...@@ -35,6 +35,9 @@ public class RoomListManager { ...@@ -35,6 +35,9 @@ public class RoomListManager {
* update ViewGroups with room list. * update ViewGroups with room list.
*/ */
public void setRooms(List<RoomSubscription> roomSubscriptionList) { public void setRooms(List<RoomSubscription> roomSubscriptionList) {
removeDeletedItem(channelsContainer, roomSubscriptionList);
removeDeletedItem(dmContainer, roomSubscriptionList);
for (RoomSubscription roomSubscription : roomSubscriptionList) { for (RoomSubscription roomSubscription : roomSubscriptionList) {
String name = roomSubscription.getName(); String name = roomSubscription.getName();
if (TextUtils.isEmpty(name)) { if (TextUtils.isEmpty(name)) {
...@@ -61,6 +64,25 @@ public class RoomListManager { ...@@ -61,6 +64,25 @@ public class RoomListManager {
this.listener = listener; this.listener = listener;
} }
private void removeDeletedItem(ViewGroup parent, List<RoomSubscription> roomSubscriptionList) {
for (int index = parent.getChildCount() - 1; index >= 0; index--) {
RoomListItemView roomListItemView = (RoomListItemView) parent.getChildAt(index);
final String targetRoomName = roomListItemView.getRoomName();
if (!TextUtils.isEmpty(targetRoomName)) {
boolean found = false;
for (RoomSubscription roomSubscription : roomSubscriptionList) {
if (targetRoomName.equals(roomSubscription.getName())) {
found = true;
break;
}
}
if (!found) {
parent.removeViewAt(index);
}
}
}
}
private void insertOrUpdateItem(ViewGroup parent, RoomSubscription roomSubscription) { private void insertOrUpdateItem(ViewGroup parent, RoomSubscription roomSubscription) {
final String roomName = roomSubscription.getName(); final String roomName = roomSubscription.getName();
......
package chat.rocket.android.model.internal; package chat.rocket.android.model.internal;
import chat.rocket.android.helper.LogcatIfError; import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.realm_helper.RealmHelper; import chat.rocket.android.realm_helper.RealmHelper;
import hugo.weaving.DebugLog; import hugo.weaving.DebugLog;
import io.realm.RealmObject; import io.realm.RealmObject;
...@@ -53,12 +54,19 @@ public class Session extends RealmObject { ...@@ -53,12 +54,19 @@ public class Session extends RealmObject {
* Log the server connection is lost due to soem exception. * Log the server connection is lost due to soem exception.
*/ */
@DebugLog public static void logError(RealmHelper realmHelper, Exception exception) { @DebugLog public static void logError(RealmHelper realmHelper, Exception exception) {
// TODO: should remove token if 403. String errString = exception.getMessage();
realmHelper.executeTransaction( if (!TextUtils.isEmpty(errString) && errString.contains("[403]")) {
realm -> realm.createOrUpdateObjectFromJson(Session.class, new JSONObject() realmHelper.executeTransaction(realm -> {
.put("sessionId", Session.DEFAULT_ID) realm.delete(Session.class);
.put("tokenVerified", false) return null;
.put("error", exception.getMessage()))) }).continueWith(new LogcatIfError());
.continueWith(new LogcatIfError()); } else {
realmHelper.executeTransaction(
realm -> realm.createOrUpdateObjectFromJson(Session.class, new JSONObject()
.put("sessionId", Session.DEFAULT_ID)
.put("tokenVerified", false)
.put("error", errString)))
.continueWith(new LogcatIfError());
}
} }
} }
...@@ -4,6 +4,8 @@ import android.content.Context; ...@@ -4,6 +4,8 @@ import android.content.Context;
import chat.rocket.android.api.DDPClientWraper; import chat.rocket.android.api.DDPClientWraper;
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.LogcatIfError; import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.model.internal.LoadMessageProcedure;
import chat.rocket.android.model.internal.MethodCall;
import chat.rocket.android.model.internal.Session; import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmHelper; import chat.rocket.android.realm_helper.RealmHelper;
import hugo.weaving.DebugLog; import hugo.weaving.DebugLog;
...@@ -60,6 +62,11 @@ public class SessionObserver extends AbstractModelObserver<Session> { ...@@ -60,6 +62,11 @@ public class SessionObserver extends AbstractModelObserver<Session> {
} }
@DebugLog private void onLogout() { @DebugLog private void onLogout() {
realmHelper.executeTransaction(realm -> {
// remove all tables. ONLY INTERNAL TABLES!.
realm.delete(MethodCall.class);
realm.delete(LoadMessageProcedure.class);
return null;
}).continueWith(new LogcatIfError());
} }
} }
...@@ -2,6 +2,7 @@ package chat.rocket.android.service.observer; ...@@ -2,6 +2,7 @@ package chat.rocket.android.service.observer;
import android.content.Context; import android.content.Context;
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.model.internal.Session; import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmHelper; import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.api.DDPClientWraper; import chat.rocket.android.api.DDPClientWraper;
...@@ -32,12 +33,6 @@ public class TokenLoginObserver extends AbstractModelObserver<Session> { ...@@ -32,12 +33,6 @@ public class TokenLoginObserver extends AbstractModelObserver<Session> {
} }
Session session = results.get(0); Session session = results.get(0);
methodCall.loginWithToken(session.getToken()) methodCall.loginWithToken(session.getToken()).continueWith(new LogcatIfError());
.continueWith(task -> {
if (task.isFaulted()) {
Session.logError(realmHelper, task.getError());
}
return null;
});
} }
} }
...@@ -168,8 +168,4 @@ public class RealmHelper { ...@@ -168,8 +168,4 @@ public class RealmHelper {
RealmObjectObserver.Query<T> query) { RealmObjectObserver.Query<T> query) {
return new RealmObjectObserver<T>(this, query); return new RealmObjectObserver<T>(this, query);
} }
public void delete() {
Realm.deleteRealm(realmConfiguration);
}
} }
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