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);
});
});
}
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);
});
}
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);
}
});
}
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;
});
}
Aggregations