1 package org
.asamk
.signal
.manager
.helper
;
3 import org
.asamk
.signal
.manager
.SignalDependencies
;
4 import org
.asamk
.signal
.manager
.TrustLevel
;
5 import org
.asamk
.signal
.manager
.api
.PhoneNumberSharingMode
;
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
.zkgroup
.InvalidInputException
;
10 import org
.signal
.zkgroup
.groups
.GroupMasterKey
;
11 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
12 import org
.slf4j
.Logger
;
13 import org
.slf4j
.LoggerFactory
;
14 import org
.whispersystems
.libsignal
.IdentityKey
;
15 import org
.whispersystems
.libsignal
.InvalidKeyException
;
16 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
17 import org
.whispersystems
.signalservice
.api
.storage
.SignalAccountRecord
;
18 import org
.whispersystems
.signalservice
.api
.storage
.SignalStorageManifest
;
19 import org
.whispersystems
.signalservice
.api
.storage
.SignalStorageRecord
;
20 import org
.whispersystems
.signalservice
.api
.storage
.StorageId
;
21 import org
.whispersystems
.signalservice
.internal
.storage
.protos
.AccountRecord
;
22 import org
.whispersystems
.signalservice
.internal
.storage
.protos
.ManifestRecord
;
24 import java
.io
.IOException
;
25 import java
.util
.Collections
;
26 import java
.util
.Date
;
27 import java
.util
.List
;
28 import java
.util
.stream
.Collectors
;
30 public class StorageHelper
{
32 private final static Logger logger
= LoggerFactory
.getLogger(StorageHelper
.class);
34 private final SignalAccount account
;
35 private final SignalDependencies dependencies
;
36 private final GroupHelper groupHelper
;
37 private final ProfileHelper profileHelper
;
40 final SignalAccount account
,
41 final SignalDependencies dependencies
,
42 final GroupHelper groupHelper
,
43 final ProfileHelper profileHelper
45 this.account
= account
;
46 this.dependencies
= dependencies
;
47 this.groupHelper
= groupHelper
;
48 this.profileHelper
= profileHelper
;
51 public void readDataFromStorage() throws IOException
{
52 logger
.debug("Reading data from remote storage");
53 Optional
<SignalStorageManifest
> manifest
;
55 manifest
= dependencies
.getAccountManager()
56 .getStorageManifestIfDifferentVersion(account
.getStorageKey(), account
.getStorageManifestVersion());
57 } catch (InvalidKeyException e
) {
58 logger
.warn("Manifest couldn't be decrypted, ignoring.");
62 if (!manifest
.isPresent()) {
63 logger
.debug("Manifest is up to date, does not exist or couldn't be decrypted, ignoring.");
67 account
.setStorageManifestVersion(manifest
.get().getVersion());
69 readAccountRecord(manifest
.get());
71 final var storageIds
= manifest
.get()
74 .filter(id
-> !id
.isUnknown() && id
.getType() != ManifestRecord
.Identifier
.Type
.ACCOUNT_VALUE
)
75 .collect(Collectors
.toList());
77 for (final var record : getSignalStorageRecords(storageIds
)) {
78 if (record.getType() == ManifestRecord
.Identifier
.Type
.GROUPV2_VALUE
) {
79 readGroupV2Record(record);
80 } else if (record.getType() == ManifestRecord
.Identifier
.Type
.GROUPV1_VALUE
) {
81 readGroupV1Record(record);
82 } else if (record.getType() == ManifestRecord
.Identifier
.Type
.CONTACT_VALUE
) {
83 readContactRecord(record);
86 logger
.debug("Done reading data from remote storage");
89 private void readContactRecord(final SignalStorageRecord
record) {
90 if (record == null || !record.getContact().isPresent()) {
94 final var contactRecord
= record.getContact().get();
95 final var address
= contactRecord
.getAddress();
97 final var recipientId
= account
.getRecipientStore().resolveRecipient(address
);
98 final var contact
= account
.getContactStore().getContact(recipientId
);
99 if (contactRecord
.getGivenName().isPresent() || contactRecord
.getFamilyName().isPresent() || (
100 (contact
== null || !contact
.isBlocked()) && contactRecord
.isBlocked()
102 final var newContact
= (contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
)).withBlocked(
103 contactRecord
.isBlocked())
104 .withName((contactRecord
.getGivenName().or("") + " " + contactRecord
.getFamilyName().or("")).trim())
106 account
.getContactStore().storeContact(recipientId
, newContact
);
109 if (contactRecord
.getProfileKey().isPresent()) {
111 final var profileKey
= new ProfileKey(contactRecord
.getProfileKey().get());
112 account
.getProfileStore().storeProfileKey(recipientId
, profileKey
);
113 } catch (InvalidInputException e
) {
114 logger
.warn("Received invalid contact profile key from storage");
117 if (contactRecord
.getIdentityKey().isPresent()) {
119 final var identityKey
= new IdentityKey(contactRecord
.getIdentityKey().get());
120 account
.getIdentityKeyStore().saveIdentity(recipientId
, identityKey
, new Date());
122 final var trustLevel
= TrustLevel
.fromIdentityState(contactRecord
.getIdentityState());
123 if (trustLevel
!= null) {
124 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, identityKey
, trustLevel
);
126 } catch (InvalidKeyException e
) {
127 logger
.warn("Received invalid contact identity key from storage");
132 private void readGroupV1Record(final SignalStorageRecord
record) {
133 if (record == null || !record.getGroupV1().isPresent()) {
137 final var groupV1Record
= record.getGroupV1().get();
138 final var groupIdV1
= GroupId
.v1(groupV1Record
.getGroupId());
140 final var group
= account
.getGroupStore().getGroup(groupIdV1
);
143 groupHelper
.sendGroupInfoRequest(groupIdV1
, account
.getSelfRecipientId());
144 } catch (Throwable e
) {
145 logger
.warn("Failed to send group request", e
);
148 final var groupV1
= account
.getGroupStore().getOrCreateGroupV1(groupIdV1
);
149 if (groupV1
.isBlocked() != groupV1Record
.isBlocked()) {
150 groupV1
.setBlocked(groupV1Record
.isBlocked());
151 account
.getGroupStore().updateGroup(groupV1
);
155 private void readGroupV2Record(final SignalStorageRecord
record) {
156 if (record == null || !record.getGroupV2().isPresent()) {
160 final var groupV2Record
= record.getGroupV2().get();
161 if (groupV2Record
.isArchived()) {
165 final GroupMasterKey groupMasterKey
;
167 groupMasterKey
= new GroupMasterKey(groupV2Record
.getMasterKeyBytes());
168 } catch (InvalidInputException e
) {
169 logger
.warn("Received invalid group master key from storage");
173 final var group
= groupHelper
.getOrMigrateGroup(groupMasterKey
, 0, null);
174 if (group
.isBlocked() != groupV2Record
.isBlocked()) {
175 group
.setBlocked(groupV2Record
.isBlocked());
176 account
.getGroupStore().updateGroup(group
);
180 private void readAccountRecord(final SignalStorageManifest manifest
) throws IOException
{
181 Optional
<StorageId
> accountId
= manifest
.getAccountStorageId();
182 if (!accountId
.isPresent()) {
183 logger
.warn("Manifest has no account record, ignoring.");
187 SignalStorageRecord
record = getSignalStorageRecord(accountId
.get());
188 if (record == null) {
189 logger
.warn("Could not find account record, even though we had an ID, ignoring.");
193 SignalAccountRecord accountRecord
= record.getAccount().orNull();
194 if (accountRecord
== null) {
195 logger
.warn("The storage record didn't actually have an account, ignoring.");
199 if (!accountRecord
.getE164().equals(account
.getAccount())) {
200 // TODO implement changed number handling
203 account
.getConfigurationStore().setReadReceipts(accountRecord
.isReadReceiptsEnabled());
204 account
.getConfigurationStore().setTypingIndicators(accountRecord
.isTypingIndicatorsEnabled());
205 account
.getConfigurationStore()
206 .setUnidentifiedDeliveryIndicators(accountRecord
.isSealedSenderIndicatorsEnabled());
207 account
.getConfigurationStore().setLinkPreviews(accountRecord
.isLinkPreviewsEnabled());
208 if (accountRecord
.getPhoneNumberSharingMode() != AccountRecord
.PhoneNumberSharingMode
.UNRECOGNIZED
) {
209 account
.getConfigurationStore()
210 .setPhoneNumberSharingMode(switch (accountRecord
.getPhoneNumberSharingMode()) {
211 case EVERYBODY
-> PhoneNumberSharingMode
.EVERYBODY
;
212 case NOBODY
-> PhoneNumberSharingMode
.NOBODY
;
213 default -> PhoneNumberSharingMode
.CONTACTS
;
216 account
.getConfigurationStore().setPhoneNumberUnlisted(accountRecord
.isPhoneNumberUnlisted());
218 if (accountRecord
.getProfileKey().isPresent()) {
219 ProfileKey profileKey
;
221 profileKey
= new ProfileKey(accountRecord
.getProfileKey().get());
222 } catch (InvalidInputException e
) {
223 logger
.warn("Received invalid profile key from storage");
226 if (profileKey
!= null) {
227 account
.setProfileKey(profileKey
);
228 final var avatarPath
= accountRecord
.getAvatarUrlPath().orNull();
229 profileHelper
.downloadProfileAvatar(account
.getSelfRecipientId(), avatarPath
, profileKey
);
233 profileHelper
.setProfile(false,
234 accountRecord
.getGivenName().orNull(),
235 accountRecord
.getFamilyName().orNull(),
241 private SignalStorageRecord
getSignalStorageRecord(final StorageId accountId
) throws IOException
{
242 List
<SignalStorageRecord
> records
;
244 records
= dependencies
.getAccountManager()
245 .readStorageRecords(account
.getStorageKey(), Collections
.singletonList(accountId
));
246 } catch (InvalidKeyException e
) {
247 logger
.warn("Failed to read storage records, ignoring.");
250 return records
.size() > 0 ? records
.get(0) : null;
253 private List
<SignalStorageRecord
> getSignalStorageRecords(final List
<StorageId
> storageIds
) throws IOException
{
254 List
<SignalStorageRecord
> records
;
256 records
= dependencies
.getAccountManager().readStorageRecords(account
.getStorageKey(), storageIds
);
257 } catch (InvalidKeyException e
) {
258 logger
.warn("Failed to read storage records, ignoring.");