Commit e5ed1f44 authored by Yusuke Iwaki's avatar Yusuke Iwaki

fix MethodCall's paramJson to set JSONArray for accepting multiple params.

parent feb65d69
...@@ -57,7 +57,7 @@ public class MethodCallHelper { ...@@ -57,7 +57,7 @@ public class MethodCallHelper {
} }
private interface ParamBuilder { private interface ParamBuilder {
void buildParam(JSONObject param) throws JSONException; void buildParam(JSONArray param) throws JSONException;
} }
private <T> Task<T> call(String methodName, private <T> Task<T> call(String methodName,
...@@ -68,15 +68,15 @@ public class MethodCallHelper { ...@@ -68,15 +68,15 @@ public class MethodCallHelper {
private <T> Task<T> call(String methodName, ParamBuilder paramBuilder, private <T> Task<T> call(String methodName, ParamBuilder paramBuilder,
Continuation<JSONObject, Task<T>> onSuccess) { Continuation<JSONObject, Task<T>> onSuccess) {
JSONObject param = new JSONObject(); JSONArray params = new JSONArray();
try { try {
paramBuilder.buildParam(param); paramBuilder.buildParam(params);
} catch (JSONException exception) { } catch (JSONException exception) {
return Task.forError(exception); return Task.forError(exception);
} }
return injectErrorHandler(executeMethodCall(methodName, param.toString())) return injectErrorHandler(executeMethodCall(methodName, params.toString()))
.onSuccessTask(onSuccess); .onSuccessTask(onSuccess);
} }
...@@ -85,11 +85,11 @@ public class MethodCallHelper { ...@@ -85,11 +85,11 @@ public class MethodCallHelper {
*/ */
public Task<Void> registerUser(final String name, final String email, public Task<Void> registerUser(final String name, final String email,
final String password, final String confirmPassword) { final String password, final String confirmPassword) {
return call("registerUser", param -> param return call("registerUser", params -> params.put(new JSONObject()
.put("name", name) .put("name", name)
.put("email", email) .put("email", email)
.put("pass", password) .put("pass", password)
.put("confirm-pass", confirmPassword), .put("confirm-pass", confirmPassword)),
task -> Task.forResult(null)); // nothing to do. task -> Task.forResult(null)); // nothing to do.
} }
...@@ -105,7 +105,8 @@ public class MethodCallHelper { ...@@ -105,7 +105,8 @@ public class MethodCallHelper {
* Login with username/email and password. * Login with username/email and password.
*/ */
public Task<Void> loginWithEmail(final String usernameOrEmail, final String password) { public Task<Void> loginWithEmail(final String usernameOrEmail, final String password) {
return call("login", param -> { return call("login", params -> {
JSONObject param = new JSONObject();
if (Patterns.EMAIL_ADDRESS.matcher(usernameOrEmail).matches()) { if (Patterns.EMAIL_ADDRESS.matcher(usernameOrEmail).matches()) {
param.put("user", new JSONObject().put("email", usernameOrEmail)); param.put("user", new JSONObject().put("email", usernameOrEmail));
} else { } else {
...@@ -114,6 +115,7 @@ public class MethodCallHelper { ...@@ -114,6 +115,7 @@ public class MethodCallHelper {
param.put("password", new JSONObject() param.put("password", new JSONObject()
.put("digest", CheckSum.sha256(password)) .put("digest", CheckSum.sha256(password))
.put("algorithm", "sha-256")); .put("algorithm", "sha-256"));
params.put(param);
}, task -> Task.forResult(task.getResult().getString("token"))).onSuccessTask(this::saveToken); }, task -> Task.forResult(task.getResult().getString("token"))).onSuccessTask(this::saveToken);
} }
...@@ -122,18 +124,18 @@ public class MethodCallHelper { ...@@ -122,18 +124,18 @@ public class MethodCallHelper {
*/ */
public Task<Void> loginWithGitHub(final String credentialToken, public Task<Void> loginWithGitHub(final String credentialToken,
final String credentialSecret) { final String credentialSecret) {
return call("login", param -> param return call("login", params -> params.put(new JSONObject()
.put("oauth", new JSONObject() .put("oauth", new JSONObject()
.put("credentialToken", credentialToken) .put("credentialToken", credentialToken)
.put("credentialSecret", credentialSecret)), .put("credentialSecret", credentialSecret))
task -> Task.forResult(task.getResult().getString("token"))).onSuccessTask(this::saveToken); ), task -> Task.forResult(task.getResult().getString("token"))).onSuccessTask(this::saveToken);
} }
/** /**
* Login with token. * Login with token.
*/ */
public Task<Void> loginWithToken(final String token) { public Task<Void> loginWithToken(final String token) {
return call("login", param -> param.put("resume", token), return call("login", param -> param.put(new JSONObject().put("resume", token)),
task -> Task.forResult(task.getResult().getString("token"))).onSuccessTask(this::saveToken); task -> Task.forResult(task.getResult().getString("token"))).onSuccessTask(this::saveToken);
} }
...@@ -178,35 +180,39 @@ public class MethodCallHelper { ...@@ -178,35 +180,39 @@ public class MethodCallHelper {
} }
private Task<Long> getObjectRecursive(String objName, Customizer customizer, long timestamp) { private Task<Long> getObjectRecursive(String objName, Customizer customizer, long timestamp) {
return call(objName + "/get", param -> param.put("$date", timestamp), task -> { return call(objName + "/get",
JSONObject result = task.getResult(); params -> params.put(new JSONObject().put("$date", timestamp)),
task -> {
long nextTimestamp = 0; JSONObject result = task.getResult();
try {
nextTimestamp = result.getJSONArray("remove") long nextTimestamp = 0;
.getJSONObject(0).getJSONObject("_deletedAt").getLong("$date"); try {
} catch (JSONException exception) { nextTimestamp = result.getJSONArray("remove")
// keep nextTimestamp = 0 .getJSONObject(0).getJSONObject("_deletedAt").getLong("$date");
} } catch (JSONException exception) {
// keep nextTimestamp = 0
try { }
JSONArray updatedRooms = result.getJSONArray("update");
customizer.customizeResult(updatedRooms); try {
JSONArray updatedRooms = result.getJSONArray("update");
Task<Void> saveToDB = RealmHelperBolts.executeTransaction(realm -> { customizer.customizeResult(updatedRooms);
realm.createOrUpdateAllFromJson(RoomSubscription.class, result.getJSONArray("update"));
return null; Task<Void> saveToDB = RealmHelperBolts.executeTransaction(realm -> {
realm.createOrUpdateAllFromJson(
RoomSubscription.class, result.getJSONArray("update"));
return null;
});
if (nextTimestamp > 0 && (timestamp == 0 || nextTimestamp < timestamp)) {
final long _next = nextTimestamp;
return saveToDB.onSuccessTask(_task ->
getObjectRecursive(objName, customizer, _next));
} else {
return saveToDB.onSuccessTask(_task -> Task.forResult(0L));
}
} catch (JSONException exception) {
return Task.forError(exception);
}
}); });
if (nextTimestamp > 0 && (timestamp == 0 || nextTimestamp < timestamp)) {
final long _next = nextTimestamp;
return saveToDB.onSuccessTask(_task -> getObjectRecursive(objName, customizer, _next));
} else {
return saveToDB.onSuccessTask(_task -> Task.forResult(0L));
}
} catch (JSONException exception) {
return Task.forError(exception);
}
});
} }
} }
...@@ -10,7 +10,6 @@ import chat.rocket.android_ddp.DDPSubscription; ...@@ -10,7 +10,6 @@ import chat.rocket.android_ddp.DDPSubscription;
import java.util.UUID; import java.util.UUID;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject;
import rx.Observable; import rx.Observable;
/** /**
...@@ -87,7 +86,7 @@ public class RocketChatWebSocketAPI { ...@@ -87,7 +86,7 @@ public class RocketChatWebSocketAPI {
} }
try { try {
return ddpClient.rpc(methodName, new JSONArray().put(new JSONObject(params)), methodCallId); return ddpClient.rpc(methodName, new JSONArray(params), methodCallId);
} catch (JSONException exception) { } catch (JSONException exception) {
return Task.forError(exception); return Task.forError(exception);
} }
......
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