Search in sources :

Example 1 with RealmMessage

use of chat.rocket.persistence.realm.models.ddp.RealmMessage in project Rocket.Chat.Android by RocketChat.

the class RealmMessageRepository method delete.

@Override
public Single<Boolean> delete(Message message) {
    return Single.defer(() -> {
        final Realm realm = RealmStore.getRealm(hostname);
        final Looper looper = Looper.myLooper();
        if (realm == null || looper == null) {
            return Single.just(false);
        }
        realm.beginTransaction();
        return RxJavaInterop.toV2Flowable(realm.where(RealmMessage.class).equalTo(RealmMessage.ID, message.getId()).findAll().<RealmResults<RealmMessage>>asObservable()).filter(realmObject -> realmObject.isLoaded() && realmObject.isValid()).firstElement().toSingle().flatMap(realmMessages -> Single.just(realmMessages.deleteAllFromRealm())).doOnEvent((success, throwable) -> {
            if (success) {
                realm.commitTransaction();
            } else {
                realm.cancelTransaction();
            }
            close(realm, looper);
        });
    });
}
Also used : User(chat.rocket.core.models.User) MessageRepository(chat.rocket.core.repositories.MessageRepository) Realm(io.realm.Realm) RealmUser(chat.rocket.persistence.realm.models.ddp.RealmUser) Optional(com.fernandocejas.arrow.optional.Optional) RealmResults(io.realm.RealmResults) RxJavaInterop(hu.akarnokd.rxjava.interop.RxJavaInterop) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage) ArrayList(java.util.ArrayList) Message(chat.rocket.core.models.Message) List(java.util.List) Flowable(io.reactivex.Flowable) Pair(android.support.v4.util.Pair) Sort(io.realm.Sort) Room(chat.rocket.core.models.Room) Looper(android.os.Looper) RealmStore(chat.rocket.persistence.realm.RealmStore) Looper(android.os.Looper) Realm(io.realm.Realm) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage) RealmResults(io.realm.RealmResults)

Example 2 with RealmMessage

use of chat.rocket.persistence.realm.models.ddp.RealmMessage in project Rocket.Chat.Android by RocketChat.

the class RealmMessageRepository method save.

@Override
public Single<Boolean> save(Message message) {
    return Single.defer(() -> {
        final Realm realm = RealmStore.getRealm(hostname);
        final Looper looper = Looper.myLooper();
        if (realm == null || looper == null) {
            return Single.just(false);
        }
        RealmMessage realmMessage = realm.where(RealmMessage.class).equalTo(RealmMessage.ID, message.getId()).findFirst();
        if (realmMessage == null) {
            realmMessage = new RealmMessage();
        } else {
            realmMessage = realm.copyFromRealm(realmMessage);
        }
        realmMessage.setId(message.getId());
        realmMessage.setSyncState(message.getSyncState());
        realmMessage.setTimestamp(message.getTimestamp());
        realmMessage.setRoomId(message.getRoomId());
        realmMessage.setMessage(message.getMessage());
        realmMessage.setEditedAt(message.getEditedAt());
        RealmUser realmUser = realmMessage.getUser();
        if (realmUser == null) {
            realmUser = realm.where(RealmUser.class).equalTo(RealmUser.ID, message.getUser().getId()).findFirst();
        }
        realmMessage.setUser(realmUser);
        realm.beginTransaction();
        return RxJavaInterop.toV2Flowable(realm.copyToRealmOrUpdate(realmMessage).asObservable()).filter(it -> it.isLoaded() && it.isValid()).firstElement().doOnSuccess(it -> realm.commitTransaction()).doOnError(throwable -> realm.cancelTransaction()).doOnEvent((realmObject, throwable) -> close(realm, looper)).toSingle().map(realmObject -> true);
    });
}
Also used : User(chat.rocket.core.models.User) MessageRepository(chat.rocket.core.repositories.MessageRepository) Realm(io.realm.Realm) RealmUser(chat.rocket.persistence.realm.models.ddp.RealmUser) Optional(com.fernandocejas.arrow.optional.Optional) RealmResults(io.realm.RealmResults) RxJavaInterop(hu.akarnokd.rxjava.interop.RxJavaInterop) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage) ArrayList(java.util.ArrayList) Message(chat.rocket.core.models.Message) List(java.util.List) Flowable(io.reactivex.Flowable) Pair(android.support.v4.util.Pair) Sort(io.realm.Sort) Room(chat.rocket.core.models.Room) Looper(android.os.Looper) RealmStore(chat.rocket.persistence.realm.RealmStore) Looper(android.os.Looper) RealmUser(chat.rocket.persistence.realm.models.ddp.RealmUser) Realm(io.realm.Realm) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage)

Example 3 with RealmMessage

use of chat.rocket.persistence.realm.models.ddp.RealmMessage in project Rocket.Chat.Android by RocketChat.

the class LoadMessageProcedureObserver method onUpdateResults.

