1 package org
.asamk
.signal
.manager
.storage
.recipients
;
3 import org
.asamk
.signal
.manager
.api
.Contact
;
4 import org
.asamk
.signal
.manager
.api
.Pair
;
5 import org
.asamk
.signal
.manager
.api
.PhoneNumberSharingMode
;
6 import org
.asamk
.signal
.manager
.api
.Profile
;
7 import org
.asamk
.signal
.manager
.api
.UnregisteredRecipientException
;
8 import org
.asamk
.signal
.manager
.storage
.Database
;
9 import org
.asamk
.signal
.manager
.storage
.Utils
;
10 import org
.asamk
.signal
.manager
.storage
.contacts
.ContactsStore
;
11 import org
.asamk
.signal
.manager
.storage
.profiles
.ProfileStore
;
12 import org
.asamk
.signal
.manager
.util
.KeyUtils
;
13 import org
.signal
.libsignal
.zkgroup
.InvalidInputException
;
14 import org
.signal
.libsignal
.zkgroup
.profiles
.ExpiringProfileKeyCredential
;
15 import org
.signal
.libsignal
.zkgroup
.profiles
.ProfileKey
;
16 import org
.slf4j
.Logger
;
17 import org
.slf4j
.LoggerFactory
;
18 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
;
19 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
.ACI
;
20 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
.PNI
;
21 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
22 import org
.whispersystems
.signalservice
.api
.storage
.StorageId
;
24 import java
.sql
.Connection
;
25 import java
.sql
.ResultSet
;
26 import java
.sql
.SQLException
;
27 import java
.sql
.Types
;
28 import java
.util
.ArrayList
;
29 import java
.util
.Arrays
;
30 import java
.util
.Collection
;
31 import java
.util
.HashMap
;
32 import java
.util
.List
;
34 import java
.util
.Objects
;
35 import java
.util
.Optional
;
37 import java
.util
.function
.Supplier
;
38 import java
.util
.stream
.Collectors
;
40 public class RecipientStore
implements RecipientIdCreator
, RecipientResolver
, RecipientTrustedResolver
, ContactsStore
, ProfileStore
{
42 private static final Logger logger
= LoggerFactory
.getLogger(RecipientStore
.class);
43 private static final String TABLE_RECIPIENT
= "recipient";
44 private static final String SQL_IS_CONTACT
= "r.given_name IS NOT NULL OR r.family_name IS NOT NULL OR r.nick_name IS NOT NULL OR r.nick_name_given_name IS NOT NULL OR r.nick_name_family_name IS NOT NULL OR r.note IS NOT NULL OR r.expiration_time > 0 OR r.profile_sharing = TRUE OR r.color IS NOT NULL OR r.blocked = TRUE OR r.archived = TRUE";
46 private final RecipientMergeHandler recipientMergeHandler
;
47 private final SelfAddressProvider selfAddressProvider
;
48 private final SelfProfileKeyProvider selfProfileKeyProvider
;
49 private final Database database
;
51 private final Map
<Long
, Long
> recipientsMerged
= new HashMap
<>();
53 private final Map
<ServiceId
, RecipientWithAddress
> recipientAddressCache
= new HashMap
<>();
55 public static void createSql(Connection connection
) throws SQLException
{
56 // When modifying the CREATE statement here, also add a migration in AccountDatabase.java
57 try (final var statement
= connection
.createStatement()) {
58 statement
.executeUpdate("""
59 CREATE TABLE recipient (
60 _id INTEGER PRIMARY KEY AUTOINCREMENT,
61 storage_id BLOB UNIQUE,
67 unregistered_timestamp INTEGER,
70 profile_key_credential BLOB,
71 needs_pni_signature INTEGER NOT NULL DEFAULT FALSE,
76 nick_name_given_name TEXT,
77 nick_name_family_name TEXT,
81 expiration_time INTEGER NOT NULL DEFAULT 0,
82 mute_until INTEGER NOT NULL DEFAULT 0,
83 blocked INTEGER NOT NULL DEFAULT FALSE,
84 archived INTEGER NOT NULL DEFAULT FALSE,
85 profile_sharing INTEGER NOT NULL DEFAULT FALSE,
86 hide_story INTEGER NOT NULL DEFAULT FALSE,
87 hidden INTEGER NOT NULL DEFAULT FALSE,
89 profile_last_update_timestamp INTEGER NOT NULL DEFAULT 0,
90 profile_given_name TEXT,
91 profile_family_name TEXT,
93 profile_about_emoji TEXT,
94 profile_avatar_url_path TEXT,
95 profile_mobile_coin_address BLOB,
96 profile_unidentified_access_mode TEXT,
97 profile_capabilities TEXT,
98 profile_phone_number_sharing TEXT
104 public RecipientStore(
105 final RecipientMergeHandler recipientMergeHandler
,
106 final SelfAddressProvider selfAddressProvider
,
107 final SelfProfileKeyProvider selfProfileKeyProvider
,
108 final Database database
110 this.recipientMergeHandler
= recipientMergeHandler
;
111 this.selfAddressProvider
= selfAddressProvider
;
112 this.selfProfileKeyProvider
= selfProfileKeyProvider
;
113 this.database
= database
;
116 public RecipientAddress
resolveRecipientAddress(RecipientId recipientId
) {
117 try (final var connection
= database
.getConnection()) {
118 return resolveRecipientAddress(connection
, recipientId
);
119 } catch (SQLException e
) {
120 throw new RuntimeException("Failed read from recipient store", e
);
124 public Collection
<RecipientId
> getRecipientIdsWithEnabledProfileSharing() {
129 WHERE r.blocked = FALSE AND r.profile_sharing = TRUE
131 ).formatted(TABLE_RECIPIENT
);
132 try (final var connection
= database
.getConnection()) {
133 try (final var statement
= connection
.prepareStatement(sql
)) {
134 try (var result
= Utils
.executeQueryForStream(statement
, this::getRecipientIdFromResultSet
)) {
135 return result
.toList();
138 } catch (SQLException e
) {
139 throw new RuntimeException("Failed read from recipient store", e
);
144 public RecipientId
resolveRecipient(final long rawRecipientId
) {
151 ).formatted(TABLE_RECIPIENT
);
152 try (final var connection
= database
.getConnection()) {
153 try (final var statement
= connection
.prepareStatement(sql
)) {
154 statement
.setLong(1, rawRecipientId
);
155 return Utils
.executeQueryForOptional(statement
, this::getRecipientIdFromResultSet
).orElse(null);
157 } catch (SQLException e
) {
158 throw new RuntimeException("Failed read from recipient store", e
);
163 public RecipientId
resolveRecipient(final String identifier
) {
164 final var serviceId
= ServiceId
.parseOrNull(identifier
);
165 if (serviceId
!= null) {
166 return resolveRecipient(serviceId
);
168 return resolveRecipientByNumber(identifier
);
172 private RecipientId
resolveRecipientByNumber(final String number
) {
173 final RecipientId recipientId
;
174 try (final var connection
= database
.getConnection()) {
175 connection
.setAutoCommit(false);
176 recipientId
= resolveRecipientLocked(connection
, number
);
178 } catch (SQLException e
) {
179 throw new RuntimeException("Failed read recipient store", e
);
185 public RecipientId
resolveRecipient(final ServiceId serviceId
) {
186 try (final var connection
= database
.getConnection()) {
187 connection
.setAutoCommit(false);
188 final var recipientWithAddress
= recipientAddressCache
.get(serviceId
);
189 if (recipientWithAddress
!= null) {
190 return recipientWithAddress
.id();
192 final var recipientId
= resolveRecipientLocked(connection
, serviceId
);
195 } catch (SQLException e
) {
196 throw new RuntimeException("Failed read recipient store", e
);
201 * Should only be used for recipientIds from the database.
202 * Where the foreign key relations ensure a valid recipientId.
205 public RecipientId
create(final long recipientId
) {
206 return new RecipientId(recipientId
, this);
209 public RecipientId
resolveRecipientByNumber(
210 final String number
, Supplier
<ServiceId
> serviceIdSupplier
211 ) throws UnregisteredRecipientException
{
212 final Optional
<RecipientWithAddress
> byNumber
;
213 try (final var connection
= database
.getConnection()) {
214 byNumber
= findByNumber(connection
, number
);
215 } catch (SQLException e
) {
216 throw new RuntimeException("Failed read from recipient store", e
);
218 if (byNumber
.isEmpty() || byNumber
.get().address().serviceId().isEmpty()) {
219 final var serviceId
= serviceIdSupplier
.get();
220 if (serviceId
== null) {
221 throw new UnregisteredRecipientException(new org
.asamk
.signal
.manager
.api
.RecipientAddress(number
));
224 return resolveRecipient(serviceId
);
226 return byNumber
.get().id();
229 public Optional
<RecipientId
> resolveRecipientByNumberOptional(final String number
) {
230 final Optional
<RecipientWithAddress
> byNumber
;
231 try (final var connection
= database
.getConnection()) {
232 byNumber
= findByNumber(connection
, number
);
233 } catch (SQLException e
) {
234 throw new RuntimeException("Failed read from recipient store", e
);
236 return byNumber
.map(RecipientWithAddress
::id
);
239 public RecipientId
resolveRecipientByUsername(
240 final String username
, Supplier
<ACI
> aciSupplier
241 ) throws UnregisteredRecipientException
{
242 final Optional
<RecipientWithAddress
> byUsername
;
243 try (final var connection
= database
.getConnection()) {
244 byUsername
= findByUsername(connection
, username
);
245 } catch (SQLException e
) {
246 throw new RuntimeException("Failed read from recipient store", e
);
248 if (byUsername
.isEmpty() || byUsername
.get().address().serviceId().isEmpty()) {
249 final var aci
= aciSupplier
.get();
251 throw new UnregisteredRecipientException(new org
.asamk
.signal
.manager
.api
.RecipientAddress(null,
257 return resolveRecipientTrusted(aci
, username
);
259 return byUsername
.get().id();
262 public RecipientId
resolveRecipient(RecipientAddress address
) {
263 final RecipientId recipientId
;
264 try (final var connection
= database
.getConnection()) {
265 connection
.setAutoCommit(false);
266 recipientId
= resolveRecipientLocked(connection
, address
);
268 } catch (SQLException e
) {
269 throw new RuntimeException("Failed read recipient store", e
);
274 public RecipientId
resolveRecipient(Connection connection
, RecipientAddress address
) throws SQLException
{
275 return resolveRecipientLocked(connection
, address
);
279 public RecipientId
resolveSelfRecipientTrusted(RecipientAddress address
) {
280 return resolveRecipientTrusted(address
, true);
284 public RecipientId
resolveRecipientTrusted(RecipientAddress address
) {
285 return resolveRecipientTrusted(address
, false);
288 public RecipientId
resolveRecipientTrusted(Connection connection
, RecipientAddress address
) throws SQLException
{
289 final var pair
= resolveRecipientTrustedLocked(connection
, address
, false);
290 if (!pair
.second().isEmpty()) {
291 mergeRecipients(connection
, pair
.first(), pair
.second());
297 public RecipientId
resolveRecipientTrusted(SignalServiceAddress address
) {
298 return resolveRecipientTrusted(new RecipientAddress(address
));
302 public RecipientId
resolveRecipientTrusted(
303 final Optional
<ACI
> aci
, final Optional
<PNI
> pni
, final Optional
<String
> number
305 return resolveRecipientTrusted(new RecipientAddress(aci
, pni
, number
, Optional
.empty()));
309 public RecipientId
resolveRecipientTrusted(final ACI aci
, final String username
) {
310 return resolveRecipientTrusted(new RecipientAddress(aci
, null, null, username
));
314 public void storeContact(RecipientId recipientId
, final Contact contact
) {
315 try (final var connection
= database
.getConnection()) {
316 storeContact(connection
, recipientId
, contact
);
317 } catch (SQLException e
) {
318 throw new RuntimeException("Failed update recipient store", e
);
323 public Contact
getContact(RecipientId recipientId
) {
324 try (final var connection
= database
.getConnection()) {
325 return getContact(connection
, recipientId
);
326 } catch (SQLException e
) {
327 throw new RuntimeException("Failed read from recipient store", e
);
332 public List
<Pair
<RecipientId
, Contact
>> getContacts() {
335 SELECT r._id, r.given_name, r.family_name, r.nick_name, r.nick_name_given_name, r.nick_name_family_name, r.note, r.expiration_time, r.mute_until, r.hide_story, r.profile_sharing, r.color, r.blocked, r.archived, r.hidden, r.unregistered_timestamp
337 WHERE (r.number IS NOT NULL OR r.aci IS NOT NULL) AND %s AND r.hidden = FALSE
339 ).formatted(TABLE_RECIPIENT
, SQL_IS_CONTACT
);
340 try (final var connection
= database
.getConnection()) {
341 try (final var statement
= connection
.prepareStatement(sql
)) {
342 try (var result
= Utils
.executeQueryForStream(statement
,
343 resultSet
-> new Pair
<>(getRecipientIdFromResultSet(resultSet
),
344 getContactFromResultSet(resultSet
)))) {
345 return result
.toList();
348 } catch (SQLException e
) {
349 throw new RuntimeException("Failed read from recipient store", e
);
353 public Recipient
getRecipient(Connection connection
, RecipientId recipientId
) throws SQLException
{
357 r.number, r.aci, r.pni, r.username,
358 r.profile_key, r.profile_key_credential,
359 r.given_name, r.family_name, r.nick_name, r.nick_name_given_name, r.nick_name_family_name, r.note, r.expiration_time, r.mute_until, r.hide_story, r.profile_sharing, r.color, r.blocked, r.archived, r.hidden, r.unregistered_timestamp,
360 r.profile_last_update_timestamp, r.profile_given_name, r.profile_family_name, r.profile_about, r.profile_about_emoji, r.profile_avatar_url_path, r.profile_mobile_coin_address, r.profile_unidentified_access_mode, r.profile_capabilities, r.profile_phone_number_sharing,
366 ).formatted(TABLE_RECIPIENT
);
367 try (final var statement
= connection
.prepareStatement(sql
)) {
368 statement
.setLong(1, recipientId
.id());
369 return Utils
.executeQuerySingleRow(statement
, this::getRecipientFromResultSet
);
373 public Recipient
getRecipient(Connection connection
, StorageId storageId
) throws SQLException
{
377 r.number, r.aci, r.pni, r.username,
378 r.profile_key, r.profile_key_credential,
379 r.given_name, r.family_name, r.nick_name, r.nick_name_given_name, r.nick_name_family_name, r.note, r.expiration_time, r.mute_until, r.hide_story, r.profile_sharing, r.color, r.blocked, r.archived, r.hidden, r.unregistered_timestamp,
380 r.profile_last_update_timestamp, r.profile_given_name, r.profile_family_name, r.profile_about, r.profile_about_emoji, r.profile_avatar_url_path, r.profile_mobile_coin_address, r.profile_unidentified_access_mode, r.profile_capabilities, r.profile_phone_number_sharing,
384 WHERE r.storage_id = ?
386 ).formatted(TABLE_RECIPIENT
);
387 try (final var statement
= connection
.prepareStatement(sql
)) {
388 statement
.setBytes(1, storageId
.getRaw());
389 return Utils
.executeQuerySingleRow(statement
, this::getRecipientFromResultSet
);
393 public List
<Recipient
> getRecipients(
394 boolean onlyContacts
, Optional
<Boolean
> blocked
, Set
<RecipientId
> recipientIds
, Optional
<String
> name
396 final var sqlWhere
= new ArrayList
<String
>();
398 sqlWhere
.add("r.unregistered_timestamp IS NULL");
399 sqlWhere
.add("(" + SQL_IS_CONTACT
+ ")");
400 sqlWhere
.add("r.hidden = FALSE");
402 if (blocked
.isPresent()) {
403 sqlWhere
.add("r.blocked = ?");
405 if (!recipientIds
.isEmpty()) {
406 final var recipientIdsCommaSeparated
= recipientIds
.stream()
407 .map(recipientId
-> String
.valueOf(recipientId
.id()))
408 .collect(Collectors
.joining(","));
409 sqlWhere
.add("r._id IN (" + recipientIdsCommaSeparated
+ ")");
414 r.number, r.aci, r.pni, r.username,
415 r.profile_key, r.profile_key_credential,
416 r.given_name, r.family_name, r.nick_name, r.nick_name_given_name, r.nick_name_family_name, r.note, r.expiration_time, r.mute_until, r.hide_story, r.profile_sharing, r.color, r.blocked, r.archived, r.hidden, r.unregistered_timestamp,
417 r.profile_last_update_timestamp, r.profile_given_name, r.profile_family_name, r.profile_about, r.profile_about_emoji, r.profile_avatar_url_path, r.profile_mobile_coin_address, r.profile_unidentified_access_mode, r.profile_capabilities, r.profile_phone_number_sharing,
421 WHERE (r.number IS NOT NULL OR r.aci IS NOT NULL) AND %s
423 ).formatted(TABLE_RECIPIENT
, sqlWhere
.isEmpty() ?
"TRUE" : String
.join(" AND ", sqlWhere
));
424 final var selfAddress
= selfAddressProvider
.getSelfAddress();
425 try (final var connection
= database
.getConnection()) {
426 try (final var statement
= connection
.prepareStatement(sql
)) {
427 if (blocked
.isPresent()) {
428 statement
.setBoolean(1, blocked
.get());
430 try (var result
= Utils
.executeQueryForStream(statement
, this::getRecipientFromResultSet
)) {
431 return result
.filter(r
-> name
.isEmpty() || (
432 r
.getContact() != null && name
.get().equals(r
.getContact().getName())
433 ) || (r
.getProfile() != null && name
.get().equals(r
.getProfile().getDisplayName()))).map(r
-> {
434 if (r
.getAddress().matches(selfAddress
)) {
435 return Recipient
.newBuilder(r
)
436 .withProfileKey(selfProfileKeyProvider
.getSelfProfileKey())
443 } catch (SQLException e
) {
444 throw new RuntimeException("Failed read from recipient store", e
);
448 public Set
<String
> getAllNumbers() {
453 WHERE r.number IS NOT NULL
455 ).formatted(TABLE_RECIPIENT
);
456 final var selfNumber
= selfAddressProvider
.getSelfAddress().number().orElse(null);
457 try (final var connection
= database
.getConnection()) {
458 try (final var statement
= connection
.prepareStatement(sql
)) {
459 return Utils
.executeQueryForStream(statement
, resultSet
-> resultSet
.getString("number"))
460 .filter(Objects
::nonNull
)
461 .filter(n
-> !n
.equals(selfNumber
))
466 } catch (NumberFormatException e
) {
470 .collect(Collectors
.toSet());
472 } catch (SQLException e
) {
473 throw new RuntimeException("Failed read from recipient store", e
);
477 public Map
<ServiceId
, ProfileKey
> getServiceIdToProfileKeyMap() {
480 SELECT r.aci, r.profile_key
482 WHERE r.aci IS NOT NULL AND r.profile_key IS NOT NULL
484 ).formatted(TABLE_RECIPIENT
);
485 final var selfAci
= selfAddressProvider
.getSelfAddress().aci().orElse(null);
486 try (final var connection
= database
.getConnection()) {
487 try (final var statement
= connection
.prepareStatement(sql
)) {
488 return Utils
.executeQueryForStream(statement
, resultSet
-> {
489 final var aci
= ACI
.parseOrThrow(resultSet
.getString("aci"));
490 if (aci
.equals(selfAci
)) {
491 return new Pair
<>(aci
, selfProfileKeyProvider
.getSelfProfileKey());
493 final var profileKey
= getProfileKeyFromResultSet(resultSet
);
494 return new Pair
<>(aci
, profileKey
);
495 }).filter(Objects
::nonNull
).collect(Collectors
.toMap(Pair
::first
, Pair
::second
));
497 } catch (SQLException e
) {
498 throw new RuntimeException("Failed read from recipient store", e
);
502 public List
<RecipientId
> getRecipientIds(Connection connection
) throws SQLException
{
507 WHERE (r.number IS NOT NULL OR r.aci IS NOT NULL)
509 ).formatted(TABLE_RECIPIENT
);
510 try (final var statement
= connection
.prepareStatement(sql
)) {
511 return Utils
.executeQueryForStream(statement
, this::getRecipientIdFromResultSet
).toList();
515 public void setMissingStorageIds() {
516 final var selectSql
= (
520 WHERE r.storage_id IS NULL AND r.unregistered_timestamp IS NULL
522 ).formatted(TABLE_RECIPIENT
);
523 final var updateSql
= (
529 ).formatted(TABLE_RECIPIENT
);
530 try (final var connection
= database
.getConnection()) {
531 connection
.setAutoCommit(false);
532 try (final var selectStmt
= connection
.prepareStatement(selectSql
)) {
533 final var recipientIds
= Utils
.executeQueryForStream(selectStmt
, this::getRecipientIdFromResultSet
)
535 try (final var updateStmt
= connection
.prepareStatement(updateSql
)) {
536 for (final var recipientId
: recipientIds
) {
537 updateStmt
.setBytes(1, KeyUtils
.createRawStorageId());
538 updateStmt
.setLong(2, recipientId
.id());
539 updateStmt
.executeUpdate();
544 } catch (SQLException e
) {
545 throw new RuntimeException("Failed update recipient store", e
);
550 public void deleteContact(RecipientId recipientId
) {
551 storeContact(recipientId
, null);
554 public void deleteRecipientData(RecipientId recipientId
) {
555 logger
.debug("Deleting recipient data for {}", recipientId
);
556 try (final var connection
= database
.getConnection()) {
557 connection
.setAutoCommit(false);
558 recipientAddressCache
.entrySet().removeIf(e
-> e
.getValue().id().equals(recipientId
));
559 storeContact(connection
, recipientId
, null);
560 storeProfile(connection
, recipientId
, null);
561 storeProfileKey(connection
, recipientId
, null, false);
562 storeExpiringProfileKeyCredential(connection
, recipientId
, null);
563 deleteRecipient(connection
, recipientId
);
565 } catch (SQLException e
) {
566 throw new RuntimeException("Failed update recipient store", e
);
571 public Profile
getProfile(final RecipientId recipientId
) {
572 try (final var connection
= database
.getConnection()) {
573 return getProfile(connection
, recipientId
);
574 } catch (SQLException e
) {
575 throw new RuntimeException("Failed read from recipient store", e
);
580 public ProfileKey
getProfileKey(final RecipientId recipientId
) {
581 try (final var connection
= database
.getConnection()) {
582 return getProfileKey(connection
, recipientId
);
583 } catch (SQLException e
) {
584 throw new RuntimeException("Failed read from recipient store", e
);
589 public ExpiringProfileKeyCredential
getExpiringProfileKeyCredential(final RecipientId recipientId
) {
590 try (final var connection
= database
.getConnection()) {
591 return getExpiringProfileKeyCredential(connection
, recipientId
);
592 } catch (SQLException e
) {
593 throw new RuntimeException("Failed read from recipient store", e
);
598 public void storeProfile(RecipientId recipientId
, final Profile profile
) {
599 try (final var connection
= database
.getConnection()) {
600 storeProfile(connection
, recipientId
, profile
);
601 } catch (SQLException e
) {
602 throw new RuntimeException("Failed update recipient store", e
);
607 public void storeProfileKey(RecipientId recipientId
, final ProfileKey profileKey
) {
608 try (final var connection
= database
.getConnection()) {
609 storeProfileKey(connection
, recipientId
, profileKey
);
610 } catch (SQLException e
) {
611 throw new RuntimeException("Failed update recipient store", e
);
615 public void storeProfileKey(
616 Connection connection
, RecipientId recipientId
, final ProfileKey profileKey
617 ) throws SQLException
{
618 storeProfileKey(connection
, recipientId
, profileKey
, true);
622 public void storeExpiringProfileKeyCredential(
623 RecipientId recipientId
, final ExpiringProfileKeyCredential profileKeyCredential
625 try (final var connection
= database
.getConnection()) {
626 storeExpiringProfileKeyCredential(connection
, recipientId
, profileKeyCredential
);
627 } catch (SQLException e
) {
628 throw new RuntimeException("Failed update recipient store", e
);
632 public void rotateSelfStorageId() {
633 try (final var connection
= database
.getConnection()) {
634 rotateSelfStorageId(connection
);
635 } catch (SQLException e
) {
636 throw new RuntimeException("Failed update recipient store", e
);
640 public void rotateSelfStorageId(final Connection connection
) throws SQLException
{
641 final var selfRecipientId
= resolveRecipient(connection
, selfAddressProvider
.getSelfAddress());
642 rotateStorageId(connection
, selfRecipientId
);
645 public StorageId
rotateStorageId(final Connection connection
, final ServiceId serviceId
) throws SQLException
{
646 final var selfRecipientId
= resolveRecipient(connection
, new RecipientAddress(serviceId
));
647 return rotateStorageId(connection
, selfRecipientId
);
650 public List
<StorageId
> getStorageIds(Connection connection
) throws SQLException
{
653 FROM %s r WHERE r.storage_id IS NOT NULL AND r._id != ? AND (r.aci IS NOT NULL OR r.pni IS NOT NULL)
654 """.formatted(TABLE_RECIPIENT
);
655 final var selfRecipientId
= resolveRecipient(connection
, selfAddressProvider
.getSelfAddress());
656 try (final var statement
= connection
.prepareStatement(sql
)) {
657 statement
.setLong(1, selfRecipientId
.id());
658 return Utils
.executeQueryForStream(statement
, this::getContactStorageIdFromResultSet
).toList();
662 public void updateStorageId(
663 Connection connection
, RecipientId recipientId
, StorageId storageId
664 ) throws SQLException
{
671 ).formatted(TABLE_RECIPIENT
);
672 try (final var statement
= connection
.prepareStatement(sql
)) {
673 statement
.setBytes(1, storageId
.getRaw());
674 statement
.setLong(2, recipientId
.id());
675 statement
.executeUpdate();
679 public void updateStorageIds(Connection connection
, Map
<RecipientId
, StorageId
> storageIdMap
) throws SQLException
{
686 ).formatted(TABLE_RECIPIENT
);
687 try (final var statement
= connection
.prepareStatement(sql
)) {
688 for (final var entry
: storageIdMap
.entrySet()) {
689 statement
.setBytes(1, entry
.getValue().getRaw());
690 statement
.setLong(2, entry
.getKey().id());
691 statement
.executeUpdate();
696 public StorageId
getSelfStorageId(final Connection connection
) throws SQLException
{
697 final var selfRecipientId
= resolveRecipient(connection
, selfAddressProvider
.getSelfAddress());
698 return StorageId
.forAccount(getStorageId(connection
, selfRecipientId
).getRaw());
701 public StorageId
getStorageId(final Connection connection
, final RecipientId recipientId
) throws SQLException
{
704 FROM %s r WHERE r._id = ? AND r.storage_id IS NOT NULL
705 """.formatted(TABLE_RECIPIENT
);
706 try (final var statement
= connection
.prepareStatement(sql
)) {
707 statement
.setLong(1, recipientId
.id());
708 final var storageId
= Utils
.executeQueryForOptional(statement
, this::getContactStorageIdFromResultSet
);
709 if (storageId
.isPresent()) {
710 return storageId
.get();
713 return rotateStorageId(connection
, recipientId
);
716 private StorageId
rotateStorageId(final Connection connection
, final RecipientId recipientId
) throws SQLException
{
717 final var newStorageId
= StorageId
.forAccount(KeyUtils
.createRawStorageId());
718 updateStorageId(connection
, recipientId
, newStorageId
);
722 public void storeStorageRecord(
723 final Connection connection
,
724 final RecipientId recipientId
,
725 final StorageId storageId
,
726 final byte[] storageRecord
727 ) throws SQLException
{
728 final var deleteSql
= (
731 SET storage_id = NULL
734 ).formatted(TABLE_RECIPIENT
);
735 try (final var statement
= connection
.prepareStatement(deleteSql
)) {
736 statement
.setBytes(1, storageId
.getRaw());
737 statement
.executeUpdate();
739 final var insertSql
= (
742 SET storage_id = ?, storage_record = ?
745 ).formatted(TABLE_RECIPIENT
);
746 try (final var statement
= connection
.prepareStatement(insertSql
)) {
747 statement
.setBytes(1, storageId
.getRaw());
748 if (storageRecord
== null) {
749 statement
.setNull(2, Types
.BLOB
);
751 statement
.setBytes(2, storageRecord
);
753 statement
.setLong(3, recipientId
.id());
754 statement
.executeUpdate();
758 void addLegacyRecipients(final Map
<RecipientId
, Recipient
> recipients
) {
759 logger
.debug("Migrating legacy recipients to database");
760 long start
= System
.nanoTime();
763 INSERT INTO %s (_id, number, aci)
766 ).formatted(TABLE_RECIPIENT
);
767 try (final var connection
= database
.getConnection()) {
768 connection
.setAutoCommit(false);
769 try (final var statement
= connection
.prepareStatement("DELETE FROM %s".formatted(TABLE_RECIPIENT
))) {
770 statement
.executeUpdate();
772 try (final var statement
= connection
.prepareStatement(sql
)) {
773 for (final var recipient
: recipients
.values()) {
774 statement
.setLong(1, recipient
.getRecipientId().id());
775 statement
.setString(2, recipient
.getAddress().number().orElse(null));
776 statement
.setString(3, recipient
.getAddress().aci().map(ACI
::toString
).orElse(null));
777 statement
.executeUpdate();
780 logger
.debug("Initial inserts took {}ms", (System
.nanoTime() - start
) / 1000000);
782 for (final var recipient
: recipients
.values()) {
783 if (recipient
.getContact() != null) {
784 storeContact(connection
, recipient
.getRecipientId(), recipient
.getContact());
786 if (recipient
.getProfile() != null) {
787 storeProfile(connection
, recipient
.getRecipientId(), recipient
.getProfile());
789 if (recipient
.getProfileKey() != null) {
790 storeProfileKey(connection
, recipient
.getRecipientId(), recipient
.getProfileKey(), false);
792 if (recipient
.getExpiringProfileKeyCredential() != null) {
793 storeExpiringProfileKeyCredential(connection
,
794 recipient
.getRecipientId(),
795 recipient
.getExpiringProfileKeyCredential());
799 } catch (SQLException e
) {
800 throw new RuntimeException("Failed update recipient store", e
);
802 logger
.debug("Complete recipients migration took {}ms", (System
.nanoTime() - start
) / 1000000);
805 long getActualRecipientId(long recipientId
) {
806 while (recipientsMerged
.containsKey(recipientId
)) {
807 final var newRecipientId
= recipientsMerged
.get(recipientId
);
808 logger
.debug("Using {} instead of {}, because recipients have been merged", newRecipientId
, recipientId
);
809 recipientId
= newRecipientId
;
814 public void storeContact(
815 final Connection connection
, final RecipientId recipientId
, final Contact contact
816 ) throws SQLException
{
820 SET given_name = ?, family_name = ?, nick_name = ?, expiration_time = ?, mute_until = ?, hide_story = ?, profile_sharing = ?, color = ?, blocked = ?, archived = ?, unregistered_timestamp = ?, nick_name_given_name = ?, nick_name_family_name = ?, note = ?
823 ).formatted(TABLE_RECIPIENT
);
824 try (final var statement
= connection
.prepareStatement(sql
)) {
825 statement
.setString(1, contact
== null ?
null : contact
.givenName());
826 statement
.setString(2, contact
== null ?
null : contact
.familyName());
827 statement
.setString(3, contact
== null ?
null : contact
.nickName());
828 statement
.setInt(4, contact
== null ?
0 : contact
.messageExpirationTime());
829 statement
.setLong(5, contact
== null ?
0 : contact
.muteUntil());
830 statement
.setBoolean(6, contact
!= null && contact
.hideStory());
831 statement
.setBoolean(7, contact
!= null && contact
.isProfileSharingEnabled());
832 statement
.setString(8, contact
== null ?
null : contact
.color());
833 statement
.setBoolean(9, contact
!= null && contact
.isBlocked());
834 statement
.setBoolean(10, contact
!= null && contact
.isArchived());
835 if (contact
== null || contact
.unregisteredTimestamp() == null) {
836 statement
.setNull(11, Types
.INTEGER
);
838 statement
.setLong(11, contact
.unregisteredTimestamp());
840 statement
.setString(12, contact
== null ?
null : contact
.nickNameGivenName());
841 statement
.setString(13, contact
== null ?
null : contact
.nickNameFamilyName());
842 statement
.setString(14, contact
== null ?
null : contact
.note());
843 statement
.setLong(15, recipientId
.id());
844 statement
.executeUpdate();
846 if (contact
!= null && contact
.unregisteredTimestamp() != null) {
847 markUnregisteredAndSplitIfNecessary(connection
, recipientId
);
849 rotateStorageId(connection
, recipientId
);
852 public int removeStorageIdsFromLocalOnlyUnregisteredRecipients(
853 final Connection connection
, final List
<StorageId
> storageIds
854 ) throws SQLException
{
858 SET storage_id = NULL
859 WHERE storage_id = ? AND unregistered_timestamp IS NOT NULL
861 ).formatted(TABLE_RECIPIENT
);
863 try (final var statement
= connection
.prepareStatement(sql
)) {
864 for (final var storageId
: storageIds
) {
865 statement
.setBytes(1, storageId
.getRaw());
866 count
+= statement
.executeUpdate();
872 public void markNeedsPniSignature(final RecipientId recipientId
, final boolean value
) {
873 logger
.debug("Marking {} numbers as need pni signature = {}", recipientId
, value
);
874 try (final var connection
= database
.getConnection()) {
878 SET needs_pni_signature = ?
881 ).formatted(TABLE_RECIPIENT
);
882 try (final var statement
= connection
.prepareStatement(sql
)) {
883 statement
.setBoolean(1, value
);
884 statement
.setLong(2, recipientId
.id());
885 statement
.executeUpdate();
887 } catch (SQLException e
) {
888 throw new RuntimeException("Failed update recipient store", e
);
892 public boolean needsPniSignature(final RecipientId recipientId
) {
893 try (final var connection
= database
.getConnection()) {
896 SELECT needs_pni_signature
900 ).formatted(TABLE_RECIPIENT
);
901 try (final var statement
= connection
.prepareStatement(sql
)) {
902 statement
.setLong(1, recipientId
.id());
903 return Utils
.executeQuerySingleRow(statement
, resultSet
-> resultSet
.getBoolean("needs_pni_signature"));
905 } catch (SQLException e
) {
906 throw new RuntimeException("Failed read recipient store", e
);
910 public void markUndiscoverablePossiblyUnregistered(final Set
<String
> numbers
) {
911 logger
.debug("Marking {} numbers as unregistered", numbers
.size());
912 try (final var connection
= database
.getConnection()) {
913 connection
.setAutoCommit(false);
914 for (final var number
: numbers
) {
915 final var recipientAddress
= findByNumber(connection
, number
);
916 if (recipientAddress
.isPresent()) {
917 final var recipientId
= recipientAddress
.get().id();
918 markDiscoverable(connection
, recipientId
, false);
919 final var contact
= getContact(connection
, recipientId
);
920 if (recipientAddress
.get().address().aci().isEmpty() || contact
.unregisteredTimestamp() != null) {
921 markUnregisteredAndSplitIfNecessary(connection
, recipientId
);
926 } catch (SQLException e
) {
927 throw new RuntimeException("Failed update recipient store", e
);
931 public void markDiscoverable(final Set
<String
> numbers
) {
932 logger
.debug("Marking {} numbers as discoverable", numbers
.size());
933 try (final var connection
= database
.getConnection()) {
934 connection
.setAutoCommit(false);
935 for (final var number
: numbers
) {
936 final var recipientAddress
= findByNumber(connection
, number
);
937 if (recipientAddress
.isPresent()) {
938 final var recipientId
= recipientAddress
.get().id();
939 markDiscoverable(connection
, recipientId
, true);
943 } catch (SQLException e
) {
944 throw new RuntimeException("Failed update recipient store", e
);
948 public void markRegistered(final RecipientId recipientId
, final boolean registered
) {
949 logger
.debug("Marking {} as registered={}", recipientId
, registered
);
950 try (final var connection
= database
.getConnection()) {
951 connection
.setAutoCommit(false);
953 markRegistered(connection
, recipientId
);
955 markUnregistered(connection
, recipientId
);
958 } catch (SQLException e
) {
959 throw new RuntimeException("Failed update recipient store", e
);
963 private void markUnregisteredAndSplitIfNecessary(
964 final Connection connection
, final RecipientId recipientId
965 ) throws SQLException
{
966 markUnregistered(connection
, recipientId
);
967 final var address
= resolveRecipientAddress(connection
, recipientId
);
968 if (address
.aci().isPresent() && address
.pni().isPresent()) {
969 final var numberAddress
= new RecipientAddress(address
.pni().get(), address
.number().orElse(null));
970 updateRecipientAddress(connection
, recipientId
, address
.removeIdentifiersFrom(numberAddress
));
971 addNewRecipient(connection
, numberAddress
);
975 private void markDiscoverable(
976 final Connection connection
, final RecipientId recipientId
, final boolean discoverable
977 ) throws SQLException
{
984 ).formatted(TABLE_RECIPIENT
);
985 try (final var statement
= connection
.prepareStatement(sql
)) {
986 statement
.setBoolean(1, discoverable
);
987 statement
.setLong(2, recipientId
.id());
988 statement
.executeUpdate();
992 private void markRegistered(
993 final Connection connection
, final RecipientId recipientId
994 ) throws SQLException
{
998 SET unregistered_timestamp = NULL
1001 ).formatted(TABLE_RECIPIENT
);
1002 try (final var statement
= connection
.prepareStatement(sql
)) {
1003 statement
.setLong(1, recipientId
.id());
1004 statement
.executeUpdate();
1008 private void markUnregistered(
1009 final Connection connection
, final RecipientId recipientId
1010 ) throws SQLException
{
1014 SET unregistered_timestamp = ?, discoverable = FALSE
1017 ).formatted(TABLE_RECIPIENT
);
1018 try (final var statement
= connection
.prepareStatement(sql
)) {
1019 statement
.setLong(1, System
.currentTimeMillis());
1020 statement
.setLong(2, recipientId
.id());
1021 statement
.executeUpdate();
1025 private void storeExpiringProfileKeyCredential(
1026 final Connection connection
,
1027 final RecipientId recipientId
,
1028 final ExpiringProfileKeyCredential profileKeyCredential
1029 ) throws SQLException
{
1033 SET profile_key_credential = ?
1036 ).formatted(TABLE_RECIPIENT
);
1037 try (final var statement
= connection
.prepareStatement(sql
)) {
1038 statement
.setBytes(1, profileKeyCredential
== null ?
null : profileKeyCredential
.serialize());
1039 statement
.setLong(2, recipientId
.id());
1040 statement
.executeUpdate();
1044 public void storeProfile(
1045 final Connection connection
, final RecipientId recipientId
, final Profile profile
1046 ) throws SQLException
{
1050 SET profile_last_update_timestamp = ?, profile_given_name = ?, profile_family_name = ?, profile_about = ?, profile_about_emoji = ?, profile_avatar_url_path = ?, profile_mobile_coin_address = ?, profile_unidentified_access_mode = ?, profile_capabilities = ?, profile_phone_number_sharing = ?
1053 ).formatted(TABLE_RECIPIENT
);
1054 try (final var statement
= connection
.prepareStatement(sql
)) {
1055 statement
.setLong(1, profile
== null ?
0 : profile
.getLastUpdateTimestamp());
1056 statement
.setString(2, profile
== null ?
null : profile
.getGivenName());
1057 statement
.setString(3, profile
== null ?
null : profile
.getFamilyName());
1058 statement
.setString(4, profile
== null ?
null : profile
.getAbout());
1059 statement
.setString(5, profile
== null ?
null : profile
.getAboutEmoji());
1060 statement
.setString(6, profile
== null ?
null : profile
.getAvatarUrlPath());
1061 statement
.setBytes(7, profile
== null ?
null : profile
.getMobileCoinAddress());
1062 statement
.setString(8, profile
== null ?
null : profile
.getUnidentifiedAccessMode().name());
1063 statement
.setString(9,
1066 : profile
.getCapabilities().stream().map(Enum
::name
).collect(Collectors
.joining(",")));
1067 statement
.setString(10,
1068 profile
== null || profile
.getPhoneNumberSharingMode() == null
1070 : profile
.getPhoneNumberSharingMode().name());
1071 statement
.setLong(11, recipientId
.id());
1072 statement
.executeUpdate();
1074 rotateStorageId(connection
, recipientId
);
1077 private void storeProfileKey(
1078 Connection connection
, RecipientId recipientId
, final ProfileKey profileKey
, boolean resetProfile
1079 ) throws SQLException
{
1080 if (profileKey
!= null) {
1081 final var recipientProfileKey
= getProfileKey(connection
, recipientId
);
1082 if (profileKey
.equals(recipientProfileKey
)) {
1083 final var recipientProfile
= getProfile(connection
, recipientId
);
1084 if (recipientProfile
== null || (
1085 recipientProfile
.getUnidentifiedAccessMode() != Profile
.UnidentifiedAccessMode
.UNKNOWN
1086 && recipientProfile
.getUnidentifiedAccessMode()
1087 != Profile
.UnidentifiedAccessMode
.DISABLED
1097 SET profile_key = ?, profile_key_credential = NULL%s
1100 ).formatted(TABLE_RECIPIENT
, resetProfile ?
", profile_last_update_timestamp = 0" : "");
1101 try (final var statement
= connection
.prepareStatement(sql
)) {
1102 statement
.setBytes(1, profileKey
== null ?
null : profileKey
.serialize());
1103 statement
.setLong(2, recipientId
.id());
1104 statement
.executeUpdate();
1106 rotateStorageId(connection
, recipientId
);
1109 private RecipientAddress
resolveRecipientAddress(
1110 final Connection connection
, final RecipientId recipientId
1111 ) throws SQLException
{
1114 SELECT r.number, r.aci, r.pni, r.username
1118 ).formatted(TABLE_RECIPIENT
);
1119 try (final var statement
= connection
.prepareStatement(sql
)) {
1120 statement
.setLong(1, recipientId
.id());
1121 return Utils
.executeQuerySingleRow(statement
, this::getRecipientAddressFromResultSet
);
1125 private RecipientId
resolveRecipientTrusted(RecipientAddress address
, boolean isSelf
) {
1126 final Pair
<RecipientId
, List
<RecipientId
>> pair
;
1127 try (final var connection
= database
.getConnection()) {
1128 connection
.setAutoCommit(false);
1129 pair
= resolveRecipientTrustedLocked(connection
, address
, isSelf
);
1130 connection
.commit();
1131 } catch (SQLException e
) {
1132 throw new RuntimeException("Failed update recipient store", e
);
1135 if (!pair
.second().isEmpty()) {
1136 logger
.debug("Resolved address {}, merging {} other recipients", address
, pair
.second().size());
1137 try (final var connection
= database
.getConnection()) {
1138 connection
.setAutoCommit(false);
1139 mergeRecipients(connection
, pair
.first(), pair
.second());
1140 connection
.commit();
1141 } catch (SQLException e
) {
1142 throw new RuntimeException("Failed update recipient store", e
);
1145 return pair
.first();
1148 private Pair
<RecipientId
, List
<RecipientId
>> resolveRecipientTrustedLocked(
1149 final Connection connection
, final RecipientAddress address
, final boolean isSelf
1150 ) throws SQLException
{
1151 if (address
.hasSingleIdentifier() || (
1152 !isSelf
&& selfAddressProvider
.getSelfAddress().matches(address
)
1154 return new Pair
<>(resolveRecipientLocked(connection
, address
), List
.of());
1156 final var pair
= MergeRecipientHelper
.resolveRecipientTrustedLocked(new HelperStore(connection
), address
);
1157 markRegistered(connection
, pair
.first());
1159 for (final var toBeMergedRecipientId
: pair
.second()) {
1160 mergeRecipientsLocked(connection
, pair
.first(), toBeMergedRecipientId
);
1166 private void mergeRecipients(
1167 final Connection connection
, final RecipientId recipientId
, final List
<RecipientId
> toBeMergedRecipientIds
1168 ) throws SQLException
{
1169 for (final var toBeMergedRecipientId
: toBeMergedRecipientIds
) {
1170 recipientMergeHandler
.mergeRecipients(connection
, recipientId
, toBeMergedRecipientId
);
1171 deleteRecipient(connection
, toBeMergedRecipientId
);
1172 recipientAddressCache
.entrySet().removeIf(e
-> e
.getValue().id().equals(toBeMergedRecipientId
));
1176 private RecipientId
resolveRecipientLocked(
1177 Connection connection
, RecipientAddress address
1178 ) throws SQLException
{
1179 final var byAci
= address
.aci().isEmpty()
1180 ? Optional
.<RecipientWithAddress
>empty()
1181 : findByServiceId(connection
, address
.aci().get());
1183 if (byAci
.isPresent()) {
1184 return byAci
.get().id();
1187 final var byPni
= address
.pni().isEmpty()
1188 ? Optional
.<RecipientWithAddress
>empty()
1189 : findByServiceId(connection
, address
.pni().get());
1191 if (byPni
.isPresent()) {
1192 return byPni
.get().id();
1195 final var byNumber
= address
.number().isEmpty()
1196 ? Optional
.<RecipientWithAddress
>empty()
1197 : findByNumber(connection
, address
.number().get());
1199 if (byNumber
.isPresent()) {
1200 return byNumber
.get().id();
1203 logger
.debug("Got new recipient, both serviceId and number are unknown");
1205 if (address
.serviceId().isEmpty()) {
1206 return addNewRecipient(connection
, address
);
1209 return addNewRecipient(connection
, new RecipientAddress(address
.serviceId().get()));
1212 private RecipientId
resolveRecipientLocked(Connection connection
, ServiceId serviceId
) throws SQLException
{
1213 final var recipient
= findByServiceId(connection
, serviceId
);
1215 if (recipient
.isEmpty()) {
1216 logger
.debug("Got new recipient, serviceId is unknown");
1217 return addNewRecipient(connection
, new RecipientAddress(serviceId
));
1220 return recipient
.get().id();
1223 private RecipientId
resolveRecipientLocked(Connection connection
, String number
) throws SQLException
{
1224 final var recipient
= findByNumber(connection
, number
);
1226 if (recipient
.isEmpty()) {
1227 logger
.debug("Got new recipient, number is unknown");
1228 return addNewRecipient(connection
, new RecipientAddress(number
));
1231 return recipient
.get().id();
1234 private RecipientId
addNewRecipient(
1235 final Connection connection
, final RecipientAddress address
1236 ) throws SQLException
{
1239 INSERT INTO %s (number, aci, pni, username)
1243 ).formatted(TABLE_RECIPIENT
);
1244 try (final var statement
= connection
.prepareStatement(sql
)) {
1245 statement
.setString(1, address
.number().orElse(null));
1246 statement
.setString(2, address
.aci().map(ACI
::toString
).orElse(null));
1247 statement
.setString(3, address
.pni().map(PNI
::toString
).orElse(null));
1248 statement
.setString(4, address
.username().orElse(null));
1249 final var generatedKey
= Utils
.executeQueryForOptional(statement
, Utils
::getIdMapper
);
1250 if (generatedKey
.isPresent()) {
1251 final var recipientId
= new RecipientId(generatedKey
.get(), this);
1252 logger
.debug("Added new recipient {} with address {}", recipientId
, address
);
1255 throw new RuntimeException("Failed to add new recipient to database");
1260 private void removeRecipientAddress(Connection connection
, RecipientId recipientId
) throws SQLException
{
1261 recipientAddressCache
.entrySet().removeIf(e
-> e
.getValue().id().equals(recipientId
));
1265 SET number = NULL, aci = NULL, pni = NULL, username = NULL, storage_id = NULL
1268 ).formatted(TABLE_RECIPIENT
);
1269 try (final var statement
= connection
.prepareStatement(sql
)) {
1270 statement
.setLong(1, recipientId
.id());
1271 statement
.executeUpdate();
1275 private void updateRecipientAddress(
1276 Connection connection
, RecipientId recipientId
, final RecipientAddress address
1277 ) throws SQLException
{
1278 recipientAddressCache
.entrySet().removeIf(e
-> e
.getValue().id().equals(recipientId
));
1282 SET number = ?, aci = ?, pni = ?, username = ?
1285 ).formatted(TABLE_RECIPIENT
);
1286 try (final var statement
= connection
.prepareStatement(sql
)) {
1287 statement
.setString(1, address
.number().orElse(null));
1288 statement
.setString(2, address
.aci().map(ACI
::toString
).orElse(null));
1289 statement
.setString(3, address
.pni().map(PNI
::toString
).orElse(null));
1290 statement
.setString(4, address
.username().orElse(null));
1291 statement
.setLong(5, recipientId
.id());
1292 statement
.executeUpdate();
1294 rotateStorageId(connection
, recipientId
);
1297 private void deleteRecipient(final Connection connection
, final RecipientId recipientId
) throws SQLException
{
1303 ).formatted(TABLE_RECIPIENT
);
1304 try (final var statement
= connection
.prepareStatement(sql
)) {
1305 statement
.setLong(1, recipientId
.id());
1306 statement
.executeUpdate();
1310 private void mergeRecipientsLocked(
1311 Connection connection
, RecipientId recipientId
, RecipientId toBeMergedRecipientId
1312 ) throws SQLException
{
1313 final var contact
= getContact(connection
, recipientId
);
1314 if (contact
== null) {
1315 final var toBeMergedContact
= getContact(connection
, toBeMergedRecipientId
);
1316 storeContact(connection
, recipientId
, toBeMergedContact
);
1319 final var profileKey
= getProfileKey(connection
, recipientId
);
1320 if (profileKey
== null) {
1321 final var toBeMergedProfileKey
= getProfileKey(connection
, toBeMergedRecipientId
);
1322 storeProfileKey(connection
, recipientId
, toBeMergedProfileKey
, false);
1325 final var profileKeyCredential
= getExpiringProfileKeyCredential(connection
, recipientId
);
1326 if (profileKeyCredential
== null) {
1327 final var toBeMergedProfileKeyCredential
= getExpiringProfileKeyCredential(connection
,
1328 toBeMergedRecipientId
);
1329 storeExpiringProfileKeyCredential(connection
, recipientId
, toBeMergedProfileKeyCredential
);
1332 final var profile
= getProfile(connection
, recipientId
);
1333 if (profile
== null) {
1334 final var toBeMergedProfile
= getProfile(connection
, toBeMergedRecipientId
);
1335 storeProfile(connection
, recipientId
, toBeMergedProfile
);
1338 recipientsMerged
.put(toBeMergedRecipientId
.id(), recipientId
.id());
1341 private Optional
<RecipientWithAddress
> findByNumber(
1342 final Connection connection
, final String number
1343 ) throws SQLException
{
1345 SELECT r._id, r.number, r.aci, r.pni, r.username
1349 """.formatted(TABLE_RECIPIENT
);
1350 try (final var statement
= connection
.prepareStatement(sql
)) {
1351 statement
.setString(1, number
);
1352 return Utils
.executeQueryForOptional(statement
, this::getRecipientWithAddressFromResultSet
);
1356 private Optional
<RecipientWithAddress
> findByUsername(
1357 final Connection connection
, final String username
1358 ) throws SQLException
{
1360 SELECT r._id, r.number, r.aci, r.pni, r.username
1362 WHERE r.username = ?
1364 """.formatted(TABLE_RECIPIENT
);
1365 try (final var statement
= connection
.prepareStatement(sql
)) {
1366 statement
.setString(1, username
);
1367 return Utils
.executeQueryForOptional(statement
, this::getRecipientWithAddressFromResultSet
);
1371 private Optional
<RecipientWithAddress
> findByServiceId(
1372 final Connection connection
, final ServiceId serviceId
1373 ) throws SQLException
{
1374 var recipientWithAddress
= Optional
.ofNullable(recipientAddressCache
.get(serviceId
));
1375 if (recipientWithAddress
.isPresent()) {
1376 return recipientWithAddress
;
1379 SELECT r._id, r.number, r.aci, r.pni, r.username
1383 """.formatted(TABLE_RECIPIENT
, serviceId
instanceof ACI ?
"r.aci" : "r.pni");
1384 try (final var statement
= connection
.prepareStatement(sql
)) {
1385 statement
.setString(1, serviceId
.toString());
1386 recipientWithAddress
= Utils
.executeQueryForOptional(statement
, this::getRecipientWithAddressFromResultSet
);
1387 recipientWithAddress
.ifPresent(r
-> recipientAddressCache
.put(serviceId
, r
));
1388 return recipientWithAddress
;
1392 private Set
<RecipientWithAddress
> findAllByAddress(
1393 final Connection connection
, final RecipientAddress address
1394 ) throws SQLException
{
1396 SELECT r._id, r.number, r.aci, r.pni, r.username
1402 """.formatted(TABLE_RECIPIENT
);
1403 try (final var statement
= connection
.prepareStatement(sql
)) {
1404 statement
.setString(1, address
.aci().map(ServiceId
::toString
).orElse(null));
1405 statement
.setString(2, address
.pni().map(ServiceId
::toString
).orElse(null));
1406 statement
.setString(3, address
.number().orElse(null));
1407 statement
.setString(4, address
.username().orElse(null));
1408 return Utils
.executeQueryForStream(statement
, this::getRecipientWithAddressFromResultSet
)
1409 .collect(Collectors
.toSet());
1413 private Contact
getContact(final Connection connection
, final RecipientId recipientId
) throws SQLException
{
1416 SELECT r.given_name, r.family_name, r.nick_name, r.nick_name_given_name, r.nick_name_family_name, r.note, r.expiration_time, r.mute_until, r.hide_story, r.profile_sharing, r.color, r.blocked, r.archived, r.hidden, r.unregistered_timestamp
1418 WHERE r._id = ? AND (%s)
1420 ).formatted(TABLE_RECIPIENT
, SQL_IS_CONTACT
);
1421 try (final var statement
= connection
.prepareStatement(sql
)) {
1422 statement
.setLong(1, recipientId
.id());
1423 return Utils
.executeQueryForOptional(statement
, this::getContactFromResultSet
).orElse(null);
1427 private ProfileKey
getProfileKey(final Connection connection
, final RecipientId recipientId
) throws SQLException
{
1428 final var selfRecipientId
= resolveRecipientLocked(connection
, selfAddressProvider
.getSelfAddress());
1429 if (recipientId
.equals(selfRecipientId
)) {
1430 return selfProfileKeyProvider
.getSelfProfileKey();
1434 SELECT r.profile_key
1438 ).formatted(TABLE_RECIPIENT
);
1439 try (final var statement
= connection
.prepareStatement(sql
)) {
1440 statement
.setLong(1, recipientId
.id());
1441 return Utils
.executeQueryForOptional(statement
, this::getProfileKeyFromResultSet
).orElse(null);
1445 private ExpiringProfileKeyCredential
getExpiringProfileKeyCredential(
1446 final Connection connection
, final RecipientId recipientId
1447 ) throws SQLException
{
1450 SELECT r.profile_key_credential
1454 ).formatted(TABLE_RECIPIENT
);
1455 try (final var statement
= connection
.prepareStatement(sql
)) {
1456 statement
.setLong(1, recipientId
.id());
1457 return Utils
.executeQueryForOptional(statement
, this::getExpiringProfileKeyCredentialFromResultSet
)
1462 public Profile
getProfile(final Connection connection
, final RecipientId recipientId
) throws SQLException
{
1465 SELECT r.profile_last_update_timestamp, r.profile_given_name, r.profile_family_name, r.profile_about, r.profile_about_emoji, r.profile_avatar_url_path, r.profile_mobile_coin_address, r.profile_unidentified_access_mode, r.profile_capabilities, r.profile_phone_number_sharing
1467 WHERE r._id = ? AND r.profile_capabilities IS NOT NULL
1469 ).formatted(TABLE_RECIPIENT
);
1470 try (final var statement
= connection
.prepareStatement(sql
)) {
1471 statement
.setLong(1, recipientId
.id());
1472 return Utils
.executeQueryForOptional(statement
, this::getProfileFromResultSet
).orElse(null);
1476 private RecipientAddress
getRecipientAddressFromResultSet(ResultSet resultSet
) throws SQLException
{
1477 final var aci
= Optional
.ofNullable(resultSet
.getString("aci")).map(ACI
::parseOrNull
);
1478 final var pni
= Optional
.ofNullable(resultSet
.getString("pni")).map(PNI
::parseOrNull
);
1479 final var number
= Optional
.ofNullable(resultSet
.getString("number"));
1480 final var username
= Optional
.ofNullable(resultSet
.getString("username"));
1481 return new RecipientAddress(aci
, pni
, number
, username
);
1484 private RecipientId
getRecipientIdFromResultSet(ResultSet resultSet
) throws SQLException
{
1485 return new RecipientId(resultSet
.getLong("_id"), this);
1488 private RecipientWithAddress
getRecipientWithAddressFromResultSet(final ResultSet resultSet
) throws SQLException
{
1489 return new RecipientWithAddress(getRecipientIdFromResultSet(resultSet
),
1490 getRecipientAddressFromResultSet(resultSet
));
1493 private Recipient
getRecipientFromResultSet(final ResultSet resultSet
) throws SQLException
{
1494 return new Recipient(getRecipientIdFromResultSet(resultSet
),
1495 getRecipientAddressFromResultSet(resultSet
),
1496 getContactFromResultSet(resultSet
),
1497 getProfileKeyFromResultSet(resultSet
),
1498 getExpiringProfileKeyCredentialFromResultSet(resultSet
),
1499 getProfileFromResultSet(resultSet
),
1500 getDiscoverableFromResultSet(resultSet
),
1501 getStorageRecordFromResultSet(resultSet
));
1504 private Contact
getContactFromResultSet(ResultSet resultSet
) throws SQLException
{
1505 final var unregisteredTimestamp
= resultSet
.getLong("unregistered_timestamp");
1506 return new Contact(resultSet
.getString("given_name"),
1507 resultSet
.getString("family_name"),
1508 resultSet
.getString("nick_name"),
1509 resultSet
.getString("nick_name_given_name"),
1510 resultSet
.getString("nick_name_family_name"),
1511 resultSet
.getString("note"),
1512 resultSet
.getString("color"),
1513 resultSet
.getInt("expiration_time"),
1514 resultSet
.getLong("mute_until"),
1515 resultSet
.getBoolean("hide_story"),
1516 resultSet
.getBoolean("blocked"),
1517 resultSet
.getBoolean("archived"),
1518 resultSet
.getBoolean("profile_sharing"),
1519 resultSet
.getBoolean("hidden"),
1520 unregisteredTimestamp
== 0 ?
null : unregisteredTimestamp
);
1523 private static Boolean
getDiscoverableFromResultSet(final ResultSet resultSet
) throws SQLException
{
1524 final var discoverable
= resultSet
.getBoolean("discoverable");
1525 if (resultSet
.wasNull()) {
1528 return discoverable
;
1531 private Profile
getProfileFromResultSet(ResultSet resultSet
) throws SQLException
{
1532 final var profileCapabilities
= resultSet
.getString("profile_capabilities");
1533 final var profileUnidentifiedAccessMode
= resultSet
.getString("profile_unidentified_access_mode");
1534 return new Profile(resultSet
.getLong("profile_last_update_timestamp"),
1535 resultSet
.getString("profile_given_name"),
1536 resultSet
.getString("profile_family_name"),
1537 resultSet
.getString("profile_about"),
1538 resultSet
.getString("profile_about_emoji"),
1539 resultSet
.getString("profile_avatar_url_path"),
1540 resultSet
.getBytes("profile_mobile_coin_address"),
1541 profileUnidentifiedAccessMode
== null
1542 ? Profile
.UnidentifiedAccessMode
.UNKNOWN
1543 : Profile
.UnidentifiedAccessMode
.valueOfOrUnknown(profileUnidentifiedAccessMode
),
1544 profileCapabilities
== null
1546 : Arrays
.stream(profileCapabilities
.split(","))
1547 .map(Profile
.Capability
::valueOfOrNull
)
1548 .filter(Objects
::nonNull
)
1549 .collect(Collectors
.toSet()),
1550 PhoneNumberSharingMode
.valueOfOrNull(resultSet
.getString("profile_phone_number_sharing")));
1553 private ProfileKey
getProfileKeyFromResultSet(ResultSet resultSet
) throws SQLException
{
1554 final var profileKey
= resultSet
.getBytes("profile_key");
1556 if (profileKey
== null) {
1560 return new ProfileKey(profileKey
);
1561 } catch (InvalidInputException ignored
) {
1566 private ExpiringProfileKeyCredential
getExpiringProfileKeyCredentialFromResultSet(ResultSet resultSet
) throws SQLException
{
1567 final var profileKeyCredential
= resultSet
.getBytes("profile_key_credential");
1569 if (profileKeyCredential
== null) {
1573 return new ExpiringProfileKeyCredential(profileKeyCredential
);
1574 } catch (Throwable ignored
) {
1579 private StorageId
getContactStorageIdFromResultSet(ResultSet resultSet
) throws SQLException
{
1580 final var storageId
= resultSet
.getBytes("storage_id");
1581 return StorageId
.forContact(storageId
);
1584 private byte[] getStorageRecordFromResultSet(ResultSet resultSet
) throws SQLException
{
1585 return resultSet
.getBytes("storage_record");
1588 public interface RecipientMergeHandler
{
1590 void mergeRecipients(
1591 final Connection connection
, RecipientId recipientId
, RecipientId toBeMergedRecipientId
1592 ) throws SQLException
;
1595 private class HelperStore
implements MergeRecipientHelper
.Store
{
1597 private final Connection connection
;
1599 public HelperStore(final Connection connection
) {
1600 this.connection
= connection
;
1604 public Set
<RecipientWithAddress
> findAllByAddress(final RecipientAddress address
) throws SQLException
{
1605 return RecipientStore
.this.findAllByAddress(connection
, address
);
1609 public RecipientId
addNewRecipient(final RecipientAddress address
) throws SQLException
{
1610 return RecipientStore
.this.addNewRecipient(connection
, address
);
1614 public void updateRecipientAddress(
1615 final RecipientId recipientId
, final RecipientAddress address
1616 ) throws SQLException
{
1617 RecipientStore
.this.updateRecipientAddress(connection
, recipientId
, address
);
1621 public void removeRecipientAddress(final RecipientId recipientId
) throws SQLException
{
1622 RecipientStore
.this.removeRecipientAddress(connection
, recipientId
);