use of chat.rocket.core.SyncState in project Rocket.Chat.Android by RocketChat.
the class MethodCallHelper method loadHistory.
/**
* Load messages for room.
*/
public Task<JSONArray> loadHistory(final String roomId, final long timestamp, final int count, final long lastSeen) {
return call("loadHistory", TIMEOUT_MS, () -> new JSONArray().put(roomId).put(timestamp > 0 ? new JSONObject().put("$date", timestamp) : JSONObject.NULL).put(count).put(lastSeen > 0 ? new JSONObject().put("$date", lastSeen) : JSONObject.NULL)).onSuccessTask(CONVERT_TO_JSON_OBJECT).onSuccessTask(task -> {
JSONObject result = task.getResult();
final JSONArray messages = result.getJSONArray("messages");
for (int i = 0; i < messages.length(); i++) {
RealmMessage.customizeJson(messages.getJSONObject(i));
}
return realmHelper.executeTransaction(realm -> {
if (timestamp == 0) {
realm.where(RealmMessage.class).equalTo("rid", roomId).equalTo("syncstate", SyncState.SYNCED).findAll().deleteAllFromRealm();
}
if (messages.length() > 0) {
realm.createOrUpdateAllFromJson(RealmMessage.class, messages);
}
return null;
}).onSuccessTask(_task -> Task.forResult(messages));
});
}
use of chat.rocket.core.SyncState in project Rocket.Chat.Android by RocketChat.
the class MethodCall method execute.
/**
* insert a new record to request a method call.
*/
public static Task<String> execute(RealmHelper realmHelper, String name, String paramsJson, long timeout) {
final String newId = UUID.randomUUID().toString();
TaskCompletionSource<String> task = new TaskCompletionSource<>();
realmHelper.executeTransaction(realm -> {
MethodCall call = realm.createObjectFromJson(MethodCall.class, new JSONObject().put(ID, newId).put(SYNC_STATE, SyncState.NOT_SYNCED).put(TIMEOUT, timeout).put(NAME, name));
call.setParamsJson(paramsJson);
return null;
}).continueWith(_task -> {
if (_task.isFaulted()) {
task.setError(_task.getError());
} else {
final RealmObjectObserver<MethodCall> observer = realmHelper.createObjectObserver(realm -> realm.where(MethodCall.class).equalTo(ID, newId));
observer.setOnUpdateListener(methodCall -> {
if (methodCall == null) {
observer.unsub();
REF_MAP.remove(newId);
return;
}
int syncState = methodCall.getSyncState();
RCLog.d("MethodCall[%s] syncstate=%d", methodCall.getMethodCallId(), syncState);
if (syncState == SyncState.SYNCED) {
String resultJson = methodCall.getResultJson();
if (TextUtils.isEmpty(resultJson)) {
task.setResult(null);
} else {
task.setResult(resultJson);
}
observer.unsub();
REF_MAP.remove(methodCall.getMethodCallId());
remove(realmHelper, methodCall.getMethodCallId()).continueWith(new LogcatIfError());
} else if (syncState == SyncState.FAILED) {
task.setError(new Error(methodCall.getResultJson()));
observer.unsub();
REF_MAP.remove(methodCall.getMethodCallId());
remove(realmHelper, methodCall.getMethodCallId()).continueWith(new LogcatIfError());
}
});
observer.sub();
REF_MAP.put(newId, observer);
}
return null;
});
return task.getTask();
}
use of chat.rocket.core.SyncState in project Rocket.Chat.Android by RocketChat.
the class GetUsersOfRoomsProcedureObserver method onUpdateResults.
@Override
public void onUpdateResults(List<GetUsersOfRoomsProcedure> results) {
if (results == null || results.isEmpty()) {
return;
}
GetUsersOfRoomsProcedure procedure = results.get(0);
final String roomId = procedure.getRoomId();
final boolean showAll = procedure.isShowAll();
realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(GetUsersOfRoomsProcedure.class, new JSONObject().put(GetUsersOfRoomsProcedure.ID, roomId).put(GetUsersOfRoomsProcedure.SYNC_STATE, SyncState.SYNCING))).onSuccessTask(task -> methodCall.getUsersOfRoom(roomId, showAll).onSuccessTask(_task -> {
JSONObject result = _task.getResult().put("roomId", roomId).put("syncstate", SyncState.SYNCED);
return realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(GetUsersOfRoomsProcedure.class, result));
})).continueWithTask(task -> {
if (task.isFaulted()) {
RCLog.w(task.getError());
return realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(GetUsersOfRoomsProcedure.class, new JSONObject().put(GetUsersOfRoomsProcedure.ID, roomId).put(GetUsersOfRoomsProcedure.SYNC_STATE, SyncState.FAILED)));
} else {
return Task.forResult(null);
}
});
}
Aggregations