1 package org
.asamk
.signal
.manager
.helper
;
3 import org
.asamk
.signal
.manager
.SignalDependencies
;
4 import org
.asamk
.signal
.manager
.api
.PhoneNumberSharingMode
;
5 import org
.asamk
.signal
.manager
.api
.TrustLevel
;
6 import org
.asamk
.signal
.manager
.groups
.GroupId
;
7 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
8 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
9 import org
.signal
.libsignal
.protocol
.IdentityKey
;
10 import org
.signal
.libsignal
.protocol
.InvalidKeyException
;
11 import org
.signal
.libsignal
.zkgroup
.InvalidInputException
;
12 import org
.signal
.libsignal
.zkgroup
.groups
.GroupMasterKey
;
13 import org
.signal
.libsignal
.zkgroup
.profiles
.ProfileKey
;
14 import org
.slf4j
.Logger
;
15 import org
.slf4j
.LoggerFactory
;
16 import org
.whispersystems
.signalservice
.api
.storage
.SignalAccountRecord
;
17 import org
.whispersystems
.signalservice
.api
.storage
.SignalStorageManifest
;
18 import org
.whispersystems
.signalservice
.api
.storage
.SignalStorageRecord
;
19 import org
.whispersystems
.signalservice
.api
.storage
.StorageId
;
20 import org
.whispersystems
.signalservice
.internal
.storage
.protos
.AccountRecord
;
21 import org
.whispersystems
.signalservice
.internal
.storage
.protos
.ManifestRecord
;
23 import java
.io
.IOException
;
24 import java
.util
.Collections
;
25 import java
.util
.Date
;
26 import java
.util
.List
;
27 import java
.util
.Optional
;
29 public class StorageHelper
{
31 private final static Logger logger
= LoggerFactory
.getLogger(StorageHelper
.class);
33 private final SignalAccount account
;
34 private final SignalDependencies dependencies
;
35 private final Context context
;
37 public StorageHelper(final Context context
) {
38 this.account
= context
.getAccount();
39 this.dependencies
= context
.getDependencies();
40 this.context
= context
;
43 public void readDataFromStorage() throws IOException
{
44 logger
.debug("Reading data from remote storage");
45 Optional
<SignalStorageManifest
> manifest
;
47 manifest
= dependencies
.getAccountManager()
48 .getStorageManifestIfDifferentVersion(account
.getStorageKey(), account
.getStorageManifestVersion());
49 } catch (InvalidKeyException e
) {
50 logger
.warn("Manifest couldn't be decrypted, ignoring.");
54 if (manifest
.isEmpty()) {
55 logger
.debug("Manifest is up to date, does not exist or couldn't be decrypted, ignoring.");
59 account
.setStorageManifestVersion(manifest
.get().getVersion());
61 final var storageIds
= manifest
.get().getStorageIds().stream().filter(id
-> !id
.isUnknown()).toList();
63 for (final var record : getSignalStorageRecords(storageIds
)) {
64 if (record.getType() == ManifestRecord
.Identifier
.Type
.ACCOUNT_VALUE
) {
65 readAccountRecord(record);
66 } else if (record.getType() == ManifestRecord
.Identifier
.Type
.GROUPV2_VALUE
) {
67 readGroupV2Record(record);
68 } else if (record.getType() == ManifestRecord
.Identifier
.Type
.GROUPV1_VALUE
) {
69 readGroupV1Record(record);
70 } else if (record.getType() == ManifestRecord
.Identifier
.Type
.CONTACT_VALUE
) {
71 readContactRecord(record);
74 logger
.debug("Done reading data from remote storage");
77 private void readContactRecord(final SignalStorageRecord
record) {
78 if (record == null || record.getContact().isEmpty()) {
82 final var contactRecord
= record.getContact().get();
83 final var address
= contactRecord
.getAddress();
85 final var recipientId
= account
.getRecipientResolver().resolveRecipient(address
);
86 final var contact
= account
.getContactStore().getContact(recipientId
);
87 final var blocked
= contact
!= null && contact
.isBlocked();
88 final var profileShared
= contact
!= null && contact
.isProfileSharingEnabled();
89 if (contactRecord
.getGivenName().isPresent()
90 || contactRecord
.getFamilyName().isPresent()
91 || blocked
!= contactRecord
.isBlocked()
92 || profileShared
!= contactRecord
.isProfileSharingEnabled()) {
93 final var contactBuilder
= contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
);
94 final var newContact
= contactBuilder
.withBlocked(contactRecord
.isBlocked())
95 .withGivenName(contactRecord
.getGivenName().orElse(null))
96 .withFamilyName(contactRecord
.getFamilyName().orElse(null))
97 .withProfileSharingEnabled(contactRecord
.isProfileSharingEnabled())
99 account
.getContactStore().storeContact(recipientId
, newContact
);
102 if (contactRecord
.getProfileKey().isPresent()) {
104 final var profileKey
= new ProfileKey(contactRecord
.getProfileKey().get());
105 account
.getProfileStore().storeProfileKey(recipientId
, profileKey
);
106 } catch (InvalidInputException e
) {
107 logger
.warn("Received invalid contact profile key from storage");
110 if (contactRecord
.getIdentityKey().isPresent()) {
112 final var identityKey
= new IdentityKey(contactRecord
.getIdentityKey().get());
113 account
.getIdentityKeyStore().saveIdentity(recipientId
, identityKey
, new Date());
115 final var trustLevel
= TrustLevel
.fromIdentityState(contactRecord
.getIdentityState());
116 if (trustLevel
!= null) {
117 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, identityKey
, trustLevel
);
119 } catch (InvalidKeyException e
) {
120 logger
.warn("Received invalid contact identity key from storage");
125 private void readGroupV1Record(final SignalStorageRecord
record) {
126 if (record == null || record.getGroupV1().isEmpty()) {
130 final var groupV1Record
= record.getGroupV1().get();
131 final var groupIdV1
= GroupId
.v1(groupV1Record
.getGroupId());
133 var group
= account
.getGroupStore().getGroup(groupIdV1
);
136 context
.getGroupHelper().sendGroupInfoRequest(groupIdV1
, account
.getSelfRecipientId());
137 } catch (Throwable e
) {
138 logger
.warn("Failed to send group request", e
);
140 group
= account
.getGroupStore().getOrCreateGroupV1(groupIdV1
);
142 if (group
!= null && group
.isBlocked() != groupV1Record
.isBlocked()) {
143 group
.setBlocked(groupV1Record
.isBlocked());
144 account
.getGroupStore().updateGroup(group
);
148 private void readGroupV2Record(final SignalStorageRecord
record) {
149 if (record == null || record.getGroupV2().isEmpty()) {
153 final var groupV2Record
= record.getGroupV2().get();
154 if (groupV2Record
.isArchived()) {
158 final GroupMasterKey groupMasterKey
;
160 groupMasterKey
= new GroupMasterKey(groupV2Record
.getMasterKeyBytes());
161 } catch (InvalidInputException e
) {
162 logger
.warn("Received invalid group master key from storage");
166 final var group
= context
.getGroupHelper().getOrMigrateGroup(groupMasterKey
, 0, null);
167 if (group
.isBlocked() != groupV2Record
.isBlocked()) {
168 group
.setBlocked(groupV2Record
.isBlocked());
169 account
.getGroupStore().updateGroup(group
);
173 private void readAccountRecord(final SignalStorageRecord
record) throws IOException
{
174 if (record == null) {
175 logger
.warn("Could not find account record, even though we had an ID, ignoring.");
179 SignalAccountRecord accountRecord
= record.getAccount().orElse(null);
180 if (accountRecord
== null) {
181 logger
.warn("The storage record didn't actually have an account, ignoring.");
185 if (!accountRecord
.getE164().equals(account
.getNumber())) {
186 context
.getAccountHelper().checkWhoAmiI();
189 account
.getConfigurationStore().setReadReceipts(accountRecord
.isReadReceiptsEnabled());
190 account
.getConfigurationStore().setTypingIndicators(accountRecord
.isTypingIndicatorsEnabled());
191 account
.getConfigurationStore()
192 .setUnidentifiedDeliveryIndicators(accountRecord
.isSealedSenderIndicatorsEnabled());
193 account
.getConfigurationStore().setLinkPreviews(accountRecord
.isLinkPreviewsEnabled());
194 if (accountRecord
.getPhoneNumberSharingMode() != AccountRecord
.PhoneNumberSharingMode
.UNRECOGNIZED
) {
195 account
.getConfigurationStore()
196 .setPhoneNumberSharingMode(switch (accountRecord
.getPhoneNumberSharingMode()) {
197 case EVERYBODY
-> PhoneNumberSharingMode
.EVERYBODY
;
198 case NOBODY
-> PhoneNumberSharingMode
.NOBODY
;
199 default -> PhoneNumberSharingMode
.CONTACTS
;
202 account
.getConfigurationStore().setPhoneNumberUnlisted(accountRecord
.isPhoneNumberUnlisted());
204 if (accountRecord
.getProfileKey().isPresent()) {
205 ProfileKey profileKey
;
207 profileKey
= new ProfileKey(accountRecord
.getProfileKey().get());
208 } catch (InvalidInputException e
) {
209 logger
.warn("Received invalid profile key from storage");
212 if (profileKey
!= null) {
213 account
.setProfileKey(profileKey
);
214 final var avatarPath
= accountRecord
.getAvatarUrlPath().orElse(null);
215 context
.getProfileHelper().downloadProfileAvatar(account
.getSelfRecipientId(), avatarPath
, profileKey
);
219 context
.getProfileHelper()
222 accountRecord
.getGivenName().orElse(null),
223 accountRecord
.getFamilyName().orElse(null),
230 private SignalStorageRecord
getSignalStorageRecord(final StorageId accountId
) throws IOException
{
231 List
<SignalStorageRecord
> records
;
233 records
= dependencies
.getAccountManager()
234 .readStorageRecords(account
.getStorageKey(), Collections
.singletonList(accountId
));
235 } catch (InvalidKeyException e
) {
236 logger
.warn("Failed to read storage records, ignoring.");
239 return records
.size() > 0 ? records
.get(0) : null;
242 private List
<SignalStorageRecord
> getSignalStorageRecords(final List
<StorageId
> storageIds
) throws IOException
{
243 List
<SignalStorageRecord
> records
;
245 records
= dependencies
.getAccountManager().readStorageRecords(account
.getStorageKey(), storageIds
);
246 } catch (InvalidKeyException e
) {
247 logger
.warn("Failed to read storage records, ignoring.");