Search in sources :

Example 1 with SyncState

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));
    });
}
Also used : Context(android.content.Context) DDPClientCallback(chat.rocket.android_ddp.DDPClientCallback) ConnectivityManager(chat.rocket.android.service.ConnectivityManager) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage) DebugLog(hugo.weaving.DebugLog) JSONException(org.json.JSONException) Patterns(android.util.Patterns) JSONObject(org.json.JSONObject) SyncState(chat.rocket.core.SyncState) RealmRoomRole(chat.rocket.persistence.realm.models.ddp.RealmRoomRole) Task(bolts.Task) TextUtils(chat.rocket.android.helper.TextUtils) MethodCall(chat.rocket.persistence.realm.models.internal.MethodCall) Continuation(bolts.Continuation) RealmSpotlightRoom(chat.rocket.persistence.realm.models.ddp.RealmSpotlightRoom) RealmSession(chat.rocket.persistence.realm.models.internal.RealmSession) UUID(java.util.UUID) RealmPublicSetting(chat.rocket.persistence.realm.models.ddp.RealmPublicSetting) RealmSpotlightUser(chat.rocket.persistence.realm.models.ddp.RealmSpotlightUser) RealmRoom(chat.rocket.persistence.realm.models.ddp.RealmRoom) RealmHelper(chat.rocket.persistence.realm.RealmHelper) DDPClientRef(chat.rocket.android.service.DDPClientRef) RealmPermission(chat.rocket.persistence.realm.models.ddp.RealmPermission) CheckSum(chat.rocket.android.helper.CheckSum) JSONArray(org.json.JSONArray) RealmStore(chat.rocket.persistence.realm.RealmStore) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage)

Example 2 with SyncState

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();
}
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 3 with SyncState

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);
        }
    });
}
Also used : MethodCallHelper(chat.rocket.android.api.MethodCallHelper) Context(android.content.Context) Realm(io.realm.Realm) List(java.util.List) JSONObject(org.json.JSONObject) RealmResults(io.realm.RealmResults) RealmHelper(chat.rocket.persistence.realm.RealmHelper) SyncState(chat.rocket.core.SyncState) GetUsersOfRoomsProcedure(chat.rocket.persistence.realm.models.internal.GetUsersOfRoomsProcedure) DDPClientRef(chat.rocket.android.service.DDPClientRef) RCLog(chat.rocket.android.log.RCLog) Task(bolts.Task) JSONObject(org.json.JSONObject) GetUsersOfRoomsProcedure(chat.rocket.persistence.realm.models.internal.GetUsersOfRoomsProcedure)

Aggregations

Task (bolts.Task)3 SyncState (chat.rocket.core.SyncState)3 RealmHelper (chat.rocket.persistence.realm.RealmHelper)3 JSONObject (org.json.JSONObject)3 Context (android.content.Context)2 RCLog (chat.rocket.android.log.RCLog)2 DDPClientRef (chat.rocket.android.service.DDPClientRef)2 UUID (java.util.UUID)2 TextUtils (android.text.TextUtils)1 Patterns (android.util.Patterns)1 Continuation (bolts.Continuation)1 TaskCompletionSource (bolts.TaskCompletionSource)1 MethodCallHelper (chat.rocket.android.api.MethodCallHelper)1 CheckSum (chat.rocket.android.helper.CheckSum)1 TextUtils (chat.rocket.android.helper.TextUtils)1 ConnectivityManager (chat.rocket.android.service.ConnectivityManager)1 DDPClientCallback (chat.rocket.android_ddp.DDPClientCallback)1 RealmObjectObserver (chat.rocket.persistence.realm.RealmObjectObserver)1 RealmStore (chat.rocket.persistence.realm.RealmStore)1 LogcatIfError (chat.rocket.persistence.realm.helpers.LogcatIfError)1