@Override
public void onUpdateResults(List<LoadMessageProcedure> results) {
    if (results == null || results.isEmpty()) {
        return;
    }
    LoadMessageProcedure procedure = results.get(0);
    final String roomId = procedure.getRoomId();
    final boolean isReset = procedure.isReset();
    final long timestamp = procedure.getTimestamp();
    final int count = procedure.getCount();
    // TODO: Not implemented yet.
    final long lastSeen = 0;
    realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(LoadMessageProcedure.class, new JSONObject().put(LoadMessageProcedure.ID, roomId).put(LoadMessageProcedure.SYNC_STATE, SyncState.SYNCING))).onSuccessTask(task -> methodCall.loadHistory(roomId, isReset ? 0 : timestamp, count, lastSeen).onSuccessTask(_task -> {
        RealmMessage lastMessage = realmHelper.executeTransactionForRead(realm -> realm.where(RealmMessage.class).equalTo(RealmMessage.ROOM_ID, roomId).equalTo(RealmMessage.SYNC_STATE, SyncState.SYNCED).findAllSorted(RealmMessage.TIMESTAMP, Sort.ASCENDING).first(null));
        long lastTs = lastMessage != null ? lastMessage.getTimestamp() : 0;
        int messageCount = _task.getResult().length();
        return realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(LoadMessageProcedure.class, new JSONObject().put(LoadMessageProcedure.ID, roomId).put(LoadMessageProcedure.SYNC_STATE, SyncState.SYNCED).put(LoadMessageProcedure.TIMESTAMP, lastTs).put(LoadMessageProcedure.RESET, false).put(LoadMessageProcedure.HAS_NEXT, messageCount == count)));
    })).continueWithTask(task -> {
        if (task.isFaulted()) {
            RCLog.w(task.getError());
            return realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(LoadMessageProcedure.class, new JSONObject().put(LoadMessageProcedure.ID, roomId).put(LoadMessageProcedure.SYNC_STATE, SyncState.FAILED)));
        } else {
            return Task.forResult(null);
        }
    });
}
Also used : Context(android.content.Context) Realm(io.realm.Realm) RealmResults(io.realm.RealmResults) RCLog(chat.rocket.android.log.RCLog) LoadMessageProcedure(chat.rocket.persistence.realm.models.internal.LoadMessageProcedure) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage) MethodCallHelper(chat.rocket.android.api.MethodCallHelper) List(java.util.List) JSONObject(org.json.JSONObject) Sort(io.realm.Sort) RealmHelper(chat.rocket.persistence.realm.RealmHelper) SyncState(chat.rocket.core.SyncState) DDPClientRef(chat.rocket.android.service.DDPClientRef) Task(bolts.Task) LoadMessageProcedure(chat.rocket.persistence.realm.models.internal.LoadMessageProcedure) JSONObject(org.json.JSONObject) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage)

Example 4 with RealmMessage

use of chat.rocket.persistence.realm.models.ddp.RealmMessage in project Rocket.Chat.Android by RocketChat.

the class NewMessageObserver method onUpdateResults.

@Override
public void onUpdateResults(List<RealmMessage> results) {
    if (results.isEmpty()) {
        return;
    }
    final RealmMessage message = results.get(0);
    final String messageId = message.getId();
    final String roomId = message.getRoomId();
    final String msg = message.getMessage();
    final long editedAt = message.getEditedAt();
    realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(RealmMessage.class, new JSONObject().put(RealmMessage.ID, messageId).put(RealmMessage.SYNC_STATE, SyncState.SYNCING))).onSuccessTask(task -> methodCall.sendMessage(messageId, roomId, msg, editedAt)).continueWith(task -> {
        if (task.isFaulted()) {
            RCLog.w(task.getError());
            realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(RealmMessage.class, new JSONObject().put(RealmMessage.ID, messageId).put(RealmMessage.SYNC_STATE, SyncState.FAILED)));
        }
        return 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) DDPClientRef(chat.rocket.android.service.DDPClientRef) RCLog(chat.rocket.android.log.RCLog) LogIfError(chat.rocket.android.helper.LogIfError) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage) JSONObject(org.json.JSONObject) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage)

Aggregations

RealmMessage (chat.rocket.persistence.realm.models.ddp.RealmMessage)4 Realm (io.realm.Realm)4 RealmResults (io.realm.RealmResults)4 List (java.util.List)4 Sort (io.realm.Sort)3 Context (android.content.Context)2 Looper (android.os.Looper)2 Pair (android.support.v4.util.Pair)2 MethodCallHelper (chat.rocket.android.api.MethodCallHelper)2 RCLog (chat.rocket.android.log.RCLog)2 DDPClientRef (chat.rocket.android.service.DDPClientRef)2 SyncState (chat.rocket.core.SyncState)2 Message (chat.rocket.core.models.Message)2 Room (chat.rocket.core.models.Room)2 User (chat.rocket.core.models.User)2 MessageRepository (chat.rocket.core.repositories.MessageRepository)2 RealmHelper (chat.rocket.persistence.realm.RealmHelper)2 RealmStore (chat.rocket.persistence.realm.RealmStore)2 RealmUser (chat.rocket.persistence.realm.models.ddp.RealmUser)2 Optional (com.fernandocejas.arrow.optional.Optional)2