use of chat.rocket.persistence.realm.models.internal.RealmSession in project Rocket.Chat.Android by RocketChat.
the class RealmSessionRepository method save.
@Override
public Single<Boolean> save(Session session) {
return Single.defer(() -> {
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null || looper == null) {
return Single.just(false);
}
RealmSession realmSession = realm.where(RealmSession.class).equalTo(RealmSession.ID, session.getSessionId()).findFirst();
if (realmSession == null) {
realmSession = new RealmSession();
} else {
realmSession = realm.copyFromRealm(realmSession);
}
realmSession.setSessionId(session.getSessionId());
realmSession.setToken(session.getToken());
realmSession.setTokenVerified(session.isTokenVerified());
realmSession.setError(session.getError());
realm.beginTransaction();
return RxJavaInterop.toV2Flowable(realm.copyToRealmOrUpdate(realmSession).asObservable()).filter(it -> it != null && 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.internal.RealmSession in project Rocket.Chat.Android by RocketChat.
the class FileUploadingWithUfsObserver 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() >= 1) {
// do not upload multiple files simultaneously
return;
}
RealmUser currentUser = realmHelper.executeTransactionForRead(realm -> RealmUser.queryCurrentUser(realm).findFirst());
RealmSession session = realmHelper.executeTransactionForRead(realm -> RealmSession.queryDefaultSession(realm).findFirst());
if (currentUser == null || session == null) {
return;
}
final String cookie = String.format("rc_uid=%s; rc_token=%s", currentUser.getId(), session.getToken());
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 store = FileUploading.STORAGE_TYPE_GRID_FS.equals(fileUploading.getStorageType()) ? "rocketchat_uploads" : (FileUploading.STORAGE_TYPE_FILE_SYSTEM.equals(fileUploading.getStorageType()) ? "fileSystem" : null);
realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(FileUploading.class, new JSONObject().put(FileUploading.ID, uplId).put(FileUploading.SYNC_STATE, SyncState.SYNCING))).onSuccessTask(_task -> methodCall.ufsCreate(filename, filesize, mimeType, store, roomId)).onSuccessTask(task -> {
final JSONObject info = task.getResult();
final String fileId = info.getString("fileId");
final String token = info.getString("token");
final String url = info.getString("url");
final int bufSize = 16384;
final byte[] buffer = new byte[bufSize];
int offset = 0;
final MediaType contentType = MediaType.parse(mimeType);
try (InputStream inputStream = context.getContentResolver().openInputStream(fileUri)) {
int read;
while ((read = inputStream.read(buffer)) > 0) {
offset += read;
double progress = 1.0 * offset / filesize;
Request request = new Request.Builder().url(url + "&progress=" + progress).header("Cookie", cookie).post(RequestBody.create(contentType, buffer, 0, read)).build();
Response response = OkHttpHelper.getClientForUploadFile().newCall(request).execute();
if (response.isSuccessful()) {
final JSONObject obj = new JSONObject().put(FileUploading.ID, uplId).put(FileUploading.UPLOADED_SIZE, offset);
realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(FileUploading.class, obj));
} else {
return Task.forError(new Exception(response.message()));
}
}
}
return methodCall.ufsComplete(fileId, token, store);
}).onSuccessTask(task -> methodCall.sendFileMessage(roomId, null, task.getResult())).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.persistence.realm.models.internal.RealmSession in project Rocket.Chat.Android by RocketChat.
the class TokenLoginObserver method onUpdateResults.
@Override
public void onUpdateResults(List<RealmSession> results) {
if (results.isEmpty()) {
return;
}
RealmSession session = results.get(0);
methodCall.loginWithToken(session.getToken()).continueWith(new LogIfError());
}
use of chat.rocket.persistence.realm.models.internal.RealmSession in project Rocket.Chat.Android by RocketChat.
the class RocketChatWebSocketThread method connectDDPClient.
private Single<Boolean> connectDDPClient() {
return prepareDDPClient().flatMap(_val -> Single.fromEmitter(emitter -> {
ServerInfo info = connectivityManager.getServerInfoForHost(hostname);
RCLog.d("DDPClient#connect");
ddpClient.connect(info.getSession(), info.isSecure()).onSuccessTask(task -> {
final String newSession = task.getResult().session;
connectivityManager.notifyConnectionEstablished(hostname, newSession);
task.getResult().client.getOnCloseCallback().onSuccess(_task -> {
if (listenersRegistered) {
terminate();
}
return null;
});
return realmHelper.executeTransaction(realm -> {
RealmSession sessionObj = RealmSession.queryDefaultSession(realm).findFirst();
if (sessionObj == null) {
realm.createOrUpdateObjectFromJson(RealmSession.class, new JSONObject().put(RealmSession.ID, RealmSession.DEFAULT_ID));
} else {
if (!TextUtils.isEmpty(sessionObj.getToken()) && sessionObj.isTokenVerified()) {
sessionObj.setTokenVerified(false);
sessionObj.setError(null);
}
}
return null;
});
}).continueWith(task -> {
if (task.isFaulted()) {
emitter.onError(task.getError());
} else {
emitter.onSuccess(true);
}
return null;
});
}));
}
use of chat.rocket.persistence.realm.models.internal.RealmSession in project Rocket.Chat.Android by RocketChat.
the class DefaultCookieProvider method getCookie.
@Override
public String getCookie() {
final String hostname = getHostnameFromCache();
if (hostname == null) {
return "";
}
final RealmHelper realmHelper = RealmStore.get(getHostnameFromCache());
if (realmHelper == null) {
return "";
}
final RealmUser user = realmHelper.executeTransactionForRead(realm -> RealmUser.queryCurrentUser(realm).findFirst());
final RealmSession session = realmHelper.executeTransactionForRead(realm -> RealmSession.queryDefaultSession(realm).findFirst());
if (user == null || session == null) {
return "";
}
return "rc_uid=" + user.getId() + ";rc_token=" + session.getToken();
}
Aggregations