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
.groups
.GroupId
;
6 import org
.asamk
.signal
.manager
.storage
.SignalAccount
;
7 import org
.asamk
.signal
.manager
.storage
.recipients
.Contact
;
8 import org
.signal
.zkgroup
.InvalidInputException
;
9 import org
.signal
.zkgroup
.groups
.GroupMasterKey
;
10 import org
.signal
.zkgroup
.profiles
.ProfileKey
;
11 import org
.slf4j
.Logger
;
12 import org
.slf4j
.LoggerFactory
;
13 import org
.whispersystems
.libsignal
.IdentityKey
;
14 import org
.whispersystems
.libsignal
.InvalidKeyException
;
15 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
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
.ManifestRecord
;
22 import java
.io
.IOException
;
23 import java
.util
.Collections
;
24 import java
.util
.Date
;
25 import java
.util
.List
;
26 import java
.util
.stream
.Collectors
;
28 public class StorageHelper
{
30 private final static Logger logger
= LoggerFactory
.getLogger(StorageHelper
.class);
32 private final SignalAccount account
;
33 private final SignalDependencies dependencies
;
34 private final GroupHelper groupHelper
;
35 private final ProfileHelper profileHelper
;
38 final SignalAccount account
,
39 final SignalDependencies dependencies
,
40 final GroupHelper groupHelper
,
41 final ProfileHelper profileHelper
43 this.account
= account
;
44 this.dependencies
= dependencies
;
45 this.groupHelper
= groupHelper
;
46 this.profileHelper
= profileHelper
;
49 public void readDataFromStorage() throws IOException
{
50 logger
.debug("Reading data from remote storage");
51 Optional
<SignalStorageManifest
> manifest
;
53 manifest
= dependencies
.getAccountManager()
54 .getStorageManifestIfDifferentVersion(account
.getStorageKey(), account
.getStorageManifestVersion());
55 } catch (InvalidKeyException e
) {
56 logger
.warn("Manifest couldn't be decrypted, ignoring.");
60 if (!manifest
.isPresent()) {
61 logger
.debug("Manifest is up to date, does not exist or couldn't be decrypted, ignoring.");
65 account
.setStorageManifestVersion(manifest
.get().getVersion());
67 readAccountRecord(manifest
.get());
69 final var storageIds
= manifest
.get()
72 .filter(id
-> !id
.isUnknown() && id
.getType() != ManifestRecord
.Identifier
.Type
.ACCOUNT_VALUE
)
73 .collect(Collectors
.toList());
75 for (final var record : getSignalStorageRecords(storageIds
)) {
76 if (record.getType() == ManifestRecord
.Identifier
.Type
.GROUPV2_VALUE
) {
77 readGroupV2Record(record);
78 } else if (record.getType() == ManifestRecord
.Identifier
.Type
.GROUPV1_VALUE
) {
79 readGroupV1Record(record);
80 } else if (record.getType() == ManifestRecord
.Identifier
.Type
.CONTACT_VALUE
) {
81 readContactRecord(record);
86 private void readContactRecord(final SignalStorageRecord
record) {
87 if (record == null || !record.getContact().isPresent()) {
91 final var contactRecord
= record.getContact().get();
92 final var address
= contactRecord
.getAddress();
94 final var recipientId
= account
.getRecipientStore().resolveRecipient(address
);
95 final var contact
= account
.getContactStore().getContact(recipientId
);
96 if (contactRecord
.getGivenName().isPresent() || contactRecord
.getFamilyName().isPresent() || (
97 (contact
== null || !contact
.isBlocked()) && contactRecord
.isBlocked()
99 final var newContact
= (contact
== null ? Contact
.newBuilder() : Contact
.newBuilder(contact
)).withBlocked(
100 contactRecord
.isBlocked())
101 .withName((contactRecord
.getGivenName().or("") + " " + contactRecord
.getFamilyName().or("")).trim())
103 account
.getContactStore().storeContact(recipientId
, newContact
);
106 if (contactRecord
.getProfileKey().isPresent()) {
108 final var profileKey
= new ProfileKey(contactRecord
.getProfileKey().get());
109 account
.getProfileStore().storeProfileKey(recipientId
, profileKey
);
110 } catch (InvalidInputException e
) {
111 logger
.warn("Received invalid contact profile key from storage");
114 if (contactRecord
.getIdentityKey().isPresent()) {
116 final var identityKey
= new IdentityKey(contactRecord
.getIdentityKey().get());
117 account
.getIdentityKeyStore().saveIdentity(recipientId
, identityKey
, new Date());
119 final var trustLevel
= TrustLevel
.fromIdentityState(contactRecord
.getIdentityState());
120 if (trustLevel
!= null) {
121 account
.getIdentityKeyStore().setIdentityTrustLevel(recipientId
, identityKey
, trustLevel
);
123 } catch (InvalidKeyException e
) {
124 logger
.warn("Received invalid contact identity key from storage");
129 private void readGroupV1Record(final SignalStorageRecord
record) {
130 if (record == null || !record.getGroupV1().isPresent()) {
134 final var groupV1Record
= record.getGroupV1().get();
135 final var groupIdV1
= GroupId
.v1(groupV1Record
.getGroupId());
137 final var group
= account
.getGroupStore().getGroup(groupIdV1
);
140 groupHelper
.sendGroupInfoRequest(groupIdV1
, account
.getSelfRecipientId());
141 } catch (Throwable e
) {
142 logger
.warn("Failed to send group request", e
);
145 final var groupV1
= account
.getGroupStore().getOrCreateGroupV1(groupIdV1
);
146 if (groupV1
.isBlocked() != groupV1Record
.isBlocked()) {
147 groupV1
.setBlocked(groupV1Record
.isBlocked());
148 account
.getGroupStore().updateGroup(groupV1
);
152 private void readGroupV2Record(final SignalStorageRecord
record) {
153 if (record == null || !record.getGroupV2().isPresent()) {
157 final var groupV2Record
= record.getGroupV2().get();
158 if (groupV2Record
.isArchived()) {
162 final GroupMasterKey groupMasterKey
;
164 groupMasterKey
= new GroupMasterKey(groupV2Record
.getMasterKeyBytes());
165 } catch (InvalidInputException e
) {
166 logger
.warn("Received invalid group master key from storage");
170 final var group
= groupHelper
.getOrMigrateGroup(groupMasterKey
, 0, null);
171 if (group
.isBlocked() != groupV2Record
.isBlocked()) {
172 group
.setBlocked(groupV2Record
.isBlocked());
173 account
.getGroupStore().updateGroup(group
);
177 private void readAccountRecord(final SignalStorageManifest manifest
) throws IOException
{
178 Optional
<StorageId
> accountId
= manifest
.getAccountStorageId();
179 if (!accountId
.isPresent()) {
180 logger
.warn("Manifest has no account record, ignoring.");
184 SignalStorageRecord
record = getSignalStorageRecord(accountId
.get());
185 if (record == null) {
186 logger
.warn("Could not find account record, even though we had an ID, ignoring.");
190 SignalAccountRecord accountRecord
= record.getAccount().orNull();
191 if (accountRecord
== null) {
192 logger
.warn("The storage record didn't actually have an account, ignoring.");
196 if (!accountRecord
.getE164().equals(account
.getUsername())) {
197 // TODO implement changed number handling
200 account
.getConfigurationStore().setReadReceipts(accountRecord
.isReadReceiptsEnabled());
201 account
.getConfigurationStore().setTypingIndicators(accountRecord
.isTypingIndicatorsEnabled());
202 account
.getConfigurationStore()
203 .setUnidentifiedDeliveryIndicators(accountRecord
.isSealedSenderIndicatorsEnabled());
204 account
.getConfigurationStore().setLinkPreviews(accountRecord
.isLinkPreviewsEnabled());
206 if (accountRecord
.getProfileKey().isPresent()) {
207 ProfileKey profileKey
;
209 profileKey
= new ProfileKey(accountRecord
.getProfileKey().get());
210 } catch (InvalidInputException e
) {
211 logger
.warn("Received invalid profile key from storage");
214 if (profileKey
!= null) {
215 account
.setProfileKey(profileKey
);
216 final var avatarPath
= accountRecord
.getAvatarUrlPath().orNull();
217 profileHelper
.downloadProfileAvatar(account
.getSelfRecipientId(), avatarPath
, profileKey
);
221 profileHelper
.setProfile(false,
222 accountRecord
.getGivenName().orNull(),
223 accountRecord
.getFamilyName().orNull(),
229 private SignalStorageRecord
getSignalStorageRecord(final StorageId accountId
) throws IOException
{
230 List
<SignalStorageRecord
> records
;
232 records
= dependencies
.getAccountManager()
233 .readStorageRecords(account
.getStorageKey(), Collections
.singletonList(accountId
));
234 } catch (InvalidKeyException e
) {
235 logger
.warn("Failed to read storage records, ignoring.");
238 return records
.size() > 0 ? records
.get(0) : null;
241 private List
<SignalStorageRecord
> getSignalStorageRecords(final List
<StorageId
> storageIds
) throws IOException
{
242 List
<SignalStorageRecord
> records
;
244 records
= dependencies
.getAccountManager().readStorageRecords(account
.getStorageKey(), storageIds
);
245 } catch (InvalidKeyException e
) {
246 logger
.warn("Failed to read storage records, ignoring.");