Search in sources :

Example 1 with RealmHelper

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();
}
Also used : RealmObject(io.realm.RealmObject) TextUtils(android.text.TextUtils) HashMap(java.util.HashMap) UUID(java.util.UUID) RCLog(chat.rocket.android.log.RCLog) RealmObjectObserver(chat.rocket.persistence.realm.RealmObjectObserver) TaskCompletionSource(bolts.TaskCompletionSource) PrimaryKey(io.realm.annotations.PrimaryKey) JSONObject(org.json.JSONObject) RealmHelper(chat.rocket.persistence.realm.RealmHelper) SyncState(chat.rocket.core.SyncState) LogcatIfError(chat.rocket.persistence.realm.helpers.LogcatIfError) Task(bolts.Task) TaskCompletionSource(bolts.TaskCompletionSource) JSONObject(org.json.JSONObject) LogcatIfError(chat.rocket.persistence.realm.helpers.LogcatIfError) LogcatIfError(chat.rocket.persistence.realm.helpers.LogcatIfError)

Example 2 with RealmHelper

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());
}
Also used : Context(android.content.Context) StreamNotifyUserSubscriptionsChanged(chat.rocket.android.service.ddp.stream.StreamNotifyUserSubscriptionsChanged) Realm(io.realm.Realm) RealmUser(chat.rocket.persistence.realm.models.ddp.RealmUser) RealmResults(io.realm.RealmResults) LogIfError(chat.rocket.android.helper.LogIfError) DebugLog(hugo.weaving.DebugLog) ArrayList(java.util.ArrayList) MethodCallHelper(chat.rocket.android.api.MethodCallHelper) List(java.util.List) Registrable(chat.rocket.android.service.Registrable) RealmHelper(chat.rocket.persistence.realm.RealmHelper) DDPClientRef(chat.rocket.android.service.DDPClientRef) Registrable(chat.rocket.android.service.Registrable) StreamNotifyUserSubscriptionsChanged(chat.rocket.android.service.ddp.stream.StreamNotifyUserSubscriptionsChanged) LogIfError(chat.rocket.android.helper.LogIfError) DebugLog(hugo.weaving.DebugLog)

Example 3 with RealmHelper

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();
}
Also used : RealmHelper(chat.rocket.persistence.realm.RealmHelper) RealmUser(chat.rocket.persistence.realm.models.ddp.RealmUser) RealmSession(chat.rocket.persistence.realm.models.internal.RealmSession)

Aggregations

RealmHelper (chat.rocket.persistence.realm.RealmHelper)3 RealmUser (chat.rocket.persistence.realm.models.ddp.RealmUser)2 Context (android.content.Context)1 TextUtils (android.text.TextUtils)1 Task (bolts.Task)1 TaskCompletionSource (bolts.TaskCompletionSource)1 MethodCallHelper (chat.rocket.android.api.MethodCallHelper)1 LogIfError (chat.rocket.android.helper.LogIfError)1 RCLog (chat.rocket.android.log.RCLog)1 DDPClientRef (chat.rocket.android.service.DDPClientRef)1 Registrable (chat.rocket.android.service.Registrable)1 StreamNotifyUserSubscriptionsChanged (chat.rocket.android.service.ddp.stream.StreamNotifyUserSubscriptionsChanged)1 SyncState (chat.rocket.core.SyncState)1 RealmObjectObserver (chat.rocket.persistence.realm.RealmObjectObserver)1 LogcatIfError (chat.rocket.persistence.realm.helpers.LogcatIfError)1 RealmSession (chat.rocket.persistence.realm.models.internal.RealmSession)1 DebugLog (hugo.weaving.DebugLog)1 Realm (io.realm.Realm)1 RealmObject (io.realm.RealmObject)1 RealmResults (io.realm.RealmResults)1