54 lines
2.1 KiB
Java
54 lines
2.1 KiB
Java
package org.mercury_im.messenger.data.repository;
|
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
import org.mercury_im.messenger.core.data.repository.OpenPgpRepository;
|
|
import org.mercury_im.messenger.data.model.OpenPgpPublicKeyRing;
|
|
import org.mercury_im.messenger.data.model.OpenPgpSecretKeyRing;
|
|
import org.pgpainless.PGPainless;
|
|
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
|
|
|
import java.util.UUID;
|
|
|
|
import io.reactivex.Single;
|
|
import io.requery.Persistable;
|
|
import io.requery.query.ResultDelegate;
|
|
import io.requery.reactivex.ReactiveEntityStore;
|
|
|
|
public class RxOpenPgpRepository implements OpenPgpRepository {
|
|
|
|
private final ReactiveEntityStore<Persistable> data;
|
|
|
|
public RxOpenPgpRepository(ReactiveEntityStore<Persistable> data) {
|
|
this.data = data;
|
|
}
|
|
|
|
@Override
|
|
public Single<PGPPublicKeyRingCollection> loadPublicKeysOfContact(UUID accountId, EntityBareJid jid) {
|
|
return data.select(OpenPgpPublicKeyRing.class)
|
|
.where(OpenPgpPublicKeyRing.ACCOUNT_ID.eq(accountId))
|
|
.and(OpenPgpPublicKeyRing.OWNER.eq(jid))
|
|
.get().observableResult()
|
|
.map(ResultDelegate::toList)
|
|
.map(keys -> PGPainless.readKeyRing().publicKeyRing(keys.get(0).getBytes()).)
|
|
}
|
|
|
|
@Override
|
|
public Single<Integer> deletePublicKeyRing(UUID accountId, EntityBareJid jid, OpenPgpV4Fingerprint fingerprint) {
|
|
return data.delete(OpenPgpPublicKeyRing.class)
|
|
.where(OpenPgpPublicKeyRing.ACCOUNT_ID.eq(accountId))
|
|
.and(OpenPgpPublicKeyRing.OWNER.eq(jid))
|
|
.and(OpenPgpPublicKeyRing.FINGERPRINT.eq(fingerprint))
|
|
.get().single();
|
|
}
|
|
|
|
@Override
|
|
public Single<Integer> deleteSecretKeyRing(UUID accountId, EntityBareJid jid, OpenPgpV4Fingerprint fingerprint) {
|
|
return data.delete(OpenPgpSecretKeyRing.class)
|
|
.where(OpenPgpSecretKeyRing.ACCOUNT_ID.eq(accountId))
|
|
.and(OpenPgpPublicKeyRing.OWNER.eq(jid))
|
|
.and(OpenPgpSecretKeyRing.FINGERPRINT.eq(fingerprint))
|
|
.get().single();
|
|
}
|
|
}
|