Unverified Commit 449e639e authored by Leonardo Aramaki's avatar Leonardo Aramaki Committed by GitHub

Merge pull request #677 from RocketChat/fixes

Fix crash when caching username and login failure when LDAP_Enable setting doesn't exist
parents a4fb6a00 9c64b6c2
......@@ -408,7 +408,11 @@ object RocketChatCache {
}, BackpressureStrategy.LATEST)
}
fun setUserId(userId: String) = setString(KEY_USER_ID, userId)
fun setUserId(userId: String?) {
userId?.let {
setString(KEY_USER_ID, userId)
}
}
fun getUserId(): String? = getString(KEY_USER_ID, null)
......@@ -420,7 +424,11 @@ object RocketChatCache {
fun getUserName(): String? = getString(KEY_USER_NAME, null)
fun setUserUsername(username: String) = setString(KEY_USER_USERNAME, username)
fun setUserUsername(username: String?) {
username?.let {
setString(KEY_USER_USERNAME, username)
}
}
fun getUserUsername(): String? = getString(KEY_USER_USERNAME, null)
}
\ No newline at end of file
......@@ -20,68 +20,71 @@ import io.realm.RealmResults;
* observe the user with emails.
*/
public class CurrentUserObserver extends AbstractModelObserver<RealmUser> {
private final MethodCallHelper methodCall;
private boolean currentUserExists;
private ArrayList<Registrable> listeners;
private final MethodCallHelper methodCall;
private boolean currentUserExists;
private ArrayList<Registrable> listeners;
public CurrentUserObserver(Context context, String hostname,
RealmHelper realmHelper) {
super(context, hostname, realmHelper);
methodCall = new MethodCallHelper(realmHelper);
currentUserExists = false;
}
public CurrentUserObserver(Context context, String hostname,
RealmHelper realmHelper) {
super(context, hostname, realmHelper);
methodCall = new MethodCallHelper(realmHelper);
currentUserExists = false;
}
@Override
public RealmResults<RealmUser> queryItems(Realm realm) {
return RealmUser.queryCurrentUser(realm).findAll();
}
@Override
public RealmResults<RealmUser> queryItems(Realm realm) {
return RealmUser.queryCurrentUser(realm).findAll();
}
@Override
public void onUpdateResults(List<RealmUser> results) {
boolean exists = !results.isEmpty();
@Override
public void onUpdateResults(List<RealmUser> results) {
boolean exists = !results.isEmpty();
if (currentUserExists != exists) {
if (exists) {
onLogin(results.get(0));
} else {
onLogout();
}
currentUserExists = exists;
if (currentUserExists != exists) {
if (exists) {
onLogin(results.get(0));
} else {
onLogout();
}
currentUserExists = exists;
}
}
}
@DebugLog
private void onLogin(RealmUser user) {
if (listeners != null) {
onLogout();
}
listeners = new ArrayList<>();
@DebugLog
private void onLogin(RealmUser user) {
if (listeners != null) {
onLogout();
}
listeners = new ArrayList<>();
RocketChatCache.INSTANCE.setUserId(user.getId());
RocketChatCache.INSTANCE.setUserUsername(user.getUsername());
RocketChatCache.INSTANCE.setUserName(user.getName());
String userId = user.getId();
String username = user.getUsername();
String name = user.getName();
final String userId = user.getId();
// get and observe Room subscriptions.
methodCall.getRoomSubscriptions().onSuccess(task -> {
if (listeners != null) {
Registrable listener = new StreamNotifyUserSubscriptionsChanged(
context, hostname, realmHelper, userId);
listener.register();
listeners.add(listener);
}
return null;
}).continueWith(new LogIfError());
}
RocketChatCache.INSTANCE.setUserId(userId);
RocketChatCache.INSTANCE.setUserUsername(username);
RocketChatCache.INSTANCE.setUserName(name);
// get and observe Room subscriptions.
methodCall.getRoomSubscriptions().onSuccess(task -> {
if (listeners != null) {
Registrable listener = new StreamNotifyUserSubscriptionsChanged(
context, hostname, realmHelper, userId);
listener.register();
listeners.add(listener);
}
return null;
}).continueWith(new LogIfError());
}
@DebugLog
private void onLogout() {
if (listeners != null) {
for (Registrable listener : listeners) {
listener.unregister();
}
@DebugLog
private void onLogout() {
if (listeners != null) {
for (Registrable listener : listeners) {
listener.unregister();
}
}
listeners = null;
}
listeners = null;
}
}
......@@ -65,7 +65,10 @@ public class GcmPushRegistrationObserver extends AbstractModelObserver<GcmPushRe
).continueWith(task -> {
if (task.isFaulted()) {
realmHelper.executeTransaction(realm -> {
GcmPushRegistration.queryDefault(realm).findFirst().setSyncState(SyncState.FAILED);
GcmPushRegistration gcmPushRegistration = GcmPushRegistration.queryDefault(realm).findFirst();
if (gcmPushRegistration != null) {
gcmPushRegistration.setSyncState(SyncState.FAILED);
}
return null;
}).continueWith(new LogIfError());
}
......
......@@ -15,32 +15,46 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
import io.realm.RealmResults;
public class RealmPublicSettingRepository extends RealmRepository
implements PublicSettingRepository {
private final String hostname;
public RealmPublicSettingRepository(String hostname) {
this.hostname = hostname;
}
@Override
public Single<Optional<PublicSetting>> getById(String id) {
return Single.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> {
if (pair.first == null) {
return Flowable.empty();
}
return pair.first.where(RealmPublicSetting.class)
.equalTo(RealmPublicSetting.ID, id)
.findAll()
.<RealmResults<RealmPublicSetting>>asFlowable();
},
pair -> close(pair.first, pair.second)
)
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
.filter(it -> it.isLoaded() && it.isValid() && it.size() > 0)
.map(it -> Optional.of(it.get(0).asPublicSetting()))
.first(Optional.absent()));
}
implements PublicSettingRepository {
private final String hostname;
public RealmPublicSettingRepository(String hostname) {
this.hostname = hostname;
}
@Override
public Single<Optional<PublicSetting>> getById(String id) {
return Single.defer(() -> Flowable.using(
() -> new Pair<>(RealmStore.getRealm(hostname), Looper.myLooper()),
pair -> {
if (pair.first == null) {
return Flowable.empty();
}
return pair.first.where(RealmPublicSetting.class)
.equalTo(RealmPublicSetting.ID, id)
.findAll()
.<RealmResults<RealmPublicSetting>>asFlowable();
},
pair -> close(pair.first, pair.second)
)
.unsubscribeOn(AndroidSchedulers.from(Looper.myLooper()))
.filter(it -> it.isLoaded() && it.isValid())
.map(it -> getPublicSettingOrDefault(id, it))
.first(Optional.absent()));
}
private Optional<PublicSetting> getPublicSettingOrDefault(String id, RealmResults<RealmPublicSetting> results) {
if (results.size() > 0) {
return Optional.of(results.get(0).asPublicSetting());
}
PublicSetting defaultSetting = PublicSetting.builder()
.setId(id)
.setValue("")
.setUpdatedAt(0L)
.build();
return Optional.of(defaultSetting);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment