use of chat.rocket.persistence.realm.RealmHelper 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.persistence.realm.RealmHelper in project Rocket.Chat.Android by RocketChat.
the class CurrentUserObserver method onLogin.
@DebugLog
private void onLogin(RealmUser user) {
if (listeners != null) {
onLogout();
}
listeners = new ArrayList<>();
final String userId = user.getId();
// get and observe Room subscriptions.
methodCall.getRoomSubscriptions().onSuccess(task -> {
if (listeners != null) {
Registrable listener = new StreamNotifyUserSubscriptionsChanged(context, hostname, realmHelper, ddpClientRef, userId);
listener.register();
listeners.add(listener);
}
return null;
}).continueWith(new LogIfError());
}
use of chat.rocket.persistence.realm.RealmHelper in project Rocket.Chat.Android by RocketChat.
the class DefaultCookieProvider method getCookie.
@Override
public String getCookie() {
final String hostname = getHostnameFromCache();
if (hostname == null) {
return "";
}
final RealmHelper realmHelper = RealmStore.get(getHostnameFromCache());
if (realmHelper == null) {
return "";
}
final RealmUser user = realmHelper.executeTransactionForRead(realm -> RealmUser.queryCurrentUser(realm).findFirst());
final RealmSession session = realmHelper.executeTransactionForRead(realm -> RealmSession.queryDefaultSession(realm).findFirst());
if (user == null || session == null) {
return "";
}
return "rc_uid=" + user.getId() + ";rc_token=" + session.getToken();
}
Aggregations