Commit 8e9506c0 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Cache the Site_Url public setting value for each hostname

parent 5b476274
...@@ -28,6 +28,7 @@ import io.reactivex.annotations.Nullable; ...@@ -28,6 +28,7 @@ import io.reactivex.annotations.Nullable;
*/ */
public class RocketChatCache { public class RocketChatCache {
private static final String KEY_SELECTED_SERVER_HOSTNAME = "KEY_SELECTED_SERVER_HOSTNAME"; private static final String KEY_SELECTED_SERVER_HOSTNAME = "KEY_SELECTED_SERVER_HOSTNAME";
private static final String KEY_SELECTED_SITE_URL = "KEY_SELECTED_SITE_URL";
private static final String KEY_SELECTED_ROOM_ID = "KEY_SELECTED_ROOM_ID"; private static final String KEY_SELECTED_ROOM_ID = "KEY_SELECTED_ROOM_ID";
private static final String KEY_PUSH_ID = "KEY_PUSH_ID"; private static final String KEY_PUSH_ID = "KEY_PUSH_ID";
private static final String KEY_HOSTNAME_LIST = "KEY_HOSTNAME_LIST"; private static final String KEY_HOSTNAME_LIST = "KEY_HOSTNAME_LIST";
...@@ -50,6 +51,38 @@ public class RocketChatCache { ...@@ -50,6 +51,38 @@ public class RocketChatCache {
setString(KEY_SELECTED_SERVER_HOSTNAME, newHostname); setString(KEY_SELECTED_SERVER_HOSTNAME, newHostname);
} }
public void setSelectedServerHostnameAlias(@Nullable String hostnameAlias) {
String alias = null;
if (hostnameAlias != null) {
alias = hostnameAlias.toLowerCase();
}
try {
String selectedHostnameAliasJson = getLoginHostnamesJson();
JSONObject jsonObject = selectedHostnameAliasJson == null ?
new JSONObject() : new JSONObject(selectedHostnameAliasJson);
String selectedServerHostname = getSelectedServerHostname();
jsonObject.put(alias, selectedServerHostname);
setString(KEY_SELECTED_SITE_URL, jsonObject.toString());
} catch (JSONException e) {
RCLog.e(e);
}
}
public @NonNull String getLoginHostnameFrom(String hostname) {
try {
String selectedServerHostname = getSelectedServerHostname();
return new JSONObject(getLoginHostnamesJson())
.optString(hostname, selectedServerHostname);
} catch (JSONException e) {
RCLog.e(e);
}
return null;
}
private @Nullable String getLoginHostnamesJson() {
return getString(KEY_SELECTED_SITE_URL, null);
}
public void addHostname(@NonNull String hostname, @Nullable String hostnameAvatarUri, String siteName) { public void addHostname(@NonNull String hostname, @Nullable String hostnameAvatarUri, String siteName) {
String hostnameList = getString(KEY_HOSTNAME_LIST, null); String hostnameList = getString(KEY_HOSTNAME_LIST, null);
try { try {
......
...@@ -61,7 +61,9 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity { ...@@ -61,7 +61,9 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
String hostname = intent.getStringExtra(PushConstants.HOSTNAME); String hostname = intent.getStringExtra(PushConstants.HOSTNAME);
HttpUrl url = HttpUrl.parse(hostname); HttpUrl url = HttpUrl.parse(hostname);
if (url != null) { if (url != null) {
rocketChatCache.setSelectedServerHostname(url.host()); String hostnameFromPush = url.host();
String loginHostname = rocketChatCache.getLoginHostnameFrom(hostnameFromPush);
rocketChatCache.setSelectedServerHostname(loginHostname);
if (intent.hasExtra(PushConstants.ROOM_ID)) { if (intent.hasExtra(PushConstants.ROOM_ID)) {
rocketChatCache.setSelectedRoomId(intent.getStringExtra(PushConstants.ROOM_ID)); rocketChatCache.setSelectedRoomId(intent.getStringExtra(PushConstants.ROOM_ID));
......
...@@ -11,11 +11,13 @@ import java.util.UUID; ...@@ -11,11 +11,13 @@ import java.util.UUID;
import bolts.Continuation; import bolts.Continuation;
import bolts.Task; import bolts.Task;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.helper.CheckSum; import chat.rocket.android.helper.CheckSum;
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.DDPClientRef; import chat.rocket.android.service.DDPClientRef;
import chat.rocket.android_ddp.DDPClientCallback; import chat.rocket.android_ddp.DDPClientCallback;
import chat.rocket.core.PublicSettingsConstants;
import chat.rocket.core.SyncState; import chat.rocket.core.SyncState;
import chat.rocket.persistence.realm.RealmHelper; import chat.rocket.persistence.realm.RealmHelper;
import chat.rocket.persistence.realm.RealmStore; import chat.rocket.persistence.realm.RealmStore;
...@@ -30,6 +32,7 @@ import chat.rocket.persistence.realm.models.ddp.RealmSpotlightUser; ...@@ -30,6 +32,7 @@ import chat.rocket.persistence.realm.models.ddp.RealmSpotlightUser;
import chat.rocket.persistence.realm.models.internal.MethodCall; import chat.rocket.persistence.realm.models.internal.MethodCall;
import chat.rocket.persistence.realm.models.internal.RealmSession; import chat.rocket.persistence.realm.models.internal.RealmSession;
import hugo.weaving.DebugLog; import hugo.weaving.DebugLog;
import okhttp3.HttpUrl;
/** /**
* Utility class for creating/handling MethodCall or RPC. * Utility class for creating/handling MethodCall or RPC.
...@@ -65,6 +68,12 @@ public class MethodCallHelper { ...@@ -65,6 +68,12 @@ public class MethodCallHelper {
this.ddpClientRef = ddpClientRef; this.ddpClientRef = ddpClientRef;
} }
public MethodCallHelper(Context context, RealmHelper realmHelper, DDPClientRef ddpClientRef) {
this.context = context.getApplicationContext();
this.realmHelper = realmHelper;
this.ddpClientRef = ddpClientRef;
}
@DebugLog @DebugLog
private Task<String> executeMethodCall(String methodName, String param, long timeout) { private Task<String> executeMethodCall(String methodName, String param, long timeout) {
if (ddpClientRef != null) { if (ddpClientRef != null) {
...@@ -412,7 +421,15 @@ public class MethodCallHelper { ...@@ -412,7 +421,15 @@ public class MethodCallHelper {
.onSuccessTask(task -> { .onSuccessTask(task -> {
final JSONArray settings = task.getResult(); final JSONArray settings = task.getResult();
for (int i = 0; i < settings.length(); i++) { for (int i = 0; i < settings.length(); i++) {
RealmPublicSetting.customizeJson(settings.getJSONObject(i)); JSONObject jsonObject = settings.getJSONObject(i);
RealmPublicSetting.customizeJson(jsonObject);
if (jsonObject.getString(RealmPublicSetting.ID).equalsIgnoreCase(PublicSettingsConstants.General.SITE_URL)) {
String siteUrl = jsonObject.getString(RealmPublicSetting.VALUE);
HttpUrl httpUrl = HttpUrl.parse(siteUrl);
if (httpUrl != null) {
new RocketChatCache(context).setSelectedServerHostnameAlias(httpUrl.host());
}
}
} }
return realmHelper.executeTransaction(realm -> { return realmHelper.executeTransaction(realm -> {
......
...@@ -343,7 +343,7 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -343,7 +343,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
} }
private Task<Void> fetchPublicSettings() { private Task<Void> fetchPublicSettings() {
return new MethodCallHelper(realmHelper, ddpClientRef).getPublicSettings(); return new MethodCallHelper(appContext, realmHelper, ddpClientRef).getPublicSettings();
} }
private Task<Void> fetchPermissions() { private Task<Void> fetchPermissions() {
......
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