use of chat.rocket.android.helper.LogIfError in project Rocket.Chat.Android by RocketChat.
the class FileUploadingToUrlObserver method onUpdateResults.
@Override
public void onUpdateResults(List<FileUploading> results) {
if (results.isEmpty()) {
return;
}
List<FileUploading> uploadingList = realmHelper.executeTransactionForReadResults(realm -> realm.where(FileUploading.class).equalTo(FileUploading.SYNC_STATE, SyncState.SYNCING).findAll());
if (uploadingList.size() >= 3) {
// do not upload more than 3 files simultaneously
return;
}
FileUploading fileUploading = results.get(0);
final String roomId = fileUploading.getRoomId();
final String uplId = fileUploading.getUplId();
final String filename = fileUploading.getFilename();
final long filesize = fileUploading.getFilesize();
final String mimeType = fileUploading.getMimeType();
final Uri fileUri = Uri.parse(fileUploading.getUri());
final String storageType = fileUploading.getStorageType();
realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(FileUploading.class, new JSONObject().put(FileUploading.ID, uplId).put(FileUploading.SYNC_STATE, SyncState.SYNCING))).onSuccessTask(_task -> {
if (FileUploading.STORAGE_TYPE_GOOGLE.equals(storageType)) {
return methodCall.uploadGoogleRequest(filename, filesize, mimeType, roomId);
} else {
return methodCall.uploadS3Request(filename, filesize, mimeType, roomId);
}
}).onSuccessTask(task -> {
final JSONObject info = task.getResult();
final String uploadUrl = info.getString("upload");
final String downloadUrl = info.getString("download");
final JSONArray postDataList = info.getJSONArray("postData");
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for (int i = 0; i < postDataList.length(); i++) {
JSONObject postData = postDataList.getJSONObject(i);
bodyBuilder.addFormDataPart(postData.getString("name"), postData.getString("value"));
}
bodyBuilder.addFormDataPart("file", filename, new RequestBody() {
private long numBytes = 0;
@Override
public MediaType contentType() {
return MediaType.parse(mimeType);
}
@Override
public long contentLength() throws IOException {
return filesize;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
InputStream inputStream = context.getContentResolver().openInputStream(fileUri);
try (Source source = Okio.source(inputStream)) {
long readBytes;
while ((readBytes = source.read(sink.buffer(), 8192)) > 0) {
numBytes += readBytes;
realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(FileUploading.class, new JSONObject().put(FileUploading.ID, uplId).put(FileUploading.UPLOADED_SIZE, numBytes))).continueWith(new LogIfError());
}
}
}
});
Request request = new Request.Builder().url(uploadUrl).post(bodyBuilder.build()).build();
Response response = OkHttpHelper.getClientForUploadFile().newCall(request).execute();
if (response.isSuccessful()) {
return Task.forResult(downloadUrl);
} else {
return Task.forError(new Exception(response.message()));
}
}).onSuccessTask(task -> {
String downloadUrl = task.getResult();
String storage = FileUploading.STORAGE_TYPE_GOOGLE.equals(storageType) ? storageType : "s3";
return methodCall.sendFileMessage(roomId, storage, new JSONObject().put("_id", Uri.parse(downloadUrl).getLastPathSegment()).put("type", mimeType).put("size", filesize).put("name", filename).put("url", downloadUrl));
}).onSuccessTask(task -> realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(FileUploading.class, new JSONObject().put(FileUploading.ID, uplId).put(FileUploading.SYNC_STATE, SyncState.SYNCED).put(FileUploading.ERROR, JSONObject.NULL)))).continueWithTask(task -> {
if (task.isFaulted()) {
RCLog.w(task.getError());
return realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(FileUploading.class, new JSONObject().put(FileUploading.ID, uplId).put(FileUploading.SYNC_STATE, SyncState.FAILED).put(FileUploading.ERROR, task.getError().getMessage())));
} else {
return Task.forResult(null);
}
});
}
use of chat.rocket.android.helper.LogIfError in project Rocket.Chat.Android by RocketChat.
the class MethodCallObserver method onUpdateResults.
@Override
public void onUpdateResults(List<MethodCall> results) {
String digest = getDigestFor(results);
if (prevDigest == null) {
if (digest == null) {
return;
}
} else {
if (prevDigest.equals(digest)) {
return;
}
}
prevDigest = digest;
if (results == null || results.isEmpty()) {
return;
}
MethodCall call = results.get(0);
final String methodCallId = call.getMethodCallId();
final String methodName = call.getName();
final String params = call.getParamsJson();
final long timeout = call.getTimeout();
realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(MethodCall.class, new JSONObject().put(MethodCall.ID, methodCallId).put(MethodCall.SYNC_STATE, SyncState.SYNCING))).onSuccessTask(task -> ddpClientRef.get().rpc(methodCallId, methodName, params, timeout).onSuccessTask(_task -> realmHelper.executeTransaction(realm -> {
String json = _task.getResult().result;
return realm.createOrUpdateObjectFromJson(MethodCall.class, new JSONObject().put(MethodCall.ID, methodCallId).put(MethodCall.SYNC_STATE, SyncState.SYNCED).put(MethodCall.RESULT_JSON, json));
}))).continueWithTask(task -> {
if (task.isFaulted()) {
return realmHelper.executeTransaction(realm -> {
Exception exception = task.getError();
final String errMessage;
if (exception instanceof DDPClientCallback.RPC.Error) {
errMessage = ((DDPClientCallback.RPC.Error) exception).error.toString();
} else if (exception instanceof DDPClientCallback.RPC.Timeout) {
errMessage = "{\"message\": \"Connection Timeout\"}";
} else if (exception instanceof RxWebSocketCallback.Failure) {
errMessage = "{\"message\": \"Connection Failure\"}";
} else {
errMessage = exception.getMessage();
}
realm.createOrUpdateObjectFromJson(MethodCall.class, new JSONObject().put(MethodCall.ID, methodCallId).put(MethodCall.SYNC_STATE, SyncState.FAILED).put(MethodCall.RESULT_JSON, errMessage));
return null;
});
}
return task;
}).continueWith(new LogIfError());
}
use of chat.rocket.android.helper.LogIfError in project Rocket.Chat.Android by RocketChat.
the class SessionObserver method onLogout.
@DebugLog
private void onLogout() {
streamNotifyMessage.unregister();
realmHelper.executeTransaction(realm -> {
realm.delete(MethodCall.class);
realm.delete(LoadMessageProcedure.class);
realm.delete(GetUsersOfRoomsProcedure.class);
return null;
}).continueWith(new LogIfError());
}
use of chat.rocket.android.helper.LogIfError in project Rocket.Chat.Android by RocketChat.
the class AbstractStreamNotifyEventSubscriber method handleArgs.
protected void handleArgs(JSONArray args) throws JSONException {
String msg = args.length() > 0 ? args.getString(0) : null;
JSONObject target = args.getJSONObject(args.length() - 1);
if ("removed".equals(msg)) {
realmHelper.executeTransaction(realm -> realm.where(getModelClass()).equalTo(getPrimaryKeyForModel(), target.getString(getPrimaryKeyForModel())).findAll().deleteAllFromRealm()).continueWith(new LogIfError());
} else {
//inserted, updated
realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(getModelClass(), customizeFieldJson(target))).continueWith(new LogIfError());
}
}
use of chat.rocket.android.helper.LogIfError in project Rocket.Chat.Android by RocketChat.
the class RoomPresenter method onMarkAsRead.
@Override
public void onMarkAsRead() {
final Disposable subscription = roomRepository.getById(roomId).filter(Optional::isPresent).map(Optional::get).firstElement().filter(Room::isAlert).subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())).observeOn(AndroidSchedulers.mainThread()).subscribe(room -> methodCallHelper.readMessages(room.getRoomId()).continueWith(new LogIfError()), Logger::report);
addSubscription(subscription);
}
Aggregations