2020-07-13 18:49:29 +02:00
|
|
|
package org.mercury_im.messenger.core.viewmodel.account.detail;
|
2020-07-10 14:15:58 +02:00
|
|
|
|
|
|
|
import org.jivesoftware.smackx.ox.store.definition.OpenPgpTrustStore;
|
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
|
|
import org.mercury_im.messenger.core.SchedulersFacade;
|
|
|
|
import org.mercury_im.messenger.core.data.repository.AccountRepository;
|
|
|
|
import org.mercury_im.messenger.core.data.repository.OpenPgpRepository;
|
|
|
|
import org.mercury_im.messenger.core.util.Optional;
|
|
|
|
import org.mercury_im.messenger.core.viewmodel.MercuryViewModel;
|
2020-07-13 18:49:29 +02:00
|
|
|
import org.mercury_im.messenger.core.viewmodel.openpgp.FingerprintViewItem;
|
2020-07-10 14:15:58 +02:00
|
|
|
import org.mercury_im.messenger.entity.Account;
|
|
|
|
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
|
|
|
|
2020-07-14 19:47:49 +02:00
|
|
|
import java.util.ArrayList;
|
2020-07-10 14:15:58 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import io.reactivex.Observable;
|
|
|
|
import io.reactivex.Single;
|
|
|
|
|
|
|
|
public class AccountDetailsViewModel implements MercuryViewModel {
|
|
|
|
|
|
|
|
private final OpenPgpRepository openPgpRepository;
|
|
|
|
private final AccountRepository accountRepository;
|
|
|
|
private final SchedulersFacade schedulers;
|
|
|
|
|
|
|
|
public AccountDetailsViewModel(OpenPgpRepository openPgpRepository, AccountRepository accountRepository, SchedulersFacade schedulers) {
|
|
|
|
this.openPgpRepository = openPgpRepository;
|
|
|
|
this.accountRepository = accountRepository;
|
|
|
|
this.schedulers = schedulers;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void markFingerprintTrusted(UUID accountId, OpenPgpV4Fingerprint fingerprint, boolean trusted) {
|
|
|
|
addDisposable(openPgpRepository.storeTrust(accountId, fingerprint, trusted ? OpenPgpTrustStore.Trust.trusted : OpenPgpTrustStore.Trust.untrusted)
|
|
|
|
.compose(schedulers.executeUiSafeCompletable())
|
|
|
|
.subscribe());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Single<EntityBareJid> getJid(UUID accountId) {
|
|
|
|
return accountRepository.getAccount(accountId).toSingle()
|
|
|
|
.compose(schedulers.executeUiSafeSingle())
|
|
|
|
.map(Account::getJid);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Observable<Optional<OpenPgpV4Fingerprint>> observeLocalFingerprint(UUID accountId) {
|
|
|
|
return openPgpRepository.observeLocalFingerprintOf(accountId);
|
|
|
|
}
|
|
|
|
|
2020-07-13 14:43:30 +02:00
|
|
|
public Observable<List<FingerprintViewItem>> observeRemoteFingerprints(UUID accountId) {
|
2020-07-14 19:47:49 +02:00
|
|
|
return observeLocalFingerprint(accountId)
|
|
|
|
.flatMap(optional -> accountRepository.getAccount(accountId).toSingle()
|
|
|
|
.flatMapObservable(account -> openPgpRepository.observeFingerprints(accountId, account.getJid())
|
|
|
|
.map(list -> {
|
|
|
|
if (!optional.isPresent()) {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<FingerprintViewItem> remoteFingerprints = new ArrayList<>();
|
|
|
|
for(FingerprintViewItem f : list) {
|
|
|
|
if (!f.getFingerprint().equals(optional.getItem())) {
|
|
|
|
remoteFingerprints.add(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return remoteFingerprints;
|
|
|
|
})));
|
2020-07-10 14:15:58 +02:00
|
|
|
}
|
|
|
|
}
|