Split unregistered recipients

This commit is contained in:
AsamK 2024-01-28 22:12:52 +01:00
parent e2f308a57a
commit 888d6bf091
2 changed files with 38 additions and 12 deletions

View file

@ -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());
}

View file

@ -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<RecipientId, List<RecipientId>> pair;
synchronized (recipientsLock) {