From: AsamK Date: Sun, 28 Jan 2024 21:12:52 +0000 (+0100) Subject: Split unregistered recipients X-Git-Tag: v0.13.0~40 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/888d6bf09178e800051d79dca66da00a171d02e7 Split unregistered recipients --- diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java index 33b8a5f9..b655e067 100644 --- a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java +++ b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java @@ -42,6 +42,10 @@ public record RecipientAddress( this(Optional.ofNullable(aci), Optional.empty(), Optional.ofNullable(e164), Optional.empty()); } + public RecipientAddress(PNI pni, String e164) { + this(Optional.empty(), Optional.ofNullable(pni), Optional.ofNullable(e164), Optional.empty()); + } + public RecipientAddress(String e164) { this(Optional.empty(), Optional.empty(), Optional.ofNullable(e164), Optional.empty()); } diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java index 6be22754..68ea4895 100644 --- a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java +++ b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java @@ -108,18 +108,8 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re } public RecipientAddress resolveRecipientAddress(RecipientId recipientId) { - final var sql = ( - """ - SELECT r.number, r.aci, r.pni, r.username - FROM %s r - WHERE r._id = ? - """ - ).formatted(TABLE_RECIPIENT); try (final var connection = database.getConnection()) { - try (final var statement = connection.prepareStatement(sql)) { - statement.setLong(1, recipientId.id()); - return Utils.executeQuerySingleRow(statement, this::getRecipientAddressFromResultSet); - } + return resolveRecipientAddress(connection, recipientId); } catch (SQLException e) { throw new RuntimeException("Failed read from recipient store", e); } @@ -848,6 +838,9 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re statement.setLong(12, recipientId.id()); statement.executeUpdate(); } + if (contact != null && contact.unregisteredTimestamp() != null) { + markUnregisteredAndSplitIfNecessary(connection, recipientId); + } rotateStorageId(connection, recipientId); } @@ -878,7 +871,8 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re for (final var number : unregisteredUsers) { final var recipient = findByNumber(connection, number); if (recipient.isPresent()) { - markUnregistered(connection, recipient.get().id()); + final var recipientId = recipient.get().id(); + markUnregisteredAndSplitIfNecessary(connection, recipientId); } } connection.commit(); @@ -887,6 +881,18 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re } } + private void markUnregisteredAndSplitIfNecessary( + final Connection connection, final RecipientId recipientId + ) throws SQLException { + markUnregistered(connection, recipientId); + final var address = resolveRecipientAddress(connection, recipientId); + if (address.aci().isPresent() && address.pni().isPresent()) { + final var numberAddress = new RecipientAddress(address.pni().get(), address.number().orElse(null)); + updateRecipientAddress(connection, recipientId, address.removeIdentifiersFrom(numberAddress)); + addNewRecipient(connection, numberAddress); + } + } + private void markRegistered( final Connection connection, final RecipientId recipientId ) throws SQLException { @@ -1001,6 +1007,22 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re rotateStorageId(connection, recipientId); } + private RecipientAddress resolveRecipientAddress( + final Connection connection, final RecipientId recipientId + ) throws SQLException { + final var sql = ( + """ + SELECT r.number, r.aci, r.pni, r.username + FROM %s r + WHERE r._id = ? + """ + ).formatted(TABLE_RECIPIENT); + try (final var statement = connection.prepareStatement(sql)) { + statement.setLong(1, recipientId.id()); + return Utils.executeQuerySingleRow(statement, this::getRecipientAddressFromResultSet); + } + } + private RecipientId resolveRecipientTrusted(RecipientAddress address, boolean isSelf) { final Pair> pair; synchronized (recipientsLock) {