use of chat.rocket.core.models.Message in project Rocket.Chat.Android by RocketChat.
the class MessageOptionsDialogFragment method setUpDialog.
private void setUpDialog(final BottomSheetDialog bottomSheetDialog, String messageId) {
RocketChatCache cache = new RocketChatCache(bottomSheetDialog.getContext());
String hostname = cache.getSelectedServerHostname();
EditMessageInteractor editMessageInteractor = getEditMessageInteractor(hostname);
MessageRepository messageRepository = new RealmMessageRepository(hostname);
Disposable disposable = messageRepository.getById(messageId).flatMap(it -> {
if (!it.isPresent()) {
return Single.just(Pair.<Message, Boolean>create(null, false));
}
Message message = it.get();
return Single.zip(Single.just(message), editMessageInteractor.isAllowed(message), Pair::create);
}).subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())).observeOn(AndroidSchedulers.mainThread()).subscribe(pair -> {
if (pair.second) {
bottomSheetDialog.findViewById(R.id.message_options_info).setVisibility(View.GONE);
View editView = bottomSheetDialog.findViewById(R.id.message_options_edit_action);
editView.setVisibility(View.VISIBLE);
editView.setOnClickListener(view -> internalListener.onEdit(pair.first));
} else {
((TextView) bottomSheetDialog.findViewById(R.id.message_options_info)).setText(R.string.message_options_no_permissions_info);
}
}, throwable -> {
((TextView) bottomSheetDialog.findViewById(R.id.message_options_info)).setText(R.string.message_options_no_message_info);
Logger.report(throwable);
});
compositeDisposable.add(disposable);
}
use of chat.rocket.core.models.Message 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.core.models.Message 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.core.models.Message in project Rocket.Chat.Android by RocketChat.
the class RoomPresenter method resendMessage.
@Override
public void resendMessage(Message message) {
final Disposable subscription = getCurrentUser().flatMap(user -> messageInteractor.resend(message, user)).subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())).observeOn(AndroidSchedulers.mainThread()).subscribe();
addSubscription(subscription);
}
use of chat.rocket.core.models.Message in project Rocket.Chat.Android by RocketChat.
the class RoomPresenter method updateMessage.
@Override
public void updateMessage(Message message, String content) {
final Disposable subscription = getCurrentUser().flatMap(user -> messageInteractor.update(message, user, content)).subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())).observeOn(AndroidSchedulers.mainThread()).subscribe(success -> {
if (success) {
view.onMessageSendSuccessfully();
}
}, Logger::report);
addSubscription(subscription);
}
Aggregations