From: AsamK Date: Mon, 23 May 2022 11:18:33 +0000 (+0200) Subject: Reduce direct use of recipient store X-Git-Tag: v0.10.7~19 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/3abb641c7c554bf8a719b0a47a494aee1f9d8eba Reduce direct use of recipient store --- diff --git a/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java b/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java index 96196f8f..19ff8b54 100644 --- a/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java +++ b/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java @@ -168,9 +168,9 @@ class ManagerImpl implements Manager { logger.trace("Archiving old sessions for {}", recipientId); account.getSessionStore().archiveSessions(recipientId); account.getSenderKeyStore().deleteSharedWith(recipientId); - final var profile = account.getRecipientStore().getProfile(recipientId); + final var profile = account.getProfileStore().getProfile(recipientId); if (profile != null) { - account.getRecipientStore() + account.getProfileStore() .storeProfile(recipientId, Profile.newBuilder(profile) .withUnidentifiedAccessMode(Profile.UnidentifiedAccessMode.UNKNOWN) @@ -215,7 +215,8 @@ class ManagerImpl implements Manager { final var aci = registeredUsers.get(number); final var profile = aci == null ? null - : context.getProfileHelper().getRecipientProfile(account.getRecipientStore().resolveRecipient(aci)); + : context.getProfileHelper() + .getRecipientProfile(account.getRecipientResolver().resolveRecipient(aci)); return new UserStatus(number.isEmpty() ? null : number, aci == null ? null : aci.uuid(), profile != null @@ -356,9 +357,7 @@ class ManagerImpl implements Manager { return null; } - return Group.from(groupInfo, - account.getRecipientStore()::resolveRecipientAddress, - account.getSelfRecipientId()); + return Group.from(groupInfo, account.getRecipientAddressResolver(), account.getSelfRecipientId()); } @Override @@ -459,9 +458,7 @@ class ManagerImpl implements Manager { } private SendMessageResult toSendMessageResult(final org.whispersystems.signalservice.api.messages.SendMessageResult result) { - return SendMessageResult.from(result, - account.getRecipientStore(), - account.getRecipientStore()::resolveRecipientAddress); + return SendMessageResult.from(result, account.getRecipientResolver(), account.getRecipientAddressResolver()); } private SendMessageResults sendTypingMessage( @@ -683,13 +680,13 @@ class ManagerImpl implements Manager { @Override public void deleteRecipient(final RecipientIdentifier.Single recipient) { - account.removeRecipient(account.getRecipientStore().resolveRecipient(recipient.toPartialRecipientAddress())); + account.removeRecipient(account.getRecipientResolver().resolveRecipient(recipient.toPartialRecipientAddress())); } @Override public void deleteContact(final RecipientIdentifier.Single recipient) { account.getContactStore() - .deleteContact(account.getRecipientStore().resolveRecipient(recipient.toPartialRecipientAddress())); + .deleteContact(account.getRecipientResolver().resolveRecipient(recipient.toPartialRecipientAddress())); } @Override @@ -1018,7 +1015,8 @@ class ManagerImpl implements Manager { return null; } - final var address = account.getRecipientStore().resolveRecipientAddress(identityInfo.getRecipientId()); + final var address = account.getRecipientAddressResolver() + .resolveRecipientAddress(identityInfo.getRecipientId()); final var scannableFingerprint = context.getIdentityHelper() .computeSafetyNumberForScanning(identityInfo.getRecipientId(), identityInfo.getIdentityKey()); return new Identity(address, diff --git a/lib/src/main/java/org/asamk/signal/manager/helper/AccountHelper.java b/lib/src/main/java/org/asamk/signal/manager/helper/AccountHelper.java index b75cce52..224ac4da 100644 --- a/lib/src/main/java/org/asamk/signal/manager/helper/AccountHelper.java +++ b/lib/src/main/java/org/asamk/signal/manager/helper/AccountHelper.java @@ -86,7 +86,7 @@ public class AccountHelper { account.setNumber(number); account.setAci(aci); account.setPni(pni); - account.getRecipientStore().resolveSelfRecipientTrusted(account.getSelfRecipientAddress()); + account.getRecipientTrustedResolver().resolveSelfRecipientTrusted(account.getSelfRecipientAddress()); // TODO check and update remote storage context.getUnidentifiedAccessHelper().rotateSenderCertificates(); dependencies.resetAfterAddressChange(); diff --git a/lib/src/main/java/org/asamk/signal/manager/helper/GroupHelper.java b/lib/src/main/java/org/asamk/signal/manager/helper/GroupHelper.java index 9346372c..f4e92320 100644 --- a/lib/src/main/java/org/asamk/signal/manager/helper/GroupHelper.java +++ b/lib/src/main/java/org/asamk/signal/manager/helper/GroupHelper.java @@ -153,7 +153,7 @@ public class GroupHelper { downloadGroupAvatar(groupId, groupSecretParams, avatar); } } - groupInfoV2.setGroup(group, account.getRecipientStore()); + groupInfoV2.setGroup(group, account.getRecipientResolver()); account.getGroupStore().updateGroup(groupInfoV2); } @@ -183,7 +183,7 @@ public class GroupHelper { final var gv2 = gv2Pair.first(); final var decryptedGroup = gv2Pair.second(); - gv2.setGroup(decryptedGroup, account.getRecipientStore()); + gv2.setGroup(decryptedGroup, account.getRecipientResolver()); if (avatarFile != null) { context.getAvatarStore() .storeGroupAvatar(gv2.getGroupId(), @@ -398,7 +398,7 @@ public class GroupHelper { downloadGroupAvatar(groupInfoV2.getGroupId(), groupSecretParams, avatar); } } - groupInfoV2.setGroup(decryptedGroup, account.getRecipientStore()); + groupInfoV2.setGroup(decryptedGroup, account.getRecipientResolver()); account.getGroupStore().updateGroup(group); } } @@ -441,7 +441,7 @@ public class GroupHelper { private void storeProfileKeysFromMembers(final DecryptedGroup group) { for (var member : group.getMembersList()) { final var serviceId = ServiceId.fromByteString(member.getUuid()); - final var recipientId = account.getRecipientStore().resolveRecipient(serviceId); + final var recipientId = account.getRecipientResolver().resolveRecipient(serviceId); final var profileStore = account.getProfileStore(); if (profileStore.getProfileKey(recipientId) != null) { // We already have a profile key, not updating it from a non-authoritative source @@ -461,7 +461,7 @@ public class GroupHelper { if (profileKeyFromChange != null) { final var serviceId = profileKeyFromChange.first(); final var profileKey = profileKeyFromChange.second(); - final var recipientId = account.getRecipientStore().resolveRecipient(serviceId); + final var recipientId = account.getRecipientResolver().resolveRecipient(serviceId); account.getProfileStore().storeProfileKey(recipientId, profileKey); } } @@ -487,7 +487,7 @@ public class GroupHelper { .forEach(p -> { final var serviceId = p.first(); final var profileKey = p.second(); - final var recipientId = account.getRecipientStore().resolveRecipient(serviceId); + final var recipientId = account.getRecipientResolver().resolveRecipient(serviceId); newProfileKeys.put(recipientId, profileKey); }); if (!page.getPagingData().hasMorePages()) { @@ -729,7 +729,7 @@ public class GroupHelper { throw new LastGroupAdminException(groupInfoV2.getGroupId(), groupInfoV2.getTitle()); } final var groupGroupChangePair = context.getGroupV2Helper().leaveGroup(groupInfoV2, newAdmins); - groupInfoV2.setGroup(groupGroupChangePair.first(), account.getRecipientStore()); + groupInfoV2.setGroup(groupGroupChangePair.first(), account.getRecipientResolver()); account.getGroupStore().updateGroup(groupInfoV2); var messageBuilder = getGroupUpdateMessageBuilder(groupInfoV2, groupGroupChangePair.second().toByteArray()); @@ -773,7 +773,7 @@ public class GroupHelper { ) throws IOException { final var selfRecipientId = account.getSelfRecipientId(); final var members = group.getMembersIncludingPendingWithout(selfRecipientId); - group.setGroup(newDecryptedGroup, account.getRecipientStore()); + group.setGroup(newDecryptedGroup, account.getRecipientResolver()); members.addAll(group.getMembersIncludingPendingWithout(selfRecipientId)); account.getGroupStore().updateGroup(group); @@ -792,8 +792,8 @@ public class GroupHelper { return new SendGroupMessageResults(timestamp, results.stream() .map(sendMessageResult -> SendMessageResult.from(sendMessageResult, - account.getRecipientStore(), - account.getRecipientStore()::resolveRecipientAddress)) + account.getRecipientResolver(), + account.getRecipientAddressResolver())) .toList()); } } diff --git a/lib/src/main/java/org/asamk/signal/manager/helper/IncomingMessageHandler.java b/lib/src/main/java/org/asamk/signal/manager/helper/IncomingMessageHandler.java index 5e310a48..b28c21e6 100644 --- a/lib/src/main/java/org/asamk/signal/manager/helper/IncomingMessageHandler.java +++ b/lib/src/main/java/org/asamk/signal/manager/helper/IncomingMessageHandler.java @@ -89,8 +89,8 @@ public final class IncomingMessageHandler { try { content = dependencies.getCipher().decrypt(envelope); } catch (ProtocolUntrustedIdentityException e) { - final var recipientId = account.getRecipientStore().resolveRecipient(e.getSender()); - final var exception = new UntrustedIdentityException(account.getRecipientStore() + final var recipientId = account.getRecipientResolver().resolveRecipient(e.getSender()); + final var exception = new UntrustedIdentityException(account.getRecipientAddressResolver() .resolveRecipientAddress(recipientId), e.getSenderDevice()); return new Pair<>(List.of(), exception); } catch (Exception e) { @@ -112,7 +112,7 @@ public final class IncomingMessageHandler { if (envelope.hasSourceUuid()) { // Store uuid if we don't have it already // address/uuid in envelope is sent by server - account.getRecipientStore().resolveRecipientTrusted(envelope.getSourceAddress()); + account.getRecipientTrustedResolver().resolveRecipientTrusted(envelope.getSourceAddress()); } SignalServiceContent content = null; Exception exception = null; @@ -120,14 +120,14 @@ public final class IncomingMessageHandler { try { content = dependencies.getCipher().decrypt(envelope); } catch (ProtocolUntrustedIdentityException e) { - final var recipientId = account.getRecipientStore().resolveRecipient(e.getSender()); + final var recipientId = account.getRecipientResolver().resolveRecipient(e.getSender()); actions.add(new RetrieveProfileAction(recipientId)); - exception = new UntrustedIdentityException(account.getRecipientStore() + exception = new UntrustedIdentityException(account.getRecipientAddressResolver() .resolveRecipientAddress(recipientId), e.getSenderDevice()); } catch (ProtocolInvalidKeyIdException | ProtocolInvalidKeyException | ProtocolNoSessionException | ProtocolInvalidMessageException e) { logger.debug("Failed to decrypt incoming message", e); - final var sender = account.getRecipientStore().resolveRecipient(e.getSender()); + final var sender = account.getRecipientResolver().resolveRecipient(e.getSender()); if (context.getContactHelper().isContactBlocked(sender)) { logger.debug("Received invalid message from blocked contact, ignoring."); } else { @@ -169,7 +169,7 @@ public final class IncomingMessageHandler { if (!envelope.hasSourceUuid() && content != null) { // Store uuid if we don't have it already // address/uuid is validated by unidentified sender certificate - account.getRecipientStore().resolveRecipientTrusted(content.getSender()); + account.getRecipientTrustedResolver().resolveRecipientTrusted(content.getSender()); } if (envelope.isReceipt()) { final var senderPair = getSender(envelope, content); @@ -195,8 +195,8 @@ public final class IncomingMessageHandler { } handler.handleMessage(MessageEnvelope.from(envelope, content, - account.getRecipientStore(), - account.getRecipientStore()::resolveRecipientAddress, + account.getRecipientResolver(), + account.getRecipientAddressResolver(), context.getAttachmentHelper()::getAttachmentFile, exception), exception); return actions; @@ -391,7 +391,7 @@ public final class IncomingMessageHandler { if (syncMessage.getVerified().isPresent()) { final var verifiedMessage = syncMessage.getVerified().get(); account.getIdentityKeyStore() - .setIdentityTrustLevel(account.getRecipientStore() + .setIdentityTrustLevel(account.getRecipientTrustedResolver() .resolveRecipientTrusted(verifiedMessage.getDestination()), verifiedMessage.getIdentityKey(), TrustLevel.fromVerifiedState(verifiedMessage.getVerified())); diff --git a/lib/src/main/java/org/asamk/signal/manager/helper/ProfileHelper.java b/lib/src/main/java/org/asamk/signal/manager/helper/ProfileHelper.java index cc01dc73..38788278 100644 --- a/lib/src/main/java/org/asamk/signal/manager/helper/ProfileHelper.java +++ b/lib/src/main/java/org/asamk/signal/manager/helper/ProfileHelper.java @@ -289,7 +289,7 @@ public final class ProfileHelper { var profile = account.getProfileStore().getProfile(recipientId); if (profile == null || !Objects.equals(avatarPath, profile.getAvatarUrlPath())) { logger.trace("Downloading profile avatar for {}", recipientId); - downloadProfileAvatar(account.getRecipientStore().resolveRecipientAddress(recipientId), + downloadProfileAvatar(account.getRecipientAddressResolver().resolveRecipientAddress(recipientId), avatarPath, profileKey); var builder = profile == null ? Profile.newBuilder() : Profile.newBuilder(profile); diff --git a/lib/src/main/java/org/asamk/signal/manager/helper/ReceiveHelper.java b/lib/src/main/java/org/asamk/signal/manager/helper/ReceiveHelper.java index 7bc2c224..7083caae 100644 --- a/lib/src/main/java/org/asamk/signal/manager/helper/ReceiveHelper.java +++ b/lib/src/main/java/org/asamk/signal/manager/helper/ReceiveHelper.java @@ -141,7 +141,7 @@ public class ReceiveHelper { isWaitingForMessage = true; var result = signalWebSocket.readOrEmpty(timeout.toMillis(), envelope1 -> { isWaitingForMessage = false; - final var recipientId = envelope1.hasSourceUuid() ? account.getRecipientStore() + final var recipientId = envelope1.hasSourceUuid() ? account.getRecipientResolver() .resolveRecipient(envelope1.getSourceAddress()) : null; logger.trace("Storing new message from {}", recipientId); // store message on disk, before acknowledging receipt to the server @@ -211,7 +211,7 @@ public class ReceiveHelper { if (exception instanceof UntrustedIdentityException) { logger.debug("Keeping message with untrusted identity in message cache"); final var address = ((UntrustedIdentityException) exception).getSender(); - final var recipientId = account.getRecipientStore().resolveRecipient(address); + final var recipientId = account.getRecipientResolver().resolveRecipient(address); if (!envelope.hasSourceUuid()) { try { cachedMessage[0] = account.getMessageCache().replaceSender(cachedMessage[0], recipientId); @@ -260,7 +260,7 @@ public class ReceiveHelper { } if (!envelope.hasSourceUuid()) { final var identifier = ((UntrustedIdentityException) exception).getSender(); - final var recipientId = account.getRecipientStore().resolveRecipient(identifier); + final var recipientId = account.getRecipientResolver().resolveRecipient(identifier); try { account.getMessageCache().replaceSender(cachedMessage, recipientId); } catch (IOException ioException) { diff --git a/lib/src/main/java/org/asamk/signal/manager/helper/RecipientHelper.java b/lib/src/main/java/org/asamk/signal/manager/helper/RecipientHelper.java index c253d602..2f254b5d 100644 --- a/lib/src/main/java/org/asamk/signal/manager/helper/RecipientHelper.java +++ b/lib/src/main/java/org/asamk/signal/manager/helper/RecipientHelper.java @@ -40,7 +40,7 @@ public class RecipientHelper { } public SignalServiceAddress resolveSignalServiceAddress(RecipientId recipientId) { - final var address = account.getRecipientStore().resolveRecipientAddress(recipientId); + final var address = account.getRecipientAddressResolver().resolveRecipientAddress(recipientId); if (address.number().isEmpty() || address.uuid().isPresent()) { return address.toSignalServiceAddress(); } @@ -60,13 +60,13 @@ public class RecipientHelper { // Return SignalServiceAddress with unknown UUID return address.toSignalServiceAddress(); } - return account.getRecipientStore() - .resolveRecipientAddress(account.getRecipientStore().resolveRecipient(aci)) + return account.getRecipientAddressResolver() + .resolveRecipientAddress(account.getRecipientResolver().resolveRecipient(aci)) .toSignalServiceAddress(); } public RecipientId resolveRecipient(final SignalServiceAddress address) { - return account.getRecipientStore().resolveRecipient(address); + return account.getRecipientResolver().resolveRecipient(address); } public Set resolveRecipients(Collection recipients) throws UnregisteredRecipientException { @@ -80,7 +80,7 @@ public class RecipientHelper { public RecipientId resolveRecipient(final RecipientIdentifier.Single recipient) throws UnregisteredRecipientException { if (recipient instanceof RecipientIdentifier.Uuid uuidRecipient) { - return account.getRecipientStore().resolveRecipient(ServiceId.from(uuidRecipient.uuid())); + return account.getRecipientResolver().resolveRecipient(ServiceId.from(uuidRecipient.uuid())); } else { final var number = ((RecipientIdentifier.Number) recipient).number(); return account.getRecipientStore().resolveRecipient(number, () -> { @@ -100,7 +100,7 @@ public class RecipientHelper { } final var number = address.getNumber().get(); final var uuid = getRegisteredUser(number); - return account.getRecipientStore().resolveRecipientTrusted(new SignalServiceAddress(uuid, number)); + return account.getRecipientTrustedResolver().resolveRecipientTrusted(new SignalServiceAddress(uuid, number)); } public Map getRegisteredUsers(final Set numbers) throws IOException { @@ -116,7 +116,7 @@ public class RecipientHelper { } // Store numbers as recipients, so we have the number/uuid association - registeredUsers.forEach((number, aci) -> account.getRecipientStore() + registeredUsers.forEach((number, aci) -> account.getRecipientTrustedResolver() .resolveRecipientTrusted(new SignalServiceAddress(aci, number))); return registeredUsers; diff --git a/lib/src/main/java/org/asamk/signal/manager/helper/SendHelper.java b/lib/src/main/java/org/asamk/signal/manager/helper/SendHelper.java index 8b2a054e..85d5cc32 100644 --- a/lib/src/main/java/org/asamk/signal/manager/helper/SendHelper.java +++ b/lib/src/main/java/org/asamk/signal/manager/helper/SendHelper.java @@ -656,12 +656,12 @@ public class SendHelper { private void handleSendMessageResult(final SendMessageResult r) { if (r.isSuccess() && !r.getSuccess().isUnidentified()) { final var recipientId = context.getRecipientHelper().resolveRecipient(r.getAddress()); - final var profile = account.getRecipientStore().getProfile(recipientId); + final var profile = account.getProfileStore().getProfile(recipientId); if (profile != null && ( profile.getUnidentifiedAccessMode() == Profile.UnidentifiedAccessMode.ENABLED || profile.getUnidentifiedAccessMode() == Profile.UnidentifiedAccessMode.UNRESTRICTED )) { - account.getRecipientStore() + account.getProfileStore() .storeProfile(recipientId, Profile.newBuilder(profile) .withUnidentifiedAccessMode(Profile.UnidentifiedAccessMode.UNKNOWN) @@ -670,12 +670,12 @@ public class SendHelper { } if (r.isUnregisteredFailure()) { final var recipientId = context.getRecipientHelper().resolveRecipient(r.getAddress()); - final var profile = account.getRecipientStore().getProfile(recipientId); + final var profile = account.getProfileStore().getProfile(recipientId); if (profile != null && ( profile.getUnidentifiedAccessMode() == Profile.UnidentifiedAccessMode.ENABLED || profile.getUnidentifiedAccessMode() == Profile.UnidentifiedAccessMode.UNRESTRICTED )) { - account.getRecipientStore() + account.getProfileStore() .storeProfile(recipientId, Profile.newBuilder(profile) .withUnidentifiedAccessMode(Profile.UnidentifiedAccessMode.UNKNOWN) diff --git a/lib/src/main/java/org/asamk/signal/manager/helper/StorageHelper.java b/lib/src/main/java/org/asamk/signal/manager/helper/StorageHelper.java index 88fe84b9..b1a653fd 100644 --- a/lib/src/main/java/org/asamk/signal/manager/helper/StorageHelper.java +++ b/lib/src/main/java/org/asamk/signal/manager/helper/StorageHelper.java @@ -86,7 +86,7 @@ public class StorageHelper { final var contactRecord = record.getContact().get(); final var address = contactRecord.getAddress(); - final var recipientId = account.getRecipientStore().resolveRecipient(address); + final var recipientId = account.getRecipientResolver().resolveRecipient(address); final var contact = account.getContactStore().getContact(recipientId); final var blocked = contact != null && contact.isBlocked(); final var profileShared = contact != null && contact.isProfileSharingEnabled(); diff --git a/lib/src/main/java/org/asamk/signal/manager/helper/SyncHelper.java b/lib/src/main/java/org/asamk/signal/manager/helper/SyncHelper.java index 86fb0e66..d98f9eef 100644 --- a/lib/src/main/java/org/asamk/signal/manager/helper/SyncHelper.java +++ b/lib/src/main/java/org/asamk/signal/manager/helper/SyncHelper.java @@ -257,7 +257,7 @@ public class SyncHelper { if (c.getAddress().matches(account.getSelfAddress()) && c.getProfileKey().isPresent()) { account.setProfileKey(c.getProfileKey().get()); } - final var recipientId = account.getRecipientStore().resolveRecipientTrusted(c.getAddress()); + final var recipientId = account.getRecipientTrustedResolver().resolveRecipientTrusted(c.getAddress()); var contact = account.getContactStore().getContact(recipientId); final var builder = contact == null ? Contact.newBuilder() : Contact.newBuilder(contact); if (c.getName().isPresent()) { @@ -272,7 +272,7 @@ public class SyncHelper { if (c.getVerified().isPresent()) { final var verifiedMessage = c.getVerified().get(); account.getIdentityKeyStore() - .setIdentityTrustLevel(account.getRecipientStore() + .setIdentityTrustLevel(account.getRecipientTrustedResolver() .resolveRecipientTrusted(verifiedMessage.getDestination()), verifiedMessage.getIdentityKey(), TrustLevel.fromVerifiedState(verifiedMessage.getVerified())); diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java b/lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java index ac41d791..521e21d9 100644 --- a/lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java +++ b/lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java @@ -7,6 +7,7 @@ import org.asamk.signal.manager.api.Pair; import org.asamk.signal.manager.api.TrustLevel; import org.asamk.signal.manager.config.ServiceEnvironment; import org.asamk.signal.manager.groups.GroupId; +import org.asamk.signal.manager.helper.RecipientAddressResolver; import org.asamk.signal.manager.storage.configuration.ConfigurationStore; import org.asamk.signal.manager.storage.contacts.ContactsStore; import org.asamk.signal.manager.storage.contacts.LegacyJsonContactsStore; @@ -27,7 +28,9 @@ import org.asamk.signal.manager.storage.recipients.LegacyRecipientStore; import org.asamk.signal.manager.storage.recipients.Profile; import org.asamk.signal.manager.storage.recipients.RecipientAddress; import org.asamk.signal.manager.storage.recipients.RecipientId; +import org.asamk.signal.manager.storage.recipients.RecipientResolver; import org.asamk.signal.manager.storage.recipients.RecipientStore; +import org.asamk.signal.manager.storage.recipients.RecipientTrustedResolver; import org.asamk.signal.manager.storage.sendLog.MessageSendLogStore; import org.asamk.signal.manager.storage.senderKeys.SenderKeyStore; import org.asamk.signal.manager.storage.sessions.SessionStore; @@ -201,7 +204,7 @@ public class SignalAccount implements Closeable { signalAccount.localRegistrationId = registrationId; signalAccount.trustNewIdentity = trustNewIdentity; signalAccount.groupStore = new GroupStore(getGroupCachePath(dataPath, accountPath), - signalAccount.getRecipientStore(), + signalAccount.getRecipientResolver(), signalAccount::saveGroupStore); signalAccount.stickerStore = new StickerStore(signalAccount::saveStickerStore); signalAccount.configurationStore = new ConfigurationStore(signalAccount::saveConfigurationStore); @@ -260,7 +263,8 @@ public class SignalAccount implements Closeable { aciIdentityKey, pniIdentityKey, profileKey); - signalAccount.getRecipientStore().resolveSelfRecipientTrusted(signalAccount.getSelfRecipientAddress()); + signalAccount.getRecipientTrustedResolver() + .resolveSelfRecipientTrusted(signalAccount.getSelfRecipientAddress()); signalAccount.getSessionStore().archiveAllSessions(); signalAccount.getSenderKeyStore().deleteAll(); signalAccount.clearAllPreKeys(); @@ -321,12 +325,13 @@ public class SignalAccount implements Closeable { signalAccount.localRegistrationId = registrationId; signalAccount.trustNewIdentity = trustNewIdentity; signalAccount.groupStore = new GroupStore(getGroupCachePath(dataPath, accountPath), - signalAccount.getRecipientStore(), + signalAccount.getRecipientResolver(), signalAccount::saveGroupStore); signalAccount.stickerStore = new StickerStore(signalAccount::saveStickerStore); signalAccount.configurationStore = new ConfigurationStore(signalAccount::saveConfigurationStore); - signalAccount.getRecipientStore().resolveSelfRecipientTrusted(signalAccount.getSelfRecipientAddress()); + signalAccount.getRecipientTrustedResolver() + .resolveSelfRecipientTrusted(signalAccount.getSelfRecipientAddress()); signalAccount.previousStorageVersion = CURRENT_STORAGE_VERSION; signalAccount.migrateLegacyConfigs(); signalAccount.clearAllPreKeys(); @@ -608,11 +613,11 @@ public class SignalAccount implements Closeable { groupStoreStorage = jsonProcessor.convertValue(rootNode.get("groupStore"), GroupStore.Storage.class); groupStore = GroupStore.fromStorage(groupStoreStorage, getGroupCachePath(dataPath, accountPath), - getRecipientStore(), + getRecipientResolver(), this::saveGroupStore); } else { groupStore = new GroupStore(getGroupCachePath(dataPath, accountPath), - getRecipientStore(), + getRecipientResolver(), this::saveGroupStore); } @@ -650,7 +655,7 @@ public class SignalAccount implements Closeable { if (legacyRecipientStore != null) { getRecipientStore().resolveRecipientsTrusted(legacyRecipientStore.getAddresses()); } - getRecipientStore().resolveSelfRecipientTrusted(getSelfRecipientAddress()); + getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress()); migrated = true; } @@ -710,7 +715,7 @@ public class SignalAccount implements Closeable { final var contactStore = jsonProcessor.convertValue(contactStoreNode, LegacyJsonContactsStore.class); for (var contact : contactStore.getContacts()) { final var recipientId = getRecipientStore().resolveRecipientTrusted(contact.getAddress()); - getRecipientStore().storeContact(recipientId, + getContactStore().storeContact(recipientId, new Contact(contact.name, contact.color, contact.messageExpirationTime, @@ -738,9 +743,9 @@ public class SignalAccount implements Closeable { var profileStoreNode = rootNode.get("profileStore"); final var legacyProfileStore = jsonProcessor.convertValue(profileStoreNode, LegacyProfileStore.class); for (var profileEntry : legacyProfileStore.getProfileEntries()) { - var recipientId = getRecipientStore().resolveRecipient(profileEntry.getAddress()); - getRecipientStore().storeProfileKeyCredential(recipientId, profileEntry.getProfileKeyCredential()); - getRecipientStore().storeProfileKey(recipientId, profileEntry.getProfileKey()); + var recipientId = getRecipientResolver().resolveRecipient(profileEntry.getAddress()); + getProfileStore().storeProfileKeyCredential(recipientId, profileEntry.getProfileKeyCredential()); + getProfileStore().storeProfileKey(recipientId, profileEntry.getProfileKey()); final var profile = profileEntry.getProfile(); if (profile != null) { final var capabilities = new HashSet(); @@ -765,7 +770,7 @@ public class SignalAccount implements Closeable { ? Profile.UnidentifiedAccessMode.ENABLED : Profile.UnidentifiedAccessMode.DISABLED, capabilities); - getRecipientStore().storeProfile(recipientId, newProfile); + getProfileStore().storeProfile(recipientId, newProfile); } } } @@ -784,10 +789,10 @@ public class SignalAccount implements Closeable { } try { if (UuidUtil.isUuid(thread.id) || thread.id.startsWith("+")) { - final var recipientId = getRecipientStore().resolveRecipient(thread.id); - var contact = getRecipientStore().getContact(recipientId); + final var recipientId = getRecipientResolver().resolveRecipient(thread.id); + var contact = getContactStore().getContact(recipientId); if (contact != null) { - getRecipientStore().storeContact(recipientId, + getContactStore().storeContact(recipientId, Contact.newBuilder(contact) .withMessageExpirationTime(thread.messageExpirationTime) .build()); @@ -1028,13 +1033,13 @@ public class SignalAccount implements Closeable { public SessionStore getSessionStore() { return getOrCreate(() -> sessionStore, - () -> sessionStore = new SessionStore(getSessionsPath(dataPath, accountPath), getRecipientStore())); + () -> sessionStore = new SessionStore(getSessionsPath(dataPath, accountPath), getRecipientResolver())); } public IdentityKeyStore getIdentityKeyStore() { return getOrCreate(() -> identityKeyStore, () -> identityKeyStore = new IdentityKeyStore(getIdentitiesPath(dataPath, accountPath), - getRecipientStore(), + getRecipientResolver(), aciIdentityKeyPair, localRegistrationId, trustNewIdentity)); @@ -1048,6 +1053,18 @@ public class SignalAccount implements Closeable { return getRecipientStore(); } + public RecipientResolver getRecipientResolver() { + return getRecipientStore(); + } + + public RecipientTrustedResolver getRecipientTrustedResolver() { + return getRecipientStore(); + } + + public RecipientAddressResolver getRecipientAddressResolver() { + return getRecipientStore()::resolveRecipientAddress; + } + public RecipientStore getRecipientStore() { return getOrCreate(() -> recipientStore, () -> recipientStore = RecipientStore.load(getRecipientsStoreFile(dataPath, accountPath), @@ -1067,8 +1084,8 @@ public class SignalAccount implements Closeable { return getOrCreate(() -> senderKeyStore, () -> senderKeyStore = new SenderKeyStore(getSharedSenderKeysFile(dataPath, accountPath), getSenderKeysPath(dataPath, accountPath), - getRecipientStore()::resolveRecipientAddress, - getRecipientStore())); + getRecipientAddressResolver(), + getRecipientResolver())); } public ConfigurationStore getConfigurationStore() { @@ -1092,7 +1109,7 @@ public class SignalAccount implements Closeable { public MessageSendLogStore getMessageSendLogStore() { return getOrCreate(() -> messageSendLogStore, - () -> messageSendLogStore = new MessageSendLogStore(getRecipientStore(), getAccountDatabase())); + () -> messageSendLogStore = new MessageSendLogStore(getRecipientResolver(), getAccountDatabase())); } public CredentialsProvider getCredentialsProvider() { @@ -1169,7 +1186,7 @@ public class SignalAccount implements Closeable { } public RecipientId getSelfRecipientId() { - return getRecipientStore().resolveRecipient(getSelfRecipientAddress()); + return getRecipientResolver().resolveRecipient(getSelfRecipientAddress()); } public byte[] getEncryptedDeviceName() { @@ -1337,7 +1354,7 @@ public class SignalAccount implements Closeable { clearAllPreKeys(); getSessionStore().archiveAllSessions(); getSenderKeyStore().deleteAll(); - final var recipientId = getRecipientStore().resolveSelfRecipientTrusted(getSelfRecipientAddress()); + final var recipientId = getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress()); final var publicKey = getAciIdentityKeyPair().getPublicKey(); getIdentityKeyStore().saveIdentity(recipientId, publicKey, new Date()); getIdentityKeyStore().setIdentityTrustLevel(recipientId, publicKey, TrustLevel.TRUSTED_VERIFIED); diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java index 0297f6d7..d2cc94d6 100644 --- a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java +++ b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java @@ -37,7 +37,7 @@ import java.util.UUID; import java.util.function.Supplier; import java.util.stream.Collectors; -public class RecipientStore implements RecipientResolver, ContactsStore, ProfileStore { +public class RecipientStore implements RecipientResolver, RecipientTrustedResolver, ContactsStore, ProfileStore { private final static Logger logger = LoggerFactory.getLogger(RecipientStore.class); @@ -224,6 +224,7 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile return resolveRecipient(new RecipientAddress(address), false, false); } + @Override public RecipientId resolveSelfRecipientTrusted(RecipientAddress address) { return resolveRecipient(address, true, true); } @@ -232,6 +233,7 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile return resolveRecipient(address, true, false); } + @Override public RecipientId resolveRecipientTrusted(SignalServiceAddress address) { return resolveRecipient(new RecipientAddress(address), true, false); } diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientTrustedResolver.java b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientTrustedResolver.java new file mode 100644 index 00000000..0bc522e3 --- /dev/null +++ b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientTrustedResolver.java @@ -0,0 +1,10 @@ +package org.asamk.signal.manager.storage.recipients; + +import org.whispersystems.signalservice.api.push.SignalServiceAddress; + +public interface RecipientTrustedResolver { + + RecipientId resolveSelfRecipientTrusted(RecipientAddress address); + + RecipientId resolveRecipientTrusted(SignalServiceAddress address); +}