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
.Profile
;
6 import org
.asamk
.signal
.manager
.api
.UnregisteredRecipientException
;
7 import org
.asamk
.signal
.manager
.storage
.Database
;
8 import org
.asamk
.signal
.manager
.storage
.Utils
;
9 import org
.asamk
.signal
.manager
.storage
.contacts
.ContactsStore
;
10 import org
.asamk
.signal
.manager
.storage
.profiles
.ProfileStore
;
11 import org
.signal
.libsignal
.zkgroup
.InvalidInputException
;
12 import org
.signal
.libsignal
.zkgroup
.profiles
.ExpiringProfileKeyCredential
;
13 import org
.signal
.libsignal
.zkgroup
.profiles
.ProfileKey
;
14 import org
.slf4j
.Logger
;
15 import org
.slf4j
.LoggerFactory
;
16 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
;
17 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
.ACI
;
18 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
.PNI
;
19 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
20 import org
.whispersystems
.signalservice
.api
.util
.UuidUtil
;
22 import java
.sql
.Connection
;
23 import java
.sql
.ResultSet
;
24 import java
.sql
.SQLException
;
25 import java
.util
.ArrayList
;
26 import java
.util
.Arrays
;
27 import java
.util
.Collection
;
28 import java
.util
.HashMap
;
29 import java
.util
.List
;
31 import java
.util
.Objects
;
32 import java
.util
.Optional
;
34 import java
.util
.function
.Supplier
;
35 import java
.util
.stream
.Collectors
;
37 public class RecipientStore
implements RecipientIdCreator
, RecipientResolver
, RecipientTrustedResolver
, ContactsStore
, ProfileStore
{
39 private final static Logger logger
= LoggerFactory
.getLogger(RecipientStore
.class);
40 private static final String TABLE_RECIPIENT
= "recipient";
41 private static final String SQL_IS_CONTACT
= "r.given_name IS NOT NULL OR r.family_name 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";
43 private final RecipientMergeHandler recipientMergeHandler
;
44 private final SelfAddressProvider selfAddressProvider
;
45 private final Database database
;
47 private final Object recipientsLock
= new Object();
48 private final Map
<Long
, Long
> recipientsMerged
= new HashMap
<>();
50 public static void createSql(Connection connection
) throws SQLException
{
51 // When modifying the CREATE statement here, also add a migration in AccountDatabase.java
52 try (final var statement
= connection
.createStatement()) {
53 statement
.executeUpdate("""
54 CREATE TABLE recipient (
55 _id INTEGER PRIMARY KEY AUTOINCREMENT,
61 profile_key_credential BLOB,
67 expiration_time INTEGER NOT NULL DEFAULT 0,
68 blocked INTEGER NOT NULL DEFAULT FALSE,
69 archived INTEGER NOT NULL DEFAULT FALSE,
70 profile_sharing INTEGER NOT NULL DEFAULT FALSE,
72 profile_last_update_timestamp INTEGER NOT NULL DEFAULT 0,
73 profile_given_name TEXT,
74 profile_family_name TEXT,
76 profile_about_emoji TEXT,
77 profile_avatar_url_path TEXT,
78 profile_mobile_coin_address BLOB,
79 profile_unidentified_access_mode TEXT,
80 profile_capabilities TEXT
86 public RecipientStore(
87 final RecipientMergeHandler recipientMergeHandler
,
88 final SelfAddressProvider selfAddressProvider
,
89 final Database database
91 this.recipientMergeHandler
= recipientMergeHandler
;
92 this.selfAddressProvider
= selfAddressProvider
;
93 this.database
= database
;
96 public RecipientAddress
resolveRecipientAddress(RecipientId recipientId
) {
99 SELECT r.number, r.uuid, r.pni, r.username
103 ).formatted(TABLE_RECIPIENT
);
104 try (final var connection
= database
.getConnection()) {
105 try (final var statement
= connection
.prepareStatement(sql
)) {
106 statement
.setLong(1, recipientId
.id());
107 return Utils
.executeQuerySingleRow(statement
, this::getRecipientAddressFromResultSet
);
109 } catch (SQLException e
) {
110 throw new RuntimeException("Failed read from recipient store", e
);
114 public Collection
<RecipientId
> getRecipientIdsWithEnabledProfileSharing() {
119 WHERE r.blocked = FALSE AND r.profile_sharing = TRUE
121 ).formatted(TABLE_RECIPIENT
);
122 try (final var connection
= database
.getConnection()) {
123 try (final var statement
= connection
.prepareStatement(sql
)) {
124 try (var result
= Utils
.executeQueryForStream(statement
, this::getRecipientIdFromResultSet
)) {
125 return result
.toList();
128 } catch (SQLException e
) {
129 throw new RuntimeException("Failed read from recipient store", e
);
134 public RecipientId
resolveRecipient(final long rawRecipientId
) {
141 ).formatted(TABLE_RECIPIENT
);
142 try (final var connection
= database
.getConnection()) {
143 try (final var statement
= connection
.prepareStatement(sql
)) {
144 statement
.setLong(1, rawRecipientId
);
145 return Utils
.executeQueryForOptional(statement
, this::getRecipientIdFromResultSet
).orElse(null);
147 } catch (SQLException e
) {
148 throw new RuntimeException("Failed read from recipient store", e
);
153 public RecipientId
resolveRecipient(final String identifier
) {
154 final var serviceId
= ServiceId
.parseOrNull(identifier
);
155 if (serviceId
!= null) {
156 return resolveRecipient(serviceId
);
158 return resolveRecipientByNumber(identifier
);
162 private RecipientId
resolveRecipientByNumber(final String number
) {
163 synchronized (recipientsLock
) {
164 final RecipientId recipientId
;
165 try (final var connection
= database
.getConnection()) {
166 connection
.setAutoCommit(false);
167 recipientId
= resolveRecipientLocked(connection
, number
);
169 } catch (SQLException e
) {
170 throw new RuntimeException("Failed read recipient store", e
);
177 public RecipientId
resolveRecipient(final ServiceId serviceId
) {
178 synchronized (recipientsLock
) {
179 final RecipientId recipientId
;
180 try (final var connection
= database
.getConnection()) {
181 connection
.setAutoCommit(false);
182 recipientId
= resolveRecipientLocked(connection
, serviceId
);
184 } catch (SQLException e
) {
185 throw new RuntimeException("Failed read recipient store", e
);
192 * Should only be used for recipientIds from the database.
193 * Where the foreign key relations ensure a valid recipientId.
196 public RecipientId
create(final long recipientId
) {
197 return new RecipientId(recipientId
, this);
200 public RecipientId
resolveRecipientByNumber(
201 final String number
, Supplier
<ServiceId
> serviceIdSupplier
202 ) throws UnregisteredRecipientException
{
203 final Optional
<RecipientWithAddress
> byNumber
;
204 try (final var connection
= database
.getConnection()) {
205 byNumber
= findByNumber(connection
, number
);
206 } catch (SQLException e
) {
207 throw new RuntimeException("Failed read from recipient store", e
);
209 if (byNumber
.isEmpty() || byNumber
.get().address().serviceId().isEmpty()) {
210 final var serviceId
= serviceIdSupplier
.get();
211 if (serviceId
== null) {
212 throw new UnregisteredRecipientException(new org
.asamk
.signal
.manager
.api
.RecipientAddress(null,
216 return resolveRecipient(serviceId
);
218 return byNumber
.get().id();
221 public Optional
<RecipientId
> resolveRecipientByNumberOptional(final String number
) {
222 final Optional
<RecipientWithAddress
> byNumber
;
223 try (final var connection
= database
.getConnection()) {
224 byNumber
= findByNumber(connection
, number
);
225 } catch (SQLException e
) {
226 throw new RuntimeException("Failed read from recipient store", e
);
228 return byNumber
.map(RecipientWithAddress
::id
);
231 public RecipientId
resolveRecipientByUsername(
232 final String username
, Supplier
<ServiceId
> serviceIdSupplier
233 ) throws UnregisteredRecipientException
{
234 final Optional
<RecipientWithAddress
> byUsername
;
235 try (final var connection
= database
.getConnection()) {
236 byUsername
= findByUsername(connection
, username
);
237 } catch (SQLException e
) {
238 throw new RuntimeException("Failed read from recipient store", e
);
240 if (byUsername
.isEmpty() || byUsername
.get().address().serviceId().isEmpty()) {
241 final var serviceId
= serviceIdSupplier
.get();
242 if (serviceId
== null) {
243 throw new UnregisteredRecipientException(new org
.asamk
.signal
.manager
.api
.RecipientAddress(null,
248 return resolveRecipient(serviceId
);
250 return byUsername
.get().id();
253 public RecipientId
resolveRecipient(RecipientAddress address
) {
254 synchronized (recipientsLock
) {
255 final RecipientId recipientId
;
256 try (final var connection
= database
.getConnection()) {
257 connection
.setAutoCommit(false);
258 recipientId
= resolveRecipientLocked(connection
, address
);
260 } catch (SQLException e
) {
261 throw new RuntimeException("Failed read recipient store", e
);
268 public RecipientId
resolveSelfRecipientTrusted(RecipientAddress address
) {
269 return resolveRecipientTrusted(address
, true);
272 public RecipientId
resolveRecipientTrusted(RecipientAddress address
) {
273 return resolveRecipientTrusted(address
, false);
277 public RecipientId
resolveRecipientTrusted(SignalServiceAddress address
) {
278 return resolveRecipientTrusted(new RecipientAddress(address
), false);
282 public RecipientId
resolveRecipientTrusted(
283 final Optional
<ACI
> aci
, final Optional
<PNI
> pni
, final Optional
<String
> number
285 final var serviceId
= aci
.map(a
-> (ServiceId
) a
).or(() -> pni
);
286 return resolveRecipientTrusted(new RecipientAddress(serviceId
, pni
, number
, Optional
.empty()), false);
290 public RecipientId
resolveRecipientTrusted(final ServiceId serviceId
, final String username
) {
291 return resolveRecipientTrusted(new RecipientAddress(serviceId
, null, null, username
), false);
294 public RecipientId
resolveRecipientTrusted(
295 final ACI aci
, final String username
297 return resolveRecipientTrusted(new RecipientAddress(Optional
.of(aci
),
300 Optional
.of(username
)), false);
304 public void storeContact(RecipientId recipientId
, final Contact contact
) {
305 try (final var connection
= database
.getConnection()) {
306 storeContact(connection
, recipientId
, contact
);
307 } catch (SQLException e
) {
308 throw new RuntimeException("Failed update recipient store", e
);
313 public Contact
getContact(RecipientId recipientId
) {
314 try (final var connection
= database
.getConnection()) {
315 return getContact(connection
, recipientId
);
316 } catch (SQLException e
) {
317 throw new RuntimeException("Failed read from recipient store", e
);
322 public List
<Pair
<RecipientId
, Contact
>> getContacts() {
325 SELECT r._id, r.given_name, r.family_name, r.expiration_time, r.profile_sharing, r.color, r.blocked, r.archived
327 WHERE (r.number IS NOT NULL OR r.uuid IS NOT NULL) AND %s
329 ).formatted(TABLE_RECIPIENT
, SQL_IS_CONTACT
);
330 try (final var connection
= database
.getConnection()) {
331 try (final var statement
= connection
.prepareStatement(sql
)) {
332 try (var result
= Utils
.executeQueryForStream(statement
,
333 resultSet
-> new Pair
<>(getRecipientIdFromResultSet(resultSet
),
334 getContactFromResultSet(resultSet
)))) {
335 return result
.toList();
338 } catch (SQLException e
) {
339 throw new RuntimeException("Failed read from recipient store", e
);
343 public List
<Recipient
> getRecipients(
344 boolean onlyContacts
, Optional
<Boolean
> blocked
, Set
<RecipientId
> recipientIds
, Optional
<String
> name
346 final var sqlWhere
= new ArrayList
<String
>();
348 sqlWhere
.add("(" + SQL_IS_CONTACT
+ ")");
350 if (blocked
.isPresent()) {
351 sqlWhere
.add("r.blocked = ?");
353 if (!recipientIds
.isEmpty()) {
354 final var recipientIdsCommaSeparated
= recipientIds
.stream()
355 .map(recipientId
-> String
.valueOf(recipientId
.id()))
356 .collect(Collectors
.joining(","));
357 sqlWhere
.add("r._id IN (" + recipientIdsCommaSeparated
+ ")");
362 r.number, r.uuid, r.pni, r.username,
363 r.profile_key, r.profile_key_credential,
364 r.given_name, r.family_name, r.expiration_time, r.profile_sharing, r.color, r.blocked, r.archived,
365 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
367 WHERE (r.number IS NOT NULL OR r.uuid IS NOT NULL) AND %s
369 ).formatted(TABLE_RECIPIENT
, sqlWhere
.size() == 0 ?
"TRUE" : String
.join(" AND ", sqlWhere
));
370 try (final var connection
= database
.getConnection()) {
371 try (final var statement
= connection
.prepareStatement(sql
)) {
372 if (blocked
.isPresent()) {
373 statement
.setBoolean(1, blocked
.get());
375 try (var result
= Utils
.executeQueryForStream(statement
, this::getRecipientFromResultSet
)) {
376 return result
.filter(r
-> name
.isEmpty() || (
377 r
.getContact() != null && name
.get().equals(r
.getContact().getName())
378 ) || (r
.getProfile() != null && name
.get().equals(r
.getProfile().getDisplayName()))).toList();
381 } catch (SQLException e
) {
382 throw new RuntimeException("Failed read from recipient store", e
);
386 public Set
<String
> getAllNumbers() {
391 WHERE r.number IS NOT NULL
393 ).formatted(TABLE_RECIPIENT
);
394 try (final var connection
= database
.getConnection()) {
395 try (final var statement
= connection
.prepareStatement(sql
)) {
396 return Utils
.executeQueryForStream(statement
, resultSet
-> resultSet
.getString("number"))
397 .filter(Objects
::nonNull
)
398 .collect(Collectors
.toSet());
400 } catch (SQLException e
) {
401 throw new RuntimeException("Failed read from recipient store", e
);
405 public Map
<ServiceId
, ProfileKey
> getServiceIdToProfileKeyMap() {
408 SELECT r.uuid, r.profile_key
410 WHERE r.uuid IS NOT NULL AND r.profile_key IS NOT NULL
412 ).formatted(TABLE_RECIPIENT
);
413 try (final var connection
= database
.getConnection()) {
414 try (final var statement
= connection
.prepareStatement(sql
)) {
415 return Utils
.executeQueryForStream(statement
, resultSet
-> {
416 final var serviceId
= ServiceId
.parseOrThrow(resultSet
.getBytes("uuid"));
417 final var profileKey
= getProfileKeyFromResultSet(resultSet
);
418 return new Pair
<>(serviceId
, profileKey
);
419 }).filter(Objects
::nonNull
).collect(Collectors
.toMap(Pair
::first
, Pair
::second
));
421 } catch (SQLException e
) {
422 throw new RuntimeException("Failed read from recipient store", e
);
427 public void deleteContact(RecipientId recipientId
) {
428 storeContact(recipientId
, null);
431 public void deleteRecipientData(RecipientId recipientId
) {
432 logger
.debug("Deleting recipient data for {}", recipientId
);
433 try (final var connection
= database
.getConnection()) {
434 connection
.setAutoCommit(false);
435 storeContact(connection
, recipientId
, null);
436 storeProfile(connection
, recipientId
, null);
437 storeProfileKey(connection
, recipientId
, null, false);
438 storeExpiringProfileKeyCredential(connection
, recipientId
, null);
439 deleteRecipient(connection
, recipientId
);
441 } catch (SQLException e
) {
442 throw new RuntimeException("Failed update recipient store", e
);
447 public Profile
getProfile(final RecipientId recipientId
) {
448 try (final var connection
= database
.getConnection()) {
449 return getProfile(connection
, recipientId
);
450 } catch (SQLException e
) {
451 throw new RuntimeException("Failed read from recipient store", e
);
456 public ProfileKey
getProfileKey(final RecipientId recipientId
) {
457 try (final var connection
= database
.getConnection()) {
458 return getProfileKey(connection
, recipientId
);
459 } catch (SQLException e
) {
460 throw new RuntimeException("Failed read from recipient store", e
);
465 public ExpiringProfileKeyCredential
getExpiringProfileKeyCredential(final RecipientId recipientId
) {
466 try (final var connection
= database
.getConnection()) {
467 return getExpiringProfileKeyCredential(connection
, recipientId
);
468 } catch (SQLException e
) {
469 throw new RuntimeException("Failed read from recipient store", e
);
474 public void storeProfile(RecipientId recipientId
, final Profile profile
) {
475 try (final var connection
= database
.getConnection()) {
476 storeProfile(connection
, recipientId
, profile
);
477 } catch (SQLException e
) {
478 throw new RuntimeException("Failed update recipient store", e
);
483 public void storeSelfProfileKey(final RecipientId recipientId
, final ProfileKey profileKey
) {
484 try (final var connection
= database
.getConnection()) {
485 storeProfileKey(connection
, recipientId
, profileKey
, false);
486 } catch (SQLException e
) {
487 throw new RuntimeException("Failed update recipient store", e
);
492 public void storeProfileKey(RecipientId recipientId
, final ProfileKey profileKey
) {
493 try (final var connection
= database
.getConnection()) {
494 storeProfileKey(connection
, recipientId
, profileKey
, true);
495 } catch (SQLException e
) {
496 throw new RuntimeException("Failed update recipient store", e
);
501 public void storeExpiringProfileKeyCredential(
502 RecipientId recipientId
, final ExpiringProfileKeyCredential profileKeyCredential
504 try (final var connection
= database
.getConnection()) {
505 storeExpiringProfileKeyCredential(connection
, recipientId
, profileKeyCredential
);
506 } catch (SQLException e
) {
507 throw new RuntimeException("Failed update recipient store", e
);
511 void addLegacyRecipients(final Map
<RecipientId
, Recipient
> recipients
) {
512 logger
.debug("Migrating legacy recipients to database");
513 long start
= System
.nanoTime();
516 INSERT INTO %s (_id, number, uuid)
519 ).formatted(TABLE_RECIPIENT
);
520 try (final var connection
= database
.getConnection()) {
521 connection
.setAutoCommit(false);
522 try (final var statement
= connection
.prepareStatement("DELETE FROM %s".formatted(TABLE_RECIPIENT
))) {
523 statement
.executeUpdate();
525 try (final var statement
= connection
.prepareStatement(sql
)) {
526 for (final var recipient
: recipients
.values()) {
527 statement
.setLong(1, recipient
.getRecipientId().id());
528 statement
.setString(2, recipient
.getAddress().number().orElse(null));
529 statement
.setBytes(3,
530 recipient
.getAddress()
532 .map(ServiceId
::getRawUuid
)
533 .map(UuidUtil
::toByteArray
)
535 statement
.executeUpdate();
538 logger
.debug("Initial inserts took {}ms", (System
.nanoTime() - start
) / 1000000);
540 for (final var recipient
: recipients
.values()) {
541 if (recipient
.getContact() != null) {
542 storeContact(connection
, recipient
.getRecipientId(), recipient
.getContact());
544 if (recipient
.getProfile() != null) {
545 storeProfile(connection
, recipient
.getRecipientId(), recipient
.getProfile());
547 if (recipient
.getProfileKey() != null) {
548 storeProfileKey(connection
, recipient
.getRecipientId(), recipient
.getProfileKey(), false);
550 if (recipient
.getExpiringProfileKeyCredential() != null) {
551 storeExpiringProfileKeyCredential(connection
,
552 recipient
.getRecipientId(),
553 recipient
.getExpiringProfileKeyCredential());
557 } catch (SQLException e
) {
558 throw new RuntimeException("Failed update recipient store", e
);
560 logger
.debug("Complete recipients migration took {}ms", (System
.nanoTime() - start
) / 1000000);
563 long getActualRecipientId(long recipientId
) {
564 while (recipientsMerged
.containsKey(recipientId
)) {
565 final var newRecipientId
= recipientsMerged
.get(recipientId
);
566 logger
.debug("Using {} instead of {}, because recipients have been merged", newRecipientId
, recipientId
);
567 recipientId
= newRecipientId
;
572 private void storeContact(
573 final Connection connection
, final RecipientId recipientId
, final Contact contact
574 ) throws SQLException
{
578 SET given_name = ?, family_name = ?, expiration_time = ?, profile_sharing = ?, color = ?, blocked = ?, archived = ?
581 ).formatted(TABLE_RECIPIENT
);
582 try (final var statement
= connection
.prepareStatement(sql
)) {
583 statement
.setString(1, contact
== null ?
null : contact
.getGivenName());
584 statement
.setString(2, contact
== null ?
null : contact
.getFamilyName());
585 statement
.setInt(3, contact
== null ?
0 : contact
.getMessageExpirationTime());
586 statement
.setBoolean(4, contact
!= null && contact
.isProfileSharingEnabled());
587 statement
.setString(5, contact
== null ?
null : contact
.getColor());
588 statement
.setBoolean(6, contact
!= null && contact
.isBlocked());
589 statement
.setBoolean(7, contact
!= null && contact
.isArchived());
590 statement
.setLong(8, recipientId
.id());
591 statement
.executeUpdate();
595 private void storeExpiringProfileKeyCredential(
596 final Connection connection
,
597 final RecipientId recipientId
,
598 final ExpiringProfileKeyCredential profileKeyCredential
599 ) throws SQLException
{
603 SET profile_key_credential = ?
606 ).formatted(TABLE_RECIPIENT
);
607 try (final var statement
= connection
.prepareStatement(sql
)) {
608 statement
.setBytes(1, profileKeyCredential
== null ?
null : profileKeyCredential
.serialize());
609 statement
.setLong(2, recipientId
.id());
610 statement
.executeUpdate();
614 private void storeProfile(
615 final Connection connection
, final RecipientId recipientId
, final Profile profile
616 ) throws SQLException
{
620 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 = ?
623 ).formatted(TABLE_RECIPIENT
);
624 try (final var statement
= connection
.prepareStatement(sql
)) {
625 statement
.setLong(1, profile
== null ?
0 : profile
.getLastUpdateTimestamp());
626 statement
.setString(2, profile
== null ?
null : profile
.getGivenName());
627 statement
.setString(3, profile
== null ?
null : profile
.getFamilyName());
628 statement
.setString(4, profile
== null ?
null : profile
.getAbout());
629 statement
.setString(5, profile
== null ?
null : profile
.getAboutEmoji());
630 statement
.setString(6, profile
== null ?
null : profile
.getAvatarUrlPath());
631 statement
.setBytes(7, profile
== null ?
null : profile
.getMobileCoinAddress());
632 statement
.setString(8, profile
== null ?
null : profile
.getUnidentifiedAccessMode().name());
633 statement
.setString(9,
636 : profile
.getCapabilities().stream().map(Enum
::name
).collect(Collectors
.joining(",")));
637 statement
.setLong(10, recipientId
.id());
638 statement
.executeUpdate();
642 private void storeProfileKey(
643 Connection connection
, RecipientId recipientId
, final ProfileKey profileKey
, boolean resetProfile
644 ) throws SQLException
{
645 if (profileKey
!= null) {
646 final var recipientProfileKey
= getProfileKey(recipientId
);
647 if (profileKey
.equals(recipientProfileKey
)) {
648 final var recipientProfile
= getProfile(recipientId
);
649 if (recipientProfile
== null || (
650 recipientProfile
.getUnidentifiedAccessMode() != Profile
.UnidentifiedAccessMode
.UNKNOWN
651 && recipientProfile
.getUnidentifiedAccessMode()
652 != Profile
.UnidentifiedAccessMode
.DISABLED
662 SET profile_key = ?, profile_key_credential = NULL%s
665 ).formatted(TABLE_RECIPIENT
, resetProfile ?
", profile_last_update_timestamp = 0" : "");
666 try (final var statement
= connection
.prepareStatement(sql
)) {
667 statement
.setBytes(1, profileKey
== null ?
null : profileKey
.serialize());
668 statement
.setLong(2, recipientId
.id());
669 statement
.executeUpdate();
673 private RecipientId
resolveRecipientTrusted(RecipientAddress address
, boolean isSelf
) {
674 final Pair
<RecipientId
, List
<RecipientId
>> pair
;
675 synchronized (recipientsLock
) {
676 try (final var connection
= database
.getConnection()) {
677 connection
.setAutoCommit(false);
678 if (address
.hasSingleIdentifier() || (
679 !isSelf
&& selfAddressProvider
.getSelfAddress().matches(address
)
681 pair
= new Pair
<>(resolveRecipientLocked(connection
, address
), List
.of());
683 pair
= MergeRecipientHelper
.resolveRecipientTrustedLocked(new HelperStore(connection
), address
);
685 for (final var toBeMergedRecipientId
: pair
.second()) {
686 mergeRecipientsLocked(connection
, pair
.first(), toBeMergedRecipientId
);
690 } catch (SQLException e
) {
691 throw new RuntimeException("Failed update recipient store", e
);
695 if (pair
.second().size() > 0) {
696 try (final var connection
= database
.getConnection()) {
697 for (final var toBeMergedRecipientId
: pair
.second()) {
698 recipientMergeHandler
.mergeRecipients(connection
, pair
.first(), toBeMergedRecipientId
);
699 deleteRecipient(connection
, toBeMergedRecipientId
);
701 } catch (SQLException e
) {
702 throw new RuntimeException("Failed update recipient store", e
);
708 private RecipientId
resolveRecipientLocked(
709 Connection connection
, RecipientAddress address
710 ) throws SQLException
{
711 final var byServiceId
= address
.serviceId().isEmpty()
712 ? Optional
.<RecipientWithAddress
>empty()
713 : findByServiceId(connection
, address
.serviceId().get());
715 if (byServiceId
.isPresent()) {
716 return byServiceId
.get().id();
719 final var byPni
= address
.pni().isEmpty()
720 ? Optional
.<RecipientWithAddress
>empty()
721 : findByServiceId(connection
, address
.pni().get());
723 if (byPni
.isPresent()) {
724 return byPni
.get().id();
727 final var byNumber
= address
.number().isEmpty()
728 ? Optional
.<RecipientWithAddress
>empty()
729 : findByNumber(connection
, address
.number().get());
731 if (byNumber
.isPresent()) {
732 return byNumber
.get().id();
735 logger
.debug("Got new recipient, both serviceId and number are unknown");
737 if (address
.serviceId().isEmpty()) {
738 return addNewRecipient(connection
, address
);
741 return addNewRecipient(connection
, new RecipientAddress(address
.serviceId().get()));
744 private RecipientId
resolveRecipientLocked(Connection connection
, ServiceId serviceId
) throws SQLException
{
745 final var recipient
= findByServiceId(connection
, serviceId
);
747 if (recipient
.isEmpty()) {
748 logger
.debug("Got new recipient, serviceId is unknown");
749 return addNewRecipient(connection
, new RecipientAddress(serviceId
));
752 return recipient
.get().id();
755 private RecipientId
resolveRecipientLocked(Connection connection
, String number
) throws SQLException
{
756 final var recipient
= findByNumber(connection
, number
);
758 if (recipient
.isEmpty()) {
759 logger
.debug("Got new recipient, number is unknown");
760 return addNewRecipient(connection
, new RecipientAddress(null, number
));
763 return recipient
.get().id();
766 private RecipientId
addNewRecipient(
767 final Connection connection
, final RecipientAddress address
768 ) throws SQLException
{
771 INSERT INTO %s (number, uuid, pni)
775 ).formatted(TABLE_RECIPIENT
);
776 try (final var statement
= connection
.prepareStatement(sql
)) {
777 statement
.setString(1, address
.number().orElse(null));
778 statement
.setBytes(2,
779 address
.serviceId().map(ServiceId
::getRawUuid
).map(UuidUtil
::toByteArray
).orElse(null));
780 statement
.setBytes(3, address
.pni().map(PNI
::getRawUuid
).map(UuidUtil
::toByteArray
).orElse(null));
781 final var generatedKey
= Utils
.executeQueryForOptional(statement
, Utils
::getIdMapper
);
782 if (generatedKey
.isPresent()) {
783 final var recipientId
= new RecipientId(generatedKey
.get(), this);
784 logger
.debug("Added new recipient {} with address {}", recipientId
, address
);
787 throw new RuntimeException("Failed to add new recipient to database");
792 private void removeRecipientAddress(Connection connection
, RecipientId recipientId
) throws SQLException
{
796 SET number = NULL, uuid = NULL, pni = NULL
799 ).formatted(TABLE_RECIPIENT
);
800 try (final var statement
= connection
.prepareStatement(sql
)) {
801 statement
.setLong(1, recipientId
.id());
802 statement
.executeUpdate();
806 private void updateRecipientAddress(
807 Connection connection
, RecipientId recipientId
, final RecipientAddress address
808 ) throws SQLException
{
812 SET number = ?, uuid = ?, pni = ?, username = ?
815 ).formatted(TABLE_RECIPIENT
);
816 try (final var statement
= connection
.prepareStatement(sql
)) {
817 statement
.setString(1, address
.number().orElse(null));
818 statement
.setBytes(2,
819 address
.serviceId().map(ServiceId
::getRawUuid
).map(UuidUtil
::toByteArray
).orElse(null));
820 statement
.setBytes(3, address
.pni().map(PNI
::getRawUuid
).map(UuidUtil
::toByteArray
).orElse(null));
821 statement
.setString(4, address
.username().orElse(null));
822 statement
.setLong(5, recipientId
.id());
823 statement
.executeUpdate();
827 private void deleteRecipient(final Connection connection
, final RecipientId recipientId
) throws SQLException
{
833 ).formatted(TABLE_RECIPIENT
);
834 try (final var statement
= connection
.prepareStatement(sql
)) {
835 statement
.setLong(1, recipientId
.id());
836 statement
.executeUpdate();
840 private void mergeRecipientsLocked(
841 Connection connection
, RecipientId recipientId
, RecipientId toBeMergedRecipientId
842 ) throws SQLException
{
843 final var contact
= getContact(connection
, recipientId
);
844 if (contact
== null) {
845 final var toBeMergedContact
= getContact(connection
, toBeMergedRecipientId
);
846 storeContact(connection
, recipientId
, toBeMergedContact
);
849 final var profileKey
= getProfileKey(connection
, recipientId
);
850 if (profileKey
== null) {
851 final var toBeMergedProfileKey
= getProfileKey(connection
, toBeMergedRecipientId
);
852 storeProfileKey(connection
, recipientId
, toBeMergedProfileKey
, false);
855 final var profileKeyCredential
= getExpiringProfileKeyCredential(connection
, recipientId
);
856 if (profileKeyCredential
== null) {
857 final var toBeMergedProfileKeyCredential
= getExpiringProfileKeyCredential(connection
,
858 toBeMergedRecipientId
);
859 storeExpiringProfileKeyCredential(connection
, recipientId
, toBeMergedProfileKeyCredential
);
862 final var profile
= getProfile(connection
, recipientId
);
863 if (profile
== null) {
864 final var toBeMergedProfile
= getProfile(connection
, toBeMergedRecipientId
);
865 storeProfile(connection
, recipientId
, toBeMergedProfile
);
868 recipientsMerged
.put(toBeMergedRecipientId
.id(), recipientId
.id());
871 private Optional
<RecipientWithAddress
> findByNumber(
872 final Connection connection
, final String number
873 ) throws SQLException
{
875 SELECT r._id, r.number, r.uuid, r.pni, r.username
879 """.formatted(TABLE_RECIPIENT
);
880 try (final var statement
= connection
.prepareStatement(sql
)) {
881 statement
.setString(1, number
);
882 return Utils
.executeQueryForOptional(statement
, this::getRecipientWithAddressFromResultSet
);
886 private Optional
<RecipientWithAddress
> findByUsername(
887 final Connection connection
, final String username
888 ) throws SQLException
{
890 SELECT r._id, r.number, r.uuid, r.pni, r.username
894 """.formatted(TABLE_RECIPIENT
);
895 try (final var statement
= connection
.prepareStatement(sql
)) {
896 statement
.setString(1, username
);
897 return Utils
.executeQueryForOptional(statement
, this::getRecipientWithAddressFromResultSet
);
901 private Optional
<RecipientWithAddress
> findByServiceId(
902 final Connection connection
, final ServiceId serviceId
903 ) throws SQLException
{
905 SELECT r._id, r.number, r.uuid, r.pni, r.username
907 WHERE r.uuid = ?1 OR r.pni = ?1
909 """.formatted(TABLE_RECIPIENT
);
910 try (final var statement
= connection
.prepareStatement(sql
)) {
911 statement
.setBytes(1, UuidUtil
.toByteArray(serviceId
.getRawUuid()));
912 return Utils
.executeQueryForOptional(statement
, this::getRecipientWithAddressFromResultSet
);
916 private Set
<RecipientWithAddress
> findAllByAddress(
917 final Connection connection
, final RecipientAddress address
918 ) throws SQLException
{
920 SELECT r._id, r.number, r.uuid, r.pni, r.username
922 WHERE r.uuid = ?1 OR r.pni = ?1 OR
923 r.uuid = ?2 OR r.pni = ?2 OR
926 """.formatted(TABLE_RECIPIENT
);
927 try (final var statement
= connection
.prepareStatement(sql
)) {
928 statement
.setBytes(1,
929 address
.serviceId().map(ServiceId
::getRawUuid
).map(UuidUtil
::toByteArray
).orElse(null));
930 statement
.setBytes(2, address
.pni().map(ServiceId
::getRawUuid
).map(UuidUtil
::toByteArray
).orElse(null));
931 statement
.setString(3, address
.number().orElse(null));
932 statement
.setString(4, address
.username().orElse(null));
933 return Utils
.executeQueryForStream(statement
, this::getRecipientWithAddressFromResultSet
)
934 .collect(Collectors
.toSet());
938 private Contact
getContact(final Connection connection
, final RecipientId recipientId
) throws SQLException
{
941 SELECT r.given_name, r.family_name, r.expiration_time, r.profile_sharing, r.color, r.blocked, r.archived
943 WHERE r._id = ? AND (%s)
945 ).formatted(TABLE_RECIPIENT
, SQL_IS_CONTACT
);
946 try (final var statement
= connection
.prepareStatement(sql
)) {
947 statement
.setLong(1, recipientId
.id());
948 return Utils
.executeQueryForOptional(statement
, this::getContactFromResultSet
).orElse(null);
952 private ProfileKey
getProfileKey(final Connection connection
, final RecipientId recipientId
) throws SQLException
{
959 ).formatted(TABLE_RECIPIENT
);
960 try (final var statement
= connection
.prepareStatement(sql
)) {
961 statement
.setLong(1, recipientId
.id());
962 return Utils
.executeQueryForOptional(statement
, this::getProfileKeyFromResultSet
).orElse(null);
966 private ExpiringProfileKeyCredential
getExpiringProfileKeyCredential(
967 final Connection connection
, final RecipientId recipientId
968 ) throws SQLException
{
971 SELECT r.profile_key_credential
975 ).formatted(TABLE_RECIPIENT
);
976 try (final var statement
= connection
.prepareStatement(sql
)) {
977 statement
.setLong(1, recipientId
.id());
978 return Utils
.executeQueryForOptional(statement
, this::getExpiringProfileKeyCredentialFromResultSet
)
983 private Profile
getProfile(final Connection connection
, final RecipientId recipientId
) throws SQLException
{
986 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
988 WHERE r._id = ? AND r.profile_capabilities IS NOT NULL
990 ).formatted(TABLE_RECIPIENT
);
991 try (final var statement
= connection
.prepareStatement(sql
)) {
992 statement
.setLong(1, recipientId
.id());
993 return Utils
.executeQueryForOptional(statement
, this::getProfileFromResultSet
).orElse(null);
997 private RecipientAddress
getRecipientAddressFromResultSet(ResultSet resultSet
) throws SQLException
{
998 final var pni
= Optional
.ofNullable(resultSet
.getBytes("pni")).map(UuidUtil
::parseOrNull
).map(PNI
::from
);
999 final var serviceIdUuid
= Optional
.ofNullable(resultSet
.getBytes("uuid")).map(UuidUtil
::parseOrNull
);
1000 final var serviceId
= serviceIdUuid
.isPresent() && pni
.isPresent() && serviceIdUuid
.get()
1001 .equals(pni
.get().getRawUuid()) ? pni
.<ServiceId
>map(p
-> p
) : serviceIdUuid
.<ServiceId
>map(ACI
::from
);
1002 final var number
= Optional
.ofNullable(resultSet
.getString("number"));
1003 final var username
= Optional
.ofNullable(resultSet
.getString("username"));
1004 return new RecipientAddress(serviceId
, pni
, number
, username
);
1007 private RecipientId
getRecipientIdFromResultSet(ResultSet resultSet
) throws SQLException
{
1008 return new RecipientId(resultSet
.getLong("_id"), this);
1011 private RecipientWithAddress
getRecipientWithAddressFromResultSet(final ResultSet resultSet
) throws SQLException
{
1012 return new RecipientWithAddress(getRecipientIdFromResultSet(resultSet
),
1013 getRecipientAddressFromResultSet(resultSet
));
1016 private Recipient
getRecipientFromResultSet(final ResultSet resultSet
) throws SQLException
{
1017 return new Recipient(getRecipientIdFromResultSet(resultSet
),
1018 getRecipientAddressFromResultSet(resultSet
),
1019 getContactFromResultSet(resultSet
),
1020 getProfileKeyFromResultSet(resultSet
),
1021 getExpiringProfileKeyCredentialFromResultSet(resultSet
),
1022 getProfileFromResultSet(resultSet
));
1025 private Contact
getContactFromResultSet(ResultSet resultSet
) throws SQLException
{
1026 return new Contact(resultSet
.getString("given_name"),
1027 resultSet
.getString("family_name"),
1028 resultSet
.getString("color"),
1029 resultSet
.getInt("expiration_time"),
1030 resultSet
.getBoolean("blocked"),
1031 resultSet
.getBoolean("archived"),
1032 resultSet
.getBoolean("profile_sharing"));
1035 private Profile
getProfileFromResultSet(ResultSet resultSet
) throws SQLException
{
1036 final var profileCapabilities
= resultSet
.getString("profile_capabilities");
1037 final var profileUnidentifiedAccessMode
= resultSet
.getString("profile_unidentified_access_mode");
1038 return new Profile(resultSet
.getLong("profile_last_update_timestamp"),
1039 resultSet
.getString("profile_given_name"),
1040 resultSet
.getString("profile_family_name"),
1041 resultSet
.getString("profile_about"),
1042 resultSet
.getString("profile_about_emoji"),
1043 resultSet
.getString("profile_avatar_url_path"),
1044 resultSet
.getBytes("profile_mobile_coin_address"),
1045 profileUnidentifiedAccessMode
== null
1046 ? Profile
.UnidentifiedAccessMode
.UNKNOWN
1047 : Profile
.UnidentifiedAccessMode
.valueOfOrUnknown(profileUnidentifiedAccessMode
),
1048 profileCapabilities
== null
1050 : Arrays
.stream(profileCapabilities
.split(","))
1051 .map(Profile
.Capability
::valueOfOrNull
)
1052 .filter(Objects
::nonNull
)
1053 .collect(Collectors
.toSet()));
1056 private ProfileKey
getProfileKeyFromResultSet(ResultSet resultSet
) throws SQLException
{
1057 final var profileKey
= resultSet
.getBytes("profile_key");
1059 if (profileKey
== null) {
1063 return new ProfileKey(profileKey
);
1064 } catch (InvalidInputException ignored
) {
1069 private ExpiringProfileKeyCredential
getExpiringProfileKeyCredentialFromResultSet(ResultSet resultSet
) throws SQLException
{
1070 final var profileKeyCredential
= resultSet
.getBytes("profile_key_credential");
1072 if (profileKeyCredential
== null) {
1076 return new ExpiringProfileKeyCredential(profileKeyCredential
);
1077 } catch (Throwable ignored
) {
1082 public interface RecipientMergeHandler
{
1084 void mergeRecipients(
1085 final Connection connection
, RecipientId recipientId
, RecipientId toBeMergedRecipientId
1086 ) throws SQLException
;
1089 private class HelperStore
implements MergeRecipientHelper
.Store
{
1091 private final Connection connection
;
1093 public HelperStore(final Connection connection
) {
1094 this.connection
= connection
;
1098 public Set
<RecipientWithAddress
> findAllByAddress(final RecipientAddress address
) throws SQLException
{
1099 return RecipientStore
.this.findAllByAddress(connection
, address
);
1103 public RecipientId
addNewRecipient(final RecipientAddress address
) throws SQLException
{
1104 return RecipientStore
.this.addNewRecipient(connection
, address
);
1108 public void updateRecipientAddress(
1109 final RecipientId recipientId
, final RecipientAddress address
1110 ) throws SQLException
{
1111 RecipientStore
.this.updateRecipientAddress(connection
, recipientId
, address
);
1115 public void removeRecipientAddress(final RecipientId recipientId
) throws SQLException
{
1116 RecipientStore
.this.removeRecipientAddress(connection
, recipientId
);